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

Add Systems Component #17

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
80 changes: 68 additions & 12 deletions TowerForge/TowerForge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
52DF5FF92BA35D2B00135367 /* MovableComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52DF5FF82BA35D2B00135367 /* MovableComponent.swift */; };
52DF5FFB2BA3601400135367 /* HealthComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52DF5FFA2BA3601400135367 /* HealthComponent.swift */; };
52DF5FFF2BA3656500135367 /* ShootingComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52DF5FFE2BA3656500135367 /* ShootingComponent.swift */; };
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 @@ -142,6 +146,10 @@
52DF5FF82BA35D2B00135367 /* MovableComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MovableComponent.swift; sourceTree = "<group>"; };
52DF5FFA2BA3601400135367 /* HealthComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthComponent.swift; sourceTree = "<group>"; };
52DF5FFE2BA3656500135367 /* ShootingComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShootingComponent.swift; sourceTree = "<group>"; };
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 @@ -173,6 +181,8 @@
3C99559F2BA47D3E00D33FA5 /* Entities */ = {
isa = PBXGroup;
children = (
52DF5FDD2BA32D7E00135367 /* EntityManager.swift */,
52DF5FEE2BA34EA000135367 /* TFEntity.swift */,
3C9955A02BA47DA500D33FA5 /* BaseTower.swift */,
3C9955A22BA47DBB00D33FA5 /* BaseUnit.swift */,
3C9955A42BA47DC600D33FA5 /* BaseProjectile.swift */,
Expand Down Expand Up @@ -200,9 +210,11 @@
3C9955C32BA585CD00D33FA5 /* Systems */ = {
isa = PBXGroup;
children = (
3C9955AC2BA483B100D33FA5 /* TFSystem.swift */,
3C9955C42BA585DD00D33FA5 /* HealthSystem.swift */,
3C769A712BA58DE700F454F9 /* MovementSystem.swift */,
3C769A732BA591BD00F454F9 /* SpawnSystem.swift */,
BA47C9CB2BAC380800DF0714 /* RemoveSystem.swift */,
);
path = Systems;
sourceTree = "<group>";
Expand All @@ -214,6 +226,7 @@
3C9955C92BA5888F00D33FA5 /* SpawnEvent.swift */,
3C9955CB2BA5889800D33FA5 /* MoveEvent.swift */,
3C9955C72BA5865C00D33FA5 /* ConcurrentEvent.swift */,
BA47C9C92BAC373500DF0714 /* RemoveEvent.swift */,
);
path = "Implemented Events";
sourceTree = "<group>";
Expand Down Expand Up @@ -249,7 +262,7 @@
5295A2002BA9FB97005018A8 /* Scenes */ = {
isa = PBXGroup;
children = (
52DF5FA92BA32B2300135367 /* GameScene.sks */,
BA47C9C72BAC0C7900DF0714 /* SKScenes */,
52DF5FAD2BA32B2300135367 /* GameScene.swift */,
5295A2032BA9FED4005018A8 /* MenuScene.swift */,
);
Expand Down Expand Up @@ -298,27 +311,25 @@
52DF5FA62BA32B2300135367 /* TowerForge */ = {
isa = PBXGroup;
children = (
52DF5FB42BA32B2600135367 /* Assets.xcassets */,
BA47C9C62BAC0C2500DF0714 /* Main */,
BA47C9D22BAC444D00DF0714 /* Commons */,
5295A2082BAAE14B005018A8 /* Controllers */,
5295A2052BAA0208005018A8 /* Nodes */,
5295A2002BA9FB97005018A8 /* Scenes */,
52DF5FE72BA33F8800135367 /* Protocols */,
52DF5FDF2BA3347C00135367 /* TFCore */,
52DF5FDB2BA32CEF00135367 /* LevelManager */,
52DF5FA72BA32B2300135367 /* AppDelegate.swift */,
52DF5FAB2BA32B2300135367 /* Actions.sks */,
52DF5FB12BA32B2300135367 /* Main.storyboard */,
52DF5FB42BA32B2600135367 /* Assets.xcassets */,
52DF5FB62BA32B2600135367 /* LaunchScreen.storyboard */,
5295A2012BA9FBD9005018A8 /* SceneManagerDelegate.swift */,
);
path = TowerForge;
sourceTree = "<group>";
};
52DF5FC02BA32B2600135367 /* TowerForgeTests */ = {
isa = PBXGroup;
children = (
BA47C9CD2BAC3A3A00DF0714 /* EventsTests */,
BA47C9C82BAC0CA600DF0714 /* MainTests */,
52DF5FE22BA3386400135367 /* TFCoreTests */,
52DF5FC12BA32B2600135367 /* TowerForgeTests.swift */,
);
path = TowerForgeTests;
sourceTree = "<group>";
Expand All @@ -340,10 +351,6 @@
3C9955B82BA5620A00D33FA5 /* Events */,
3C99559F2BA47D3E00D33FA5 /* Entities */,
52DF5FF02BA3519D00135367 /* Components */,
52DF5FDD2BA32D7E00135367 /* EntityManager.swift */,
52DF5FEC2BA34D0300135367 /* TFComponent.swift */,
3C9955AC2BA483B100D33FA5 /* TFSystem.swift */,
52DF5FEE2BA34EA000135367 /* TFEntity.swift */,
);
path = LevelManager;
sourceTree = "<group>";
Expand Down Expand Up @@ -379,6 +386,7 @@
52DF5FF02BA3519D00135367 /* Components */ = {
isa = PBXGroup;
children = (
52DF5FEC2BA34D0300135367 /* TFComponent.swift */,
5200624C2BA8D574000DBA30 /* GameComponents */,
52578B882BA620A900B4D76C /* BaseComponents */,
527E3A232BA613EF00FE1628 /* PlayerComponent.swift */,
Expand All @@ -388,6 +396,50 @@
path = Components;
sourceTree = "<group>";
};
BA47C9C62BAC0C2500DF0714 /* Main */ = {
isa = PBXGroup;
children = (
52DF5FB12BA32B2300135367 /* Main.storyboard */,
52DF5FB62BA32B2600135367 /* LaunchScreen.storyboard */,
52DF5FA72BA32B2300135367 /* AppDelegate.swift */,
5295A2012BA9FBD9005018A8 /* SceneManagerDelegate.swift */,
);
path = Main;
sourceTree = "<group>";
};
BA47C9C72BAC0C7900DF0714 /* SKScenes */ = {
isa = PBXGroup;
children = (
52DF5FAB2BA32B2300135367 /* Actions.sks */,
52DF5FA92BA32B2300135367 /* GameScene.sks */,
);
path = SKScenes;
sourceTree = "<group>";
};
BA47C9C82BAC0CA600DF0714 /* MainTests */ = {
isa = PBXGroup;
children = (
52DF5FC12BA32B2600135367 /* TowerForgeTests.swift */,
);
path = MainTests;
sourceTree = "<group>";
};
BA47C9CD2BAC3A3A00DF0714 /* EventsTests */ = {
isa = PBXGroup;
children = (
BA47C9D02BAC3A7B00DF0714 /* DamageEventTests.swift */,
);
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 @@ -545,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 All @@ -562,6 +615,7 @@
52DF5FB02BA32B2300135367 /* GameViewController.swift in Sources */,
529F91882BA6D7A7009551D9 /* SoldierUnit.swift in Sources */,
520062522BA8DA09000DBA30 /* UnitGenerator.swift in Sources */,
BA47C9CA2BAC373500DF0714 /* RemoveEvent.swift in Sources */,
3C9955C02BA57E5500D33FA5 /* EventTarget.swift in Sources */,
5295A2152BAAF335005018A8 /* UnitSelectionNode.swift in Sources */,
52DF5FF92BA35D2B00135367 /* MovableComponent.swift in Sources */,
Expand All @@ -572,6 +626,7 @@
52DF5FE62BA33AF300135367 /* TFSpriteNode.swift in Sources */,
3C9955B42BA4B12000D33FA5 /* ArrowTower.swift in Sources */,
52DF5FE92BA33F9700135367 /* Animatable.swift in Sources */,
BA47C9CC2BAC380800DF0714 /* RemoveSystem.swift in Sources */,
3C9955AF2BA48FD200D33FA5 /* MeleeUnit.swift in Sources */,
5295A2022BA9FBD9005018A8 /* SceneManagerDelegate.swift in Sources */,
52DF5FE12BA3349600135367 /* TFTextures.swift in Sources */,
Expand Down Expand Up @@ -599,6 +654,7 @@
files = (
52DF5FC22BA32B2600135367 /* TowerForgeTests.swift in Sources */,
52DF5FE42BA3391200135367 /* TFTexturesTests.swift in Sources */,
BA47C9D12BAC3A7B00DF0714 /* DamageEventTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
19 changes: 19 additions & 0 deletions TowerForge/TowerForge/Commons/Enums.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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
case Sprite
case Shooting
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation

class AiComponent: TFComponent {
override var componentType: Enums.Components { .Ai }
private var entityManager: EntityManager
private var chosenUnit: UnitType
init(entityManager: EntityManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import CoreGraphics

class MovableComponent: TFComponent {
override var componentType: Enums.Components { .Position }
var velocity: CGVector
var position: CGPoint

Expand All @@ -18,6 +19,15 @@ class MovableComponent: TFComponent {
super.init()
}

func updatePosition(to position: CGPoint) {
self.position = position
}

func updatePosition(with vector: CGVector) {
self.position.x += vector.dx
self.position.y += vector.dy
}

override func update(deltaTime: TimeInterval) {
guard let entity = entity,
let positionComponent = entity.component(ofType: PositionComponent.self),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import CoreGraphics

class PositionComponent: TFComponent {
override var componentType: Enums.Components { .Position }
var position: CGPoint

init(position: CGPoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation

class SpriteComponent: TFComponent {
override var componentType: Enums.Components { .Sprite }
var node: TFAnimatableNode

init(textureNames: [String], height: CGFloat, width: CGFloat, position: CGPoint, animatableKey: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CoreGraphics
import SpriteKit

class DamageComponent: TFComponent {
override var componentType: Enums.Components { .Damage }
private let attackRate: TimeInterval
private var lastAttackTime = TimeInterval(0)
private let entityManager: EntityManager
Expand Down Expand Up @@ -55,7 +56,7 @@ class DamageComponent: TFComponent {
// Check if can attack
if CACurrentMediaTime() - lastAttackTime > attackRate {
lastAttackTime = CACurrentMediaTime()
oppositeHealthComponent.decreaseHealth(amount: attackPower)
oppositeHealthComponent.adjustHealth(amount: attackPower)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import Foundation

class HealthComponent: TFComponent {
override var componentType: Enums.Components { .Health }
var currentHealth: CGFloat
var maxHealth: CGFloat
private let entityManager: EntityManager
private var entityManager: EntityManager

var isZero: Bool {
currentHealth.isZero
}

init(maxHealth: CGFloat, entityManager: EntityManager) {
self.currentHealth = maxHealth
Expand All @@ -30,15 +35,27 @@ class HealthComponent: TFComponent {
}
}

func decreaseHealth(amount: CGFloat) {
self.currentHealth -= amount
/// Restores health to the maximum possible value
func restoreHealth() {
self.currentHealth = maxHealth
}

func increaseHealth(amount: CGFloat) {
self.currentHealth = min(self.currentHealth + amount, self.maxHealth)
/// Adjusts health by the specified amount.
func adjustHealth(amount: CGFloat) {
if amount < 0 {
decreaseHealth(amount: abs(amount))
} else {
increaseHealth(amount: amount)
}
}

func restoreHealth() {
self.currentHealth = maxHealth
/// Decreases currentHealth by the specified amount with a floor of 0.
private func decreaseHealth(amount: CGFloat) {
self.currentHealth = max(self.currentHealth - amount, 0)
}

/// Increases currentHealth by the specific amount with a ceiling of maxHealth
private func increaseHealth(amount: CGFloat) {
self.currentHealth = min(self.currentHealth + amount, self.maxHealth)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import SpriteKit

class ShootingComponent: TFComponent {
override var componentType: Enums.Components { .Shooting }
var fireRate: TimeInterval // Delay between shots
var range: CGFloat
private var lastShotTime = TimeInterval(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import SpriteKit

class HomeComponent: TFComponent {
override var componentType: Enums.Components { .Home }
var lifeLeft: Int
var points = 0
private var lastPointIncrease = TimeInterval(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum Player: Int {
}

class PlayerComponent: TFComponent {
override var componentType: Enums.Components { .Player }
var player: Player
init(player: Player) {
self.player = player
Expand Down
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 @@ -20,12 +21,19 @@ class TFComponent: Identifiable {
// Notify when the component is added to an entity.
// This reference provides components other access to components
// from the same entity, to allow collaboration in the ECS framework

func didAddToEntity(_ entity: TFEntity) {
self.entity = entity
}

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)
}
}
Loading