diff --git a/GoldWars/GoldWars/Components/TeamComponent.swift b/GoldWars/GoldWars/Components/TeamComponent.swift index db67deb..7fee0ae 100644 --- a/GoldWars/GoldWars/Components/TeamComponent.swift +++ b/GoldWars/GoldWars/Components/TeamComponent.swift @@ -14,7 +14,7 @@ class TeamComponent: GKComponent { var team: Team var player: GKPlayer let fire: SKEmitterNode - + init(team: Team, player: GKPlayer, position: CGPoint) { fire = SKEmitterNode(fileNamed: "Fire")! fire.zPosition = -1 @@ -22,21 +22,30 @@ class TeamComponent: GKComponent { fire.name = "fire" fire.particleColorSequence = nil fire.particleColorBlendFactor = 1.0 - - switch team { - case .team1: fire.particleColor = SKColor.red - case .team2: fire.particleColor = SKColor.purple - case .team3: fire.particleColor = SKColor.green - case .team4: fire.particleColor = SKColor.gray - } - + self.team = team self.player = player super.init() + fire.particleColor = getColor(by: team) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } + + func getColor(by team: Team) -> SKColor { + switch team { + case .team1: return SKColor.red + case .team2: return SKColor.purple + case .team3: return SKColor.green + case .team4: return SKColor.gray + } + } + + func change(to team: Team, to player: GKPlayer) -> Void { + self.team = team + self.player = player + self.fire.particleColor = getColor(by: team) + } } diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index cbeb114..264a064 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -119,17 +119,18 @@ class EntityManager { let base = (entity as! Base) if base.changeOwnership { - //FIX-ME: Find a way to update the Component without deleting it upfront - //TODO: outsource component handling to a generic function - //base.removeComponent(ofType: TeamComponent.self) - base.addComponent(TeamComponent( - team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, - player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player, - position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! + if (entity.component(ofType: TeamComponent.self) != nil) { + entity.component(ofType: TeamComponent.self)!.change(to: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, to: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player) + } else { + base.addComponent(TeamComponent( + team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, + player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player, + position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! + ) ) - ) - if let fire = entity.component(ofType: TeamComponent.self)?.fire{ - scene.addChild(fire) + if let fire = entity.component(ofType: TeamComponent.self)?.fire{ + scene.addChild(fire) + } } base.changeOwnership = false } @@ -150,17 +151,18 @@ class EntityManager { if getOwnerBySnapBase != nil { base.changeOwnership = true base.ownershipPlayer = getOwnerBySnapBase - //FIX-ME: Find a way to update the Component without deleting it upfront - //TODO: outsource component handling to a generic function - //entity.removeComponent(ofType: TeamComponent.self) - entity.addComponent(TeamComponent( - team: getTeamByPlayer(playerName: snapBase.ownership!), - player: getOwnerBySnapBase!, - position: (entity.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! + if (entity.component(ofType: TeamComponent.self) != nil) { + entity.component(ofType: TeamComponent.self)!.change(to: getTeamByPlayer(playerName: snapBase.ownership!), to: getOwnerBySnapBase!) + } else { + entity.addComponent(TeamComponent( + team: getTeamByPlayer(playerName: snapBase.ownership!), + player: getOwnerBySnapBase!, + position: (entity.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! + ) ) - ) - if let fire = entity.component(ofType: TeamComponent.self)?.fire{ - scene.addChild(fire) + if let fire = entity.component(ofType: TeamComponent.self)?.fire{ + scene.addChild(fire) + } } } }