diff --git a/TowerForge/TowerForge.xcodeproj/project.pbxproj b/TowerForge/TowerForge.xcodeproj/project.pbxproj index c63d3ed2..7d860def 100644 --- a/TowerForge/TowerForge.xcodeproj/project.pbxproj +++ b/TowerForge/TowerForge.xcodeproj/project.pbxproj @@ -25,6 +25,10 @@ 3C9955C82BA5865C00D33FA5 /* ConcurrentEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C9955C72BA5865C00D33FA5 /* ConcurrentEvent.swift */; }; 3C9955CA2BA5888F00D33FA5 /* SpawnEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C9955C92BA5888F00D33FA5 /* SpawnEvent.swift */; }; 3C9955CC2BA5889800D33FA5 /* MoveEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C9955CB2BA5889800D33FA5 /* MoveEvent.swift */; }; + 5200624E2BA8D597000DBA30 /* AiComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5200624D2BA8D597000DBA30 /* AiComponent.swift */; }; + 520062522BA8DA09000DBA30 /* UnitGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520062512BA8DA09000DBA30 /* UnitGenerator.swift */; }; + 520062562BA8E026000DBA30 /* HasCost.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520062552BA8E026000DBA30 /* HasCost.swift */; }; + 520062582BA8ED73000DBA30 /* HomeComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 520062572BA8ED73000DBA30 /* HomeComponent.swift */; }; 52578B822BA61AAF00B4D76C /* PositionComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52578B812BA61AAF00B4D76C /* PositionComponent.swift */; }; 52578B872BA6209700B4D76C /* DamageComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52578B862BA6209700B4D76C /* DamageComponent.swift */; }; 52578B8C2BA627B200B4D76C /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52578B8B2BA627B200B4D76C /* Team.swift */; }; @@ -91,6 +95,10 @@ 3C9955C72BA5865C00D33FA5 /* ConcurrentEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConcurrentEvent.swift; sourceTree = ""; }; 3C9955C92BA5888F00D33FA5 /* SpawnEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpawnEvent.swift; sourceTree = ""; }; 3C9955CB2BA5889800D33FA5 /* MoveEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoveEvent.swift; sourceTree = ""; }; + 5200624D2BA8D597000DBA30 /* AiComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AiComponent.swift; sourceTree = ""; }; + 520062512BA8DA09000DBA30 /* UnitGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnitGenerator.swift; sourceTree = ""; }; + 520062552BA8E026000DBA30 /* HasCost.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HasCost.swift; sourceTree = ""; }; + 520062572BA8ED73000DBA30 /* HomeComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeComponent.swift; sourceTree = ""; }; 52578B812BA61AAF00B4D76C /* PositionComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionComponent.swift; sourceTree = ""; }; 52578B862BA6209700B4D76C /* DamageComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamageComponent.swift; sourceTree = ""; }; 52578B8B2BA627B200B4D76C /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = ""; }; @@ -197,6 +205,24 @@ path = "Implemented Events"; sourceTree = ""; }; + 5200624C2BA8D574000DBA30 /* GameComponents */ = { + isa = PBXGroup; + children = ( + 52578B862BA6209700B4D76C /* DamageComponent.swift */, + 52DF5FFE2BA3656500135367 /* ShootingComponent.swift */, + 52DF5FFA2BA3601400135367 /* HealthComponent.swift */, + ); + path = GameComponents; + sourceTree = ""; + }; + 5200624F2BA8D9E4000DBA30 /* Generators */ = { + isa = PBXGroup; + children = ( + 520062512BA8DA09000DBA30 /* UnitGenerator.swift */, + ); + path = Generators; + sourceTree = ""; + }; 52578B882BA620A900B4D76C /* BaseComponents */ = { isa = PBXGroup; children = ( @@ -266,6 +292,7 @@ 52DF5FDB2BA32CEF00135367 /* LevelManager */ = { isa = PBXGroup; children = ( + 5200624F2BA8D9E4000DBA30 /* Generators */, 3C9955C32BA585CD00D33FA5 /* Systems */, 3C9955B82BA5620A00D33FA5 /* Events */, 3C99559F2BA47D3E00D33FA5 /* Entities */, @@ -300,6 +327,7 @@ isa = PBXGroup; children = ( 52DF5FE82BA33F9700135367 /* Animatable.swift */, + 520062552BA8E026000DBA30 /* HasCost.swift */, ); path = Protocols; sourceTree = ""; @@ -307,11 +335,11 @@ 52DF5FF02BA3519D00135367 /* Components */ = { isa = PBXGroup; children = ( + 5200624C2BA8D574000DBA30 /* GameComponents */, 52578B882BA620A900B4D76C /* BaseComponents */, - 52DF5FFA2BA3601400135367 /* HealthComponent.swift */, - 52DF5FFE2BA3656500135367 /* ShootingComponent.swift */, 527E3A232BA613EF00FE1628 /* PlayerComponent.swift */, - 52578B862BA6209700B4D76C /* DamageComponent.swift */, + 5200624D2BA8D597000DBA30 /* AiComponent.swift */, + 520062572BA8ED73000DBA30 /* HomeComponent.swift */, ); path = Components; sourceTree = ""; @@ -475,6 +503,7 @@ 52578B8C2BA627B200B4D76C /* Team.swift in Sources */, 52DF5FFF2BA3656500135367 /* ShootingComponent.swift in Sources */, 3C9955CC2BA5889800D33FA5 /* MoveEvent.swift in Sources */, + 5200624E2BA8D597000DBA30 /* AiComponent.swift in Sources */, 3C9955C22BA5838900D33FA5 /* EventOutput.swift in Sources */, 52DF5FAE2BA32B2300135367 /* GameScene.swift in Sources */, 52578B822BA61AAF00B4D76C /* PositionComponent.swift in Sources */, @@ -485,6 +514,7 @@ 52578B872BA6209700B4D76C /* DamageComponent.swift in Sources */, 52DF5FB02BA32B2300135367 /* GameViewController.swift in Sources */, 529F91882BA6D7A7009551D9 /* SoldierUnit.swift in Sources */, + 520062522BA8DA09000DBA30 /* UnitGenerator.swift in Sources */, 3C9955C02BA57E5500D33FA5 /* EventTarget.swift in Sources */, 52DF5FF92BA35D2B00135367 /* MovableComponent.swift in Sources */, 52DF5FDE2BA32D7E00135367 /* EntityManager.swift in Sources */, @@ -496,6 +526,7 @@ 52DF5FE92BA33F9700135367 /* Animatable.swift in Sources */, 3C9955AF2BA48FD200D33FA5 /* MeleeUnit.swift in Sources */, 52DF5FE12BA3349600135367 /* TFTextures.swift in Sources */, + 520062582BA8ED73000DBA30 /* HomeComponent.swift in Sources */, 52DF5FA82BA32B2300135367 /* AppDelegate.swift in Sources */, 3C769A742BA591BD00F454F9 /* SpawnSystem.swift in Sources */, 3C9955B12BA4ACA100D33FA5 /* Arrow.swift in Sources */, @@ -505,6 +536,7 @@ 52DF5FED2BA34D0300135367 /* TFComponent.swift in Sources */, 527E3A242BA613F000FE1628 /* PlayerComponent.swift in Sources */, 3C9955C52BA585DD00D33FA5 /* HealthSystem.swift in Sources */, + 520062562BA8E026000DBA30 /* HasCost.swift in Sources */, 52DF5FFB2BA3601400135367 /* HealthComponent.swift in Sources */, 3C9955BC2BA563A800D33FA5 /* TFEvent.swift in Sources */, ); diff --git a/TowerForge/TowerForge.xcodeproj/project.xcworkspace/xcuserdata/macbookpro.xcuserdatad/UserInterfaceState.xcuserstate b/TowerForge/TowerForge.xcodeproj/project.xcworkspace/xcuserdata/macbookpro.xcuserdatad/UserInterfaceState.xcuserstate index 3d142db6..882bcdad 100644 Binary files a/TowerForge/TowerForge.xcodeproj/project.xcworkspace/xcuserdata/macbookpro.xcuserdatad/UserInterfaceState.xcuserstate and b/TowerForge/TowerForge.xcodeproj/project.xcworkspace/xcuserdata/macbookpro.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/TowerForge/TowerForge/AppDelegate.swift b/TowerForge/TowerForge/AppDelegate.swift index 9fa6ed06..23828dc7 100644 --- a/TowerForge/TowerForge/AppDelegate.swift +++ b/TowerForge/TowerForge/AppDelegate.swift @@ -2,7 +2,7 @@ // AppDelegate.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import UIKit diff --git a/TowerForge/TowerForge/Assets.xcassets/Sprites.spriteatlas/road-tile.imageset/Contents.json b/TowerForge/TowerForge/Assets.xcassets/Sprites.spriteatlas/Road_Grid.imageset/Contents.json similarity index 100% rename from TowerForge/TowerForge/Assets.xcassets/Sprites.spriteatlas/road-tile.imageset/Contents.json rename to TowerForge/TowerForge/Assets.xcassets/Sprites.spriteatlas/Road_Grid.imageset/Contents.json diff --git a/TowerForge/TowerForge/Assets.xcassets/Sprites.spriteatlas/road-tile.imageset/road-tile.png b/TowerForge/TowerForge/Assets.xcassets/Sprites.spriteatlas/Road_Grid.imageset/road-tile.png similarity index 100% rename from TowerForge/TowerForge/Assets.xcassets/Sprites.spriteatlas/road-tile.imageset/road-tile.png rename to TowerForge/TowerForge/Assets.xcassets/Sprites.spriteatlas/Road_Grid.imageset/road-tile.png diff --git a/TowerForge/TowerForge/GameScene.sks b/TowerForge/TowerForge/GameScene.sks index cc1e1726..91cead8d 100644 Binary files a/TowerForge/TowerForge/GameScene.sks and b/TowerForge/TowerForge/GameScene.sks differ diff --git a/TowerForge/TowerForge/GameScene.swift b/TowerForge/TowerForge/GameScene.swift index 7fd1135f..e598a58f 100644 --- a/TowerForge/TowerForge/GameScene.swift +++ b/TowerForge/TowerForge/GameScene.swift @@ -2,7 +2,7 @@ // GameScene.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import SpriteKit @@ -14,19 +14,19 @@ class GameScene: SKScene { private var entityManager: EntityManager? override func didMove(to view: SKView) { -// let textureNames = ["melee-1", "melee-2"] -// let tfTextures = TFTextures(textureNames: textureNames, textureAtlasName: "Sprites", mainTextureName: "melee-1") -// let animatableNode = TFAnimatableNode(textures: tfTextures, height: 300, width: 300, animatableKey: "melee") -// addChild(animatableNode) -// animatableNode.playAnimation() entityManager = EntityManager() guard var entityManager = entityManager else { return } - let meleeUnit = MeleeUnit(position: CGPoint(x: 0, y: 100), entityManager: entityManager, attackRate: 1.0, velocity: CGVector(dx: 10.0, dy: 0.0)) - let soldierUnit = SoldierUnit(position: CGPoint(x: 0, y: 50), entityManager: entityManager, attackRate: 1.0, velocity: CGVector(dx: 10.0, dy: 0.0)) + let meleeUnit = MeleeUnit(position: CGPoint(x: 0, y: 100), + entityManager: entityManager, attackRate: 1.0, velocity: CGVector(dx: 10.0, dy: 0.0), team: Team(player: .ownPlayer)) + let soldierUnit = SoldierUnit(position: CGPoint(x: 0, y: 50), entityManager: entityManager, + attackRate: 1.0, + velocity: CGVector(dx: 10.0, dy: 0.0), team: Team(player: .ownPlayer)) let arrowTower = ArrowTower(position: CGPoint(x:0, y: 100), entityManager: entityManager) entityManager.add(meleeUnit) + entityManager.add(soldierUnit) + entityManager.add(arrowTower) guard let sprite = meleeUnit.component(ofType: SpriteComponent.self), let soldierSprite = soldierUnit.component(ofType: SpriteComponent.self), let arrowTowerSprite = arrowTower.component(ofType: SpriteComponent.self) else { diff --git a/TowerForge/TowerForge/GameViewController.swift b/TowerForge/TowerForge/GameViewController.swift index 6ebf3cbc..4c613620 100644 --- a/TowerForge/TowerForge/GameViewController.swift +++ b/TowerForge/TowerForge/GameViewController.swift @@ -2,7 +2,7 @@ // GameViewController.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import UIKit diff --git a/TowerForge/TowerForge/LevelManager/Components/AiComponent.swift b/TowerForge/TowerForge/LevelManager/Components/AiComponent.swift new file mode 100644 index 00000000..6d667cbc --- /dev/null +++ b/TowerForge/TowerForge/LevelManager/Components/AiComponent.swift @@ -0,0 +1,40 @@ +// +// AiComponent.swift +// TowerForge +// +// Created by Vanessa Mae on 19/03/24. +// + +import Foundation + +enum UnitType { + case melee + case soldier + static let possibleUnits = [melee, soldier] +} + +class AiComponent: TFComponent { + private var entityManager: EntityManager + private var chosenUnit: UnitType + init(entityManager: EntityManager) { + self.entityManager = entityManager + self.chosenUnit = UnitType.possibleUnits.randomElement() ?? .melee + super.init() + } + + override func update(deltaTime: TimeInterval) { + guard let homeComponent = entity?.component(ofType: HomeComponent.self) else { + return + } + if chosenUnit == .melee && homeComponent.points >= MeleeUnit.cost { + // TODO: Remove hard code of CGPoints + UnitGenerator.spawnMelee(at: CGPoint(x: 0, y: 10), player: .oppositePlayer, entityManager: entityManager) + self.chosenUnit = UnitType.possibleUnits.randomElement() ?? .melee + } + if chosenUnit == .soldier && homeComponent.points >= SoldierUnit.cost { + // TODO: Remove hard code of CGPoints + UnitGenerator.spawnSoldier(at: CGPoint(x: 0, y: 10), player: .oppositePlayer, entityManager: entityManager) + self.chosenUnit = UnitType.possibleUnits.randomElement() ?? .melee + } + } +} diff --git a/TowerForge/TowerForge/LevelManager/Components/BaseComponents/MovableComponent.swift b/TowerForge/TowerForge/LevelManager/Components/BaseComponents/MovableComponent.swift index 6b7f7e58..3a24db1c 100644 --- a/TowerForge/TowerForge/LevelManager/Components/BaseComponents/MovableComponent.swift +++ b/TowerForge/TowerForge/LevelManager/Components/BaseComponents/MovableComponent.swift @@ -21,13 +21,15 @@ class MovableComponent: TFComponent { override func update(deltaTime: TimeInterval) { guard let entity = entity, let positionComponent = entity.component(ofType: PositionComponent.self), - let spriteComponent = entity.component(ofType: SpriteComponent.self) else { + let spriteComponent = entity.component(ofType: SpriteComponent.self), + let playerComponent = entity.component(ofType: PlayerComponent.self) else { return } - - let finalX = positionComponent.position.x + velocity.dx * CGFloat(deltaTime) - let finalY = positionComponent.position.y + velocity.dy * CGFloat(deltaTime) + let directionVelocity = playerComponent.player.getDirectionVelocity() + let finalX = positionComponent.position.x + (velocity.dx * CGFloat(deltaTime) * directionVelocity.dx) + let finalY = positionComponent.position.y + (velocity.dy * CGFloat(deltaTime) * directionVelocity.dy) positionComponent.changeTo(to: CGPoint(x: finalX, y: finalY)) spriteComponent.node.position = positionComponent.position + print(spriteComponent.node.position) } } diff --git a/TowerForge/TowerForge/LevelManager/Components/BaseComponents/PositionComponent.swift b/TowerForge/TowerForge/LevelManager/Components/BaseComponents/PositionComponent.swift index 7f6da6a5..521cb0cc 100644 --- a/TowerForge/TowerForge/LevelManager/Components/BaseComponents/PositionComponent.swift +++ b/TowerForge/TowerForge/LevelManager/Components/BaseComponents/PositionComponent.swift @@ -2,7 +2,7 @@ // PositionComponent.swift // TowerForge // -// Created by MacBook Pro on 17/03/24. +// Created by Vanessa Mae on 17/03/24. // import Foundation @@ -10,12 +10,10 @@ import CoreGraphics class PositionComponent: TFComponent { var position: CGPoint - init(position: CGPoint) { self.position = position super.init() } - func changeTo(to position: CGPoint) { guard let entity = entity, let spriteComponent = entity.component(ofType: SpriteComponent.self) else { return diff --git a/TowerForge/TowerForge/LevelManager/Components/BaseComponents/SpriteComponent.swift b/TowerForge/TowerForge/LevelManager/Components/BaseComponents/SpriteComponent.swift index 5c742d51..880e1904 100644 --- a/TowerForge/TowerForge/LevelManager/Components/BaseComponents/SpriteComponent.swift +++ b/TowerForge/TowerForge/LevelManager/Components/BaseComponents/SpriteComponent.swift @@ -2,7 +2,7 @@ // SpriteComponent.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import Foundation diff --git a/TowerForge/TowerForge/LevelManager/Components/GameComponents/CostComponent.swift b/TowerForge/TowerForge/LevelManager/Components/GameComponents/CostComponent.swift new file mode 100644 index 00000000..aa7e361a --- /dev/null +++ b/TowerForge/TowerForge/LevelManager/Components/GameComponents/CostComponent.swift @@ -0,0 +1,15 @@ +// +// CostComponent.swift +// TowerForge +// +// Created by MacBook Pro on 19/03/24. +// + +import Foundation + +class CostComponent: TFComponent { + public var cost: CGFloat + init(cost: CGFloat) { + self.cost = cost + } +} diff --git a/TowerForge/TowerForge/LevelManager/Components/DamageComponent.swift b/TowerForge/TowerForge/LevelManager/Components/GameComponents/DamageComponent.swift similarity index 97% rename from TowerForge/TowerForge/LevelManager/Components/DamageComponent.swift rename to TowerForge/TowerForge/LevelManager/Components/GameComponents/DamageComponent.swift index f2cef080..5bfa77ca 100644 --- a/TowerForge/TowerForge/LevelManager/Components/DamageComponent.swift +++ b/TowerForge/TowerForge/LevelManager/Components/GameComponents/DamageComponent.swift @@ -2,7 +2,7 @@ // MeleeComponent.swift // TowerForge // -// Created by MacBook Pro on 17/03/24. +// Created by Vanessa Mae on 17/03/24. // import Foundation @@ -15,7 +15,6 @@ class DamageComponent: TFComponent { private let entityManager: EntityManager private let temporary: Bool public let attackPower: CGFloat - init(attackRate: TimeInterval, attackPower: CGFloat, temporary: Bool, entityManager: EntityManager) { self.attackRate = attackRate self.attackPower = attackPower @@ -46,7 +45,7 @@ class DamageComponent: TFComponent { let oppositeHealthComponent = entity.component(ofType: HealthComponent.self) else { return } - + // Check collision with opposite team sprite component if(oppositeSpriteComponent.node.calculateAccumulatedFrame().intersects(spriteComponent.node.calculateAccumulatedFrame())) { diff --git a/TowerForge/TowerForge/LevelManager/Components/HealthComponent.swift b/TowerForge/TowerForge/LevelManager/Components/GameComponents/HealthComponent.swift similarity index 96% rename from TowerForge/TowerForge/LevelManager/Components/HealthComponent.swift rename to TowerForge/TowerForge/LevelManager/Components/GameComponents/HealthComponent.swift index 8e5916b9..19027be8 100644 --- a/TowerForge/TowerForge/LevelManager/Components/HealthComponent.swift +++ b/TowerForge/TowerForge/LevelManager/Components/GameComponents/HealthComponent.swift @@ -2,7 +2,7 @@ // HealthComponent.swift // TowerForge // -// Created by MacBook Pro on 15/03/24. +// Created by Vanessa Mae on 15/03/24. // import Foundation diff --git a/TowerForge/TowerForge/LevelManager/Components/ShootingComponent.swift b/TowerForge/TowerForge/LevelManager/Components/GameComponents/ShootingComponent.swift similarity index 93% rename from TowerForge/TowerForge/LevelManager/Components/ShootingComponent.swift rename to TowerForge/TowerForge/LevelManager/Components/GameComponents/ShootingComponent.swift index d8799685..6fc0a670 100644 --- a/TowerForge/TowerForge/LevelManager/Components/ShootingComponent.swift +++ b/TowerForge/TowerForge/LevelManager/Components/GameComponents/ShootingComponent.swift @@ -2,7 +2,7 @@ // ShootingComponent.swift // TowerForge // -// Created by MacBook Pro on 15/03/24. +// Created by Vanessa Mae on 15/03/24. // import Foundation @@ -61,14 +61,10 @@ class ShootingComponent: TFComponent { guard let arrowSpriteComponent = arrow.component(ofType: SpriteComponent.self) else { return } - // Check if can attack - if(CACurrentMediaTime() - lastShotTime > fireRate) { + if CACurrentMediaTime() - lastShotTime > fireRate { lastShotTime = CACurrentMediaTime() } - - } - - } + } } } } diff --git a/TowerForge/TowerForge/LevelManager/Components/HomeComponent.swift b/TowerForge/TowerForge/LevelManager/Components/HomeComponent.swift new file mode 100644 index 00000000..3c395f20 --- /dev/null +++ b/TowerForge/TowerForge/LevelManager/Components/HomeComponent.swift @@ -0,0 +1,40 @@ +// +// HomeComponent.swift +// TowerForge +// +// Created by Vanessa Mae on 19/03/24. +// + +import Foundation +import SpriteKit + +class HomeComponent: TFComponent { + public var lifeLeft: Int + public var points = 0 + private var lastPointIncrease = TimeInterval(0) + private var pointInterval: TimeInterval + private var pointsPerInterval: Int = 10 + init(initialLifeCount: Int, pointInterval: TimeInterval) { + self.lifeLeft = initialLifeCount + self.pointInterval = pointInterval + super.init() + } + func decreaseLife() -> Int { + self.lifeLeft -= 1 + return self.lifeLeft + } + func increaseLife() -> Int { + self.lifeLeft += 1 + return self.lifeLeft + } + override func update(deltaTime: TimeInterval) { + super.update(deltaTime: deltaTime) + // Points update + if(CACurrentMediaTime() - lastPointIncrease > pointInterval) { + lastPointIncrease = CACurrentMediaTime() + points += self.pointsPerInterval + } + } +} + + diff --git a/TowerForge/TowerForge/LevelManager/Components/PlayerComponent.swift b/TowerForge/TowerForge/LevelManager/Components/PlayerComponent.swift index bc6849d5..31b0f658 100644 --- a/TowerForge/TowerForge/LevelManager/Components/PlayerComponent.swift +++ b/TowerForge/TowerForge/LevelManager/Components/PlayerComponent.swift @@ -8,7 +8,7 @@ import Foundation import SpriteKit -enum Player: Int { +public enum Player: Int { case ownPlayer = 1 case oppositePlayer = 2 @@ -33,38 +33,10 @@ enum Player: Int { class PlayerComponent: TFComponent { public var player: Player - public var lifeLeft: Int - public var points = 0 - private var lastPointIncrease = TimeInterval(0) - private var pointInterval: TimeInterval - private var pointsPerInterval: Int = 10 - - init(player: Player, initialLifeCount: Int, pointInterval: TimeInterval) { + init(player: Player) { self.player = player - self.lifeLeft = initialLifeCount - self.pointInterval = pointInterval super.init() } - - func decreaseLife() -> Int { - self.lifeLeft -= 1 - return self.lifeLeft - } - - func increaseLife() -> Int { - self.lifeLeft += 1 - return self.lifeLeft - } - - override func update(deltaTime: TimeInterval) { - super.update(deltaTime: deltaTime) - - // Points update - if(CACurrentMediaTime() - lastPointIncrease > pointInterval) { - lastPointIncrease = CACurrentMediaTime() - points += self.pointsPerInterval - } - } } diff --git a/TowerForge/TowerForge/LevelManager/Entities/ArrowTower.swift b/TowerForge/TowerForge/LevelManager/Entities/ArrowTower.swift index 00020bcb..e96bb595 100644 --- a/TowerForge/TowerForge/LevelManager/Entities/ArrowTower.swift +++ b/TowerForge/TowerForge/LevelManager/Entities/ArrowTower.swift @@ -21,6 +21,9 @@ class ArrowTower: BaseTower { key: ArrowTower.key, position: position, maxHealth: ArrowTower.maxHealth, entityManager: entityManager) - self.addComponent(ShootingComponent(fireRate: ArrowTower.fireRate, range: 1.0, entityManager: entityManager, attackPower: ArrowTower.damage)) + self.addComponent(ShootingComponent(fireRate: ArrowTower.fireRate, + range: 1.0, + entityManager: entityManager, + attackPower: ArrowTower.damage)) } } diff --git a/TowerForge/TowerForge/LevelManager/Entities/BaseProjectile.swift b/TowerForge/TowerForge/LevelManager/Entities/BaseProjectile.swift index c522b739..1781b013 100644 --- a/TowerForge/TowerForge/LevelManager/Entities/BaseProjectile.swift +++ b/TowerForge/TowerForge/LevelManager/Entities/BaseProjectile.swift @@ -28,7 +28,6 @@ class BaseProjectile: TFEntity { let positionComponent = PositionComponent(position: position) self.addComponent(positionComponent) } - private func createMovableComponent(position: CGPoint, velocity: CGVector) { let movableComponent = MovableComponent(position: position, velocity: velocity) self.addComponent(movableComponent) diff --git a/TowerForge/TowerForge/LevelManager/Entities/BaseUnit.swift b/TowerForge/TowerForge/LevelManager/Entities/BaseUnit.swift index d1d81967..6151099b 100644 --- a/TowerForge/TowerForge/LevelManager/Entities/BaseUnit.swift +++ b/TowerForge/TowerForge/LevelManager/Entities/BaseUnit.swift @@ -7,20 +7,24 @@ import Foundation -class BaseUnit: TFEntity { +class BaseUnit: TFEntity, HasCost { + var cost: Int init(textureNames: [String], size: CGSize, key: String, position: CGPoint, maxHealth: CGFloat, entityManager: EntityManager, - velocity: CGVector) { + cost: Int, + velocity: CGVector, + team: Team) { + self.cost = cost super.init() - createHealthComponent(maxHealth: maxHealth, entityManager: entityManager) createSpriteComponent(textureNames: textureNames, size: size, key: key, position: position) createMovableComponent(position: position, velocity: velocity) createPositionComponent(position: position) + createPlayerComponent(team: team) } private func createHealthComponent(maxHealth: CGFloat, entityManager: EntityManager) { @@ -45,4 +49,8 @@ class BaseUnit: TFEntity { let movableComponent = MovableComponent(position: position, velocity: velocity) self.addComponent(movableComponent) } + private func createPlayerComponent(team: Team) { + let playerComponent = PlayerComponent(player: team.player) + self.addComponent(playerComponent) + } } diff --git a/TowerForge/TowerForge/LevelManager/Entities/MeleeUnit.swift b/TowerForge/TowerForge/LevelManager/Entities/MeleeUnit.swift index fab19fd4..5d48a0a8 100644 --- a/TowerForge/TowerForge/LevelManager/Entities/MeleeUnit.swift +++ b/TowerForge/TowerForge/LevelManager/Entities/MeleeUnit.swift @@ -13,13 +13,18 @@ class MeleeUnit: BaseUnit { static let key = "melee" static let maxHealth = 100.0 static let damage = 10.0 + static let cost = 10 - init(position: CGPoint, entityManager: EntityManager, attackRate: TimeInterval, velocity: CGVector) { + init(position: CGPoint, entityManager: EntityManager, attackRate: TimeInterval, velocity: CGVector, team: Team) { super.init(textureNames: MeleeUnit.textureNames, size: MeleeUnit.size, key: MeleeUnit.key, position: position, - maxHealth: MeleeUnit.maxHealth, entityManager: entityManager, velocity: velocity) + maxHealth: MeleeUnit.maxHealth, + entityManager: entityManager, + cost: MeleeUnit.cost, + velocity: velocity, + team: team) self.addComponent(DamageComponent(attackRate: attackRate, attackPower: MeleeUnit.damage, temporary: false, diff --git a/TowerForge/TowerForge/LevelManager/Entities/SoldierUnit.swift b/TowerForge/TowerForge/LevelManager/Entities/SoldierUnit.swift index 8c32c76c..ff831bff 100644 --- a/TowerForge/TowerForge/LevelManager/Entities/SoldierUnit.swift +++ b/TowerForge/TowerForge/LevelManager/Entities/SoldierUnit.swift @@ -13,13 +13,22 @@ class SoldierUnit: BaseUnit { static let key = "shoot" static let maxHealth = 100.0 static let damage = 10.0 + static let cost = 5 - init(position: CGPoint, entityManager: EntityManager, attackRate: TimeInterval, velocity: CGVector) { + init(position: CGPoint, entityManager: EntityManager, attackRate: TimeInterval, velocity: CGVector, team: Team) { super.init(textureNames: SoldierUnit.textureNames, size: SoldierUnit.size, key: SoldierUnit.key, position: position, - maxHealth: SoldierUnit.maxHealth, entityManager: entityManager, velocity: velocity) - self.addComponent(ShootingComponent(fireRate: attackRate, range: 1.0, entityManager: entityManager, attackPower: 10.0)) + maxHealth: SoldierUnit.maxHealth, + entityManager: entityManager, + cost: SoldierUnit.cost, + velocity: velocity, + team: team) + self.addComponent(ShootingComponent(fireRate: attackRate, + range: 1.0, + entityManager: entityManager, + attackPower: 10.0 + )) } } diff --git a/TowerForge/TowerForge/LevelManager/Entities/Team.swift b/TowerForge/TowerForge/LevelManager/Entities/Team.swift index a079c6c5..0cb2e3f1 100644 --- a/TowerForge/TowerForge/LevelManager/Entities/Team.swift +++ b/TowerForge/TowerForge/LevelManager/Entities/Team.swift @@ -2,7 +2,7 @@ // Team.swift // TowerForge // -// Created by MacBook Pro on 17/03/24. +// Created by Vanessa Mae on 17/03/24. // import Foundation @@ -10,12 +10,14 @@ import Foundation class Team: TFEntity { static let lifeCount = 5 static let pointsInterval = TimeInterval(0.5) - override init() { + public var player: Player + init(player: Player) { + self.player = player super.init() - createPlayerComponent() + createPlayerComponent(player: player) } - private func createPlayerComponent() { - let playerComponent = PlayerComponent(player: .ownPlayer, initialLifeCount: Team.lifeCount, pointInterval: Team.pointsInterval) + private func createPlayerComponent(player: Player) { + let playerComponent = PlayerComponent(player: player) self.addComponent(playerComponent) } } diff --git a/TowerForge/TowerForge/LevelManager/EntityManager.swift b/TowerForge/TowerForge/LevelManager/EntityManager.swift index 5898756b..fbd26475 100644 --- a/TowerForge/TowerForge/LevelManager/EntityManager.swift +++ b/TowerForge/TowerForge/LevelManager/EntityManager.swift @@ -41,7 +41,6 @@ class EntityManager { func components(ofType type: T.Type) -> [T] { entities.compactMap { $0.component(ofType: type) } } - // TODO update to be changed to systems func update(_ deltaTime: TimeInterval) { for entity in entities { diff --git a/TowerForge/TowerForge/LevelManager/Events/Implemented Events/ConcurrentEvent.swift b/TowerForge/TowerForge/LevelManager/Events/Implemented Events/ConcurrentEvent.swift index 65181138..34ac938d 100644 --- a/TowerForge/TowerForge/LevelManager/Events/Implemented Events/ConcurrentEvent.swift +++ b/TowerForge/TowerForge/LevelManager/Events/Implemented Events/ConcurrentEvent.swift @@ -24,7 +24,6 @@ struct ConcurrentEvent: TFEvent { func execute(in target: any EventTarget) -> EventOutput { var eventOutput = event1.execute(in: target) let eventOutput2 = event2.execute(in: target) - eventOutput.combine(with: eventOutput2) return eventOutput diff --git a/TowerForge/TowerForge/LevelManager/Generators/UnitGenerator.swift b/TowerForge/TowerForge/LevelManager/Generators/UnitGenerator.swift new file mode 100644 index 00000000..4a8c0300 --- /dev/null +++ b/TowerForge/TowerForge/LevelManager/Generators/UnitGenerator.swift @@ -0,0 +1,33 @@ +// +// UnitGenerator.swift +// TowerForge +// +// Created by Vanessa Mae on 19/03/24. +// + +import Foundation + +class UnitGenerator { + public static func spawnMelee(at position: CGPoint, player: Player, entityManager: EntityManager) { + // TODO: Change the default value and abstract as constant + let unit = MeleeUnit(position: position, + entityManager: entityManager, + attackRate: 10.0, + velocity: CGVector(dx: 1.0, dy: 0.0), + team: Team(player: .oppositePlayer)) + let spriteComponent = unit.component(ofType: SpriteComponent.self) + spriteComponent?.node.position = position + entityManager.add(unit) + } + public static func spawnSoldier(at position: CGPoint, player: Player, entityManager: EntityManager) { + // TODO: Change the default value and abstract as constant + let unit = SoldierUnit(position: position, + entityManager: entityManager, + attackRate: 10.0, + velocity: CGVector(dx: 1.0, dy: 0.0), + team: Team(player: .oppositePlayer)) + let spriteComponent = unit.component(ofType: SpriteComponent.self) + spriteComponent?.node.position = position + entityManager.add(unit) + } +} diff --git a/TowerForge/TowerForge/LevelManager/TFComponent.swift b/TowerForge/TowerForge/LevelManager/TFComponent.swift index c1cebe12..1a81cc0f 100644 --- a/TowerForge/TowerForge/LevelManager/TFComponent.swift +++ b/TowerForge/TowerForge/LevelManager/TFComponent.swift @@ -2,7 +2,7 @@ // TFComponent.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import Foundation @@ -18,7 +18,8 @@ class TFComponent: Identifiable { func update(deltaTime: TimeInterval) {} // To be overriden by its inheriters // 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 + // 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 } diff --git a/TowerForge/TowerForge/LevelManager/TFEntity.swift b/TowerForge/TowerForge/LevelManager/TFEntity.swift index a7c69cb9..e24979b7 100644 --- a/TowerForge/TowerForge/LevelManager/TFEntity.swift +++ b/TowerForge/TowerForge/LevelManager/TFEntity.swift @@ -2,7 +2,7 @@ // TFEntity.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import Foundation diff --git a/TowerForge/TowerForge/Protocols/Animatable.swift b/TowerForge/TowerForge/Protocols/Animatable.swift index daf135aa..0489e514 100644 --- a/TowerForge/TowerForge/Protocols/Animatable.swift +++ b/TowerForge/TowerForge/Protocols/Animatable.swift @@ -2,7 +2,7 @@ // Animatable.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import Foundation diff --git a/TowerForge/TowerForge/Protocols/HasCost.swift b/TowerForge/TowerForge/Protocols/HasCost.swift new file mode 100644 index 00000000..5ad04a5e --- /dev/null +++ b/TowerForge/TowerForge/Protocols/HasCost.swift @@ -0,0 +1,12 @@ +// +// HasCost.swift +// TowerForge +// +// Created by Vanessa Mae on 19/03/24. +// + +import Foundation + +protocol HasCost { + var cost: Int { get set } +} diff --git a/TowerForge/TowerForge/TFCore/TFAnimatableNode.swift b/TowerForge/TowerForge/TFCore/TFAnimatableNode.swift index 36fcb114..996b2ef9 100644 --- a/TowerForge/TowerForge/TFCore/TFAnimatableNode.swift +++ b/TowerForge/TowerForge/TFCore/TFAnimatableNode.swift @@ -2,7 +2,7 @@ // TFAnimatableNode.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import Foundation diff --git a/TowerForge/TowerForge/TFCore/TFNode.swift b/TowerForge/TowerForge/TFCore/TFNode.swift index ba6e4078..8236244f 100644 --- a/TowerForge/TowerForge/TFCore/TFNode.swift +++ b/TowerForge/TowerForge/TFCore/TFNode.swift @@ -2,7 +2,7 @@ // TFNode.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import Foundation diff --git a/TowerForge/TowerForge/TFCore/TFTextures.swift b/TowerForge/TowerForge/TFCore/TFTextures.swift index 5218d9c3..cea58547 100644 --- a/TowerForge/TowerForge/TFCore/TFTextures.swift +++ b/TowerForge/TowerForge/TFCore/TFTextures.swift @@ -2,7 +2,7 @@ // TFTextures.swift // TowerForge // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import Foundation diff --git a/TowerForge/TowerForgeTests/TFCoreTests/TFTexturesTests.swift b/TowerForge/TowerForgeTests/TFCoreTests/TFTexturesTests.swift index aac60d58..6ec9ac1e 100644 --- a/TowerForge/TowerForgeTests/TFCoreTests/TFTexturesTests.swift +++ b/TowerForge/TowerForgeTests/TFCoreTests/TFTexturesTests.swift @@ -2,7 +2,7 @@ //// TFTexturesTests.swift //// TowerForgeTests //// -//// Created by MacBook Pro on 14/03/24. +//// Created by Vanessa Mae on 14/03/24. //// // //import XCTest diff --git a/TowerForge/TowerForgeTests/TowerForgeTests.swift b/TowerForge/TowerForgeTests/TowerForgeTests.swift index 54765baa..5cbb3c99 100644 --- a/TowerForge/TowerForgeTests/TowerForgeTests.swift +++ b/TowerForge/TowerForgeTests/TowerForgeTests.swift @@ -2,7 +2,7 @@ // TowerForgeTests.swift // TowerForgeTests // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import XCTest diff --git a/TowerForge/TowerForgeUITests/TowerForgeUITests.swift b/TowerForge/TowerForgeUITests/TowerForgeUITests.swift index 790fe267..ce767fc6 100644 --- a/TowerForge/TowerForgeUITests/TowerForgeUITests.swift +++ b/TowerForge/TowerForgeUITests/TowerForgeUITests.swift @@ -2,7 +2,7 @@ // TowerForgeUITests.swift // TowerForgeUITests // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import XCTest diff --git a/TowerForge/TowerForgeUITests/TowerForgeUITestsLaunchTests.swift b/TowerForge/TowerForgeUITests/TowerForgeUITestsLaunchTests.swift index 96fbb7a7..d55aa392 100644 --- a/TowerForge/TowerForgeUITests/TowerForgeUITestsLaunchTests.swift +++ b/TowerForge/TowerForgeUITests/TowerForgeUITestsLaunchTests.swift @@ -2,7 +2,7 @@ // TowerForgeUITestsLaunchTests.swift // TowerForgeUITests // -// Created by MacBook Pro on 14/03/24. +// Created by Vanessa Mae on 14/03/24. // import XCTest