diff --git a/Source/Engine/Actor.swift b/Source/Engine/Actor.swift index 43fbc9e..8c9f004 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 } var isDead: Bool { get } diff --git a/Source/Engine/Animation.swift b/Source/Engine/Animation.swift index d9fa445..41257a0 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/Color.swift b/Source/Engine/Color.swift index b0b590f..c584e7f 100644 --- a/Source/Engine/Color.swift +++ b/Source/Engine/Color.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public struct Color { +public struct Color: Codable { public var r, g, b, a: UInt8 public init(r: UInt8, g: UInt8, b: UInt8, a: UInt8 = 255) { diff --git a/Source/Engine/Effect.swift b/Source/Engine/Effect.swift index 8d67431..3fd19e2 100644 --- a/Source/Engine/Effect.swift +++ b/Source/Engine/Effect.swift @@ -6,13 +6,13 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public enum EffectType { +public enum EffectType: Int, Codable { case fadeIn case fadeOut case fizzleOut } -public struct Effect { +public struct Effect: Codable { public let type: EffectType public let color: Color public let duration: Double diff --git a/Source/Engine/Monster.swift b/Source/Engine/Monster.swift index 1eacf2f..869a76a 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/Player.swift b/Source/Engine/Player.swift index 33c5106..11d9a66 100644 --- a/Source/Engine/Player.swift +++ b/Source/Engine/Player.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Nick Lockwood. All rights reserved. // -public enum PlayerState { +public enum PlayerState: Int, Codable { case idle case firing } diff --git a/Source/Engine/Textures.swift b/Source/Engine/Textures.swift index d85f71f..5fd4c3d 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 12e5e8d..a042337 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 private(set) var monsters: [Monster] public private(set) var player: Player! diff --git a/Source/Rampage/ViewController.swift b/Source/Rampage/ViewController.swift index 91ccd43..6c53f93 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" public func loadMap() -> Tilemap { let jsonURL = Bundle.main.url(forResource: "Map", withExtension: "json")! @@ -52,6 +53,15 @@ class ViewController: UIViewController { view.addGestureRecognizer(tapGesture) tapGesture.addTarget(self, action: #selector(fire)) tapGesture.delegate = self + + try? restoreState() + NotificationCenter.default.addObserver( + forName: UIApplication.willResignActiveNotification, + object: nil, + queue: .main + ) { _ in + try? self.saveState() + } } private var inputVector: Vector { @@ -107,6 +117,18 @@ 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) + } } extension ViewController: UIGestureRecognizerDelegate {