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