diff --git a/GoldWars/GoldWars/Entities/Base.swift b/GoldWars/GoldWars/Entities/Base.swift index 97c4412..250521d 100644 --- a/GoldWars/GoldWars/Entities/Base.swift +++ b/GoldWars/GoldWars/Entities/Base.swift @@ -9,16 +9,19 @@ import SpriteKit import GameplayKit -class Base: GKEntity { - +class Base: GKEntity{ + static var BASE_ID_COUNT: Int = 0 var unitCount: Int var adjacencyList: Array var changeOwnerShip: Bool + var baseID: Int init(position: CGPoint, team: Team! = nil) { self.unitCount = 0 self.adjacencyList = [Base]() self.changeOwnerShip = false + baseID = Base.BASE_ID_COUNT + Base.BASE_ID_COUNT += 1 super.init() addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) @@ -28,11 +31,11 @@ class Base: GKEntity { } } - func attackBase(base: Base, units:Int) -> [GKEntity]{ + func attackBase(base: Base, units: Int) -> [GKEntity]{ base.changeOwnerShip = true self.unitCount -= units base.unitCount += units - MultiplayerNetwork.sharedInstance.sendPlayerMove(unitcount: units) + MultiplayerNetwork.sharedInstance.sendPlayerMove(playerMove: PlayerMove(fromBase: self.baseID, toBase: base.baseID, unitCount: units)) return [self, base] } diff --git a/GoldWars/GoldWars/Entities/EntityManager.swift b/GoldWars/GoldWars/Entities/EntityManager.swift index 0f7166c..54a07ec 100644 --- a/GoldWars/GoldWars/Entities/EntityManager.swift +++ b/GoldWars/GoldWars/Entities/EntityManager.swift @@ -99,6 +99,7 @@ class EntityManager { } } + func getBaseByTeam(for team: Team) -> GKEntity? { for entity in entities { if let teamComponent = entity.component(ofType: TeamComponent.self), @@ -128,7 +129,16 @@ class EntityManager { return nil } - func getBackground() -> GKEntity? { + + func getBasebyID(id: Int) -> Base?{ + for entity in entities { + if entity is Base && (entity as! Base).baseID == id { + return (entity as! Base) + } + } + return nil + } + func getBackground() -> GKEntity? { return entities.filter{$0 is Background}[0] } diff --git a/GoldWars/GoldWars/MessageProtocol.swift b/GoldWars/GoldWars/MessageProtocol.swift index 21ed95c..5038135 100644 --- a/GoldWars/GoldWars/MessageProtocol.swift +++ b/GoldWars/GoldWars/MessageProtocol.swift @@ -9,30 +9,16 @@ import GameplayKit import Foundation -enum MessageType : Int,Codable{ - case PlayerMove -} +struct PlayerMove : Codable{ -protocol Message :Codable{ - var messageType:MessageType { get } -} - -struct PlayerMove : Message,Codable{ - - let messageType: MessageType - -// let message: Message - //let fromBase: //was? - //let toBase: //was? + let fromBase: Int + let toBase: Int let unitCount: Int } class MultiplayerNetwork{ - static let sharedInstance = MultiplayerNetwork() - - - + static let sharedInstance = MultiplayerNetwork() func sendData(data: Data) { let mmHelper = MatchmakingHelper.sharedInstance @@ -45,10 +31,9 @@ class MultiplayerNetwork{ } } - func sendPlayerMove(unitcount:Int) { - let playerMoveMsg = PlayerMove(messageType: MessageType.PlayerMove, unitCount: unitcount) + func sendPlayerMove(playerMove:PlayerMove) { let encoder = JSONEncoder() - let encoded = (try? encoder.encode(playerMoveMsg))! + let encoded = (try? encoder.encode(playerMove))! sendData(data: encoded) }