* 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

@ -102,25 +102,30 @@ class EntityManager {
} }
entities.remove(entity) entities.remove(entity)
} }
func update(_ entities: [GKEntity]){ func update(_ entities: [GKEntity]){
for entity in entities { for entity in entities {
self.entities.update(with: entity) self.entities.update(with: entity)
let base = (entity as! Base) let base = (entity as! Base)
if base.changeOwnership { if base.changeOwnership {
base.addComponent(TeamComponent( if let component = entity.component(ofType: TeamComponent.self) {
team: (entities[0] as! Base).component(ofType: TeamComponent.self)!.team, component.player = entities[0].component(ofType: TeamComponent.self)!.player
player: (entities[0] as! Base).component(ofType: TeamComponent.self)!.player, component.team = entities[0].component(ofType: TeamComponent.self)!.team
position: (base.component(ofType: DefaultBaseComponent.self)?.spriteNode.position)! } 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 base.changeOwnership = false
scene.addChild(base.component(ofType: TeamComponent.self)!.fire) scene.addChild(base.component(ofType: TeamComponent.self)!.fire)
} }
} }
} }
func updateSnapshotModel(snapshotModel: SnapshotModel) { func updateSnapshotModel(snapshotModel: SnapshotModel) {
let bases = entities.filter{$0 is Base} let bases = entities.filter{$0 is Base}
for entity in bases{ for entity in bases{
@ -137,14 +142,21 @@ class EntityManager {
if getOwnerBySnapBase != nil { if getOwnerBySnapBase != nil {
base.changeOwnership = true base.changeOwnership = true
base.ownershipPlayer = getOwnerBySnapBase 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!), 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)!
)
) )
) if let fire = entity.component(ofType: TeamComponent.self)?.fire{
print(entity) scene.addChild(fire)
print(getTeamByBase(base: base)) }
}
} }
print("nach updateSnap -> Entity \(base)") print("nach updateSnap -> Entity \(base)")
@ -166,7 +178,7 @@ class EntityManager {
} }
return nil return nil
} }
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),
@ -178,15 +190,15 @@ class EntityManager {
} }
return nil return nil
} }
func getBasesByTeam(for team: Team) -> Set<Base> { func getBasesByTeam(for team: Team) -> Set<Base> {
return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set<Base> return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.team == team } as! Set<Base>
} }
func getBasesByPlayer(for player: GKPlayer) -> Set<Base> { func getBasesByPlayer(for player: GKPlayer) -> Set<Base> {
return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player == player } as! Set<Base> return entities.filter{$0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player == player } as! Set<Base>
} }
func getTeamByBase(base: Base) -> Team? { func getTeamByBase(base: Base) -> Team? {
for entity in entities { for entity in entities {
if entity is Base && entity == base{ if entity is Base && entity == base{
@ -203,24 +215,24 @@ class EntityManager {
func getTeamByPlayer(playerName: String) -> Team { func getTeamByPlayer(playerName: String) -> Team {
return entities.filter { $0 is Base && ($0 as! Base).component(ofType: TeamComponent.self)?.player.displayName == playerName }[0].component(ofType: TeamComponent.self)!.team 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?{ func getBasebyID(id: Int) -> Base?{
for entity in entities { for entity in entities {
if entity is Base && (entity as! Base).baseID == id { if entity is Base && (entity as! Base).baseID == id {
return (entity as! Base) return (entity as! Base)
} }
} }
return nil return nil
} }
func getBackground() -> GKEntity? { func getBackground() -> GKEntity? {
return entities.filter{$0 is Background}[0] return entities.filter{$0 is Background}[0]
} }
func getBaseNodeByTeam(for team: Team) -> SKSpriteNode? { func getBaseNodeByTeam(for team: Team) -> SKSpriteNode? {
return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode
} }
func getButtonByName(buttonName:String) -> Button { func getButtonByName(buttonName:String) -> Button {
return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button
} }

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?) {