Impl. AppDelegates handlings

This commit is contained in:
Aldin Duraki 2020-06-06 18:59:26 +02:00
parent 46fa90bfe3
commit a37187bbad
8 changed files with 109 additions and 30 deletions

View File

@ -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")
}
}

View File

@ -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

View File

@ -12,6 +12,7 @@ enum ModalType: String{
case BaseDetails
case BaseAttack
case BaseMoveOwnUnits
case PauseGame
}
class Modal: GKEntity{
@ -25,8 +26,13 @@ class Modal: GKEntity{
var footer: SKLabelNode
var overlay: SKSpriteNode
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?) {
unitCount = 0
if base != nil {
unitCount = base!.unitCount
}
let texture = SKTexture(imageNamed:"ModalBackground")
background = SKSpriteNode(texture: texture, size: texture.size())
@ -45,18 +51,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 +91,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
}
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
@ -180,4 +185,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()
}
}