diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index 11e05ac..199af2e 100644 --- a/GoldWars/GoldWars.xcodeproj/project.pbxproj +++ b/GoldWars/GoldWars.xcodeproj/project.pbxproj @@ -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 */; }; @@ -72,6 +73,7 @@ 11036112244B3E30008610AF /* MenuScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuScene.swift; sourceTree = ""; }; 116060F6245C57D2004E5A36 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = ""; }; 11738A3A24508F68004426F1 /* Unit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Unit.swift; sourceTree = ""; }; + 2086465B2461B66200817C23 /* TimerComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerComponent.swift; sourceTree = ""; }; 3EBD242B245D8044003CECE7 /* GameCenterHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameCenterHelper.swift; sourceTree = ""; }; 3EBD242D245D9332003CECE7 /* Team.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Team.swift; sourceTree = ""; }; 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttackActionComponent.swift; sourceTree = ""; }; @@ -186,6 +188,7 @@ 9E174C85245DD91500209FF0 /* ButtonComponent.swift */, 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */, 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */, + 2086465B2461B66200817C23 /* TimerComponent.swift */, 9EBFD7542462CF5A00E1E219 /* SliderComponent.swift */, 9EC7E48A2461FBF700396BCD /* SliderNode.swift */, ); @@ -390,6 +393,7 @@ 9EC7E48B2461FBF700396BCD /* SliderNode.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 */, diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift new file mode 100644 index 0000000..816ca04 --- /dev/null +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -0,0 +1,56 @@ +// +// 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! + 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: self.duration) + } + + func startWithDuration(duration: TimeInterval){ + endTime = Date().addingTimeInterval(duration) + } + + func timeLeft() -> Int { + let remainingSeconds = Int(endTime.timeIntervalSince(Date())) + if(remainingSeconds < 0 ){ + startWithDuration(duration: duration) + } + return remainingSeconds + } + + func isFinished() -> Bool { + return timeLeft() == 0 + } + + func update() { + self.labelNode.text = String(timeLeft()) + + if(isFinished()){ + self.labelNode.text = "Synching" + } + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + +} + diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index f10ef87..0f7166c 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -49,6 +49,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) } @@ -136,6 +139,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] + } } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 8c31654..a161285 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -25,7 +25,9 @@ class HUD: GKEntity { addComponent(DefBoostSkillComponent(text: "Def", texture: nil, anchorPoint: CGPoint(x: size.width * 0.95, y: size.height * 0.1))) - + + addComponent(TimerComponent(text: "", + anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) } required init?(coder: NSCoder) { diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index 435ff51..b306854 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -130,5 +130,6 @@ class GameScene: SKScene{ override func update(_ currentTime: TimeInterval) { entityManager.getBackground()?.update(deltaTime: currentTime) + entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() } }