diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift index 02900e9..816ca04 100644 --- a/GoldWars/GoldWars/Components/TimerComponent.swift +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -12,32 +12,40 @@ class TimerComponent: GKComponent { let labelNode :SKLabelNode var endTime :Date! + var duration :Double init(text: String, anchorPoint: CGPoint, duration: TimeInterval) { self.labelNode = SKLabelNode(text: text) self.labelNode.fontColor = UIColor.black self.labelNode.fontSize = CGFloat(45) self.labelNode.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - 15) + self.duration = duration + 1 super.init() - startWithDuration(duration: duration) + startWithDuration(duration: self.duration) } func startWithDuration(duration: TimeInterval){ endTime = Date().addingTimeInterval(duration) } - func timeLeft() -> String { - let remainingSeconds = endTime.timeIntervalSince(Date()) - let timeLeft = (secondsToMinutesSeconds( seconds: Int(remainingSeconds))) - return String(timeLeft.0) + ":" + String(timeLeft.1) + func timeLeft() -> Int { + let remainingSeconds = Int(endTime.timeIntervalSince(Date())) + if(remainingSeconds < 0 ){ + startWithDuration(duration: duration) + } + return remainingSeconds } - func secondsToMinutesSeconds (seconds : Int) -> (Int, Int) { - return ((seconds % 3600) / 60, (seconds % 3600) % 60) + func isFinished() -> Bool { + return timeLeft() == 0 } func update() { - self.labelNode.text = timeLeft() + self.labelNode.text = String(timeLeft()) + + if(isFinished()){ + self.labelNode.text = "Synching" + } } required init?(coder: NSCoder) { diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index ff717ee..a161285 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -26,8 +26,8 @@ class HUD: GKEntity { texture: nil, anchorPoint: CGPoint(x: size.width * 0.95, y: size.height * 0.1))) - addComponent(TimerComponent(text: "00:00", - anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 15 * 60)) + addComponent(TimerComponent(text: "", + anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) } required init?(coder: NSCoder) {