* 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
class TeamComponent: GKComponent {
let team: Team
var team: Team
var player: GKPlayer
let fire: SKEmitterNode
let player: GKPlayer
init(team: Team, player: GKPlayer, position: CGPoint) {
fire = SKEmitterNode(fileNamed: "Fire")!

View File

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

View File

@ -109,12 +109,17 @@ class EntityManager {
let base = (entity as! Base)
if base.changeOwnership {
base.addComponent(TeamComponent(
team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team,
player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player,
position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!
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(
team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team,
player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player,
position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!
)
)
)
}
base.changeOwnership = false
scene.addChild(base.component(ofType: TeamComponent.self)!.fire)
}
@ -137,14 +142,21 @@ class EntityManager {
if getOwnerBySnapBase != nil {
base.changeOwnership = true
base.ownershipPlayer = getOwnerBySnapBase
entity.addComponent(TeamComponent(
if let component = entity.component(ofType: TeamComponent.self) {
component.player = getOwnerBySnapBase!
component.team = getTeamByPlayer(playerName: snapBase.ownership!)
} else {
entity.addComponent(TeamComponent(
team: getTeamByPlayer(playerName: snapBase.ownership!),
player: getOwnerBySnapBase!,
position: (entity.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)!
)
)
)
print(entity)
print(getTeamByBase(base: base))
if let fire = entity.component(ofType: TeamComponent.self)?.fire{
scene.addChild(fire)
}
}
}
print("nach updateSnap -> Entity \(base)")
@ -204,16 +216,16 @@ class EntityManager {
return entities.filter { $0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player.displayName == playerName }[0].component(ofType: TeamComponent.self)!.team
}
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 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? {
func getBackground() -> GKEntity? {
return entities.filter{$0 is Background}[0]
}

View File

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

View File

@ -25,7 +25,7 @@ class GameScene: SKScene{
func initMap() {
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?) {