Implement TimerComponent

This commit is contained in:
Daniel Steckert 2020-05-05 17:19:04 +02:00
parent 0b4e9ad48e
commit 58d7a01fd1
5 changed files with 63 additions and 1 deletions

View File

@ -17,6 +17,7 @@
11036113244B3E30008610AF /* MenuScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11036112244B3E30008610AF /* MenuScene.swift */; };
116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116060F6245C57D2004E5A36 /* EntityManager.swift */; };
11738A3B24508F68004426F1 /* Unit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11738A3A24508F68004426F1 /* Unit.swift */; };
2086465C2461B66200817C23 /* TimerComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2086465B2461B66200817C23 /* TimerComponent.swift */; };
3EBD242C245D8044003CECE7 /* GameCenterHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */; };
3EBD242E245D9332003CECE7 /* Team.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EBD242D245D9332003CECE7 /* Team.swift */; };
9E04AFAF245E2B73002D5CFC /* AttackActionComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */; };
@ -70,6 +71,7 @@
11036112244B3E30008610AF /* MenuScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuScene.swift; sourceTree = "<group>"; };
116060F6245C57D2004E5A36 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = "<group>"; };
11738A3A24508F68004426F1 /* Unit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Unit.swift; sourceTree = "<group>"; };
2086465B2461B66200817C23 /* TimerComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerComponent.swift; sourceTree = "<group>"; };
3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameCenterHelper.swift; sourceTree = "<group>"; };
3EBD242D245D9332003CECE7 /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = "<group>"; };
9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttackActionComponent.swift; sourceTree = "<group>"; };
@ -182,6 +184,7 @@
9E174C85245DD91500209FF0 /* ButtonComponent.swift */,
9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */,
9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */,
2086465B2461B66200817C23 /* TimerComponent.swift */,
);
path = Components;
sourceTree = "<group>";
@ -382,6 +385,7 @@
9E174C82245DD81D00209FF0 /* ButtonNode.swift in Sources */,
9E174C84245DD8CE00209FF0 /* Button.swift in Sources */,
110360DB244B101A008610AF /* GameViewController.swift in Sources */,
2086465C2461B66200817C23 /* TimerComponent.swift in Sources */,
110360D3244B101A008610AF /* AppDelegate.swift in Sources */,
9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */,
9E78ACC2245CC9EE00526FF7 /* DefBoostSkillComponent.swift in Sources */,

View File

@ -0,0 +1,48 @@
//
// TimerComponent.swift
// GoldWars
//
// Created by Daniel Steckert on 05.05.20.
// Copyright © 2020 SP2. All rights reserved.
//
import GameplayKit
class TimerComponent: GKComponent {
let labelNode :SKLabelNode
var endTime :Date!
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)
super.init()
startWithDuration(duration: 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 secondsToMinutesSeconds (seconds : Int) -> (Int, Int) {
return ((seconds % 3600) / 60, (seconds % 3600) % 60)
}
func update() {
self.labelNode.text = timeLeft()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

View File

@ -50,6 +50,9 @@ class EntityManager {
scene.addChild(skill.shapeNode)
scene.addChild(skill.labelNode)
}
if let timer = entity.component(ofType: TimerComponent.self) {
scene.addChild(timer.labelNode)
}
if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode {
scene.addChild(buttonNode)
}
@ -104,4 +107,8 @@ class EntityManager {
func getButtonByName(buttonName:String) -> Button{
return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button
}
func getHUD() -> GKEntity? {
return entities.filter{$0 is HUD}[0]
}
}

View File

@ -26,6 +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))
}
required init?(coder: NSCoder) {

View File

@ -115,5 +115,6 @@ class GameScene: SKScene{
override func update(_ currentTime: TimeInterval) {
entityManager.getBackground()?.update(deltaTime: currentTime)
entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update()
}
}