Remove TimeComponent and set TimerLabel in the HUD. Reset Timer via RoundTimer

This commit is contained in:
Jakob Haag 2020-05-30 14:46:42 +02:00 committed by Marcel Schwarz
parent 5c6ee9fd7e
commit f882b7e615
8 changed files with 52 additions and 38 deletions

View File

@ -16,7 +16,6 @@
110360EE244B101B008610AF /* GoldWarsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360ED244B101B008610AF /* GoldWarsTests.swift */; }; 110360EE244B101B008610AF /* GoldWarsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110360ED244B101B008610AF /* GoldWarsTests.swift */; };
11036113244B3E30008610AF /* MenuScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11036112244B3E30008610AF /* MenuScene.swift */; }; 11036113244B3E30008610AF /* MenuScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11036112244B3E30008610AF /* MenuScene.swift */; };
116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116060F6245C57D2004E5A36 /* EntityManager.swift */; }; 116060F7245C57D2004E5A36 /* EntityManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116060F6245C57D2004E5A36 /* EntityManager.swift */; };
2086465C2461B66200817C23 /* TimerComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2086465B2461B66200817C23 /* TimerComponent.swift */; };
3E67854024728368007B9DE4 /* CElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElements.swift */; }; 3E67854024728368007B9DE4 /* CElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E67853F24728368007B9DE4 /* CElements.swift */; };
3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.swift */; }; 3E6785422472CBEC007B9DE4 /* Way.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785412472CBEC007B9DE4 /* Way.swift */; };
3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */; }; 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */; };
@ -80,7 +79,6 @@
110360EF244B101B008610AF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; 110360EF244B101B008610AF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
11036112244B3E30008610AF /* MenuScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuScene.swift; sourceTree = "<group>"; }; 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>"; }; 116060F6245C57D2004E5A36 /* EntityManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManager.swift; sourceTree = "<group>"; };
2086465B2461B66200817C23 /* TimerComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimerComponent.swift; sourceTree = "<group>"; };
3E67853F24728368007B9DE4 /* CElements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElements.swift; sourceTree = "<group>"; }; 3E67853F24728368007B9DE4 /* CElements.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CElements.swift; sourceTree = "<group>"; };
3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.swift; sourceTree = "<group>"; }; 3E6785412472CBEC007B9DE4 /* Way.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Way.swift; sourceTree = "<group>"; };
3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultWayComponent.swift; sourceTree = "<group>"; }; 3E6785432472CC27007B9DE4 /* DefaultWayComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultWayComponent.swift; sourceTree = "<group>"; };
@ -212,7 +210,6 @@
9EC7E48A2461FBF700396BCD /* SliderNode.swift */, 9EC7E48A2461FBF700396BCD /* SliderNode.swift */,
9EEDE02E246FCD800096C735 /* SpinningLogoComponent.swift */, 9EEDE02E246FCD800096C735 /* SpinningLogoComponent.swift */,
9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */, 9E78ACB7245CB75B00526FF7 /* TeamComponent.swift */,
2086465B2461B66200817C23 /* TimerComponent.swift */,
); );
path = Components; path = Components;
sourceTree = "<group>"; sourceTree = "<group>";
@ -413,7 +410,6 @@
8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */, 8BB6FF402472B8F000162BBD /* SingeClickButtonNode.swift in Sources */,
C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */, C064E9A8246C0EA50022B228 /* LabelNode.swift in Sources */,
3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */, 3F745DF0246F48FC00CE7375 /* PlayerMoveType.swift in Sources */,
AB1D75A0245DEC0500671525 /* MapFactory.swift in Sources */,
3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */, 3EAD889524801B6A0048A10A /* RoundTimer.swift in Sources */,
3E67854024728368007B9DE4 /* CElements.swift in Sources */, 3E67854024728368007B9DE4 /* CElements.swift in Sources */,
ABA03DA0244BD54F00A66916 /* Base.swift in Sources */, ABA03DA0244BD54F00A66916 /* Base.swift in Sources */,
@ -425,7 +421,6 @@
9E174C84245DD8CE00209FF0 /* Button.swift in Sources */, 9E174C84245DD8CE00209FF0 /* Button.swift in Sources */,
110360DB244B101A008610AF /* GameViewController.swift in Sources */, 110360DB244B101A008610AF /* GameViewController.swift in Sources */,
3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */, 3E6785442472CC27007B9DE4 /* DefaultWayComponent.swift in Sources */,
2086465C2461B66200817C23 /* TimerComponent.swift in Sources */,
C05BB9C4247D890C00411249 /* SliderComponent.swift in Sources */, C05BB9C4247D890C00411249 /* SliderComponent.swift in Sources */,
110360D3244B101A008610AF /* AppDelegate.swift in Sources */, 110360D3244B101A008610AF /* AppDelegate.swift in Sources */,
9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */, 9EC86B9F245C88A300796EF3 /* Modal.swift in Sources */,

