Skip to content

Commit

Permalink
[ark-ecs-kit] feat: consolidate entity removal
Browse files Browse the repository at this point in the history
  • Loading branch information
markusyeo committed Apr 15, 2024
1 parent b961403 commit db2d536
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
8 changes: 4 additions & 4 deletions ArkGameExample/games/TankGame/TankGameManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,12 @@ extension TankGameManager {
let eventData = event.eventData
let tankEntity = eventData.tankEntity
guard let tankHpComponent = context.ecs.getComponent(ofType: TankHpComponent.self, for: tankEntity),
tankHpComponent.hp <= 0,
var physicsComponent = context.ecs.getComponent(ofType: PhysicsComponent.self, for: tankEntity) else {
tankHpComponent.hp <= 0
else {
return
}
physicsComponent.toBeRemoved = true
context.ecs.upsertComponent(physicsComponent, to: tankEntity)
context.ecs.upsertComponent(ToRemoveComponent(toBeRemoved: true), to: tankEntity)
// context.ecs.upsertComponent(physicsComponent, to: tankEntity)
if let positionComponent = context.ecs.getComponent(ofType: PositionComponent.self, for: tankEntity) {
ImpactExplosionAnimation(perFrameDuration: 0.1).create(in: ecs, at: positionComponent.position)
}
Expand Down
6 changes: 2 additions & 4 deletions ArkGameExample/games/TankRaceGame/TankRaceGame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,10 @@ class TankRaceGame {
let eventData = event.eventData
let tankEntity = eventData.tankEntity
guard let tankHpComponent = context.ecs.getComponent(ofType: TankHpComponent.self, for: tankEntity),
tankHpComponent.hp <= 0,
var physicsComponent = context.ecs.getComponent(ofType: PhysicsComponent.self, for: tankEntity) else {
tankHpComponent.hp <= 0 else {
return
}
physicsComponent.toBeRemoved = true
context.ecs.upsertComponent(physicsComponent, to: tankEntity)
context.ecs.upsertComponent(ToRemoveComponent(toBeRemoved: true), to: tankEntity)
if let positionComponent = context.ecs.getComponent(ofType: PositionComponent.self, for: tankEntity) {
ImpactExplosionAnimation(perFrameDuration: 0.1,
width: 256.0,
Expand Down
4 changes: 4 additions & 0 deletions ArkKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
28EFCB272BCCB6130059A908 /* ArkMultiplayerGameLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28EFCB262BCCB6130059A908 /* ArkMultiplayerGameLoop.swift */; };
28EFCB292BCCC90B0059A908 /* ArkHostSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28EFCB282BCCC90B0059A908 /* ArkHostSystem.swift */; };
28EFCB2F2BCD8F5E0059A908 /* ToRemoveComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28EFCB2E2BCD8F5E0059A908 /* ToRemoveComponent.swift */; };
28EFCB312BCD94970059A908 /* ArkRemovalSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28EFCB302BCD94970059A908 /* ArkRemovalSystem.swift */; };
8F5573BF2BA425E4007030C8 /* ShapeRenderable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F5573BE2BA425E4007030C8 /* ShapeRenderable.swift */; };
8F5573C12BA42614007030C8 /* Renderable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F5573C02BA42614007030C8 /* Renderable.swift */; };
8F5573C32BA4284D007030C8 /* BitmapRenderable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F5573C22BA4284D007030C8 /* BitmapRenderable.swift */; };
Expand Down Expand Up @@ -463,6 +464,7 @@
28EFCB262BCCB6130059A908 /* ArkMultiplayerGameLoop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkMultiplayerGameLoop.swift; sourceTree = "<group>"; };
28EFCB282BCCC90B0059A908 /* ArkHostSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkHostSystem.swift; sourceTree = "<group>"; };
28EFCB2E2BCD8F5E0059A908 /* ToRemoveComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToRemoveComponent.swift; sourceTree = "<group>"; };
28EFCB302BCD94970059A908 /* ArkRemovalSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkRemovalSystem.swift; sourceTree = "<group>"; };
8F5573BE2BA425E4007030C8 /* ShapeRenderable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShapeRenderable.swift; sourceTree = "<group>"; };
8F5573C02BA42614007030C8 /* Renderable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Renderable.swift; sourceTree = "<group>"; };
8F5573C22BA4284D007030C8 /* BitmapRenderable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitmapRenderable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1185,6 +1187,7 @@
children = (
28A032DE2BAD4F2A00851BFF /* ArkTimeSystem.swift */,
0267BA1D2BBB07D80010F729 /* ArkUpdateSystem.swift */,
28EFCB302BCD94970059A908 /* ArkRemovalSystem.swift */,
);
path = InternalSystems;
sourceTree = "<group>";
Expand Down Expand Up @@ -1969,6 +1972,7 @@
AD36A7582BAC3223003E938B /* TankGameEntityCreator.swift in Sources */,
0267BA2F2BBD2BD70010F729 /* ArkCameraSystem.swift in Sources */,
282248532BA82E5800850D7F /* SKPhysicsBodyManager.swift in Sources */,
28EFCB312BCD94970059A908 /* ArkRemovalSystem.swift in Sources */,
28A032E52BAD781900851BFF /* AbstractPhysicsArkSimulator.swift in Sources */,
02B3C63D2BCD6610002331A0 /* ArkSetUpWithoutNetworkStrategy.swift in Sources */,
9454411F2BC9513900E90ECE /* SnakeBodyBlock.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions ArkKit/app/utils/set-up/ArkSetUpStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ extension ArkSetUpStrategy {
let canvasSystem = ArkCanvasSystem()
let timeSystem = ArkTimeSystem()
let cameraSystem = ArkCameraSystem()
let entityRemovalSystem = ArkEntityRemovalSystem()
ark.arkState.arkECS.addSystem(timeSystem)
ark.arkState.arkECS.addSystem(physicsSystem)
ark.arkState.arkECS.addSystem(animationSystem)
ark.arkState.arkECS.addSystem(canvasSystem)
ark.arkState.arkECS.addSystem(cameraSystem)
ark.arkState.arkECS.addSystem(entityRemovalSystem)

// inject dependency into game loop
simulator.physicsScene?.sceneContactUpdateDelegate = physicsSystem
Expand Down
1 change: 1 addition & 0 deletions ArkKit/ark-animation-kit/ArkAnimationSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ArkAnimationSystem: UpdateSystem {
animationInstance.advance(by: deltaTime)
if animationInstance.shouldDestroy {
animationsComponent.removeAnimation(animationInstance)
arkECS.upsertComponent(ToRemoveComponent(toBeRemoved: true), to: entity)
}
}

Expand Down
1 change: 0 additions & 1 deletion ArkKit/ark-physics-kit/ArkPhysicsSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class ArkPhysicsSystem: UpdateSystem {
return }

scene?.removePhysicsBody(for: entity)
arkECS.removeEntity(entity)
}

private func syncPhysicsBody(for entity: Entity, position: PositionComponent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

class ArkEntityRemovalSystem: UpdateSystem {
var active: Bool

init(active: Bool = true) {
self.active = active
}

func update(deltaTime: TimeInterval, arkECS: ArkECS) {
let toRemoveEntities = arkECS.getEntities(with: [ToRemoveComponent.self])
for entity in toRemoveEntities {
guard let toRemoveComponent = arkECS.getComponent(ofType: ToRemoveComponent.self,
for: entity),
toRemoveComponent.toBeRemoved else {
continue
}
arkECS.removeEntity(entity)
}
}
}

0 comments on commit db2d536

Please sign in to comment.