* 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
|
||||
}
|
||||
}
|
||||
|
@ -109,12 +109,17 @@ class EntityManager {
|
||||
let base = (entity as! Base)
|
||||
|
||||
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(
|
||||
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
|
||||
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)")
|
||||
|
@ -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