View File

@ -53,7 +53,7 @@ class DataService {
var mapModel: MapGenerationModel? var mapModel: MapGenerationModel?
var entityManager = EntityManager.gameEMInstance var entityManager = EntityManager.gameEMInstance
func addMove(playerMove: PlayerMove) { func addMove(playerMove: PlayerMove) {
var equalMove = localRoundData.localPlayerMoves.filter { (ele) -> Bool in var equalMove = localRoundData.localPlayerMoves.filter { (ele) -> Bool in
ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase

View File

@ -42,14 +42,15 @@ class EntityManager {
isModal = true isModal = true
} }
if let hudEnitity = entity as? HUD { if let hudEntitiy = entity as? HUD {
scene.addChild(hudEnitity.hostLabel) scene.addChild(hudEntitiy.hostLabel)
scene.addChild(hudEnitity.hostUnitsLabel) scene.addChild(hudEntitiy.hostUnitsLabel)
scene.addChild(hudEnitity.peerLabel) scene.addChild(hudEntitiy.peerLabel)
scene.addChild(hudEnitity.peerUnitsLabel) scene.addChild(hudEntitiy.peerUnitsLabel)
scene.addChild(hudEnitity.defSkill) scene.addChild(hudEntitiy.defSkill)
scene.addChild(hudEnitity.atkSkill) scene.addChild(hudEntitiy.atkSkill)
scene.addChild(hudEnitity.spySkill) scene.addChild(hudEntitiy.spySkill)
scene.addChild(hudEntitiy.roundTimerLabel)
isModal = true isModal = true
} }
@ -60,9 +61,6 @@ class EntityManager {
if let fire = entity.component(ofType: TeamComponent.self)?.fire{ if let fire = entity.component(ofType: TeamComponent.self)?.fire{
scene.addChild(fire) scene.addChild(fire)
} }
if let timer = entity.component(ofType: TimerComponent.self) {
scene.addChild(timer.labelNode)
}
if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode { if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode {
scene.addChild(buttonNode) scene.addChild(buttonNode)
} }
@ -264,9 +262,6 @@ class EntityManager {
return SnapshotModel(baseEntites: snapBase) return SnapshotModel(baseEntites: snapBase)
} }
func getTimer() -> TimerComponent {
return entities.filter{$0 is HUD}[0].component(ofType: TimerComponent.self)!
}
func getUnitSum(by player: GKPlayer) -> Int{ func getUnitSum(by player: GKPlayer) -> Int{
let bases = getBasesByPlayer(for: player) let bases = getBasesByPlayer(for: player)
@ -307,4 +302,12 @@ class EntityManager {
} }
}) })
} }
func getTimer() -> RoundTimer? {
return getHUD()?.roundTimer
}
func updateTime(time: String) {
getHUD()?.roundTimerLabel.text = time
}
} }

View File

