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

@ -9,16 +9,19 @@
import SpriteKit import SpriteKit
import GameplayKit import GameplayKit
class Base: GKEntity { class Base: GKEntity{
static var BASE_ID_COUNT: Int = 0
var unitCount: Int var unitCount: Int
var adjacencyList: Array<Base> var adjacencyList: Array<Base>
var changeOwnerShip: Bool var changeOwnerShip: Bool
var baseID: Int
init(position: CGPoint, team: Team! = nil) { init(position: CGPoint, team: Team! = nil) {
self.unitCount = 0 self.unitCount = 0
self.adjacencyList = [Base]() self.adjacencyList = [Base]()
self.changeOwnerShip = false self.changeOwnerShip = false
baseID = Base.BASE_ID_COUNT
Base.BASE_ID_COUNT += 1
super.init() super.init()
addComponent(DefaultBaseComponent(texture: SKTexture(imageNamed: "Base"), position: position)) 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 base.changeOwnerShip = true
self.unitCount -= units self.unitCount -= units
base.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] return [self, base]
} }

View File

@ -99,6 +99,7 @@ class EntityManager {
} }
} }
func getBaseByTeam(for team: Team) -> GKEntity? { func getBaseByTeam(for team: Team) -> GKEntity? {
for entity in entities { for entity in entities {
if let teamComponent = entity.component(ofType: TeamComponent.self), if let teamComponent = entity.component(ofType: TeamComponent.self),
@ -128,7 +129,16 @@ class EntityManager {
return nil 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] return entities.filter{$0 is Background}[0]
} }

View File

@ -9,30 +9,16 @@
import GameplayKit import GameplayKit
import Foundation import Foundation
enum MessageType : Int,Codable{
case PlayerMove
} struct PlayerMove : Codable{
protocol Message :Codable{ let fromBase: Int
var messageType:MessageType { get } let toBase: Int
}
struct PlayerMove : Message,Codable{
let messageType: MessageType
// let message: Message
//let fromBase: //was?
//let toBase: //was?
let unitCount: Int let unitCount: Int
} }
class MultiplayerNetwork{ class MultiplayerNetwork{
static let sharedInstance = MultiplayerNetwork() static let sharedInstance = MultiplayerNetwork()
func sendData(data: Data) { func sendData(data: Data) {
let mmHelper = MatchmakingHelper.sharedInstance let mmHelper = MatchmakingHelper.sharedInstance
@ -45,10 +31,9 @@ class MultiplayerNetwork{
} }
} }
func sendPlayerMove(unitcount:Int) { func sendPlayerMove(playerMove:PlayerMove) {
let playerMoveMsg = PlayerMove(messageType: MessageType.PlayerMove, unitCount: unitcount)
let encoder = JSONEncoder() let encoder = JSONEncoder()
let encoded = (try? encoder.encode(playerMoveMsg))! let encoded = (try? encoder.encode(playerMove))!
sendData(data: encoded) sendData(data: encoded)
} }