add method getBaseByPlayer and getBasesByPlayer, format code and import GameKit

This commit is contained in:
Jakob Haag 2020-05-15 12:01:26 +02:00
parent c53ab5f857
commit 23248876ba

View File

@ -8,6 +8,7 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
import GameKit
class EntityManager { class EntityManager {
@ -97,14 +98,32 @@ class EntityManager {
self.entities.update(with: entity) self.entities.update(with: entity)
let base = (entity as! Base) let base = (entity as! Base)
if base.changeOwnerShip { if base.changeOwnership {
base.addComponent(TeamComponent(team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!)) base.addComponent(TeamComponent(
base.changeOwnerShip = false 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)!
)
)
base.changeOwnership = false
scene.addChild(base.component(ofType: TeamComponent.self)!.fire) scene.addChild(base.component(ofType: TeamComponent.self)!.fire)
} }
} }
} }
func getBaseByPlayer(for player: GKPlayer) -> GKEntity? {
for entity in entities {
if let teamComponent = entity.component(ofType: TeamComponent.self),
let _ = entity.component(ofType: DefaultBaseComponent.self) {
if teamComponent.player == player {
return entity
}
}
}
return nil
}
func getBaseByTeam(for team: Team) -> GKEntity? { func getBaseByTeam(for team: Team) -> GKEntity? {
for entity in entities { for entity in entities {
if let teamComponent = entity.component(ofType: TeamComponent.self), if let teamComponent = entity.component(ofType: TeamComponent.self),
@ -121,6 +140,10 @@ class EntityManager {
return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set<Base> return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set<Base>
} }
func getBasesByPlayer(for player: GKPlayer) -> Set<Base> {
return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player == player } as! Set<Base>
}
func getTeamByBase(base: Base) -> Team? { func getTeamByBase(base: Base) -> Team? {
for entity in entities { for entity in entities {
if entity is Base && entity == base{ if entity is Base && entity == base{