set zPosistion on all Nodes

update host and peer Label Nodes
This commit is contained in:
Niko Jochim 2020-05-21 19:40:46 +02:00
parent bf8ce0c477
commit b2f84bc3f4
6 changed files with 51 additions and 41 deletions

View File

@ -11,26 +11,25 @@ import SpriteKit
import GameKit import GameKit
class DefaultBaseComponent: GKComponent { class DefaultBaseComponent: GKComponent {
var spriteNode: BaseNode var spriteNode: BaseNode
var labelNode : SKLabelNode? var labelNode : SKLabelNode?
init(texture: SKTexture, position: CGPoint) {
init(texture: SKTexture, position: CGPoint) { spriteNode = BaseNode(texture: texture, size: CGSize(width: 80, height: 80))
spriteNode = BaseNode(texture: texture, size: CGSize(width: 80, height: 80)) spriteNode.position = position
spriteNode.position = position
spriteNode.zPosition = 2 spriteNode.zPosition = 2
super.init() super.init()
labelNode = SKLabelNode(text: "") labelNode = SKLabelNode(text: "")
labelNode?.fontColor = SKColor.black labelNode?.fontColor = SKColor.black
labelNode?.horizontalAlignmentMode = .left labelNode?.horizontalAlignmentMode = .left
labelNode?.verticalAlignmentMode = .center labelNode?.verticalAlignmentMode = .center
labelNode?.fontName = "AvenirNext-Bold" labelNode?.fontName = "AvenirNext-Bold"
labelNode?.fontSize = 15 labelNode?.fontSize = 15
labelNode?.position = CGPoint(x: position.x + 20 , y: position.y - 40 ) labelNode?.position = CGPoint(x: position.x + 30 , y: position.y - 50 )
labelNode?.zPosition = spriteNode.zPosition - 1 labelNode?.zPosition = spriteNode.zPosition - 1
} }
required init?(coder aDecoder: NSCoder) { required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
} }

View File

@ -15,46 +15,37 @@ class PlayerInfoComponent: GKComponent {
var hostLabel:SKLabelNode var hostLabel:SKLabelNode
var hostUnitsLabel:SKLabelNode var hostUnitsLabel:SKLabelNode
var peerLabel:SKLabelNode var peerLabel:SKLabelNode
var peerUnitsLabel:SKLabelNode var peerUnitsLabel:SKLabelNode
var host: GKPlayer?
var peer: GKPlayer?
init(size: CGSize) { init(size: CGSize) {
var host: String?
var peer: String?
if MatchmakingHelper.sharedInstance.isServer { if MatchmakingHelper.sharedInstance.isServer {
host = GKLocalPlayer.local.displayName host = GKLocalPlayer.local
peer = MatchmakingHelper.sharedInstance.mpMatch?.players[0].displayName peer = MatchmakingHelper.sharedInstance.mpMatch?.players[0]
} else { } else {
host = MatchmakingHelper.sharedInstance.mpMatch?.players[0].displayName host = MatchmakingHelper.sharedInstance.mpMatch?.players[0]
peer = GKLocalPlayer.local.displayName peer = GKLocalPlayer.local
} }
hostLabel = SKLabelNode(text: host?.displayName)
hostUnitsLabel = SKLabelNode(text: "500" )
hostLabel = SKLabelNode(text: host) peerLabel = SKLabelNode(text: peer?.displayName)
hostUnitsLabel = SKLabelNode(text: "500")
peerLabel = SKLabelNode(text: peer)
peerUnitsLabel = SKLabelNode(text: "500") peerUnitsLabel = SKLabelNode(text: "500")
super.init() super.init()
hostLabel.position = CGPoint(x: size.width * 0.02, y: size.height * 0.95) hostLabel.position = CGPoint(x: size.width * 0.02, y: size.height * 0.95)
hostLabel.horizontalAlignmentMode = .left hostLabel.horizontalAlignmentMode = .left
peerLabel.position = CGPoint(x: size.width * 0.98, y: size.height * 0.95) peerLabel.position = CGPoint(x: size.width * 0.98, y: size.height * 0.95)
peerLabel.horizontalAlignmentMode = .right peerLabel.horizontalAlignmentMode = .right
hostUnitsLabel.position = CGPoint(x: size.width * 0.05, y: size.height * 0.9) hostUnitsLabel.position = CGPoint(x: size.width * 0.05, y: size.height * 0.9)
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])
}
func update(){
hostUnitsLabel.text = "\(EntityManager.sharedInstance.getUnitSum(by: host!))"
peerUnitsLabel.text = "\(EntityManager.sharedInstance.getUnitSum(by: peer!))"
} }
func setColor(labelNodes: [SKLabelNode]) -> Void { func setColor(labelNodes: [SKLabelNode]) -> Void {

View File

@ -37,8 +37,11 @@ class Base: GKEntity{
addComponent(TeamComponent(team: team!, player: player!, position: spritePos)) addComponent(TeamComponent(team: team!, player: player!, position: spritePos))
self.unitCount = 500 self.unitCount = 500
} }
if ownershipPlayer == GKLocalPlayer.local {
self.component(ofType: DefaultBaseComponent.self)?.labelNode?.text = "\(unitCount)"
}
self.component(ofType: DefaultBaseComponent.self)?.labelNode?.text = "\(unitCount)"
} }
func doPlayerMoveTypeToBase(base: Base, playerMoveType: PlayerMoveType, units: Int) -> [GKEntity]{ func doPlayerMoveTypeToBase(base: Base, playerMoveType: PlayerMoveType, units: Int) -> [GKEntity]{

View File

@ -150,13 +150,18 @@ class EntityManager {
let snapBase = self.getSnapshotBaseById(baseId: base.baseID, snapshotModel: snapshotModel) let snapBase = self.getSnapshotBaseById(baseId: base.baseID, snapshotModel: snapshotModel)
var getOwnerBySnapBase: GKPlayer? = nil var getOwnerBySnapBase: GKPlayer? = nil
base.unitCount = snapBase.unitCount base.unitCount = snapBase.unitCount
base.component(ofType: DefaultBaseComponent.self)?.labelNode?.text = "\(base.unitCount)"
if snapBase.ownership != nil { if snapBase.ownership != nil {
getOwnerBySnapBase = MatchmakingHelper.sharedInstance.getGKPlayerByUsername(displayName: snapBase.ownership!) getOwnerBySnapBase = MatchmakingHelper.sharedInstance.getGKPlayerByUsername(displayName: snapBase.ownership!)
} else { } else {
entity.removeComponent(ofType: TeamComponent.self) entity.removeComponent(ofType: TeamComponent.self)
} }
if getOwnerBySnapBase != nil { if getOwnerBySnapBase != nil {
if getOwnerBySnapBase == GKLocalPlayer.local {
base.component(ofType: DefaultBaseComponent.self)?.labelNode?.text = "\(base.unitCount)"
}else {
base.component(ofType: DefaultBaseComponent.self)?.labelNode?.text = ""
}
base.changeOwnership = true base.changeOwnership = true
base.ownershipPlayer = getOwnerBySnapBase base.ownershipPlayer = getOwnerBySnapBase
if (entity.component(ofType: TeamComponent.self) != nil) { if (entity.component(ofType: TeamComponent.self) != nil) {
@ -174,6 +179,9 @@ class EntityManager {
} }
} }
} }
getHUD()?.component(ofType: PlayerInfoComponent.self)?.update()
} }
func getSnapshotBaseById(baseId: Int, snapshotModel: SnapshotModel) -> BaseEntityModel{ func getSnapshotBaseById(baseId: Int, snapshotModel: SnapshotModel) -> BaseEntityModel{
@ -254,8 +262,6 @@ class EntityManager {
return entities.filter{$0 is HUD}[0] return entities.filter{$0 is HUD}[0]
} }
func getSnapshotModel() -> SnapshotModel { func getSnapshotModel() -> SnapshotModel {
let bases = entities.filter{$0 is Base} let bases = entities.filter{$0 is Base}
var snapBase: [BaseEntityModel] = [] var snapBase: [BaseEntityModel] = []
@ -271,4 +277,13 @@ class EntityManager {
func getTimer() -> TimerComponent { func getTimer() -> TimerComponent {
return entities.filter{$0 is HUD}[0].component(ofType: TimerComponent.self)! return entities.filter{$0 is HUD}[0].component(ofType: TimerComponent.self)!
} }
func getUnitSum(by player: GKPlayer) -> Int{
let bases = getBasesByPlayer(for: player)
var sum = 0
for base in bases {
sum += base.unitCount
}
return sum
}
} }

View File

@ -26,6 +26,7 @@ class HUD: GKEntity {
anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30)) anchorPoint: CGPoint(x: size.width * 0.5, y: size.height * 0.9), duration: 30))
addComponent(PlayerInfoComponent(size: size)) addComponent(PlayerInfoComponent(size: size))
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {

View File

@ -46,7 +46,7 @@ class GameScene: SKScene{
isMoveTouch = false isMoveTouch = false
currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBase!.position currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = currentDraggedBase!.position
currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBase!.position currentDraggedBase!.component(ofType: TeamComponent.self)?.fire.position = currentDraggedBase!.position
currentDraggedBase!.component(ofType: DefaultBaseComponent.self)?.labelNode?.position = CGPoint(x: currentDraggedBase!.position.x + 30, y: currentDraggedBase!.position.y - 50)
addAttackDetails(touchLocation: touchLocation) addAttackDetails(touchLocation: touchLocation)
} }
else { else {
@ -152,6 +152,7 @@ class GameScene: SKScene{
func moveFireAndBase(base: Base, touchLocation: CGPoint){ func moveFireAndBase(base: Base, touchLocation: CGPoint){
base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position = touchLocation
base.component(ofType: TeamComponent.self)?.fire.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)
} }
func showNearestBases(base: Base){ func showNearestBases(base: Base){