From 3e35db1d42eef92babf9e076e4491d03b921b0d5 Mon Sep 17 00:00:00 2001 From: Simon Kellner Date: Thu, 25 Jun 2020 19:09:16 +0200 Subject: [PATCH 1/2] Add Verlassen button with modal and end screen --- GoldWars/GoldWars/Components/ButtonNode.swift | 4 ++ .../GoldWars/Entities/EntityManager.swift | 10 +++ GoldWars/GoldWars/Entities/HUD.swift | 10 ++- GoldWars/GoldWars/Entities/Modal.swift | 33 ++++++--- GoldWars/GoldWars/GameCenterManager.swift | 7 ++ GoldWars/GoldWars/Scenes/GameScene.swift | 68 ++++++++++++++++++- GoldWars/GoldWars/SettingsScene.swift | 2 +- 7 files changed, 123 insertions(+), 11 deletions(-) diff --git a/GoldWars/GoldWars/Components/ButtonNode.swift b/GoldWars/GoldWars/Components/ButtonNode.swift index 8129fa9..786a4aa 100644 --- a/GoldWars/GoldWars/Components/ButtonNode.swift +++ b/GoldWars/GoldWars/Components/ButtonNode.swift @@ -43,6 +43,10 @@ class ButtonNode: SKSpriteNode { self.addChild(label) isUserInteractionEnabled = true } + + func setTexture(textureName: String) { + super.texture = SKTexture(imageNamed: textureName) + } override func touchesBegan(_ touches: Set, with event: UIEvent?) { if isEnabled { diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index a368d9e..b6a5f36 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -48,6 +48,7 @@ class EntityManager { scene.addChild(hudEntitiy.hostUnitsLabel) scene.addChild(hudEntitiy.peerLabel) scene.addChild(hudEntitiy.peerUnitsLabel) + scene.addChild(hudEntitiy.leaveGame) scene.addChild(hudEntitiy.defSkill) scene.addChild(hudEntitiy.atkSkill) scene.addChild(hudEntitiy.spySkill) @@ -357,6 +358,15 @@ class EntityManager { }) } + func exitToMenu() { + GameCenterManager.sharedInstance.reset() + EntityManager.gameEMInstance.getTimer()?.stopTimer() + RoundCalculatorService.sharedInstance.currentRound = 1 + EntityManager.gameEMInstance.scene.view?.presentScene(EntityManager.menuEMInstance.scene) + EntityManager.gameEMInstance.entities.removeAll() + EntityManager.gameEMInstance.scene.removeFromParent() + } + func getTimer() -> RoundTimer? { return getHUD()?.roundTimer } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index de2015b..070e857 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -18,6 +18,8 @@ class HUD: GKEntity { var peerLabel:SKLabelNode var peerUnitsLabel:SKLabelNode + var leaveGame: ButtonNode + var spySkill: SkillButtonNode var defSkill: SkillButtonNode var atkSkill: SkillButtonNode @@ -50,6 +52,11 @@ class HUD: GKEntity { self.roundTimer = RoundTimer() + leaveGame = ButtonNode(textureName: "yellow_button04", text: "Verlassen" , isEnabled: true, position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.17, y: EntityManager.gameEMInstance.scene.size.height * 0.04), onButtonPress: { + EntityManager.gameEMInstance.add(Modal(modaltype: .QuitGame, base: nil, anchorPoint: CGPoint(x: EntityManager.gameEMInstance.scene.size.width / 2 , y: EntityManager.gameEMInstance.scene.size.height / 2), gameScene: EntityManager.gameEMInstance.scene, currentDraggedBase: nil, touchLocation: nil, collisionBase: nil)) + }) + leaveGame.setScale(0.6) + spySkill = SkillButtonNode( textureName: "spy_button", text: "Spy", @@ -96,10 +103,11 @@ class HUD: GKEntity { isEnabled: true, position: CGPoint( x: EntityManager.gameEMInstance.scene.size.width * 0.17, - y: EntityManager.gameEMInstance.scene.size.height * 0.06), + y: EntityManager.gameEMInstance.scene.size.height * 0.09), onButtonPress: { } ) finishButton.size = CGSize(width: 125, height: 40) + finishButton.setScale(0.9) finishButton.zPosition = 2 backgroundRoundCounter = SKSpriteNode(texture: SKTexture(imageNamed: "roundInfo_texture")) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 760d3f5..cfc1e14 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -12,6 +12,7 @@ enum ModalType: String { case BaseAttack case BaseMoveOwnUnits case PauseGame + case QuitGame } class Modal: GKEntity{ @@ -26,7 +27,7 @@ class Modal: GKEntity{ var overlay: SKSpriteNode var type: ModalType - init(modaltype: ModalType, base: Base?, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint?, collisionBase: Base?) { + init(modaltype: ModalType, base: Base?, anchorPoint: CGPoint, gameScene: SKScene, currentDraggedBase: Base?, touchLocation: CGPoint?, collisionBase: Base?) { self.type = modaltype unitCount = 0 if base != nil { @@ -64,6 +65,10 @@ class Modal: GKEntity{ body = SKLabelNode(text: "Warte auf Gegner...") footer = SKLabelNode() closeButton.zPosition = -1 + case .QuitGame: + header = SKLabelNode(text: "Spiel verlassen") + body = SKLabelNode(text: "Sicher verlassen?") + footer = SKLabelNode() } self.header.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y + 90) @@ -86,13 +91,25 @@ class Modal: GKEntity{ super.init() + switch modaltype { + case .BaseAttack, .BaseMoveOwnUnits: + let text = (modaltype == .BaseAttack) ? "Angriff" : "Senden" + addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 15))) + addComponent(ButtonComponent(textureName: "yellow_button04", text: text, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: { + self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation!, gameScene: gameScene, collisionBase: collisionBase) + EntityManager.gameEMInstance.removeModal() + })) + case .QuitGame: + addComponent(ButtonComponent(textureName: "yellow_button04", text: "Verlassen", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: { + EntityManager.gameEMInstance.removeModal() + GameCenterManager.sharedInstance.sendStateToPeers(state: State(state: 6)) + GameCenterManager.sharedInstance.opponentQuit = false + GameCenterManager.sharedInstance.quitGame = true + })) + case .PauseGame: + print("") + } - let text = (modaltype == .BaseAttack) ? "Angriff" : "Senden" - addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 15))) - addComponent(ButtonComponent(textureName: "yellow_button04", text: text, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: { - self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation!, gameScene: gameScene, collisionBase: collisionBase) - EntityManager.gameEMInstance.removeModal() - })) } @@ -108,7 +125,7 @@ class Modal: GKEntity{ } } - func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){ + func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: SKScene, collisionBase: Base?){ for base in currentDraggedBase!.adjacencyList { if base == collisionBase { RoundCalculatorService.sharedInstance.increaseMoveCounter(ownBase: currentDraggedBase?.ownershipPlayer == base.ownershipPlayer) diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index f3b15df..97f4b03 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -24,6 +24,7 @@ struct State: Codable { // 3 Host hat Spiel gestartet // 4 Peer hat verloren // 5 Peer hat gewonnen + // 6 Peer hat Spiel verlassen let state: Int } @@ -47,6 +48,8 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG var gameEnded = false var winner: String? var gameScene: GameScene? + var quitGame: Bool = false + var opponentQuit: Bool = false static var isAuthenticated: Bool { return GKLocalPlayer.local.isAuthenticated } @@ -171,6 +174,10 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG os_log("State 5 erhalten, Peer hat gewonnen", log: LOG, type: .info) winner = peerPlayer?.displayName gameEnded = true + case 6: + os_log("State 6 erhalten, Peer hat Spiel verlassen ", log: LOG, type: .info) + opponentQuit = true + quitGame = true default: break } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 1ec9d7c..6acdaf1 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -81,9 +81,12 @@ class GameScene: SKScene{ if GameCenterManager.sharedInstance.gameEnded && !gameEndEffects { gameEnd() } - + if GameCenterManager.sharedInstance.quitGame { + gameQuit() + } } + func addAttackDetails(touchLocation: CGPoint){ for base in currentDraggedBase!.adjacencyList { if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { @@ -167,6 +170,66 @@ class GameScene: SKScene{ } } + func gameQuit() { + entityManager.getHUD()?.blockWholeScreenPane.isHidden = true + gameEndEffects = true + GameCenterManager.sharedInstance.quitGame = false + let move = SKAction.move(to: CGPoint(x: self.size.width / 2, y: self.size.height / 2), duration: 1) + let removeParticle = SKAction.removeFromParent() + let sequence = SKAction.sequence([move, removeParticle]) + var actionAdded = false + for nodeChild in children { + if nodeChild.name == "peerLabel" || nodeChild.name == "hostLabel"{ + nodeChild.removeFromParent() + } + if nodeChild.name == "way" { + nodeChild.run(SKAction.removeFromParent()) + continue + } + if nodeChild.name != "clouds"{ + nodeChild.run(sequence) { + if !actionAdded { + let explosion = self.getFinalExplosion() + self.addChild(explosion) + let action = SKAction.afterDelay(2) { + + + let node = ButtonNode(textureName: "yellow_button05", text: "Menü", isEnabled: true, position: CGPoint(x: self.size.width / 2, y: self.size.height / 2 - 300), onButtonPress: { + self.backToMenuAction() + }) + node.name = "BackButton" + self.addChild(node) + } + explosion.run(action) + actionAdded = true + } + } + } + } + let disconnectLabel = SKLabelNode.init(fontNamed: "Courier-Bold") + disconnectLabel.position = CGPoint(x: self.size.width/2, y: self.size.height/2) + disconnectLabel.horizontalAlignmentMode = .center + disconnectLabel.fontColor = UIColor.black + disconnectLabel.alpha = 0 + disconnectLabel.setScale(0.1) + if GameCenterManager.sharedInstance.opponentQuit { + if GameCenterManager.sharedInstance.localPlayer == GameCenterManager.sharedInstance.hostingPlayer { + disconnectLabel.text = GameCenterManager.sharedInstance.peerPlayer?.displayName + } else { + disconnectLabel.text = GameCenterManager.sharedInstance.hostingPlayer?.displayName + } + disconnectLabel.text?.append(" hat das Spiel verlassen!") + } else { + disconnectLabel.text = "Du hast das Spiel verlassen!" + } + let showUpAnimation = SKAction.sequence([ + SKAction.fadeAlpha(by: 1, duration: 2.5), + SKAction.scale(by: 10, duration: 1), + ]) + disconnectLabel.run(showUpAnimation) + self.addChild(disconnectLabel) + } + func initGameEndIcons() { let iclonSize = CGSize(width: 400, height: 400) let winnerIcon = SKSpriteNode(texture: SKTexture(imageNamed: "winner")); @@ -270,6 +333,8 @@ class GameScene: SKScene{ return collisionBase?.ownershipPlayer != currentDraggedBase?.ownershipPlayer && !(collisionBase?.changeOwnership ?? false) } + + @objc func pauseGame() -> Void { entityManager.removeModal() entityManager.getHUD()?.roundTimer.stopTimer() @@ -282,6 +347,7 @@ class GameScene: SKScene{ collisionBase: nil )) } + @objc func resumeGame() -> Void { entityManager.removeModal() entityManager.getHUD()?.roundTimer.resumeTimer() diff --git a/GoldWars/GoldWars/SettingsScene.swift b/GoldWars/GoldWars/SettingsScene.swift index f5bf7ea..3614b43 100644 --- a/GoldWars/GoldWars/SettingsScene.swift +++ b/GoldWars/GoldWars/SettingsScene.swift @@ -18,7 +18,7 @@ class SettingsScene: SKScene { entityManager.setScene(scene: self) let positionX = self.size.width * 0.1 let positionY = self.size.height * 0.05 - isMusicDeactivatedByUserDefault ? setMusicButtonTextByUserDefault(text: "Aus") : setMusicButtonTextByUserDefault(text: "Ein") + isMusicDeactivatedByUserDefault ? setMusicButtonTextByUserDefault(text: "Aus") : setMusicButtonTextByUserDefault(text: "An") entityManager.add(Button(name: "backToMenuScene", textureName: "yellow_button04", text: "Zurück", From 86a16c1d78c000905190331b7380002906a4c74c Mon Sep 17 00:00:00 2001 From: Aldin Duraki Date: Thu, 25 Jun 2020 19:32:11 +0200 Subject: [PATCH 2/2] ... --- GoldWars/GoldWars/Entities/Modal.swift | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index cfc1e14..0407dba 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -92,22 +92,22 @@ class Modal: GKEntity{ super.init() switch modaltype { - case .BaseAttack, .BaseMoveOwnUnits: - let text = (modaltype == .BaseAttack) ? "Angriff" : "Senden" - addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 15))) - addComponent(ButtonComponent(textureName: "yellow_button04", text: text, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: { - self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation!, gameScene: gameScene, collisionBase: collisionBase) - EntityManager.gameEMInstance.removeModal() - })) - case .QuitGame: - addComponent(ButtonComponent(textureName: "yellow_button04", text: "Verlassen", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: { - EntityManager.gameEMInstance.removeModal() - GameCenterManager.sharedInstance.sendStateToPeers(state: State(state: 6)) - GameCenterManager.sharedInstance.opponentQuit = false - GameCenterManager.sharedInstance.quitGame = true - })) - case .PauseGame: - print("") + case .BaseAttack, .BaseMoveOwnUnits: + let text = (modaltype == .BaseAttack) ? "Angriff" : "Senden" + addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 15))) + addComponent(ButtonComponent(textureName: "yellow_button04", text: text, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: { + self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation!, gameScene: gameScene, collisionBase: collisionBase) + EntityManager.gameEMInstance.removeModal() + })) + case .QuitGame: + addComponent(ButtonComponent(textureName: "yellow_button04", text: "Verlassen", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: { + EntityManager.gameEMInstance.removeModal() + GameCenterManager.sharedInstance.sendStateToPeers(state: State(state: 6)) + GameCenterManager.sharedInstance.opponentQuit = false + GameCenterManager.sharedInstance.quitGame = true + })) + case .PauseGame: + break }