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 Apr 25, 2020
1 parent f1ba0d0 commit 8b7633f
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 21 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 }
var isDead: Bool { get }
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/Door.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
// Copyright © 2019 Nick Lockwood. All rights reserved.
//

public enum DoorState {
public enum DoorState: Int, Codable {
case closed
case opening
case open
case closing
}

public struct Door {
public struct Door: Codable {
public let duration: Double = 0.5
public let closeDelay: Double = 3
public let position: Vector
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
22 changes: 22 additions & 0 deletions Source/Engine/Game.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public enum GameState {
case playing
}

public struct SavedGame: Codable {
let world: World?
}

public struct Game {
public weak var delegate: GameDelegate?
public let levels: [Tilemap]
Expand All @@ -39,6 +43,24 @@ public extension Game {
return HUD(player: world.player, font: font)
}

func save() -> SavedGame {
switch state {
case .playing:
return SavedGame(world: world)
default:
return SavedGame(world: nil)
}
}

mutating func load(_ savedGame: SavedGame) {
guard let world = savedGame.world else {
self.state = .title
return
}
self.state = .playing
self.world = world
}

mutating func update(timeStep: Double, input: Input) {
guard let delegate = delegate else {
return
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 blocked
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Pickup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2020 Nick Lockwood. All rights reserved.
//

public enum PickupType {
public enum PickupType: Int, Codable {
case medkit
case shotgun
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Player.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 PlayerState {
public enum PlayerState: Int, Codable {
case idle
case firing
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Engine/Sounds.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 SoundName: String, CaseIterable {
public enum SoundName: String, CaseIterable, Codable {
case pistolFire
case shotgunFire
case shotgunPickup
Expand All @@ -25,7 +25,7 @@ public enum SoundName: String, CaseIterable {
case medkit
}

public struct Sound {
public struct Sound: Codable {
public let name: SoundName?
public let channel: Int?
public let volume: Double
Expand Down
4 changes: 2 additions & 2 deletions Source/Engine/Switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
// Copyright © 2019 Nick Lockwood. All rights reserved.
//

public enum SwitchState {
public enum SwitchState: Int, Codable {
case off
case on
}

public struct Switch {
public struct Switch: Codable {
public let position: Vector
public var state: SwitchState = .off
public var animation: Animation = .switchOff
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Texture.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, Decodable {
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 @@ -12,7 +12,7 @@ public struct MapData: Decodable {
fileprivate let width: Int
}

public struct Tilemap {
public struct Tilemap: Codable {
private(set) var 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: Hashable {
public struct Vector: Hashable, Codable {
public var x, y: Double

public init(x: Double, y: Double) {
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/Weapon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2020 Nick Lockwood. All rights reserved.
//

public enum Weapon: Int {
public enum Weapon: Int, Codable {
case pistol
case shotgun
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Engine/World.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum WorldAction {
case playSounds([Sound])
}

public struct World {
public struct World: Codable {
public private(set) var map: Tilemap
public private(set) var doors: [Door]
public private(set) var pushwalls: [Pushwall]
Expand Down
26 changes: 26 additions & 0 deletions Source/Rampage/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ private let joystickRadius: Double = 40
private let maximumTimeStep: Double = 1 / 20
private let worldTimeStep: Double = 1 / 120

private let savedGameURL: URL = {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
return documentsURL.appendingPathComponent("quicksave.plist")
}()

public func loadLevels() -> [Tilemap] {
let jsonURL = Bundle.main.url(forResource: "Levels", withExtension: "json")!
let jsonData = try! Data(contentsOf: jsonURL)
Expand Down Expand Up @@ -77,6 +82,15 @@ class ViewController: UIViewController {
tapGesture.delegate = self

game.delegate = self

try? restoreState()
NotificationCenter.default.addObserver(
forName: UIApplication.willResignActiveNotification,
object: nil,
queue: .main
) { _ in
try? self.saveState()
}
}

override var prefersStatusBarHidden: Bool {
Expand Down Expand Up @@ -148,6 +162,18 @@ class ViewController: UIViewController {
imageView.backgroundColor = .black
imageView.layer.magnificationFilter = .nearest
}

func saveState() throws {
let savedGame = game.save()
let data = try PropertyListEncoder().encode(savedGame)
try data.write(to: savedGameURL, options: .atomic)
}

func restoreState() throws {
let data = try Data(contentsOf: savedGameURL)
let savedGame = try PropertyListDecoder().decode(SavedGame.self, from: data)
game.load(savedGame)
}
}

extension ViewController: UIGestureRecognizerDelegate {
Expand Down

0 comments on commit 8b7633f

Please sign in to comment.