Skip to content

Commit

Permalink
Part 17
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Apr 25, 2020
1 parent 3a5771d commit 37a9af3
Show file tree
Hide file tree
Showing 17 changed files with 390 additions and 97 deletions.
12 changes: 12 additions & 0 deletions Source/Engine/Font.swift
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]
}
78 changes: 78 additions & 0 deletions Source/Engine/Game.swift
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)
}
}
}
}
}
33 changes: 33 additions & 0 deletions Source/Engine/HUD.swift
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
}
}
44 changes: 44 additions & 0 deletions Source/Engine/Text.swift
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
}
3 changes: 2 additions & 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 {
public enum Texture: String, CaseIterable, Decodable {
case wall, wall2
case crackWall, crackWall2
case slimeWall, slimeWall2
Expand All @@ -32,4 +32,5 @@ public enum Texture: String, CaseIterable {
case healthIcon
case pistolIcon, shotgunIcon
case font
case titleBackground, titleLogo
}
Loading

0 comments on commit 37a9af3

Please sign in to comment.