* Fixed TeamComponent when updating Entities

* Minor refactor
This commit is contained in:
Aldin Duraki 2020-05-19 02:21:41 +02:00
parent 0db2be8625
commit b04bbe5036
5 changed files with 52 additions and 51 deletions

View File

@ -11,9 +11,9 @@ import GameplayKit
import GameKit import GameKit
class TeamComponent: GKComponent { class TeamComponent: GKComponent {
let team: Team var team: Team
var player: GKPlayer
let fire: SKEmitterNode let fire: SKEmitterNode
let player: GKPlayer
init(team: Team, player: GKPlayer, position: CGPoint) { init(team: Team, player: GKPlayer, position: CGPoint) {
fire = SKEmitterNode(fileNamed: "Fire")! fire = SKEmitterNode(fileNamed: "Fire")!

View File

@ -18,11 +18,7 @@ struct Host: Codable {
class SnapshotModel: Codable { class SnapshotModel: Codable {
var baseEntites: [BaseEntityModel] var baseEntites: [BaseEntityModel]
//
// init() {
// self.baseEntites = []
// }
//
init(baseEntites: [BaseEntityModel]) { init(baseEntites: [BaseEntityModel]) {
self.baseEntites = baseEntites self.baseEntites = baseEntites
} }
@ -47,8 +43,6 @@ class DataService {
var snapshotModel: SnapshotModel? var snapshotModel: SnapshotModel?
var gameHost: Host? var gameHost: Host?
// TODO: Update entries to merge equal moves
func addMove(playerMove: PlayerMove) { func addMove(playerMove: PlayerMove) {
var equalMove = localPlayerMoves.filter { (ele) -> Bool in var equalMove = localPlayerMoves.filter { (ele) -> Bool in
ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase ele.fromBase == playerMove.fromBase && ele.toBase == playerMove.toBase
@ -72,7 +66,7 @@ class DataService {
self.gameHost = host self.gameHost = host
} }
func safeSnapshot(snapshotModel: SnapshotModel) { func setSnapshotModel(snapshotModel: SnapshotModel) {
self.snapshotModel = snapshotModel self.snapshotModel = snapshotModel
} }
} }

View File

@ -109,12 +109,17 @@ class EntityManager {
let base = (entity as! Base) let base = (entity as! Base)
if base.changeOwnership { if base.changeOwnership {
if let component = entity.component(ofType: TeamComponent.self) {
component.player = entities[0].component(ofType: TeamComponent.self)!.player
component.team = entities[0].component(ofType: TeamComponent.self)!.team
} else {
base.addComponent(TeamComponent( base.addComponent(TeamComponent(
team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team,
player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player, player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player,
position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!
) )
) )
}
base.changeOwnership = false base.changeOwnership = false
scene.addChild(base.component(ofType: TeamComponent.self)!.fire) scene.addChild(base.component(ofType: TeamComponent.self)!.fire)
} }
@ -137,14 +142,21 @@ class EntityManager {
if getOwnerBySnapBase != nil { if getOwnerBySnapBase != nil {
base.changeOwnership = true base.changeOwnership = true
base.ownershipPlayer = getOwnerBySnapBase base.ownershipPlayer = getOwnerBySnapBase
if let component = entity.component(ofType: TeamComponent.self) {
component.player = getOwnerBySnapBase!
component.team = getTeamByPlayer(playerName: snapBase.ownership!)
} else {
entity.addComponent(TeamComponent( entity.addComponent(TeamComponent(
team: getTeamByPlayer(playerName: snapBase.ownership!), team: getTeamByPlayer(playerName: snapBase.ownership!),
player: getOwnerBySnapBase!, player: getOwnerBySnapBase!,
position: (entity.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! position: (entity.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!
) )
) )
print(entity) if let fire = entity.component(ofType: TeamComponent.self)?.fire{
print(getTeamByBase(base: base)) scene.addChild(fire)
}
}
} }
print("nach updateSnap -> Entity \(base)") print("nach updateSnap -> Entity \(base)")

View File

@ -30,7 +30,6 @@ class RoundCalculatorServie {
} }
addPlayerMove(playerName: GKLocalPlayer.local.displayName, playerMoves: DataService.sharedInstance.localPlayerMoves) addPlayerMove(playerName: GKLocalPlayer.local.displayName, playerMoves: DataService.sharedInstance.localPlayerMoves)
print("allPlayerMoves -> \(allPlayerMoves)")
for entry in allPlayerMoves { for entry in allPlayerMoves {
for move in entry.value { for move in entry.value {
mapPlayerMoveToAttackedBase(playerName: entry.key, playerMove: move) mapPlayerMoveToAttackedBase(playerName: entry.key, playerMove: move)
@ -38,16 +37,17 @@ class RoundCalculatorServie {
} }
// TODO-END: // TODO-END:
// TODO: Refactor -> O(n*3n^2) to maybe O(n*3n)
// We might not need to map and iterate over toBase
for (key, value) in baseSpecificMoves { for (key, value) in baseSpecificMoves {
let baseId = key let baseId = key
var playerMovesByBase = value var playerMovesByBase = value
let targetBase = currentSnapshotModel?.baseEntites.filter { $0.baseId == baseId }[0] let targetBase = currentSnapshotModel?.baseEntites.filter { $0.baseId == baseId }[0]
let possiblyOwnershipMoves = playerMovesByBase.filter { $0.key == targetBase?.ownership} let possiblyOwnershipMoves = playerMovesByBase.filter { $0.key == targetBase?.ownership}
// spieler verschiebt einheiten beim schieben
for (playerName, playerMove) in possiblyOwnershipMoves { for (playerName, playerMove) in possiblyOwnershipMoves {
for var base in currentSnapshotModel!.baseEntites { for base in currentSnapshotModel!.baseEntites {
if base.baseId == playerMove.fromBase { if base.baseId == playerMove.fromBase {
base.unitCount -= playerMove.unitCount base.unitCount -= playerMove.unitCount
} }
@ -59,14 +59,13 @@ class RoundCalculatorServie {
} }
for (_, playerMove) in playerMovesByBase { for (_, playerMove) in playerMovesByBase {
for var base in currentSnapshotModel!.baseEntites { for base in currentSnapshotModel!.baseEntites {
if base.baseId == playerMove.fromBase { if base.baseId == playerMove.fromBase {
base.unitCount -= playerMove.unitCount base.unitCount -= playerMove.unitCount
} }
} }
} }
let sorted = playerMovesByBase.sorted { (e1: (key: String, value: PlayerMove), e2: (key: String, value: PlayerMove)) -> Bool in let sorted = playerMovesByBase.sorted { (e1: (key: String, value: PlayerMove), e2: (key: String, value: PlayerMove)) -> Bool in
e1.value.unitCount > e2.value.unitCount e1.value.unitCount > e2.value.unitCount
} }
@ -75,10 +74,9 @@ class RoundCalculatorServie {
if sorted.count == 2 { if sorted.count == 2 {
let secMax = sorted[1] let secMax = sorted[1]
max.value.unitCount -= secMax.value.unitCount max.value.unitCount -= secMax.value.unitCount
} }
for var base in currentSnapshotModel!.baseEntites { for base in currentSnapshotModel!.baseEntites {
if base.baseId == max.value.toBase { if base.baseId == max.value.toBase {
base.unitCount += max.value.unitCount base.unitCount += max.value.unitCount
if max.value.unitCount == 0 { if max.value.unitCount == 0 {
@ -89,11 +87,8 @@ class RoundCalculatorServie {
} }
} }
baseSpecificMoves.removeValue(forKey: baseId) baseSpecificMoves.removeValue(forKey: baseId)
} }
print(currentSnapshotModel)
MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers() MultiplayerNetwork.sharedInstance.sendSnapshotModelToPlayers()
EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel!) EntityManager.sharedInstance.updateSnapshotModel(snapshotModel: currentSnapshotModel!)
let calcTime = CFAbsoluteTimeGetCurrent() - startTime let calcTime = CFAbsoluteTimeGetCurrent() - startTime

View File

@ -25,7 +25,7 @@ class GameScene: SKScene{
func initMap() { func initMap() {
MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap(playerCount: 2) MapFactory(scene: self, entityManager: EntityManager.sharedInstance).loadMap(playerCount: 2)
DataService.sharedInstance.safeSnapshot(snapshotModel: EntityManager.sharedInstance.getSnapshotModel()) DataService.sharedInstance.setSnapshotModel(snapshotModel: EntityManager.sharedInstance.getSnapshotModel())
} }
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {