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.

This commit is contained in:
Jakob Haag 2020-06-16 19:07:45 +02:00
parent f50d00a416
commit 07e630881e
7 changed files with 52 additions and 43 deletions

View File

@ -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()
}

View File

@ -14,6 +14,7 @@ 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")!
@ -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
}
}

View File

@ -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,

View File

@ -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]
}

View File

@ -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)
}

View File

@ -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]
}

View File

@ -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){