diff --git a/GoldWars/GoldWars/AppDelegate.swift b/GoldWars/GoldWars/AppDelegate.swift index 17e9b6c..4a72312 100644 --- a/GoldWars/GoldWars/AppDelegate.swift +++ b/GoldWars/GoldWars/AppDelegate.swift @@ -7,35 +7,45 @@ // import UIKit +import os @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + let LOG = OSLog.init(subsystem: "AppDelegate", category: "AppDelegate") func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. + os_log("application", log: LOG, type: .info) return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. + os_log("applicationWillResignActive", log: LOG, type: .debug) + } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + os_log("applicationDidEnterBackground", log: LOG, type: .debug) + NotificationCenter.default.post(name: Notification.Name(rawValue: "pauseGame"), object: nil) + MultiplayerNetwork.sharedInstance.sendNotificationToPlayer(name: "pauseGame") } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + os_log("applicationWillEnterForeground", log: LOG, type: .debug) } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + os_log("applicationDidBecomeActive", log: LOG, type: .debug) + NotificationCenter.default.post(name: Notification.Name(rawValue: "resumeGame"), object: nil) + MultiplayerNetwork.sharedInstance.sendNotificationToPlayer(name: "resumeGame") } - - } diff --git a/GoldWars/GoldWars/DataService.swift b/GoldWars/GoldWars/DataService.swift index 506478d..d8a9f24 100644 --- a/GoldWars/GoldWars/DataService.swift +++ b/GoldWars/GoldWars/DataService.swift @@ -6,6 +6,15 @@ // Copyright © 2020 SP2. All rights reserved. // + +struct NotificationModel: Codable { + let name: String + + init(name: String) { + self.name = name + } +} + struct PlayerMove: Codable { let fromBase: Int let toBase: Int diff --git a/GoldWars/GoldWars/Entities/Modal.swift b/GoldWars/GoldWars/Entities/Modal.swift index 9e9a0d4..3cbcf1d 100644 --- a/GoldWars/GoldWars/Entities/Modal.swift +++ b/GoldWars/GoldWars/Entities/Modal.swift @@ -12,6 +12,7 @@ enum ModalType: String{ case BaseDetails case BaseAttack case BaseMoveOwnUnits + case PauseGame } class Modal: GKEntity{ @@ -24,9 +25,15 @@ class Modal: GKEntity{ var body: SKLabelNode var footer: SKLabelNode var overlay: SKSpriteNode + var type: ModalType - init(modaltype: ModalType, base: Base, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint, collisionBase: Base?) { - unitCount = base.unitCount + init(modaltype: ModalType, base: Base?, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint?, collisionBase: Base?) { + self.type = modaltype + unitCount = 0 + if base != nil { + unitCount = base!.unitCount + } + let texture = SKTexture(imageNamed:"ModalBackground") background = SKSpriteNode(texture: texture, size: texture.size()) @@ -45,18 +52,23 @@ class Modal: GKEntity{ overlay.zPosition = 3 switch modaltype { - case .BaseDetails: - header = SKLabelNode(text: "Information") - body = SKLabelNode(text: "Diese Basis enthält \(base.unitCount) Einheiten") - footer = SKLabelNode() - case .BaseAttack: - header = SKLabelNode(text: "Angriff") - body = SKLabelNode(text: "Schicke \(unitCount / 2)\nEinheiten") - footer = SKLabelNode() - case .BaseMoveOwnUnits: - header = SKLabelNode(text: "Formation") - body = SKLabelNode(text: "Sende \(unitCount / 2)\nEinheiten") - footer = SKLabelNode() + case .BaseDetails: + header = SKLabelNode(text: "Information") + body = SKLabelNode(text: "Diese Basis enthält \(base!.unitCount) Einheiten") + footer = SKLabelNode() + case .BaseAttack: + header = SKLabelNode(text: "Angriff") + body = SKLabelNode(text: "Schicke \(unitCount / 2)\nEinheiten") + footer = SKLabelNode() + case .BaseMoveOwnUnits: + header = SKLabelNode(text: "Formation") + body = SKLabelNode(text: "Sende \(unitCount / 2)\nEinheiten") + footer = SKLabelNode() + case .PauseGame: + header = SKLabelNode(text: "Pause") + body = SKLabelNode(text: "Waiting for player to reconnect") + footer = SKLabelNode() + closeButton.zPosition = -1 } self.header.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y + 90) @@ -80,17 +92,19 @@ class Modal: GKEntity{ super.init() switch modaltype{ - case .BaseDetails: - addComponent(ButtonComponent(textureName: "yellow_button04", text: "Zurück", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 105), isEnabled: true, onButtonPress: { - EntityManager.gameEMInstance.removeModal() - })) - case .BaseAttack, .BaseMoveOwnUnits: - let text = (modaltype == .BaseAttack) ? "Angriff" : "Senden" - addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 50))) - addComponent(ButtonComponent(textureName: "yellow_button04", text: text, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 105), isEnabled: true, onButtonPress: { - self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation, gameScene: gameScene, collisionBase: collisionBase) - EntityManager.gameEMInstance.removeModal() - })) + case .BaseDetails: + addComponent(ButtonComponent(textureName: "yellow_button04", text: "Zurück", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 105), isEnabled: true, onButtonPress: { + EntityManager.gameEMInstance.removeModal() + })) + case .BaseAttack, .BaseMoveOwnUnits: + let text = (modaltype == .BaseAttack) ? "Angriff" : "Senden" + addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 50))) + addComponent(ButtonComponent(textureName: "yellow_button04", text: text, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 105), isEnabled: true, onButtonPress: { + self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation!, gameScene: gameScene, collisionBase: collisionBase) + EntityManager.gameEMInstance.removeModal() + })) + default: + break } } diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index d10654b..8d21a9f 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -185,6 +185,11 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG initIsFinish = true os_log("Peer startet Spiel", log: LOG, type: .info) } + + if let notification = try? jsonDecoder.decode(NotificationModel.self, from: data) { + os_log("Notification erhalten", log: LOG, type: .info) + NotificationCenter.default.post(name: Notification.Name(rawValue: notification.name), object: nil) + } MultiplayerNetwork.sharedInstance.isSending = false } diff --git a/GoldWars/GoldWars/GameViewController.swift b/GoldWars/GoldWars/GameViewController.swift index a3b45f5..0f4abbf 100644 --- a/GoldWars/GoldWars/GameViewController.swift +++ b/GoldWars/GoldWars/GameViewController.swift @@ -11,7 +11,9 @@ import SpriteKit import GameplayKit class GameViewController: UIViewController { - + public static let sharedInstance = GameViewController(); + var currentScene: SKView? + override func viewDidLoad() { super.viewDidLoad() @@ -25,9 +27,8 @@ class GameViewController: UIViewController { view.showsNodeCount = true } GameCenterManager.sharedInstance.viewController = self - - } + override var shouldAutorotate: Bool { return true } diff --git a/GoldWars/GoldWars/MultiplayerNetwork.swift b/GoldWars/GoldWars/MultiplayerNetwork.swift index aaac30d..b64d6c1 100644 --- a/GoldWars/GoldWars/MultiplayerNetwork.swift +++ b/GoldWars/GoldWars/MultiplayerNetwork.swift @@ -61,4 +61,9 @@ class MultiplayerNetwork{ sendData(data: encoded) } + func sendNotificationToPlayer(name: String) { + let encoder = JSONEncoder() + let encoded = (try? encoder.encode(NotificationModel(name: name)))! + sendData(data: encoded) + } } diff --git a/GoldWars/GoldWars/RoundTimer.swift b/GoldWars/GoldWars/RoundTimer.swift index 8f27a60..8435b7e 100644 --- a/GoldWars/GoldWars/RoundTimer.swift +++ b/GoldWars/GoldWars/RoundTimer.swift @@ -29,6 +29,20 @@ class RoundTimer: Timer { timeLeft = 30 } + func stopTimer() { + timer?.invalidate() + } + + func resumeTimer() { + timer = Timer.scheduledTimer( + timeInterval: 1.0, + target: self, + selector: #selector(onTimerFires), + userInfo: nil, + repeats: true + ) + } + @objc func onTimerFires() { timeLeft -= 1 diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index d8ad5bb..3158ca6 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -29,6 +29,11 @@ class GameScene: SKScene{ } } + 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) + } + override func touchesEnded(_ touches: Set, with event: UIEvent?) { guard let touch = touches.first else { return @@ -117,15 +122,17 @@ class GameScene: SKScene{ func checkSlider(){ for e in entityManager.entities{ if let modal = e as? Modal { - GameScene.sendUnits = ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up) - - //TODO: refactor this quick and dirty fix - if GameScene.sendUnits == 0 { - GameScene.sendUnits = 1 - } else if Int(GameScene.sendUnits) == currentDraggedBase?.unitCount { - GameScene.sendUnits -= 1 + if modal.type == ModalType.BaseAttack || modal.type == ModalType.BaseMoveOwnUnits { + GameScene.sendUnits = ((e.component(ofType: SliderComponent.self)?.sliderNode.getValue ?? 0) * CGFloat((e as! Modal).unitCount)).rounded(.up) + + //TODO: refactor this quick and dirty fix + if GameScene.sendUnits == 0 { + GameScene.sendUnits = 1 + } else if Int(GameScene.sendUnits) == currentDraggedBase?.unitCount { + GameScene.sendUnits -= 1 + } + modal.body.text = "Schicke \(GameScene.sendUnits) Einheiten " } - modal.body.text = "Schicke \(GameScene.sendUnits) Einheiten " } } } @@ -180,4 +187,21 @@ class GameScene: SKScene{ func isAttackMove() -> Bool { return collisionBase?.ownershipPlayer != currentDraggedBase?.ownershipPlayer } + + @objc func pauseGame() -> Void { + entityManager.add(Modal(modaltype: .PauseGame, + base: nil, + anchorPoint: CGPoint(x: self.size.width / 2 , y: self.size.height / 2), + gameScene: self, + currentDraggedBase: nil, + touchLocation: nil, + collisionBase: nil + )) + entityManager.getHUD()?.roundTimer.stopTimer() + } + @objc func resumeGame() -> Void { + entityManager.removeModal() + entityManager.getHUD()?.roundTimer.resumeTimer() + } + }