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 let labelNode :SKLabelNode
var endTime :Date! var endTime :Date!
var duration :Double
init(text: String, anchorPoint: CGPoint, duration: TimeInterval) { init(text: String, anchorPoint: CGPoint, duration: TimeInterval) {
self.labelNode = SKLabelNode(text: text) 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
super.init() super.init()
startWithDuration(duration: duration) startWithDuration(duration: self.duration)
} }
func startWithDuration(duration: TimeInterval){ func startWithDuration(duration: TimeInterval){
endTime = Date().addingTimeInterval(duration) endTime = Date().addingTimeInterval(duration)
} }
func timeLeft() -> String { func timeLeft() -> Int {
let remainingSeconds = endTime.timeIntervalSince(Date()) let remainingSeconds = Int(endTime.timeIntervalSince(Date()))
let timeLeft = (secondsToMinutesSeconds( seconds: Int(remainingSeconds))) if(remainingSeconds < 0 ){
return String(timeLeft.0) + ":" + String(timeLeft.1) startWithDuration(duration: duration)
}
return remainingSeconds
} }
func secondsToMinutesSeconds (seconds : Int) -> (Int, Int) { func isFinished() -> Bool {
return ((seconds % 3600) / 60, (seconds % 3600) % 60) return timeLeft() == 0
} }
func update() { func update() {
self.labelNode.text = timeLeft() self.labelNode.text = String(timeLeft())
if(isFinished()){
self.labelNode.text = "Synching"
}
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {

View File

@ -26,8 +26,8 @@ class HUD: GKEntity {
texture: nil, texture: nil,
anchorPoint: CGPoint(x: size.width * 0.95, y: size.height * 0.1))) anchorPoint: CGPoint(x: size.width * 0.95, y: size.height * 0.1)))
addComponent(TimerComponent(text: "00:00", addComponent(TimerComponent(text: "",
anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 15 * 60)) anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30))
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {