From 58d7a01fd19e8f1ebf2c1d1d2cbc03e0ce1965fc Mon Sep 17 00:00:00 2001 From: Daniel Steckert <71stda1bif@hft-stuttgart.de> Date: Tue, 5 May 2020 17:19:04 +0200 Subject: [PATCH] Implement TimerComponent --- GoldWars/GoldWars.xcodeproj/project.pbxproj | 4 ++ .../GoldWars/Components/TimerComponent.swift | 48 +++++++++++++++++++ .../GoldWars/Entities/EntityManager.swift | 7 +++ GoldWars/GoldWars/Entities/HUD.swift | 4 +- GoldWars/GoldWars/Scenes/GameScene.swift | 1 + 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 GoldWars/GoldWars/Components/TimerComponent.swift diff --git a/GoldWars/GoldWars.xcodeproj/project.pbxproj b/GoldWars/GoldWars.xcodeproj/project.pbxproj index c24d770..42aeab1 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 */; }; @@ -70,6 +71,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 = ""; }; @@ -182,6 +184,7 @@ 9E174C85245DD91500209FF0 /* ButtonComponent.swift */, 9E174C87245DF1FF00209FF0 /* BackgroundComponent.swift */, 9E04AFAE245E2B73002D5CFC /* AttackActionComponent.swift */, + 2086465B2461B66200817C23 /* TimerComponent.swift */, ); path = Components; sourceTree = ""; @@ -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 */, diff --git a/GoldWars/GoldWars/Components/TimerComponent.swift b/GoldWars/GoldWars/Components/TimerComponent.swift new file mode 100644 index 0000000..02900e9 --- /dev/null +++ b/GoldWars/GoldWars/Components/TimerComponent.swift @@ -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") + } + +} + diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index b8d8f71..ab8f38e 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -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] + } } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index 8c31654..ff717ee 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: "00:00", + anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 15 * 60)) } required init?(coder: NSCoder) { diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index c9f0dde..f3c7ddf 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -115,5 +115,6 @@ class GameScene: SKScene{ override func update(_ currentTime: TimeInterval) { entityManager.getBackground()?.update(deltaTime: currentTime) + entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update() } }