* Fixed TeamComponent when updating Entities
* Minor refactor
This commit is contained in:
parent
0db2be8625
commit
b04bbe5036
@ -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")!
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -102,25 +102,30 @@ class EntityManager {
|
||||
}
|
||||
entities.remove(entity)
|
||||
}
|
||||
|
||||
|
||||
func update(_ entities: [GKEntity]){
|
||||
for entity in entities {
|
||||
self.entities.update(with: entity)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func updateSnapshotModel(snapshotModel: SnapshotModel) {
|
||||
let bases = entities.filter{$0 is Base}
|
||||
for entity in bases{
|
||||
@ -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)")
|
||||
@ -166,7 +178,7 @@ class EntityManager {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func getBaseByTeam(for team: Team) -> GKEntity? {
|
||||
for entity in entities {
|
||||
if let teamComponent = entity.component(ofType: TeamComponent.self),
|
||||
@ -178,15 +190,15 @@ class EntityManager {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
|
||||
func getTeamByBase(base: Base) -> Team? {
|
||||
for entity in entities {
|
||||
if entity is Base && entity == base{
|
||||
@ -203,24 +215,24 @@ class EntityManager {
|
||||
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
|
||||
}
|
||||
|
||||
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 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]
|
||||
}
|
||||
|
||||
|
||||
func getBaseNodeByTeam(for team: Team) -> SKSpriteNode? {
|
||||
return getBaseByTeam(for: team)?.component(ofType: DefaultBaseComponent.self)?.spriteNode
|
||||
}
|
||||
|
||||
|
||||
func getButtonByName(buttonName:String) -> Button {
|
||||
return entities.filter{$0 is Button && ($0 as! Button).name == buttonName }[0] as! Button
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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?) {
|
||||
|
Loading…
Reference in New Issue
Block a user