Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where it is possible for multiple KillEvents to be generated for the same entity killed. #126

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}