From 07e630881e571c5d4da6faa7ce7b2278ff614bf5 Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Tue, 16 Jun 2020 19:07:45 +0200 Subject: [PATCH 1/3] Remove fireNode if base has no ownership. Move unitcount Label from BaseComponent to Teamcomponent and remove it if base has no ownership. Remove unused Team-Enum cases. Remove unused team-colors. --- .../Components/DefaultBaseComponent.swift | 9 --- .../GoldWars/Components/TeamComponent.swift | 14 ++++- GoldWars/GoldWars/Entities/Base.swift | 6 +- .../GoldWars/Entities/EntityManager.swift | 58 +++++++++++-------- GoldWars/GoldWars/Entities/HUD.swift | 2 +- GoldWars/GoldWars/Enums/Team.swift | 4 +- GoldWars/GoldWars/Scenes/GameScene.swift | 2 +- 7 files changed, 52 insertions(+), 43 deletions(-) diff --git a/GoldWars/GoldWars/Components/DefaultBaseComponent.swift b/GoldWars/GoldWars/Components/DefaultBaseComponent.swift index 9d7df44..59a1f58 100644 --- a/GoldWars/GoldWars/Components/DefaultBaseComponent.swift +++ b/GoldWars/GoldWars/Components/DefaultBaseComponent.swift @@ -12,20 +12,11 @@ import GameKit class DefaultBaseComponent: GKComponent { var spriteNode: BaseNode - var labelNode : SKLabelNode init(texture: SKTexture, position: CGPoint) { spriteNode = BaseNode(texture: texture, size: CGSize(width: 100, height: 100)) spriteNode.position = position spriteNode.zPosition = 2 - labelNode = SKLabelNode(text: "") - labelNode.fontColor = SKColor.black - labelNode.horizontalAlignmentMode = .left - labelNode.verticalAlignmentMode = .center - labelNode.fontName = "AvenirNext-Bold" - labelNode.fontSize = 15 - labelNode.position = CGPoint(x: position.x + 30 , y: position.y - 50 ) - labelNode.zPosition = spriteNode.zPosition - 1 super.init() } diff --git a/GoldWars/GoldWars/Components/TeamComponent.swift b/GoldWars/GoldWars/Components/TeamComponent.swift index 87562af..314d77b 100644 --- a/GoldWars/GoldWars/Components/TeamComponent.swift +++ b/GoldWars/GoldWars/Components/TeamComponent.swift @@ -14,7 +14,8 @@ class TeamComponent: GKComponent { var team: Team var player: GKPlayer let fire: SKEmitterNode - + var unitcountLabel : SKLabelNode + init(team: Team, player: GKPlayer, position: CGPoint) { fire = SKEmitterNode(fileNamed: "Fire")! fire.zPosition = 1 @@ -25,6 +26,15 @@ class TeamComponent: GKComponent { self.team = team self.player = player + + unitcountLabel = SKLabelNode(text: "") + unitcountLabel.fontColor = SKColor.black + unitcountLabel.horizontalAlignmentMode = .left + unitcountLabel.verticalAlignmentMode = .center + unitcountLabel.fontName = "AvenirNext-Bold" + unitcountLabel.fontSize = 15 + unitcountLabel.position = CGPoint(x: position.x + 30 , y: position.y - 50 ) + unitcountLabel.zPosition = 3 super.init() fire.particleColor = getColor(by: team) } @@ -37,8 +47,6 @@ class TeamComponent: GKComponent { switch team { case .team1: return SKColor.red case .team2: return SKColor.purple - case .team3: return SKColor.green - case .team4: return SKColor.gray } } diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index a199068..8f14a8b 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -38,7 +38,7 @@ class Base: GKEntity{ self.unitCount = 500 } if ownershipPlayer == GKLocalPlayer.local { - self.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "\(unitCount)" + self.component(ofType: TeamComponent.self)?.unitcountLabel.text = "\(unitCount)" } } @@ -48,9 +48,9 @@ class Base: GKEntity{ } self.unitCount -= units base.unitCount += units - self.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "\(self.unitCount)" + self.component(ofType: TeamComponent.self)?.unitcountLabel.text = "\(self.unitCount)" if base.ownershipPlayer == GKLocalPlayer.local { - base.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "\(base.unitCount)" + base.component(ofType: TeamComponent.self)?.unitcountLabel.text = "\(base.unitCount)" } base.ownershipPlayer = self.ownershipPlayer DataService.sharedInstance.addMove(playerMove: PlayerMove(fromBase: self.baseID, diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 1e6e383..cc0f9ed 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -64,12 +64,14 @@ class EntityManager { } if let spriteNode = entity.component(ofType: DefaultBaseComponent.self) { - scene.addChild(spriteNode.labelNode) scene.addChild(spriteNode.spriteNode) } - if let fire = entity.component(ofType: TeamComponent.self)?.fire{ - scene.addChild(fire) + + if let spriteNode = entity.component(ofType: TeamComponent.self) { + scene.addChild(spriteNode.unitcountLabel) + scene.addChild(spriteNode.fire) } + if let buttonNode = entity.component(ofType: ButtonComponent.self)?.buttonNode { scene.addChild(buttonNode) } @@ -119,8 +121,9 @@ class EntityManager { position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! ) ) - if let fire = entity.component(ofType: TeamComponent.self)?.fire{ - scene.addChild(fire) + if let spriteNode = entity.component(ofType: TeamComponent.self) { + scene.addChild(spriteNode.unitcountLabel) + scene.addChild(spriteNode.fire) } } GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.capture.fifty.bases", increasePercentComplete: 2) @@ -140,31 +143,20 @@ class EntityManager { if snapBase.ownership != nil { getOwnerBySnapBase = GameCenterManager.sharedInstance.getGKPlayerByUsername(displayName: snapBase.ownership!) } else { - entity.removeComponent(ofType: TeamComponent.self) + if( entity.component(ofType: TeamComponent.self) != nil ) { + runExplosion(base: base) + entity.component(ofType: TeamComponent.self)!.fire.removeFromParent() + entity.component(ofType: TeamComponent.self)!.unitcountLabel.removeFromParent() + entity.removeComponent(ofType: TeamComponent.self) + } base.ownershipPlayer = nil } if getOwnerBySnapBase != nil { if base.ownershipPlayer != getOwnerBySnapBase { //TODO: Outsource following with a AnimationManager - let explosion = SKEmitterNode(fileNamed: "Explosion")! - scene.addChild(explosion) - explosion.zPosition = 2 - explosion.position = base.position - explosion.name = "explosion" - explosion.particleColorSequence = nil - explosion.particleColorBlendFactor = 1.0 - let wait = SKAction.wait(forDuration: 1) - let removeParticle = SKAction.removeFromParent() - let sequence = SKAction.sequence([wait, removeParticle]) - explosion.run(sequence) + runExplosion(base: base) } - - if getOwnerBySnapBase == GKLocalPlayer.local { - base.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "\(base.unitCount)" - } else { - base.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "" - } base.ownershipPlayer = getOwnerBySnapBase if (entity.component(ofType: TeamComponent.self) != nil) { entity.component(ofType: TeamComponent.self)!.change(to: getTeamByPlayer(playerName: snapBase.ownership!), to: getOwnerBySnapBase!) @@ -179,6 +171,12 @@ class EntityManager { scene.addChild(fire) } } + + if getOwnerBySnapBase == GKLocalPlayer.local { + base.component(ofType: TeamComponent.self)?.unitcountLabel.text = "\(base.unitCount)" + } else { + base.component(ofType: TeamComponent.self)?.unitcountLabel.text = "" + } } } @@ -186,6 +184,20 @@ class EntityManager { } + func runExplosion(base: Base) { + let explosion = SKEmitterNode(fileNamed: "Explosion")! + scene.addChild(explosion) + explosion.zPosition = 2 + explosion.position = base.position + explosion.name = "explosion" + explosion.particleColorSequence = nil + explosion.particleColorBlendFactor = 1.0 + let wait = SKAction.wait(forDuration: 1) + let removeParticle = SKAction.removeFromParent() + let sequence = SKAction.sequence([wait, removeParticle]) + explosion.run(sequence) + } + func getSnapshotBaseById(baseId: Int, snapshotModel: SnapshotModel) -> BaseEntityModel{ return snapshotModel.baseEntites.filter { $0.baseId == baseId }[0] } diff --git a/GoldWars/GoldWars/Entities/HUD.swift b/GoldWars/GoldWars/Entities/HUD.swift index ed34eb6..ddc695d 100644 --- a/GoldWars/GoldWars/Entities/HUD.swift +++ b/GoldWars/GoldWars/Entities/HUD.swift @@ -56,7 +56,7 @@ class HUD: GKEntity { isEnabled: true, position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.75, y: EntityManager.gameEMInstance.scene.size.height * 0.1), onButtonPress: { - EntityManager.gameEMInstance.getOpponentBases(for: EntityManager.gameEMInstance.getTeam()).forEach({base in base.component(ofType: DefaultBaseComponent.self)?.labelNode.text = "\(base.unitCount)"}) + EntityManager.gameEMInstance.getOpponentBases(for: EntityManager.gameEMInstance.getTeam()).forEach({base in base.component(ofType: TeamComponent.self)?.unitcountLabel.text = "\(base.unitCount)"}) GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.first.time", increasePercentComplete: 100) GameCenterManager.sharedInstance.addAchievementProgress(identifier: "de.hft.stuttgart.ip2.goldwars.skill.spy.ten", increasePercentComplete: 10) } diff --git a/GoldWars/GoldWars/Enums/Team.swift b/GoldWars/GoldWars/Enums/Team.swift index 15115fa..0ca3a21 100644 --- a/GoldWars/GoldWars/Enums/Team.swift +++ b/GoldWars/GoldWars/Enums/Team.swift @@ -9,8 +9,6 @@ enum Team: Int { case team1 = 1 case team2 = 2 - case team3 = 3 - case team4 = 4 - static let allValues = [team1, team2,team3,team4] + static let allValues = [team1, team2] } diff --git a/GoldWars/GoldWars/Scenes/GameScene.swift b/GoldWars/GoldWars/Scenes/GameScene.swift index c2f1d1a..b857f8d 100644 --- a/GoldWars/GoldWars/Scenes/GameScene.swift +++ b/GoldWars/GoldWars/Scenes/GameScene.swift @@ -155,7 +155,7 @@ class GameScene: SKScene{ func moveFireAndBase(base: Base, touchLocation: CGPoint){ base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation base.component(ofType: TeamComponent.self)?.fire.position = touchLocation - base.component(ofType: DefaultBaseComponent.self)?.labelNode.position = CGPoint(x:touchLocation.x + 30, y: touchLocation.y - 50) + base.component(ofType: TeamComponent.self)?.unitcountLabel.position = CGPoint(x:touchLocation.x + 30, y: touchLocation.y - 50) } func showNearestBases(base: Base){ From 71c01d589cf1de080b36867acde5552432f24d82 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Wed, 17 Jun 2020 23:29:05 +0200 Subject: [PATCH 2/3] Fix Spy-Skill to show labels again --- GoldWars/GoldWars/Entities/EntityManager.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index cc0f9ed..7e8601d 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -167,8 +167,9 @@ class EntityManager { position: (entity.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! ) ) - if let fire = entity.component(ofType: TeamComponent.self)?.fire{ - scene.addChild(fire) + if let spriteNode = entity.component(ofType: TeamComponent.self) { + scene.addChild(spriteNode.unitcountLabel) + scene.addChild(spriteNode.fire) } } From 5aa10e3fc56e4a355df463da97bbbd26e9dbdf5d Mon Sep 17 00:00:00 2001 From: Jakob Haag Date: Wed, 17 Jun 2020 23:34:48 +0200 Subject: [PATCH 3/3] review changes --- GoldWars/GoldWars/Entities/EntityManager.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 7e8601d..364c171 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -143,7 +143,7 @@ class EntityManager { if snapBase.ownership != nil { getOwnerBySnapBase = GameCenterManager.sharedInstance.getGKPlayerByUsername(displayName: snapBase.ownership!) } else { - if( entity.component(ofType: TeamComponent.self) != nil ) { + if entity.component(ofType: TeamComponent.self) != nil { runExplosion(base: base) entity.component(ofType: TeamComponent.self)!.fire.removeFromParent() entity.component(ofType: TeamComponent.self)!.unitcountLabel.removeFromParent() @@ -158,7 +158,7 @@ class EntityManager { } base.ownershipPlayer = getOwnerBySnapBase - if (entity.component(ofType: TeamComponent.self) != nil) { + if entity.component(ofType: TeamComponent.self) != nil { entity.component(ofType: TeamComponent.self)!.change(to: getTeamByPlayer(playerName: snapBase.ownership!), to: getOwnerBySnapBase!) } else { entity.addComponent(TeamComponent(