-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
13 changed files
with
169 additions
and
89 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
29 changes: 29 additions & 0 deletions
29
TowerForge/TowerForge/Scenes/Rendering/RenderStages/ButtonRenderStage.swift
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,29 @@ | ||
// | ||
// ButtonRenderStage.swift | ||
// TowerForge | ||
// | ||
// Created by Zheng Ze on 29/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ButtonRenderStage: RenderStage { | ||
static let name = "button" | ||
func createAndAdd(node: TFNode, for entity: TFEntity) { | ||
guard let buttonComponent = entity.component(ofType: ButtonComponent.self) else { | ||
return | ||
} | ||
let buttonNode = TFButtonNode(action: buttonComponent.onTouch, size: buttonComponent.size) | ||
buttonNode.name = ButtonRenderStage.name | ||
buttonNode.zPosition = 1_000 | ||
node.add(child: buttonNode) | ||
} | ||
|
||
func update(node: TFNode, for entity: TFEntity) { | ||
guard let buttonComponent = entity.component(ofType: ButtonComponent.self), | ||
let buttonNode = node.child(withName: ButtonRenderStage.name) else { | ||
return | ||
} | ||
buttonNode.isUserInteractionEnabled = buttonComponent.userInteracterable | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
TowerForge/TowerForge/Scenes/Rendering/RenderStages/HealthRenderStage.swift
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,36 @@ | ||
// | ||
// HealthRenderStage.swift | ||
// TowerForge | ||
// | ||
// Created by Zheng Ze on 29/3/24. | ||
// | ||
|
||
import SpriteKit | ||
|
||
class HealthRenderStage: RenderStage { | ||
static let name = "health" | ||
static let size = CGSize(width: 100, height: 10) | ||
static let color: UIColor = .green | ||
func createAndAdd(node: TFNode, for entity: TFEntity) { | ||
guard entity.hasComponent(ofType: HealthComponent.self), | ||
let spriteComponent = entity.component(ofType: SpriteComponent.self) else { | ||
return | ||
} | ||
|
||
let healthNode = TFSpriteNode(color: HealthRenderStage.color, size: HealthRenderStage.size) | ||
healthNode.name = HealthRenderStage.name | ||
healthNode.position = CGPoint(x: -spriteComponent.size.width / 2, | ||
y: spriteComponent.size.height / 2 + HealthRenderStage.size.height + 10) | ||
healthNode.anchorPoint = CGPoint(x: 0, y: 0) | ||
node.add(child: healthNode) | ||
} | ||
|
||
func update(node: TFNode, for entity: TFEntity) { | ||
guard let healthNode = node.child(withName: HealthRenderStage.name), | ||
let healthComponent = entity.component(ofType: HealthComponent.self) else { | ||
return | ||
} | ||
let healthPercentage = healthComponent.currentHealth / healthComponent.maxHealth | ||
healthNode.xScale = healthPercentage | ||
} | ||
} |
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
// | ||
// TFButton.swift | ||
// TowerForge | ||
// | ||
// Created by Vanessa Mae on 20/03/24. | ||
// | ||
|
||
import SpriteKit | ||
|
||
struct TFButtonDelegate { | ||
var onTouchBegan: () -> Void | ||
var onTouchEnded: () -> Void | ||
} | ||
|
||
private class TFButton: SKSpriteNode { | ||
private var action: TFButtonDelegate? | ||
init(buttonAction: TFButtonDelegate?, size: CGSize) { | ||
self.action = buttonAction | ||
super.init(texture: nil, color: .clear, size: size) | ||
} | ||
|
||
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
action?.onTouchBegan() | ||
} | ||
|
||
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
action?.onTouchEnded() | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
} | ||
|
||
class TFButtonNode: TFNode { | ||
init(action: TFButtonDelegate?, size: CGSize) { | ||
super.init() | ||
node = TFButton(buttonAction: action, size: size) | ||
} | ||
} |
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
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
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