Skip to content

Commit

Permalink
Merge pull request #126 from zheng-ze/main
Browse files Browse the repository at this point in the history
Fix bug where it is possible for multiple KillEvents to be generated for the same entity killed.
  • Loading branch information
zheng-ze authored Apr 21, 2024
2 parents 719312f + 4653b95 commit 090f9d6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
10 changes: 10 additions & 0 deletions TowerForge/TowerForge/AppMain/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Games should use this method to pause the game.

/// TODO: ADD PAUSE METHOD HERE
guard let navigationController = window?.rootViewController as? UINavigationController,
let gameViewController = navigationController.topViewController as? GameViewController else {
return
}

if gameViewController.roomId != nil {
gameViewController.onMenu()
} else {
gameViewController.pause()
}
}

func applicationDidEnterBackground(_ application: UIApplication) {
Expand Down
5 changes: 3 additions & 2 deletions TowerForge/TowerForge/GameModule/Entities/EntityManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ class EntityManager {
/// assert(checkRepresentation())
}

func removeEntity(with id: UUID) {
func removeEntity(with id: UUID) -> Bool {
guard let entity = entitiesMap.removeValue(forKey: id) else {
return
return false
}

for (key, _) in entity.components {
componentsMap[key]?.removeValue(forKey: entity.id)
}
return true
/// assert(checkRepresentation())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ struct KillEvent: TFEvent {
}

func execute(in target: any EventTarget) -> EventOutput {
var success = false
if let removeSystem = target.system(ofType: RemoveSystem.self) {
removeSystem.handleRemove(for: entityId)
success = removeSystem.handleRemove(for: entityId)
}

guard success else {
return EventOutput()
}

if let statsSystem = target.system(ofType: StatisticSystem.self) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ class HealthSystem: TFSystem {
return
}

// Only handle DamageEvents when the entity taking damage is not already waiting to removed
guard healthComponent.currentHealth > 0 else {
return
}

healthComponent.adjustHealth(amount: hp)

guard healthComponent.currentHealth <= 0 else {
return
}

if eventManager.isHost {
let remoteRemoveEvent = RemoteKillEvent(id: entityId, player: playerComponent.player,
source: eventManager.currentPlayer ?? .defaultPlayer)
eventManager.add(remoteRemoveEvent)
let remoteKillEvent = RemoteKillEvent(id: entityId, player: playerComponent.player,
source: eventManager.currentPlayer ?? .defaultPlayer)
eventManager.add(remoteKillEvent)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RemoveSystem: TFSystem {

/// Removes the provided entity
/// - Parameter entityId: The UUID of the associated TFEntity to be removed
func handleRemove(for entityId: UUID) {
func handleRemove(for entityId: UUID) -> Bool {
entityManager.removeEntity(with: entityId)
}
}
8 changes: 6 additions & 2 deletions TowerForge/TowerForge/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class GameViewController: UIViewController {

@IBOutlet private var gamePopupButton: UIButton!
@IBAction private func onStatePressed(_ sender: Any) {
isPaused = (roomId == nil && currentPlayer == nil) // Allow pausing only on singleplayer.
gameWorld?.presentStatePopup()
self.pause()
}

override func viewDidLoad() {
Expand Down Expand Up @@ -64,6 +63,11 @@ class GameViewController: UIViewController {
self.gameWorld?.delegate = self
self.gameWorld?.statePopupDelegate = self
}

func pause() {
isPaused = (roomId == nil && currentPlayer == nil) // Allow pausing only on singleplayer.
gameWorld?.presentStatePopup()
}
}

extension GameViewController: SceneUpdateDelegate {
Expand Down

0 comments on commit 090f9d6

Please sign in to comment.