Implement timer during rounds

This commit is contained in:
Daniel Steckert 2020-05-05 22:34:08 +02:00
parent 7254183d9e
commit 423ccc3208
2 changed files with 18 additions and 10 deletions

View File

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

View File

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