Add RoundTimer and adjust TimerComponent

This commit is contained in:
Jakob Haag 2020-05-28 18:21:09 +02:00 committed by Marcel Schwarz
parent 823dda29ef
commit 5c6ee9fd7e
7 changed files with 69 additions and 31 deletions

View File

@ -20,6 +20,7 @@
3E67854024728368007B9DE4 /* CElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElements.swift */; }; 3E67854024728368007B9DE4 /* CElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElements.swift */; };
3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.swift */; }; 3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.swift */; };
3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */; }; 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */; };
3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EAD889424801B6A0048A10A /* RoundTimer.swift */; };
3EBD242E245D9332003CECE7 /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242D245D9332003CECE7 /* Team.swift */; }; 3EBD242E245D9332003CECE7 /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242D245D9332003CECE7 /* Team.swift */; };
3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */; }; 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */; };
3FE19DB5246C7A22004827AB /* RoundCalculatorService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */; }; 3FE19DB5246C7A22004827AB /* RoundCalculatorService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */; };
@ -83,6 +84,7 @@
3E67853F24728368007B9DE4 /* CElements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElements.swift; sourceTree = "<group>"; }; 3E67853F24728368007B9DE4 /* CElements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElements.swift; sourceTree = "<group>"; };
3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.swift; sourceTree = "<group>"; }; 3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.swift; sourceTree = "<group>"; };
3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultWayComponent.swift; sourceTree = "<group>"; }; 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultWayComponent.swift; sourceTree = "<group>"; };
3EAD889424801B6A0048A10A /* RoundTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundTimer.swift; sourceTree = "<group>"; };
3EBD242D245D9332003CECE7 /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = "<group>"; }; 3EBD242D245D9332003CECE7 /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = "<group>"; };
3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerMoveType.swift; sourceTree = "<group>"; }; 3F745DEF246F48FC00CE7375 /* PlayerMoveType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerMoveType.swift; sourceTree = "<group>"; };
3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundCalculatorService.swift; sourceTree = "<group>"; }; 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RoundCalculatorService.swift; sourceTree = "<group>"; };
@ -179,6 +181,7 @@
3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */, 3FE19DB4246C7A22004827AB /* RoundCalculatorService.swift */,
9E0E459624796262009817A6 /* GameCenterManager.swift */, 9E0E459624796262009817A6 /* GameCenterManager.swift */,
C04783EF24685995004961FB /* SettingsScene.swift */, C04783EF24685995004961FB /* SettingsScene.swift */,
3EAD889424801B6A0048A10A /* RoundTimer.swift */,
); );
path = GoldWars; path = GoldWars;
sourceTree = "<group>"; sourceTree = "<group>";
@ -410,6 +413,8 @@
8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */, 8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */,
C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */,
3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */,
AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */,
3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */,
3E67854024728368007B9DE4 /* CElements.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */,
ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */,
C064E9AC246C151F0022B228 /* Label.swift in Sources */, C064E9AC246C151F0022B228 /* Label.swift in Sources */,

View File

@ -11,43 +11,25 @@ import GameplayKit
class TimerComponent: GKComponent { class TimerComponent: GKComponent {
let labelNode :SKLabelNode let labelNode :SKLabelNode
var endTime :Date! let roundTimer: RoundTimer
var duration :Double init(anchorPoint: CGPoint) {
var isRunning = false self.labelNode = SKLabelNode(text: "")
init(text: String, anchorPoint: CGPoint, duration: TimeInterval) {
self.labelNode = SKLabelNode(text: text)
self.labelNode.fontColor = UIColor.black self.labelNode.fontColor = UIColor.black
self.labelNode.fontSize = CGFloat(45) self.labelNode.fontSize = CGFloat(45)
self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15)
self.duration = duration + 1 self.roundTimer = RoundTimer()
super.init() super.init()
startWithDuration(duration: self.duration) startWithDuration()
} }
func startWithDuration(duration: TimeInterval){ func startWithDuration(){
isRunning = true print("startWithDuration")
endTime = Date().addingTimeInterval(duration) roundTimer.startTimer()
RoundCalculatorService.sharedInstance.isCalculating = false RoundCalculatorService.sharedInstance.isCalculating = false
} }
func timeLeft() -> Int { func timeLeft() -> Int {
if isRunning { self.roundTimer.timeLeft
let remainingSeconds = Int(endTime.timeIntervalSince(Date()))
if(remainingSeconds == 0) {
isRunning = false
}
return remainingSeconds
}
// if(remainingSeconds < 0){
// startWithDuration(duration: duration)
// }
return 0
}
func isFinished() -> Bool {
return timeLeft() == 0
} }
func update() { func update() {
@ -72,4 +54,3 @@ class TimerComponent: GKComponent {
} }
} }

