Skip to content

Commit

Permalink
Added save/restore state
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jul 27, 2019
1 parent 885e594 commit 513522e
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Source/Engine/Actor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Animation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions Source/Engine/Effect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Monster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Textures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Thing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Tile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Tilemap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/World.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
22 changes: 22 additions & 0 deletions Source/Rampage/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")!
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 513522e

Please sign in to comment.