From 5d98027d8ae1f76ecf1adfe47be26f2bf319f4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Fri, 15 May 2020 13:19:57 +0200 Subject: [PATCH 01/12] Adding new Files for Cancel Button in ModalAttack --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 8 +++ GoldWars/GoldWars/CancelBtnComponent.swift | 27 ++++++++ GoldWars/GoldWars/CancelBtnNode.swift | 76 +++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 GoldWars/GoldWars/CancelBtnComponent.swift create mode 100644 GoldWars/GoldWars/CancelBtnNode.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index ba4f9b5..d1c2a7b 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -46,6 +46,8 @@ AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB1D759F245DEC0500671525 /* MapFactory.swift */; }; ABA03DA0244BD54F00A66916 /* Base.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA03D9F244BD54F00A66916 /* Base.swift */; }; AE151589245F18EF001D363E /* MatchmakingHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE151588245F18EF001D363E /* MatchmakingHelper.swift */; }; + AEBF3AFF246EB146004F7CD5 /* CancelBtnNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */; }; + AEBF3B01246EB187004F7CD5 /* CancelBtnComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEBF3B00246EB187004F7CD5 /* CancelBtnComponent.swift */; }; C04783EE2468583F004961FB /* intro-music.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = C04783ED2468583F004961FB /* intro-music.mp3 */; }; C04783F024685995004961FB /* SettingsScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04783EF24685995004961FB /* SettingsScene.swift */; }; C05FAED62468559D0006AF2E /* SoundManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C05FAED52468559D0006AF2E /* SoundManager.swift */; }; @@ -109,6 +111,8 @@ AB1D759F245DEC0500671525 /* MapFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapFactory.swift; sourceTree = ""; }; ABA03D9F244BD54F00A66916 /* Base.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Base.swift; sourceTree = ""; }; AE151588245F18EF001D363E /* MatchmakingHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatchmakingHelper.swift; sourceTree = ""; }; + AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancelBtnNode.swift; sourceTree = ""; }; + AEBF3B00246EB187004F7CD5 /* CancelBtnComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancelBtnComponent.swift; sourceTree = ""; }; C04783ED2468583F004961FB /* intro-music.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = "intro-music.mp3"; sourceTree = ""; }; C04783EF24685995004961FB /* SettingsScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsScene.swift; sourceTree = ""; }; C05FAED52468559D0006AF2E /* SoundManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundManager.swift; sourceTree = ""; }; @@ -174,6 +178,8 @@ 110360E4244B101B008610AF /* Info.plist */, AE151588245F18EF001D363E /* MatchmakingHelper.swift */, 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */, + AEBF3B00246EB187004F7CD5 /* CancelBtnComponent.swift */, + AEBF3AFE246EB146004F7CD5 /* CancelBtnNode.swift */, C04783EF24685995004961FB /* SettingsScene.swift */, C064E9A7246C0EA50022B228 /* LabelNode.swift */, C064E9A9246C114C0022B228 /* LabelComponent.swift */, @@ -407,6 +413,7 @@ C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, 3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */, AB1D759C245DD18100671525 /* MapProtocol.swift in Sources */, + AEBF3B01246EB187004F7CD5 /* CancelBtnComponent.swift in Sources */, AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */, AB1D759D245DD18100671525 /* TwoPlayerDefaultTestMap.swift in Sources */, 9EBFD7552462CF5A00E1E219 /* SliderComponent.swift in Sources */, @@ -415,6 +422,7 @@ 9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */, 9EC7E48B2461FBF700396BCD /* SliderNode.swift in Sources */, 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */, + AEBF3AFF246EB146004F7CD5 /* CancelBtnNode.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */, 2086465C2461B66200817C23 /* TimerComponent.swift in Sources */, 110360D3244B101A008610AF /* AppDelegate.swift in Sources */, diff --git a/GoldWars/GoldWars/CancelBtnComponent.swift b/GoldWars/GoldWars/CancelBtnComponent.swift new file mode 100644 index 0000000..7d15f68 --- /dev/null +++ b/GoldWars/GoldWars/CancelBtnComponent.swift @@ -0,0 +1,27 @@ +// +// CancelBtnComponent.swift +// GoldWars +// +// Created by Chauntalle Schüle on 15.05.20. +// Copyright © 2020 SP2. All rights reserved. +// +import GameplayKit +import SpriteKit + +class CancelBtnComponent: GKComponent { + + var cancelBtnNode: CancelBtnNode + + init(iconName: String, text: String, position: CGPoint, isEnabled:Bool, onButtonPress: @escaping () -> ()) { + cancelBtnNode = CancelBtnNode(iconName: iconName, + text: text, + isEnabled: isEnabled, + position: position, + onButtonPress: onButtonPress) + super.init() + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/GoldWars/GoldWars/CancelBtnNode.swift b/GoldWars/GoldWars/CancelBtnNode.swift new file mode 100644 index 0000000..fcd2382 --- /dev/null +++ b/GoldWars/GoldWars/CancelBtnNode.swift @@ -0,0 +1,76 @@ +// +// CancelBtnNode.swift +// GoldWars +// +// Created by Chauntalle Schüle on 15.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import SpriteKit + +class CancelBtnNode: SKSpriteNode { + + var isEnabled: Bool{ + didSet{ + if isEnabled { + self.alpha = 1 + self.childNode(withName: "label")?.alpha = 1 + } else { + self.alpha = 0.3 + self.childNode(withName: "label")?.alpha = 0.3 + } + } + } + + let onButtonPress: () -> () + + init(iconName: String, text: String, isEnabled: Bool, position: CGPoint, onButtonPress: @escaping () -> ()) { + self.onButtonPress = onButtonPress + self.isEnabled = isEnabled + let texture = SKTexture(imageNamed: "yellow_cross") + super.init(texture: texture, color: SKColor.white, size: texture.size()) + self.position = position + + let label = SKLabelNode(fontNamed: "Courier-Bold") + label.fontSize = 30 + label.fontColor = SKColor.black + label.zPosition = 1 + label.verticalAlignmentMode = .center + label.text = text + label.name = "label" + + if iconName.isEmpty { + label.position = CGPoint(x: 0, y: 0) + } else { + label.position = CGPoint(x: size.width * 0.25, y: 0) + let icon = SKSpriteNode(imageNamed: iconName) + icon.position = CGPoint(x: -size.width * 0.25, y: 0) + icon.zPosition = 1 + self.addChild(icon) + } + self.addChild(label) + + isUserInteractionEnabled = true + } + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + if isEnabled { + let action = SKAction.sequence( + [ + SKAction.scale(by: (3/4), duration: 0.3), + SKAction.scale(by: (4/3), duration: 0.3), + ] + ) + + self.childNode(withName: "label")?.run(action) + self.run(action) + onButtonPress() + } + } + + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} From 8b840b5089ad799b31484abf24fadb30eb9711ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Fri, 15 May 2020 13:29:03 +0200 Subject: [PATCH 02/12] Adding Abbrechen Button to ModalAttack --- GoldWars/GoldWars/Entities/EntityManager.swift | 14 +++++++++++--- GoldWars/GoldWars/Entities/Modal.swift | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 6afa00e..27758c7 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -68,6 +68,10 @@ class EntityManager { if let labelNode = entity.component(ofType: LabelComponent.self)?.labelNode { scene.addChild(labelNode) } + if let cancelBtnNode = entity.component(ofType: CancelBtnComponent.self)?.cancelBtnNode { + scene.addChild(cancelBtnNode) + isModal = true + } } func remove(_ entity: GKEntity) { @@ -90,6 +94,10 @@ class EntityManager { modalButton.buttonNode.removeFromParent() isModal = false } + if let cancelBtnNode = entity.component(ofType: CancelBtnComponent.self)?.cancelBtnNode { + cancelBtnNode.removeFromParent() + isModal = false + } entities.remove(entity) } @@ -100,9 +108,9 @@ class EntityManager { 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)! + 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 diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 516efb0..d7e7be9 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -30,6 +30,9 @@ class Modal: GKEntity{ addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: { self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) })) + addComponent(CancelBtnComponent(iconName: "", text: "", position: CGPoint(x: anchorPoint.x + 160, y: anchorPoint.y + 140), isEnabled: true, onButtonPress: { + self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) + })) } } From 938c35196b75f77340ea2a2a2829a77c25190432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Fri, 15 May 2020 13:30:56 +0200 Subject: [PATCH 03/12] Resizing Button Abbrechen in ModalAttack --- GoldWars/GoldWars/CancelBtnNode.swift | 39 ++++++++++++++------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/GoldWars/GoldWars/CancelBtnNode.swift b/GoldWars/GoldWars/CancelBtnNode.swift index fcd2382..34cad7f 100644 --- a/GoldWars/GoldWars/CancelBtnNode.swift +++ b/GoldWars/GoldWars/CancelBtnNode.swift @@ -29,27 +29,28 @@ class CancelBtnNode: SKSpriteNode { self.isEnabled = isEnabled let texture = SKTexture(imageNamed: "yellow_cross") super.init(texture: texture, color: SKColor.white, size: texture.size()) + self.size = CGSize(width: 30, height: 30) self.position = position - let label = SKLabelNode(fontNamed: "Courier-Bold") - label.fontSize = 30 - label.fontColor = SKColor.black - label.zPosition = 1 - label.verticalAlignmentMode = .center - label.text = text - label.name = "label" - - if iconName.isEmpty { - label.position = CGPoint(x: 0, y: 0) - } else { - label.position = CGPoint(x: size.width * 0.25, y: 0) - let icon = SKSpriteNode(imageNamed: iconName) - icon.position = CGPoint(x: -size.width * 0.25, y: 0) - icon.zPosition = 1 - self.addChild(icon) - } - self.addChild(label) - + let label = SKLabelNode(fontNamed: "Courier-Bold") + label.fontSize = 30 + label.fontColor = SKColor.black + label.zPosition = 1 + label.verticalAlignmentMode = .center + label.text = text + label.name = "label" + + if iconName.isEmpty { + label.position = CGPoint(x: 0, y: 0) + } else { + label.position = CGPoint(x: size.width * 0.25, y: 0) + let icon = SKSpriteNode(imageNamed: iconName) + icon.position = CGPoint(x: -size.width * 0.25, y: 0) + icon.zPosition = 1 + self.addChild(icon) + } + self.addChild(label) + isUserInteractionEnabled = true } From 7bebcf69df0646c95b04ffad7d9230398bf44fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Fri, 15 May 2020 13:34:26 +0200 Subject: [PATCH 04/12] cleaned Code --- GoldWars/GoldWars/CancelBtnNode.swift | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/GoldWars/GoldWars/CancelBtnNode.swift b/GoldWars/GoldWars/CancelBtnNode.swift index 34cad7f..770baa5 100644 --- a/GoldWars/GoldWars/CancelBtnNode.swift +++ b/GoldWars/GoldWars/CancelBtnNode.swift @@ -31,26 +31,6 @@ class CancelBtnNode: SKSpriteNode { super.init(texture: texture, color: SKColor.white, size: texture.size()) self.size = CGSize(width: 30, height: 30) self.position = position - - let label = SKLabelNode(fontNamed: "Courier-Bold") - label.fontSize = 30 - label.fontColor = SKColor.black - label.zPosition = 1 - label.verticalAlignmentMode = .center - label.text = text - label.name = "label" - - if iconName.isEmpty { - label.position = CGPoint(x: 0, y: 0) - } else { - label.position = CGPoint(x: size.width * 0.25, y: 0) - let icon = SKSpriteNode(imageNamed: iconName) - icon.position = CGPoint(x: -size.width * 0.25, y: 0) - icon.zPosition = 1 - self.addChild(icon) - } - self.addChild(label) - isUserInteractionEnabled = true } From a7e6cd9f10dcb40e8d804f2db9646c22f9bf8e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Sat, 16 May 2020 18:09:41 +0200 Subject: [PATCH 05/12] =?UTF-8?q?Feuer=20ergreift=20neue=20Basis=20nur,=20?= =?UTF-8?q?wenn=20im=20Modal=20Einheiten=20senden=20gedru=CC=88ckt=20wurde?= =?UTF-8?q?=20und=20gesendete=20Einheiten=20kommen=20an?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GoldWars/GoldWars/Entities/Modal.swift | 18 ++++++- GoldWars/GoldWars/Scenes/GameScene.swift | 67 ++++++++++++++++++++---- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index d7e7be9..9529120 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -11,8 +11,9 @@ import GameplayKit class Modal: GKEntity{ var unitCount:Int + //var currentDraggedBase: Base? - init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, entityManager: EntityManager, gameScene: SKScene) { + init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, entityManager: EntityManager, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint) { unitCount = base.unitCount super.init() switch modaltype{ @@ -28,7 +29,22 @@ class Modal: GKEntity{ addComponent(ModalContentComponent(header: "Angriff", body: "Schicke \(unitCount / 2) Einheiten", footer: "", anchorPoint: anchorPoint)) addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: { + //on Button press: sollen einheiten verrechnet werden,bei einheiten größer 0 feuer, entities removed werden +// self.currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos + for base in currentDraggedBase!.adjacencyList { + print("in here") + if gameScene.atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { + print("in here 2") + if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ + print("sendUnits \(Int(GameScene.sendUnits))") + entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) + // print("in here 3") + // sendUnits = 0 + } + } + } self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) + })) addComponent(CancelBtnComponent(iconName: "", text: "", position: CGPoint(x: anchorPoint.x + 160, y: anchorPoint.y + 140), isEnabled: true, onButtonPress: { self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index dae1566..2f2fa08 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -16,6 +16,7 @@ class GameScene: SKScene{ var isMoveTouch = false var currentDraggedBasePos = CGPoint() var currentDraggedBase : Base? + static var sendUnits: CGFloat = 0 override func sceneDidLoad() { entityManager = EntityManager(scene: self) @@ -28,7 +29,11 @@ class GameScene: SKScene{ MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) } + /* + Aufgerufen: wenn Prozess beendet: Basis auf eine andere gezogen + */ override func touchesEnded(_ touches: Set, with event: UIEvent?) { + print("touchesEnded") guard let touch = touches.first else { return } @@ -36,31 +41,57 @@ class GameScene: SKScene{ let touchLocation = touch.location(in: self) if isMoveTouch{ + /* + Aufgerufen: wenn Prozess beendet: Basis auf eine andere gezogen + sendet 0 Einheiten, weil noch nicht slider betätugt wurde + */ isMoveTouch = false - currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos + print("isMoveTouch false") + currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos //currentdraggedbase position gespeichert currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos + //currentDraggedBase bekommt Feuerposition?: wenn auskommentiert verliert nur die alte Position das Feuer, 2 Feuer sind dann auf der neusten Position for base in currentDraggedBase!.adjacencyList { + //basis wurde berührt if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { // TODO: change interaction based on collision instead of touchlocation - if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ + entityManager.add(Modal(modaltype: .BaseAttack, base: currentDraggedBase!, anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - entityManager: entityManager, gameScene: self)) - entityManager.update((currentDraggedBase?.attackBase(base: base, units: 100))!) - }else { + entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) + /* print("if") + print("sendUnits \(Int(self.sendUnits))") + entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(self.sendUnits)))!)*/ + }else { entityManager.add(Modal(modaltype: .BaseAttack, base: currentDraggedBase!, anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - entityManager: entityManager, gameScene: self)) + entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) + print("else") } } } } else { + print("else ismoveTouch is false") + /* + for base in currentDraggedBase!.adjacencyList { + print("in here") + if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { + print("in here 2") + // if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ + print("sendUnits \(Int(self.sendUnits))") + entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(self.sendUnits)))!) + // print("in here 3") + self.sendUnits = 0 + // } + } + }*/ + //add Base Details + for entity in entityManager.entities { let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode @@ -75,7 +106,8 @@ class GameScene: SKScene{ 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)) + entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) + print("Added Base Details") } } } @@ -83,7 +115,11 @@ class GameScene: SKScene{ } + /* + Dauernd aufgerufen beim Prozess: Eine Basis auf eine andere zu ziehen. + */ override func touchesMoved(_ touches: Set, with event: UIEvent?) { + print("touchesMoved") guard let touch = touches.first else { return } @@ -94,24 +130,33 @@ class GameScene: SKScene{ child.touchesMoved(touches, with: event) } } - + //checken des Modals/slider des Modals, anzeigen der ausgewählten Units for e in entityManager.entities{ if let body = e.component(ofType: ModalContentComponent.self)?.body{ - body.text = "Schicke \( ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up)) Einheiten " - } } - + GameScene.sendUnits = ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up) + print("slider unit \(GameScene.sendUnits)") + body.text = "Schicke \(GameScene.sendUnits) Einheiten " + } + } + // alle Basen eines Players let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) for base in bases { + //wenn die Touchlocation die position der Basis trifft if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ if !isMoveTouch { + print("neue currentdraggedbase + position") currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position currentDraggedBase = base } isMoveTouch = true + print("isMoveTouch true") + //Feuer & Basis müssen sich mitbewegen können base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation base.component(ofType: TeamComponent.self)?.fire.position = touchLocation + + // jede basis die in der Nähe ist soll vergrößert werden. for adjacencyBase in base.adjacencyList { let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode node?.run(SKAction.sequence([ From 25c57ceb74448f3ac367e3528fd4c37265c4afdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Sat, 16 May 2020 18:25:22 +0200 Subject: [PATCH 06/12] cleaned up modal, added func for sending units --- GoldWars/GoldWars/Entities/Modal.swift | 27 +++++++++++--------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 9529120..66e8ae6 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -11,7 +11,6 @@ import GameplayKit class Modal: GKEntity{ var unitCount:Int - //var currentDraggedBase: Base? init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, entityManager: EntityManager, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint) { unitCount = base.unitCount @@ -29,22 +28,8 @@ class Modal: GKEntity{ addComponent(ModalContentComponent(header: "Angriff", body: "Schicke \(unitCount / 2) Einheiten", footer: "", anchorPoint: anchorPoint)) addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: { - //on Button press: sollen einheiten verrechnet werden,bei einheiten größer 0 feuer, entities removed werden -// self.currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos - for base in currentDraggedBase!.adjacencyList { - print("in here") - if gameScene.atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { - print("in here 2") - if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ - print("sendUnits \(Int(GameScene.sendUnits))") - entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) - // print("in here 3") - // sendUnits = 0 - } - } - } + self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, entityManager: entityManager, gameScene: gameScene) self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) - })) addComponent(CancelBtnComponent(iconName: "", text: "", position: CGPoint(x: anchorPoint.x + 160, y: anchorPoint.y + 140), isEnabled: true, onButtonPress: { self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) @@ -68,4 +53,14 @@ class Modal: GKEntity{ } } } + + func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, entityManager: EntityManager, gameScene: GameScene){ + for base in currentDraggedBase!.adjacencyList { + if gameScene.atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { + if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ + entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) + } + } + } + } } From 8fabdda71840ace6805b4f09747e64d8cba10f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Sat, 16 May 2020 18:46:40 +0200 Subject: [PATCH 07/12] Prints und Kommentare entfernt --- GoldWars/GoldWars/Scenes/GameScene.swift | 51 ++---------------------- 1 file changed, 4 insertions(+), 47 deletions(-) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 2f2fa08..5b29421 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -16,7 +16,7 @@ class GameScene: SKScene{ var isMoveTouch = false var currentDraggedBasePos = CGPoint() var currentDraggedBase : Base? - static var sendUnits: CGFloat = 0 + static var sendUnits: CGFloat = 0 override func sceneDidLoad() { entityManager = EntityManager(scene: self) @@ -29,11 +29,7 @@ class GameScene: SKScene{ MapFactory(scene: self, entityManager: self.entityManager).loadMap(playerCount: 2) } - /* - Aufgerufen: wenn Prozess beendet: Basis auf eine andere gezogen - */ override func touchesEnded(_ touches: Set, with event: UIEvent?) { - print("touchesEnded") guard let touch = touches.first else { return } @@ -41,57 +37,29 @@ class GameScene: SKScene{ let touchLocation = touch.location(in: self) if isMoveTouch{ - /* - Aufgerufen: wenn Prozess beendet: Basis auf eine andere gezogen - sendet 0 Einheiten, weil noch nicht slider betätugt wurde - */ isMoveTouch = false - print("isMoveTouch false") currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos //currentdraggedbase position gespeichert currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos - //currentDraggedBase bekommt Feuerposition?: wenn auskommentiert verliert nur die alte Position das Feuer, 2 Feuer sind dann auf der neusten Position for base in currentDraggedBase!.adjacencyList { - //basis wurde berührt if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { // TODO: change interaction based on collision instead of touchlocation if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ - 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)) - /* print("if") - print("sendUnits \(Int(self.sendUnits))") - entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(self.sendUnits)))!)*/ - }else { + }else { 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)) - print("else") } } } } else { - print("else ismoveTouch is false") - /* - for base in currentDraggedBase!.adjacencyList { - print("in here") - if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { - print("in here 2") - // if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ - print("sendUnits \(Int(self.sendUnits))") - entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(self.sendUnits)))!) - // print("in here 3") - self.sendUnits = 0 - // } - } - }*/ - //add Base Details - for entity in entityManager.entities { let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode @@ -107,7 +75,6 @@ class GameScene: SKScene{ base: entity as! Base, anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), entityManager: entityManager, gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) - print("Added Base Details") } } } @@ -115,11 +82,7 @@ class GameScene: SKScene{ } - /* - Dauernd aufgerufen beim Prozess: Eine Basis auf eine andere zu ziehen. - */ override func touchesMoved(_ touches: Set, with event: UIEvent?) { - print("touchesMoved") guard let touch = touches.first else { return } @@ -130,33 +93,27 @@ class GameScene: SKScene{ child.touchesMoved(touches, with: event) } } - //checken des Modals/slider des Modals, anzeigen der ausgewählten Units + 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) - print("slider unit \(GameScene.sendUnits)") body.text = "Schicke \(GameScene.sendUnits) Einheiten " } } - // alle Basen eines Players + let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) for base in bases { - //wenn die Touchlocation die position der Basis trifft if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ if !isMoveTouch { - print("neue currentdraggedbase + position") currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position currentDraggedBase = base } isMoveTouch = true - print("isMoveTouch true") - //Feuer & Basis müssen sich mitbewegen können base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation base.component(ofType: TeamComponent.self)?.fire.position = touchLocation - // jede basis die in der Nähe ist soll vergrößert werden. for adjacencyBase in base.adjacencyList { let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode node?.run(SKAction.sequence([ From b79677a699650fe421b92ed8f166a13e19f183a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Sat, 16 May 2020 18:50:55 +0200 Subject: [PATCH 08/12] deleted comment and redundant if else --- GoldWars/GoldWars/Scenes/GameScene.swift | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 5b29421..2fa42b3 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -38,24 +38,16 @@ class GameScene: SKScene{ if isMoveTouch{ isMoveTouch = false - currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos //currentdraggedbase position gespeichert + 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 - if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ 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)) - }else { - 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)) - } - } } } 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 09/12] 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) + ])) + } } } From 735065412247678549e6e8dd5e0184e9c085ec8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Sun, 17 May 2020 21:09:38 +0200 Subject: [PATCH 10/12] resolved Merge conflicts --- GoldWars/GoldWars/Entities/Modal.swift | 24 ++++++++++++------------ GoldWars/GoldWars/Scenes/GameScene.swift | 22 +++++++++++----------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 66e8ae6..0414657 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -12,7 +12,7 @@ class Modal: GKEntity{ var unitCount:Int - init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, entityManager: EntityManager, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint) { + init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint) { unitCount = base.unitCount super.init() switch modaltype{ @@ -20,7 +20,7 @@ class Modal: GKEntity{ addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) addComponent(ModalContentComponent(header: "Basis Information", body: "Diese Basis enthält \(base.unitCount) Einheiten", footer: "", anchorPoint: anchorPoint)) addComponent(ButtonComponent(iconName: "", text: "Zurück", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: { - self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) + self.removeModalEntities(gameScene: gameScene) })) case .BaseAttack: addComponent(ModalBackgroundComponent(anchorPoint: anchorPoint)) @@ -28,11 +28,11 @@ class Modal: GKEntity{ addComponent(ModalContentComponent(header: "Angriff", body: "Schicke \(unitCount / 2) Einheiten", footer: "", anchorPoint: anchorPoint)) addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: { - self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, entityManager: entityManager, gameScene: gameScene) - self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) + self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, gameScene: gameScene) + self.removeModalEntities(gameScene: gameScene) })) addComponent(CancelBtnComponent(iconName: "", text: "", position: CGPoint(x: anchorPoint.x + 160, y: anchorPoint.y + 140), isEnabled: true, onButtonPress: { - self.removeModalEntities(entityManager: entityManager, gameScene: gameScene) + self.removeModalEntities(gameScene: gameScene) })) } } @@ -41,10 +41,10 @@ class Modal: GKEntity{ fatalError("init(coder:) has not been implemented") } - func removeModalEntities(entityManager: EntityManager, gameScene: SKScene){ - for entity in entityManager.entities { - if entityManager.isModal && entity.isMember(of: Modal.self) { - entityManager.remove(entity) + func removeModalEntities(gameScene: SKScene){ + for entity in EntityManager.sharedInstance.entities { + if EntityManager.sharedInstance.isModal && entity.isMember(of: Modal.self) { + EntityManager.sharedInstance.remove(entity) } for child in gameScene.children { if(child.name != "fire"){ @@ -54,11 +54,11 @@ class Modal: GKEntity{ } } - func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, entityManager: EntityManager, gameScene: GameScene){ + func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene){ for base in currentDraggedBase!.adjacencyList { if gameScene.atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { - if !(entityManager.getTeamByBase(base: currentDraggedBase!) == entityManager.getTeamByBase(base: base)){ - entityManager.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) + if !(EntityManager.sharedInstance.getTeamByBase(base: currentDraggedBase!) == EntityManager.sharedInstance.getTeamByBase(base: base)){ + EntityManager.sharedInstance.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) } } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 723bad6..ff7264a 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -43,7 +43,7 @@ class GameScene: SKScene{ addAttackDetails(touchLocation: touchLocation) } else { - for entity in entityManager.entities { + for entity in EntityManager.sharedInstance.entities { let spriteNode = entity.component(ofType: DefaultBaseComponent.self)?.spriteNode addBaseDetails(touchLocation: touchLocation, spriteNode: spriteNode, touches: touches, event: event, entity: entity) @@ -65,29 +65,29 @@ class GameScene: SKScene{ } checkSlider() - let bases = entityManager.getBasesByPlayer(for: GKLocalPlayer.local) + let bases = EntityManager.sharedInstance.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() + EntityManager.sharedInstance.getBackground()?.update(deltaTime: currentTime) + EntityManager.sharedInstance.getHUD()?.component(ofType: TimerComponent.self)?.update() } func addBaseDetails(touchLocation: CGPoint, spriteNode: SKNode?, touches: Set, event: UIEvent?, entity: GKEntity){ - if atPoint(touchLocation) == spriteNode && !entityManager.isModal { + if atPoint(touchLocation) == spriteNode && !EntityManager.sharedInstance.isModal { spriteNode?.touchesBegan(touches, with: event) - if !entityManager.isModal { + if !EntityManager.sharedInstance.isModal { for child in self.children { if(child.name != "fire"){ child.alpha = 0.3 } } - entityManager.add(Modal(modaltype: .BaseDetails, + EntityManager.sharedInstance.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)) + gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) } } } @@ -96,16 +96,16 @@ class GameScene: SKScene{ 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, + EntityManager.sharedInstance.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)) + gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) } } } func checkSlider(){ - for e in entityManager.entities{ + for e in EntityManager.sharedInstance.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 " From a41c9f4b30986fc8c9479409d1c1264b3ec6f32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Mon, 18 May 2020 21:21:06 +0200 Subject: [PATCH 11/12] Found Bug, base collision now always registered and bases with fire can still send units --- GoldWars/GoldWars/Entities/Modal.swift | 43 ++++++++++++++++++++---- GoldWars/GoldWars/Scenes/GameScene.swift | 21 ++++++++++-- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 0414657..c775179 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -12,7 +12,10 @@ class Modal: GKEntity{ var unitCount:Int - init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint) { + init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint, collisionBase: Base?) { + if currentDraggedBase == nil{ + print("nil") + } unitCount = base.unitCount super.init() switch modaltype{ @@ -28,7 +31,11 @@ class Modal: GKEntity{ addComponent(ModalContentComponent(header: "Angriff", body: "Schicke \(unitCount / 2) Einheiten", footer: "", anchorPoint: anchorPoint)) addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: { - self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, gameScene: gameScene) + if currentDraggedBase == nil{ + print("nil") + } + self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, gameScene: gameScene, collisionBase: collisionBase) + print("send units") self.removeModalEntities(gameScene: gameScene) })) addComponent(CancelBtnComponent(iconName: "", text: "", position: CGPoint(x: anchorPoint.x + 160, y: anchorPoint.y + 140), isEnabled: true, onButtonPress: { @@ -54,13 +61,37 @@ class Modal: GKEntity{ } } - func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene){ + func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){ + print("sendUnits: sendunits \(GameScene.sendUnits)") for base in currentDraggedBase!.adjacencyList { - if gameScene.atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { - if !(EntityManager.sharedInstance.getTeamByBase(base: currentDraggedBase!) == EntityManager.sharedInstance.getTeamByBase(base: base)){ + print("sendUnits: in here") + //print(base.component(ofType: DefaultBaseComponent.self)?.spriteNode) + if base == collisionBase { + print("sendUnits: in here 1") + print("sendUnits: sendunits \(GameScene.sendUnits)") EntityManager.sharedInstance.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) - } } } + /* if currentDraggedBase == nil{ + print("nil") + } + print(touchLocation.x) + print(touchLocation.y) + print("sendUnits: sendunits \(GameScene.sendUnits)") + for base in currentDraggedBase!.adjacencyList { + print("sendUnits: in here") + //print(base.component(ofType: DefaultBaseComponent.self)?.spriteNode) + if gameScene.atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { + // if !(EntityManager.sharedInstance.getTeamByBase(base: currentDraggedBase!) == EntityManager.sharedInstance.getTeamByBase(base: base)){ + print("sendUnits: in here 1") + print("sendUnits: sendunits \(GameScene.sendUnits)") + EntityManager.sharedInstance.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) + // } + } else if gameScene.atPoint(touchLocation) == currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode{ + print("sendUnits: in here 2") + print("sendUnits: sendunits \(GameScene.sendUnits)") + EntityManager.sharedInstance.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) + } + }*/ } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index ff7264a..6d790df 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -16,6 +16,7 @@ class GameScene: SKScene{ var currentDraggedBasePos = CGPoint() var currentDraggedBase : Base? static var sendUnits: CGFloat = 0 + var collisionBase: Base? override func sceneDidLoad() { EntityManager.sharedInstance.setScene(scene: self) @@ -29,6 +30,7 @@ class GameScene: SKScene{ } override func touchesEnded(_ touches: Set, with event: UIEvent?) { + print("touches ended") guard let touch = touches.first else { return } @@ -37,6 +39,7 @@ class GameScene: SKScene{ if isMoveTouch{ isMoveTouch = false + print("touches Ended: isMoveTouch false") currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos @@ -87,24 +90,32 @@ class GameScene: SKScene{ EntityManager.sharedInstance.add(Modal(modaltype: .BaseDetails, base: entity as! Base, anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) + gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) } } } func addAttackDetails(touchLocation: CGPoint){ + print("addAttackDetails") for base in currentDraggedBase!.adjacencyList { + print("base attack for") if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { + collisionBase = base + print(touchLocation.x) + print(touchLocation.y) + print("add Modal BaseAttack") + print("add AttackDetails: sendunits \(GameScene.sendUnits)") // TODO: change interaction based on collision instead of touchlocation EntityManager.sharedInstance.add(Modal(modaltype: .BaseAttack, base: currentDraggedBase!, anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation)) + gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) } } } func checkSlider(){ + print("slider") for e in EntityManager.sharedInstance.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) @@ -114,14 +125,17 @@ class GameScene: SKScene{ } func checkBases(bases: Set, touchLocation: CGPoint){ + print("checkbases") for base in bases { if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ + print("checkbases: touched base") if !isMoveTouch { currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position currentDraggedBase = base + print("checkbases: isMoveTouch false") } isMoveTouch = true - + print("checkbases: isMoveTouch true") moveFireAndBase(base: base, touchLocation: touchLocation) showNearestBases(base: base) } @@ -134,6 +148,7 @@ class GameScene: SKScene{ } func showNearestBases(base: Base){ + print("show nearest Bases") for adjacencyBase in base.adjacencyList { let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode node?.run(SKAction.sequence([ From 04170eb886fd3e2e266ddc862c89285b99935f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chauntalle=20Schu=CC=88le?= Date: Mon, 18 May 2020 21:31:32 +0200 Subject: [PATCH 12/12] cleaned up code --- GoldWars/GoldWars/Entities/Modal.swift | 35 +----------------------- GoldWars/GoldWars/Scenes/GameScene.swift | 33 ++++++---------------- 2 files changed, 10 insertions(+), 58 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index c775179..aadd374 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -13,9 +13,6 @@ class Modal: GKEntity{ var unitCount:Int init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint, collisionBase: Base?) { - if currentDraggedBase == nil{ - print("nil") - } unitCount = base.unitCount super.init() switch modaltype{ @@ -31,11 +28,7 @@ class Modal: GKEntity{ addComponent(ModalContentComponent(header: "Angriff", body: "Schicke \(unitCount / 2) Einheiten", footer: "", anchorPoint: anchorPoint)) addComponent(ButtonComponent(iconName: "", text: "Senden", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 120), isEnabled: true, onButtonPress: { - if currentDraggedBase == nil{ - print("nil") - } self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, gameScene: gameScene, collisionBase: collisionBase) - print("send units") self.removeModalEntities(gameScene: gameScene) })) addComponent(CancelBtnComponent(iconName: "", text: "", position: CGPoint(x: anchorPoint.x + 160, y: anchorPoint.y + 140), isEnabled: true, onButtonPress: { @@ -62,36 +55,10 @@ class Modal: GKEntity{ } func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){ - print("sendUnits: sendunits \(GameScene.sendUnits)") for base in currentDraggedBase!.adjacencyList { - print("sendUnits: in here") - //print(base.component(ofType: DefaultBaseComponent.self)?.spriteNode) if base == collisionBase { - print("sendUnits: in here 1") - print("sendUnits: sendunits \(GameScene.sendUnits)") - EntityManager.sharedInstance.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) - } - } - /* if currentDraggedBase == nil{ - print("nil") - } - print(touchLocation.x) - print(touchLocation.y) - print("sendUnits: sendunits \(GameScene.sendUnits)") - for base in currentDraggedBase!.adjacencyList { - print("sendUnits: in here") - //print(base.component(ofType: DefaultBaseComponent.self)?.spriteNode) - if gameScene.atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { - // if !(EntityManager.sharedInstance.getTeamByBase(base: currentDraggedBase!) == EntityManager.sharedInstance.getTeamByBase(base: base)){ - print("sendUnits: in here 1") - print("sendUnits: sendunits \(GameScene.sendUnits)") - EntityManager.sharedInstance.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) - // } - } else if gameScene.atPoint(touchLocation) == currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode{ - print("sendUnits: in here 2") - print("sendUnits: sendunits \(GameScene.sendUnits)") EntityManager.sharedInstance.update((currentDraggedBase?.attackBase(base: base, units: Int(GameScene.sendUnits)))!) } - }*/ + } } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 6d790df..fb229f8 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -30,7 +30,6 @@ class GameScene: SKScene{ } override func touchesEnded(_ touches: Set, with event: UIEvent?) { - print("touches ended") guard let touch = touches.first else { return } @@ -39,7 +38,6 @@ class GameScene: SKScene{ if isMoveTouch{ isMoveTouch = false - print("touches Ended: isMoveTouch false") currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBasePos currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBasePos @@ -88,34 +86,27 @@ class GameScene: SKScene{ } } EntityManager.sharedInstance.add(Modal(modaltype: .BaseDetails, - base: entity as! Base, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) + base: entity as! Base, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) } } } func addAttackDetails(touchLocation: CGPoint){ - print("addAttackDetails") for base in currentDraggedBase!.adjacencyList { - print("base attack for") if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { collisionBase = base - print(touchLocation.x) - print(touchLocation.y) - print("add Modal BaseAttack") - print("add AttackDetails: sendunits \(GameScene.sendUnits)") // TODO: change interaction based on collision instead of touchlocation EntityManager.sharedInstance.add(Modal(modaltype: .BaseAttack, - base: currentDraggedBase!, - anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), - gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) + base: currentDraggedBase!, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + gameScene: self, currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, collisionBase: collisionBase)) } } } func checkSlider(){ - print("slider") for e in EntityManager.sharedInstance.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) @@ -125,17 +116,12 @@ class GameScene: SKScene{ } func checkBases(bases: Set, touchLocation: CGPoint){ - print("checkbases") for base in bases { - if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ - print("checkbases: touched base") - if !isMoveTouch { - currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position - currentDraggedBase = base - print("checkbases: isMoveTouch false") + if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode{ if !isMoveTouch { + currentDraggedBasePos = base.component(ofType: DefaultBaseComponent.self)!.spriteNode.position + currentDraggedBase = base } isMoveTouch = true - print("checkbases: isMoveTouch true") moveFireAndBase(base: base, touchLocation: touchLocation) showNearestBases(base: base) } @@ -148,7 +134,6 @@ class GameScene: SKScene{ } func showNearestBases(base: Base){ - print("show nearest Bases") for adjacencyBase in base.adjacencyList { let node = adjacencyBase.component(ofType: DefaultBaseComponent.self)?.spriteNode node?.run(SKAction.sequence([