View File

@ -62,7 +62,7 @@ class HUD: GKEntity {
peerUnitsLabel.position = CGPoint(x: size.width * 0.95, y: size.height * 0.9) peerUnitsLabel.position = CGPoint(x: size.width * 0.95, y: size.height * 0.9)
setColor(labelNodes: [hostLabel,hostUnitsLabel,peerLabel,peerUnitsLabel]) setColor(labelNodes: [hostLabel,hostUnitsLabel,peerLabel,peerUnitsLabel])
addComponent(TimerComponent(text: "", anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) addComponent(TimerComponent(anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9)))
} }
func updateUnitSum(){ func updateUnitSum(){

View File

@ -128,7 +128,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
DataService.sharedInstance.snapshotModel = snapshotModel DataService.sharedInstance.snapshotModel = snapshotModel
entityManager.updateSnapshotModel(snapshotModel: snapshotModel) entityManager.updateSnapshotModel(snapshotModel: snapshotModel)
entityManager.getTimer().startWithDuration(duration: 31) entityManager.getTimer().startWithDuration()
} }
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {
os_log("Peer hat Map erhalten", log: LOG, type: .info) os_log("Peer hat Map erhalten", log: LOG, type: .info)

View File

@ -38,6 +38,7 @@ class MultiplayerNetwork{
func sendPlayerMoves(localRoundData: LocalRoundData) { func sendPlayerMoves(localRoundData: LocalRoundData) {
if GameCenterManager.sharedInstance.isServer == false { if GameCenterManager.sharedInstance.isServer == false {
print("I am client")
self.isSending = true self.isSending = true
let encoder = JSONEncoder() let encoder = JSONEncoder()
let encoded = (try? encoder.encode(localRoundData))! let encoded = (try? encoder.encode(localRoundData))!

View File

@ -129,7 +129,7 @@ class RoundCalculatorService {
DataService.sharedInstance.snapshotModel = currentSnapshotModel DataService.sharedInstance.snapshotModel = currentSnapshotModel
entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
sleep(1) sleep(1)
entityManager.getTimer().startWithDuration(duration: 31) entityManager.getTimer().startWithDuration()
os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info)
} }

View File

@ -0,0 +1,51 @@
//
// RoundTimer.swift
// GoldWars
//
// Created by Jakob Haag on 28.05.20.
// Copyright © 2020 SP2. All rights reserved.
//
import Foundation
class RoundTimer: Timer {
var timer: Timer?
var timeLeft: Int = 0
func startTimer() {
print("start Timer")
self.timeLeft = 10
timer = Timer.scheduledTimer(
timeInterval: 1.0,
target: self,
selector: #selector(onTimerFires),
userInfo: nil,
repeats: true
)
}
@objc func onTimerFires()
{
print(timeLeft)
self.timeLeft = self.timeLeft - 1
if timeLeft == 0 {
print("Time is up")
RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats()
print(!MultiplayerNetwork.sharedInstance.isSending)
if !MultiplayerNetwork.sharedInstance.isSending {
MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves)
}
print(!RoundCalculatorService.sharedInstance.isCalculating)
print(DataService.sharedInstance.didReceiveAllData())
print(GameCenterManager.sharedInstance.isServer)
if !RoundCalculatorService.sharedInstance.isCalculating
&& DataService.sharedInstance.didReceiveAllData()
&& GameCenterManager.sharedInstance.isServer {
RoundCalculatorService.sharedInstance.calculateRound()
}
print("end method \(timeLeft)")
}
}
}