create Base ID, added fromBase - toBase to sending data

This commit is contained in:
Niko Jochim 2020-05-13 15:47:53 +02:00
parent a8e0bac437
commit a0a6bb9b28
3 changed files with 24 additions and 26 deletions

View File

@ -10,15 +10,18 @@ import SpriteKit
import GameplayKit
class Base: GKEntity{
static var BASE_ID_COUNT: Int = 0
var unitCount: Int
var adjacencyList: Array<Base>
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))
@ -32,7 +35,7 @@ class Base: 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]
}

View File

@ -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,6 +129,15 @@ class EntityManager {
return nil
}
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]
}

View File

@ -9,31 +9,17 @@
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()
func sendData(data: Data) {
let mmHelper = MatchmakingHelper.sharedInstance
if let multiplayerMatch = mmHelper.mpMatch {
@ -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)
}