diff --git a/Source/Engine/Actor.swift b/Source/Engine/Actor.swift index bdff51b..e9b32f4 100644 --- a/Source/Engine/Actor.swift +++ b/Source/Engine/Actor.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public protocol Actor { +public protocol Actor: Codable { var radius: Double { get } var position: Vector { get set } } diff --git a/Source/Engine/Animation.swift b/Source/Engine/Animation.swift index a22301e..409bd23 100644 --- a/Source/Engine/Animation.swift +++ b/Source/Engine/Animation.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public struct Animation { +public struct Animation: Codable { public let frames: [Texture] public let duration: Double public var time: Double = 0 diff --git a/Source/Engine/Monster.swift b/Source/Engine/Monster.swift index 201c867..285d754 100644 --- a/Source/Engine/Monster.swift +++ b/Source/Engine/Monster.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public enum MonsterState { +public enum MonsterState: Int, Codable { case idle case chasing case scratching diff --git a/Source/Engine/Textures.swift b/Source/Engine/Textures.swift index 1088d84..dd61ee8 100644 --- a/Source/Engine/Textures.swift +++ b/Source/Engine/Textures.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public enum Texture: String, CaseIterable { +public enum Texture: String, CaseIterable, Codable { case wall, wall2 case crackWall, crackWall2 case slimeWall, slimeWall2 diff --git a/Source/Engine/Thing.swift b/Source/Engine/Thing.swift index 786b7c4..1d5d9b7 100644 --- a/Source/Engine/Thing.swift +++ b/Source/Engine/Thing.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public enum Thing: Int, Decodable { +public enum Thing: Int, Codable { case nothing case player case monster diff --git a/Source/Engine/Tile.swift b/Source/Engine/Tile.swift index 1e9b15b..3e48846 100644 --- a/Source/Engine/Tile.swift +++ b/Source/Engine/Tile.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public enum Tile: Int, Decodable { +public enum Tile: Int, Codable { case floor case wall case crackWall diff --git a/Source/Engine/Tilemap.swift b/Source/Engine/Tilemap.swift index aa26d59..27121cd 100644 --- a/Source/Engine/Tilemap.swift +++ b/Source/Engine/Tilemap.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public struct Tilemap: Decodable { +public struct Tilemap: Codable { private let tiles: [Tile] public let things: [Thing] public let width: Int diff --git a/Source/Engine/Vector.swift b/Source/Engine/Vector.swift index 40ac090..3b3d0bb 100644 --- a/Source/Engine/Vector.swift +++ b/Source/Engine/Vector.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public struct Vector { +public struct Vector: Codable { public var x, y: Double public init(x: Double, y: Double) { diff --git a/Source/Engine/World.swift b/Source/Engine/World.swift index 708d7d7..c4ad36b 100644 --- a/Source/Engine/World.swift +++ b/Source/Engine/World.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public struct World { +public struct World: Codable { public let map: Tilemap public var monsters: [Monster] public var player: Player! diff --git a/Source/Rampage.xcodeproj/project.pbxproj b/Source/Rampage.xcodeproj/project.pbxproj index 8b3497f..e4c4390 100644 --- a/Source/Rampage.xcodeproj/project.pbxproj +++ b/Source/Rampage.xcodeproj/project.pbxproj @@ -152,6 +152,7 @@ 012A0C6022CC200D0068E8EF /* Billboard.swift */, 01D09AF222A482030052745A /* Bitmap.swift */, 01D09AF022A481AB0052745A /* Color.swift */, + 01D09B0E22AA49D40052745A /* Game.swift */, 01D09B0222A4958E0052745A /* Input.swift */, 012A0C6122CC200D0068E8EF /* Monster.swift */, 01D09AFA22A485040052745A /* Player.swift */, @@ -297,6 +298,7 @@ 012A0C6322CC200E0068E8EF /* Monster.swift in Sources */, 01D09B0B22A7F7570052745A /* Textures.swift in Sources */, 01D09AFF22A48E990052745A /* Tilemap.swift in Sources */, + 01D09B0F22AA49D40052745A /* Game.swift in Sources */, 01D09AFD22A4873B0052745A /* Rect.swift in Sources */, 01ADC63C22B957FD00DC8AAD /* Renderer.swift in Sources */, 012A0C9E22D47C220068E8EF /* Actor.swift in Sources */, diff --git a/Source/Rampage/ViewController.swift b/Source/Rampage/ViewController.swift index dee112e..6e8101a 100644 --- a/Source/Rampage/ViewController.swift +++ b/Source/Rampage/ViewController.swift @@ -12,6 +12,7 @@ import Engine private let joystickRadius: Double = 40 private let maximumTimeStep: Double = 1 / 20 private let worldTimeStep: Double = 1 / 120 +private let savePath = "~/Documents/quicksave.json" private func loadMap() -> Tilemap { let jsonURL = Bundle.main.url(forResource: "Map", withExtension: "json")! @@ -40,6 +41,15 @@ class ViewController: UIViewController { displayLink.add(to: .main, forMode: .common) view.addGestureRecognizer(panGesture) + + try? restoreState() + NotificationCenter.default.addObserver( + forName: UIApplication.willResignActiveNotification, + object: nil, + queue: .main + ) { _ in + try? self.saveState() + } } private var inputVector: Vector { @@ -90,4 +100,16 @@ class ViewController: UIViewController { imageView.backgroundColor = .black imageView.layer.magnificationFilter = .nearest } + + func saveState() throws { + let data = try JSONEncoder().encode(world) + let url = URL(fileURLWithPath: (savePath as NSString).expandingTildeInPath) + try data.write(to: url, options: .atomic) + } + + func restoreState() throws { + let url = URL(fileURLWithPath: (savePath as NSString).expandingTildeInPath) + let data = try Data(contentsOf: url) + world = try JSONDecoder().decode(World.self, from: data) + } }