From 9b57a1bcea8bdd8c8d72a212e3d75c6aab81a24c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Sat, 16 May 2020 20:12:32 +0200 Subject: [PATCH] making smaller funcs in GameScene for betterUnderstanding --- GoldWars/GoldWars/Scenes/GameScene.swift | 100 ++++++++++++++--------- 1 file changed, 61 insertions(+), 39 deletions(-) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 2fa42b3..c8cd2c5 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -41,34 +41,13 @@ class GameScene: SKScene{ currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos - for base in currentDraggedBase!.adjacencyList { - if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { - // TODO: change interaction based on collision instead of touchlocation - entityManager.add(Modal(modaltype: .BaseAttack, - base: currentDraggedBase!, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) - } - } + addAttackDetails(touchLocation: touchLocation) } else { for entity in entityManager.entities { let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode - if atPoint(touchLocation) == spriteNode && !entityManager.isModal { - spriteNode?.touchesBegan(touches, with: event) - if !entityManager.isModal { - for child in self.children { - if(child.name != "fire"){ - child.alpha = 0.3 - } - } - entityManager.add(Modal(modaltype: .BaseDetails, - base: entity as! Base, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) - } - } + addBaseDetails(touchLocation: touchLocation, spriteNode: spriteNode, touches: touches, event: event, entity: entity) } } @@ -85,16 +64,57 @@ class GameScene: SKScene{ child.touchesMoved(touches, with: event) } } + checkSlider() + let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) + + checkBases(bases: bases, touchLocation: touchLocation) + } + + override func update(_ currentTime: TimeInterval) { + entityManager.getBackground()?.update(deltaTime: currentTime) + entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() + } + + func addBaseDetails(touchLocation: CGPoint, spriteNode: SKNode?, touches: Set, event: UIEvent?, entity: GKEntity){ + if atPoint(touchLocation) == spriteNode && !entityManager.isModal { + spriteNode?.touchesBegan(touches, with: event) + if !entityManager.isModal { + for child in self.children { + if(child.name != "fire"){ + child.alpha = 0.3 + } + } + entityManager.add(Modal(modaltype: .BaseDetails, + base: entity as! Base, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) + } + } + } + + func addAttackDetails(touchLocation: CGPoint){ + for base in currentDraggedBase!.adjacencyList { + if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { + // TODO: change interaction based on collision instead of touchlocation + entityManager.add(Modal(modaltype: .BaseAttack, + base: currentDraggedBase!, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) + } + } + } + + func checkSlider(){ for e in entityManager.entities{ if let body = e.component(ofType: ModalContentComponent.self)?.body{ GameScene.sendUnits = ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up) body.text = "Schicke \(GameScene.sendUnits) Einheiten " } } - - let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) - + } + + func checkBases(bases: Set, touchLocation: CGPoint){ for base in bases { if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ if !isMoveTouch { @@ -103,22 +123,24 @@ class GameScene: SKScene{ } isMoveTouch = true - base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation - base.component(ofType: TeamComponent.self)?.fire.position = touchLocation - - for adjacencyBase in base.adjacencyList { - let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode - node?.run(SKAction.sequence([ - SKAction.resize(byWidth: 2, height: 2, duration: 0.5), - SKAction.resize(byWidth: -2, height: -2, duration: 0.5) - ])) - } + moveFireAndBase(base: base, touchLocation: touchLocation) + showNearestBases(base: base) } } } - override func update(_ currentTime: TimeInterval) { - entityManager.getBackground()?.update(deltaTime: currentTime) - entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() + func moveFireAndBase(base: Base, touchLocation: CGPoint){ + base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation + base.component(ofType: TeamComponent.self)?.fire.position = touchLocation + } + + func showNearestBases(base: Base){ + for adjacencyBase in base.adjacencyList { + let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode + node?.run(SKAction.sequence([ + SKAction.resize(byWidth: 2, height: 2, duration: 0.5), + SKAction.resize(byWidth: -2, height: -2, duration: 0.5) + ])) + } } }