Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from cs3217-2324/add-score-component
Add score component
- Loading branch information
Showing
11 changed files
with
123 additions
and
19 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
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,24 @@ | ||
// | ||
// GameConstants.swift | ||
// star-dash | ||
// | ||
// Created by Jason Qiu on 18/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct GameConstants { | ||
struct InitialHealth { | ||
static let player = 100 | ||
static let monster = 100 | ||
} | ||
|
||
struct HealthChange { | ||
static let attackedByMonster = -20 | ||
static let attackedByPlayer = -200 | ||
} | ||
|
||
struct ScoreChange { | ||
static let pickupCollectible = 100 | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
star-dash/star-dash/GameEngine/Components/ScoreComponent.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,21 @@ | ||
// | ||
// ScoreComponent.swift | ||
// star-dash | ||
// | ||
// Created by Jason Qiu on 18/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ScoreComponent: Component { | ||
var score: Int | ||
|
||
init(id: ComponentId, entityId: EntityId, score: Int) { | ||
self.score = score | ||
super.init(id: id, entityId: entityId) | ||
} | ||
|
||
convenience init(entityId: EntityId, score: Int) { | ||
self.init(id: UUID(), entityId: entityId, score: score) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// ScoreSystem.swift | ||
// star-dash | ||
// | ||
// Created by Jason Qiu on 17/3/24. | ||
// | ||
|
||
import Foundation | ||
|
||
class ScoreSystem: System { | ||
var isActive: Bool | ||
var dispatcher: EventModifiable? | ||
var entityManager: EntityManager | ||
|
||
init(_ entityManager: EntityManager, dispatcher: EventModifiable? = nil) { | ||
self.isActive = true | ||
self.entityManager = entityManager | ||
self.dispatcher = dispatcher | ||
} | ||
|
||
func applyScoreChange(to entityId: EntityId, scoreChange: Int) { | ||
guard let scoreComponent = getScoreComponent(of: entityId) else { | ||
return | ||
} | ||
|
||
scoreComponent.score += scoreChange | ||
} | ||
|
||
private func getScoreComponent(of entityId: EntityId) -> ScoreComponent? { | ||
entityManager.component(ofType: ScoreComponent.self, of: entityId) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
star-dash/star-dashTests/GameEngine/Components/ScoreComponentTests.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,18 @@ | ||
// | ||
// ScoreComponentTests.swift | ||
// star-dashTests | ||
// | ||
// Created by Jason Qiu on 18/3/24. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import star_dash | ||
|
||
final class ScoreComponentTests: XCTestCase { | ||
func testEqual_initComponent() { | ||
let player = createPlayerEntity() | ||
let scoreComponent = ScoreComponent(entityId: player.id, score: 0) | ||
XCTAssertEqual(scoreComponent.score, 0, "Score should be initialized") | ||
} | ||
} |