Skip to content

Commit

Permalink
Update TFComponent to be compatible with Set
Browse files Browse the repository at this point in the history
TFEntity contains TFComponent as a Dictionary.

Change this to a Set to implicitly ensure that each entity only has one
instance of any given type of component at any point.
  • Loading branch information
sp4ce-cowboy committed Mar 21, 2024
1 parent f0e735f commit e8aca30
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
12 changes: 12 additions & 0 deletions TowerForge/TowerForge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
BA47C9CA2BAC373500DF0714 /* RemoveEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA47C9C92BAC373500DF0714 /* RemoveEvent.swift */; };
BA47C9CC2BAC380800DF0714 /* RemoveSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA47C9CB2BAC380800DF0714 /* RemoveSystem.swift */; };
BA47C9D12BAC3A7B00DF0714 /* DamageEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA47C9D02BAC3A7B00DF0714 /* DamageEventTests.swift */; };
BA47C9D42BAC445C00DF0714 /* Enums.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA47C9D32BAC445C00DF0714 /* Enums.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -148,6 +149,7 @@
BA47C9C92BAC373500DF0714 /* RemoveEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoveEvent.swift; sourceTree = "<group>"; };
BA47C9CB2BAC380800DF0714 /* RemoveSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoveSystem.swift; sourceTree = "<group>"; };
BA47C9D02BAC3A7B00DF0714 /* DamageEventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamageEventTests.swift; sourceTree = "<group>"; };
BA47C9D32BAC445C00DF0714 /* Enums.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Enums.swift; sourceTree = "<group>"; };
BABB7C052BA9A41000D54DAE /* TowerForceTestPlan.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = TowerForceTestPlan.xctestplan; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -311,6 +313,7 @@
children = (
52DF5FB42BA32B2600135367 /* Assets.xcassets */,
BA47C9C62BAC0C2500DF0714 /* Main */,
BA47C9D22BAC444D00DF0714 /* Commons */,
5295A2082BAAE14B005018A8 /* Controllers */,
5295A2052BAA0208005018A8 /* Nodes */,
5295A2002BA9FB97005018A8 /* Scenes */,
Expand Down Expand Up @@ -429,6 +432,14 @@
path = EventsTests;
sourceTree = "<group>";
};
BA47C9D22BAC444D00DF0714 /* Commons */ = {
isa = PBXGroup;
children = (
BA47C9D32BAC445C00DF0714 /* Enums.swift */,
);
path = Commons;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -586,6 +597,7 @@
files = (
3C9955A12BA47DA500D33FA5 /* BaseTower.swift in Sources */,
5295A20F2BAAE7CF005018A8 /* TeamController.swift in Sources */,
BA47C9D42BAC445C00DF0714 /* Enums.swift in Sources */,
3C9955A52BA47DC600D33FA5 /* BaseProjectile.swift in Sources */,
52578B8C2BA627B200B4D76C /* Team.swift in Sources */,
52DF5FFF2BA3656500135367 /* ShootingComponent.swift in Sources */,
Expand Down
17 changes: 17 additions & 0 deletions TowerForge/TowerForge/Commons/Enums.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

/// Global enums class, to ensure all enums are organized at a single
/// source of truth point.
class Enums {

enum Components: String {
case Default
case Health
case Movement
case Damage
case Position
case Ai
case Home
case Player
}
}
13 changes: 11 additions & 2 deletions TowerForge/TowerForge/LevelManager/Components/TFComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import Foundation

class TFComponent: Identifiable {
var id = UUID()
class TFComponent: Identifiable, Hashable {

var componentType: Enums.Components = .Default
weak var entity: TFEntity?

init() {
Expand All @@ -28,4 +29,12 @@ class TFComponent: Identifiable {
func willRemoveFromEntity() {
self.entity = nil
}

static func == (lhs: TFComponent, rhs: TFComponent) -> Bool {
lhs.componentType == rhs.componentType
}

func hash(into hasher: inout Hasher) {
hasher.combine(componentType)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EntityManager {
// TODO update to be changed to systems
func update(_ deltaTime: TimeInterval) {
for entity in entities {
for component in entity.components.values {
for component in entity.components {
component.update(deltaTime: deltaTime)
}
}
Expand Down
15 changes: 9 additions & 6 deletions TowerForge/TowerForge/LevelManager/Entities/TFEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ import Foundation

class TFEntity {
let id: UUID
private(set) var components: [UUID: TFComponent] // Should we change this to Set?
private(set) var components: Set<TFComponent> = []

init() {
id = UUID()
components = Dictionary()
}

init(withId id: UUID) {
self.id = id
components = Dictionary()
}

func component<T: TFComponent>(ofType type: T.Type) -> T? {
for component in components.values {
for component in components {
guard let component = component as? T else {
continue
}
Expand All @@ -40,14 +38,19 @@ class TFEntity {
return
}
component.didAddToEntity(self)
components[component.id] = (component)
components.insert(component)
}

/// Adds component if it does not exist and overrides if it does
func updateComponent<T: TFComponent>(_ component: T) {
components.update(with: component)
}

func removeComponent<T: TFComponent>(ofType type: T.Type) {
guard let componentToBeRemoved = component(ofType: T.self) else {
return
}
componentToBeRemoved.willRemoveFromEntity()
components.removeValue(forKey: componentToBeRemoved.id)
components.remove(componentToBeRemoved)
}
}

0 comments on commit e8aca30

Please sign in to comment.