Skip to content

Commit

Permalink
Fix bug where it is possible for multiple KillEvents to be generated …
Browse files Browse the repository at this point in the history
…for the same entity killed.
  • Loading branch information
zheng-ze committed Apr 21, 2024
1 parent 719312f commit 34e5e96
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
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)
}
}

0 comments on commit 34e5e96

Please sign in to comment.