@ -24,6 +24,9 @@ class HUD: GKEntity {
var defSkill: SingeClickButtonNode var defSkill: SingeClickButtonNode
var atkSkill: SingeClickButtonNode var atkSkill: SingeClickButtonNode
var roundTimerLabel: SKLabelNode
let roundTimer: RoundTimer
init(size: CGSize) { init(size: CGSize) {
host = GameCenterManager.sharedInstance.hostingPlayer host = GameCenterManager.sharedInstance.hostingPlayer
peer = GameCenterManager.sharedInstance.peerPlayer peer = GameCenterManager.sharedInstance.peerPlayer
@ -32,6 +35,13 @@ class HUD: GKEntity {
peerLabel = SKLabelNode(text: peer?.displayName) peerLabel = SKLabelNode(text: peer?.displayName)
peerUnitsLabel = SKLabelNode(text: "500") peerUnitsLabel = SKLabelNode(text: "500")
roundTimerLabel = SKLabelNode(text: "")
roundTimerLabel.fontColor = UIColor.black
roundTimerLabel.fontSize = CGFloat(45)
roundTimerLabel.position = CGPoint(x: size.width * 0.5, y: size.height * 0.9 - 15)
self.roundTimer = RoundTimer()
spySkill = SingeClickButtonNode( spySkill = SingeClickButtonNode(
textureName: "yellow_circle", textureName: "yellow_circle",
text: "Spy", text: "Spy",
@ -62,7 +72,8 @@ class HUD: GKEntity {
peerUnitsLabel.position = CGPoint(x: size.width * 0.95, y: size.height * 0.9) peerUnitsLabel.position = CGPoint(x: size.width * 0.95, y: size.height * 0.9)
setColor(labelNodes: [hostLabel,hostUnitsLabel,peerLabel,peerUnitsLabel]) setColor(labelNodes: [hostLabel,hostUnitsLabel,peerLabel,peerUnitsLabel])
addComponent(TimerComponent(anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9))) roundTimer.initTimer()
startWithDuration()
} }
func updateUnitSum(){ func updateUnitSum(){
@ -80,4 +91,8 @@ class HUD: GKEntity {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
func startWithDuration(){
roundTimer.startTimer()
RoundCalculatorService.sharedInstance.isCalculating = false
}
} }

View File

@ -128,7 +128,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate ,GKM
if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) { if let snapshotModel = try? jsonDecoder.decode(SnapshotModel.self, from: data) {
DataService.sharedInstance.snapshotModel = snapshotModel DataService.sharedInstance.snapshotModel = snapshotModel
entityManager.updateSnapshotModel(snapshotModel: snapshotModel) entityManager.updateSnapshotModel(snapshotModel: snapshotModel)
entityManager.getTimer().startWithDuration() entityManager.getHUD()?.startWithDuration()
} }
if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) { if let mapModel = try? jsonDecoder.decode(MapGenerationModel.self, from: data) {
os_log("Peer hat Map erhalten", log: LOG, type: .info) os_log("Peer hat Map erhalten", log: LOG, type: .info)

View File

@ -128,8 +128,7 @@ class RoundCalculatorService {
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
DataService.sharedInstance.snapshotModel = currentSnapshotModel DataService.sharedInstance.snapshotModel = currentSnapshotModel
entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!) entityManager.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
sleep(1) entityManager.getHUD()?.startWithDuration()
entityManager.getTimer().startWithDuration()
os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info) os_log("Finished calculating Round", log: RoundCalculatorService.LOG, type: .info)
} }

View File

@ -12,10 +12,9 @@ class RoundTimer: Timer {
var timer: Timer? var timer: Timer?
var timeLeft: Int = 0 var timeLeft: Int = 0
var calculate = false
func startTimer() { func initTimer() {
print("start Timer")
self.timeLeft = 10
timer = Timer.scheduledTimer( timer = Timer.scheduledTimer(
timeInterval: 1.0, timeInterval: 1.0,
target: self, target: self,
@ -25,27 +24,31 @@ class RoundTimer: Timer {
) )
} }
func startTimer() {
timeLeft = 30
}
@objc func onTimerFires() @objc func onTimerFires()
{ {
print(timeLeft) timeLeft = timeLeft - 1
self.timeLeft = self.timeLeft - 1
EntityManager.gameEMInstance.updateTime(time: (timeLeft > 0 ? String(timeLeft) : "Syncing"))
if timeLeft == 0 { if timeLeft == 0 {
print("Time is up")
RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats() RoundCalculatorService.sharedInstance.resetNumberOfAttacksAndFormats()
print(!MultiplayerNetwork.sharedInstance.isSending)
if !MultiplayerNetwork.sharedInstance.isSending { if !MultiplayerNetwork.sharedInstance.isSending {
MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves) MultiplayerNetwork.sharedInstance.sendPlayerMoves(playerMoves: DataService.sharedInstance.localPlayerMoves)
} }
print(!RoundCalculatorService.sharedInstance.isCalculating) calculate = true
print(DataService.sharedInstance.didReceiveAllData()) }
print(GameCenterManager.sharedInstance.isServer) if timeLeft <= 0 {
if !RoundCalculatorService.sharedInstance.isCalculating if calculate
&& !RoundCalculatorService.sharedInstance.isCalculating
&& DataService.sharedInstance.didReceiveAllData() && DataService.sharedInstance.didReceiveAllData()
&& GameCenterManager.sharedInstance.isServer { && GameCenterManager.sharedInstance.isServer {
RoundCalculatorService.sharedInstance.calculateRound() RoundCalculatorService.sharedInstance.calculateRound()
calculate = false
} }
print("end method \(timeLeft)")
} }
} }
} }

View File

@ -76,7 +76,6 @@ class GameScene: SKScene{
override func update(_ currentTime: TimeInterval) { override func update(_ currentTime: TimeInterval) {
entityManager.getBackground()?.update(deltaTime: currentTime) entityManager.getBackground()?.update(deltaTime: currentTime)
entityManager.getHUD()?.component(ofType: TimerComponent.self)?.update()
} }
func addBaseDetails(touchLocation: CGPoint, spriteNode: SKNode?, touches: Set<UITouch>, event: UIEvent?, entity: GKEntity){ func addBaseDetails(touchLocation: CGPoint, spriteNode: SKNode?, touches: Set<UITouch>, event: UIEvent?, entity: GKEntity){