From 3082297415b21bc611b1dcfbaadec576566a9a5e Mon Sep 17 00:00:00 2001 From: Niko Jochim <82joni1bif@hft-stuttgart.de> Date: Thu, 11 Jun 2020 21:21:11 +0200 Subject: [PATCH] create GameEnd Animation --- .../Components/DefaultWayComponent.swift | 29 +++++++ GoldWars/GoldWars/Entities/HUD.swift | 8 +- GoldWars/GoldWars/GameCenterManager.swift | 13 +++- GoldWars/GoldWars/GameViewController.swift | 1 + GoldWars/GoldWars/MultiplayerNetwork.swift | 19 +++-- GoldWars/GoldWars/RoundTimer.swift | 17 ++-- GoldWars/GoldWars/Scenes/GameScene.swift | 78 ++++++++++++++++++- 7 files changed, 138 insertions(+), 27 deletions(-) create mode 100644 GoldWars/GoldWars/Components/DefaultWayComponent.swift diff --git a/GoldWars/GoldWars/Components/DefaultWayComponent.swift b/GoldWars/GoldWars/Components/DefaultWayComponent.swift new file mode 100644 index 0000000..118532d --- /dev/null +++ b/GoldWars/GoldWars/Components/DefaultWayComponent.swift @@ -0,0 +1,29 @@ +// +// DefaultWayComponent.swift +// GoldWars +// +// Created by Jakob Haag on 18.05.20. +// Copyright © 2020 SP2. All rights reserved. +// + +import Foundation +import GameplayKit +import SpriteKit + +class DefaultWayComponent: GKComponent { + var shapeNode: SKShapeNode + + init(pathToDraw: CGMutablePath) { + self.shapeNode = SKShapeNode() + shapeNode.path = pathToDraw + shapeNode.strokeColor = UIColor(red: 0.852, green: 0.649, blue: 0.123, alpha: 1) + shapeNode.lineWidth = 10 + shapeNode.zPosition = 0 + shapeNode.name = "way" + super.init() + } + + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 3245b00..53ced57 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -57,7 +57,7 @@ class HUD: GKEntity { EntityManager.gameEMInstance.getOpponentBases(for: EntityManager.gameEMInstance.getTeam()).forEach({base in base.component(ofType: TeamComponent.self)?.unitcountLabel.text = "\(base.unitCount)"}) GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.first.time", increasePercentComplete: 100) GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.spy.ten", increasePercentComplete: 10) - } + } ) defSkill = SingeClickButtonNode( textureName: "yellow_circle", @@ -180,9 +180,9 @@ class HUD: GKEntity { func setCurrentRound(round: Int) -> Void { currentRoundLabel.text = "\(round)" let newRoundAction = SKAction.sequence([ - SKAction.scale(by: 2, duration: 1), - SKAction.scale(by: 0.5, duration: 1), - ]) + SKAction.scale(by: 2, duration: 1), + SKAction.scale(by: 0.5, duration: 1), + ]) currentRoundLabel.run(newRoundAction) } } diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index 9a81295..f6dcd69 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -29,7 +29,7 @@ struct State: Codable { final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKGameCenterControllerDelegate ,GKMatchDelegate,GKLocalPlayerListener{ - static let sharedInstance = GameCenterManager() + static var sharedInstance = GameCenterManager() let LOG = OSLog.init(subsystem: "GameCenterManager", category: "GameCenterManager") @@ -58,6 +58,17 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG localPlayerRandomNumber = RandomNumber() } + func reset(){ + + isMatchStarted = false + isServer = false + localPlayerRandomNumber = RandomNumber() + initIsFinish = false + gameEnded = false + winner = nil + gameScene = nil + } + func authUser() -> Void { GKLocalPlayer.local.authenticateHandler = { gcAuthVC, error in NotificationCenter.default diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index 0f4abbf..8061d97 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -44,4 +44,5 @@ class GameViewController: UIViewController { override var prefersStatusBarHidden: Bool { return true } + } diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index b64d6c1..0327243 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -15,15 +15,15 @@ class MultiplayerNetwork{ var isSending = false - func sendData(data: Data) { - let mmHelper = GameCenterManager.sharedInstance - if let multiplayerMatch = mmHelper.myMatch { - do { - try multiplayerMatch.sendData(toAllPlayers: data, with: .reliable) - } catch { - } - } - } + func sendData(data: Data) { + let mmHelper = GameCenterManager.sharedInstance + if let multiplayerMatch = mmHelper.myMatch { + do { + try multiplayerMatch.sendData(toAllPlayers: data, with: .reliable) + } catch { + } + } + } func sendDataToHost(data: Data) { @@ -38,7 +38,6 @@ class MultiplayerNetwork{ func sendPlayerMoves(localRoundData: LocalRoundData) { if GameCenterManager.sharedInstance.isServer == false { - print("I am client") self.isSending = true let encoder = JSONEncoder() let encoded = (try? encoder.encode(localRoundData))! diff --git a/GoldWars/GoldWars/RoundTimer.swift b/GoldWars/GoldWars/RoundTimer.swift index 819b5d7..4d4a699 100644 --- a/GoldWars/GoldWars/RoundTimer.swift +++ b/GoldWars/GoldWars/RoundTimer.swift @@ -45,7 +45,6 @@ class RoundTimer: Timer { @objc func onTimerFires() { - timeLeft -= 1 EntityManager.gameEMInstance.updateTime(time: (timeLeft > 0 ? String(timeLeft) : roundEnded)) @@ -55,16 +54,18 @@ class RoundTimer: Timer { if !MultiplayerNetwork.sharedInstance.isSending { MultiplayerNetwork.sharedInstance.sendPlayerMoves(localRoundData: DataService.sharedInstance.localRoundData) } - calculate = true - } - if timeLeft <= 0 { - if calculate - && !RoundCalculatorService.sharedInstance.isCalculating - && DataService.sharedInstance.didReceiveAllData() - && GameCenterManager.sharedInstance.isServer { + if timeLeft <= 0 { + if calculate + && !RoundCalculatorService.sharedInstance.isCalculating + && DataService.sharedInstance.didReceiveAllData() + && GameCenterManager.sharedInstance.isServer { RoundCalculatorService.sharedInstance.calculateRound() calculate = false + } } } + + + } } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index c8be9a7..b23726b 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -18,6 +18,7 @@ class GameScene: SKScene{ var currentDraggedBase : Base? static var sendUnits: Int = 0 var collisionBase: Base? + var gameEndEffects = false override func sceneDidLoad() { entityManager.setScene(scene: self) @@ -25,11 +26,12 @@ class GameScene: SKScene{ entityManager.add(Background(size: self.size)) if CommandLine.arguments.contains("--no-matchmaking") { _ = MapFactory(scene: self, entityManager: entityManager).load() - return + // let node = ButtonNode(textureName: "yellow_button05", text: "Menu", isEnabled: true, position: CGPoint(x: self.size.width / 2, y: self.size.height / 2 - 200), onButtonPress: {self.view?.presentScene(MenuScene(size: self.size)) }) + // self.addChild(node) } + return } - - override func didMove(to view: SKView) { + override func didMove(to view: SKView) { NotificationCenter.default.addObserver(self, selector: #selector(pauseGame), name: Notification.Name("pauseGame"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(resumeGame), name: Notification.Name("resumeGame"), object: nil) } @@ -76,7 +78,12 @@ class GameScene: SKScene{ } override func update(_ currentTime: TimeInterval) { - entityManager.getBackground()?.update(deltaTime: currentTime) + if entityManager.entities.count != 0 { + entityManager.getBackground()?.update(deltaTime: currentTime) + if GameCenterManager.sharedInstance.gameEnded && !gameEndEffects { + gameEnd() + } + } } func addAttackDetails(touchLocation: CGPoint){ @@ -121,6 +128,69 @@ class GameScene: SKScene{ } } + func gameEnd(){ + gameEndEffects = true + let winnerNode:String? + GameCenterManager.sharedInstance.gameEnded = false + + + if GameCenterManager.sharedInstance.isServer { + winnerNode = "hostLabel" + } else { + winnerNode = "peerLabel" + } + + 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 n = 0 + for nodeChild in children { + if nodeChild.name == winnerNode { + continue + } + if nodeChild.name == "way" { + nodeChild.run(SKAction.removeFromParent()) + continue + } + if nodeChild.name != "clouds"{ + nodeChild.run(sequence,completion: { + if n < 1 { + let explosion = SKEmitterNode(fileNamed: "Explosion")! + explosion.zPosition = 2 + explosion.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2) + explosion.name = "explosion" + explosion.particleColorSequence = nil + explosion.particleColorBlendFactor = 1.0 + self.addChild(explosion) + let action = SKAction.afterDelay(2) { + let size = SKAction.scale(by: 3, duration: 1) + (self.childNode(withName: winnerNode!) as! SKLabelNode).horizontalAlignmentMode = .center + self.childNode(withName: winnerNode!)?.run(SKAction.sequence([move,size])) + let node = ButtonNode(textureName: "yellow_button05", text: "Menu", isEnabled: true, position: CGPoint(x: self.size.width / 2, y: self.size.height / 2 - 200), onButtonPress: { + self.buttonPressed() + }) + node.name = "BackButton" + self.addChild(node) + } + explosion.run(action) + n += 1 + } + + }) + } + } + } + + func buttonPressed() { + GameCenterManager.sharedInstance.reset(); + self.gameEndEffects = false + self.view?.presentScene(MenuScene(size: self.size)) + entityManager.entities.removeAll() + self.removeFromParent() + + } + func checkBases(bases: Set, touchLocation: CGPoint){ for base in bases { if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode {