Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State restoration using Codable #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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 var monsters: [Monster]
public var player: Player!
Expand Down
2 changes: 2 additions & 0 deletions Source/Rampage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
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)
}
}