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,46 +8,47 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
import GameKit
class EntityManager { class EntityManager {
var entities = Set<GKEntity>() var entities = Set<GKEntity>()
let scene: SKScene let scene: SKScene
var isModal: Bool var isModal: Bool
init(scene: SKScene) { init(scene: SKScene) {
self.scene = scene self.scene = scene
isModal = false isModal = false
} }
func add(_ entity: GKEntity) { func add(_ entity: GKEntity) {
entities.insert(entity) entities.insert(entity)
if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode {
scene.addChild(spriteNode) scene.addChild(spriteNode)
} }
if let fire = entity.component(ofType: TeamComponent.self)?.fire{ if let fire = entity.component(ofType: TeamComponent.self)?.fire{
scene.addChild(fire) scene.addChild(fire)
} }
if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode {
scene.addChild(spriteNode) scene.addChild(spriteNode)
} }
if let modal = entity.component(ofType: ModalContentComponent.self) { if let modal = entity.component(ofType: ModalContentComponent.self) {
scene.addChild(modal.header) scene.addChild(modal.header)
scene.addChild(modal.body) scene.addChild(modal.body)
scene.addChild(modal.footer) scene.addChild(modal.footer)
} }
if let skill = entity.component(ofType: AtkBoostSkillComponent.self) { if let skill = entity.component(ofType: AtkBoostSkillComponent.self) {
scene.addChild(skill.shapeNode) scene.addChild(skill.shapeNode)
scene.addChild(skill.labelNode) scene.addChild(skill.labelNode)
} }
if let skill = entity.component(ofType: DefBoostSkillComponent.self) { if let skill = entity.component(ofType: DefBoostSkillComponent.self) {
scene.addChild(skill.shapeNode) scene.addChild(skill.shapeNode)
scene.addChild(skill.labelNode) scene.addChild(skill.labelNode)
} }
if let skill = entity.component(ofType: SpySkillComponent.self) { if let skill = entity.component(ofType: SpySkillComponent.self) {
scene.addChild(skill.shapeNode) scene.addChild(skill.shapeNode)
scene.addChild(skill.labelNode) scene.addChild(skill.labelNode)
} }
if let timer = entity.component(ofType: TimerComponent.self) { if let timer = entity.component(ofType: TimerComponent.self) {
scene.addChild(timer.labelNode) scene.addChild(timer.labelNode)
} }
@ -60,10 +61,10 @@ class EntityManager {
scene.addChild(node) scene.addChild(node)
} }
} }
if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode {
scene.addChild(sliderNode.sliderKnob) scene.addChild(sliderNode.sliderKnob)
scene.addChild(sliderNode.sliderLine) scene.addChild(sliderNode.sliderLine)
} }
if let labelNode = entity.component(ofType: LabelComponent.self)?.labelNode { if let labelNode = entity.component(ofType: LabelComponent.self)?.labelNode {
scene.addChild(labelNode) scene.addChild(labelNode)
} }
@ -73,75 +74,97 @@ class EntityManager {
if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode { if let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode {
spriteNode.removeFromParent() spriteNode.removeFromParent()
} }
if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode { if let spriteNode = entity.component(ofType: ModalBackgroundComponent.self)?.spriteNode {
spriteNode.removeFromParent() spriteNode.removeFromParent()
} }
if let modal = entity.component(ofType: ModalContentComponent.self) { if let modal = entity.component(ofType: ModalContentComponent.self) {
modal.header.removeFromParent() modal.header.removeFromParent()
modal.body.removeFromParent() modal.body.removeFromParent()
modal.footer.removeFromParent() modal.footer.removeFromParent()
} }
if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode { if let sliderNode = entity.component(ofType: SliderComponent.self)?.sliderNode {
sliderNode.sliderKnob.removeFromParent() sliderNode.sliderKnob.removeFromParent()
sliderNode.sliderLine.removeFromParent() sliderNode.sliderLine.removeFromParent()
} }
if let modalButton = entity.component(ofType: ButtonComponent.self) { if let modalButton = entity.component(ofType: ButtonComponent.self) {
modalButton.buttonNode.removeFromParent() modalButton.buttonNode.removeFromParent()
isModal = false isModal = false
} }
entities.remove(entity) entities.remove(entity)
} }
func update(_ entities: [GKEntity]){ func update(_ entities: [GKEntity]){
for entity in entities { for entity in entities {
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,
scene.addChild(base.component(ofType: TeamComponent.self)!.fire) player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player,
} position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!
} )
)
} base.changeOwnership = false
func getBaseByTeam(for team: Team) -> GKEntity? { scene.addChild(base.component(ofType: TeamComponent.self)!.fire)
for entity in entities { }
if let teamComponent = entity.component(ofType: TeamComponent.self), }
let _ = entity.component(ofType: DefaultBaseComponent.self) {
}
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? {
for entity in entities {
if let teamComponent = entity.component(ofType: TeamComponent.self),
let _ = entity.component(ofType: DefaultBaseComponent.self) {
if teamComponent.team == team { if teamComponent.team == team {
return entity return entity
} }
} }
} }
return nil return nil
} }
func getBasesByTeam(for team: Team) -> Set<Base> { func getBasesByTeam(for team: Team) -> Set<Base> {
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 getTeamByBase(base: Base) -> Team? { func getBasesByPlayer(for player: GKPlayer) -> Set<Base> {
for entity in entities { return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player == player } as! Set<Base>
if entity is Base && entity == base{ }
for component in entity.components{
if component is TeamComponent { func getTeamByBase(base: Base) -> Team? {
return entity.component(ofType: TeamComponent.self)!.team for entity in entities {
} if entity is Base && entity == base{
} for component in entity.components{
} if component is TeamComponent {
} return entity.component(ofType: TeamComponent.self)!.team
return nil }
} }
}
}
return nil
}
func getBackground() -> GKEntity? { func getBackground() -> GKEntity? {
return entities.filter{$0 is Background}[0] return entities.filter{$0 is Background}[0]
} }
func getBaseNodeByTeam(for team: Team) -> SKSpriteNode? { func getBaseNodeByTeam(for team: Team) -> SKSpriteNode? {
return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode
} }
func getButtonByName(buttonName:String) -> Button { func getButtonByName(buttonName:String) -> Button {
return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button
} }