-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a5771d
commit 37a9af3
Showing
17 changed files
with
390 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Font.swift | ||
// Engine | ||
// | ||
// Created by Nick Lockwood on 21/04/2020. | ||
// Copyright © 2020 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
public struct Font: Decodable { | ||
public let texture: Texture | ||
public let characters: [String] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Game.swift | ||
// Engine | ||
// | ||
// Created by Nick Lockwood on 07/10/2019. | ||
// Copyright © 2019 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
public protocol GameDelegate: AnyObject { | ||
func playSound(_ sound: Sound) | ||
func clearSounds() | ||
} | ||
|
||
public enum GameState { | ||
case title | ||
case starting | ||
case playing | ||
} | ||
|
||
public struct Game { | ||
public weak var delegate: GameDelegate? | ||
public let levels: [Tilemap] | ||
public private(set) var world: World | ||
public private(set) var state: GameState | ||
public private(set) var transition: Effect? | ||
public let font: Font | ||
public var titleText = "TAP TO START" | ||
|
||
public init(levels: [Tilemap], font: Font) { | ||
self.state = .title | ||
self.levels = levels | ||
self.world = World(map: levels[0]) | ||
self.font = font | ||
} | ||
} | ||
|
||
public extension Game { | ||
var hud: HUD { | ||
return HUD(player: world.player, font: font) | ||
} | ||
|
||
mutating func update(timeStep: Double, input: Input) { | ||
guard let delegate = delegate else { | ||
return | ||
} | ||
|
||
// Update transition | ||
if var effect = transition { | ||
effect.time += timeStep | ||
transition = effect | ||
} | ||
|
||
// Update state | ||
switch state { | ||
case .title: | ||
if input.isFiring { | ||
transition = Effect(type: .fadeOut, color: .black, duration: 0.5) | ||
state = .starting | ||
} | ||
case .starting: | ||
if transition?.isCompleted == true { | ||
transition = Effect(type: .fadeIn, color: .black, duration: 0.5) | ||
state = .playing | ||
} | ||
case .playing: | ||
if let action = world.update(timeStep: timeStep, input: input) { | ||
switch action { | ||
case .loadLevel(let index): | ||
let index = index % levels.count | ||
world.setLevel(levels[index]) | ||
delegate.clearSounds() | ||
case .playSounds(let sounds): | ||
sounds.forEach(delegate.playSound) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// HUD.swift | ||
// Engine | ||
// | ||
// Created by Nick Lockwood on 19/04/2020. | ||
// Copyright © 2020 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
public struct HUD { | ||
public let healthString: String | ||
public let healthTint: Color | ||
public let ammoString: String | ||
public let playerWeapon: Texture | ||
public let weaponIcon: Texture | ||
public let font: Font | ||
|
||
public init(player: Player, font: Font) { | ||
let health = Int(max(0, player.health)) | ||
switch health { | ||
case ...10: | ||
self.healthTint = .red | ||
case 10 ... 30: | ||
self.healthTint = .yellow | ||
default: | ||
self.healthTint = .green | ||
} | ||
self.healthString = String(health) | ||
self.ammoString = String(Int(max(0, min(99, player.ammo)))) | ||
self.playerWeapon = player.animation.texture | ||
self.weaponIcon = player.weapon.attributes.hudIcon | ||
self.font = font | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Text.swift | ||
// Engine | ||
// | ||
// Created by Nick Lockwood on 21/04/2020. | ||
// Copyright © 2020 Nick Lockwood. All rights reserved. | ||
// | ||
|
||
public struct TextStyle { | ||
public let color: Color | ||
public let shadowColor: Color | ||
public let shadowOffset: Vector | ||
|
||
public init(color: Color, shadowColor: Color) { | ||
self.color = color | ||
self.shadowColor = shadowColor | ||
self.shadowOffset = Vector(x: 0, y: 1) | ||
} | ||
} | ||
|
||
public extension TextStyle { | ||
static let green = TextStyle( | ||
color: .green, | ||
shadowColor: Color(r: 55, g: 148, b: 110) | ||
) | ||
static let yellow = TextStyle( | ||
color: .yellow, | ||
shadowColor: Color(r: 223, g: 113, b: 38) | ||
) | ||
static let red = TextStyle( | ||
color: .red, | ||
shadowColor: Color(r: 172, g: 50, b: 50) | ||
) | ||
static let white = TextStyle( | ||
color: .white, | ||
shadowColor: Color(r: 34, g: 32, b: 52) | ||
) | ||
} | ||
|
||
public struct Text { | ||
public let font: Font | ||
public let style: TextStyle | ||
public let text: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.