add isGameOver and determineHost func
* adjust position finish button small refactoring and delete prints -> ready to merge
This commit is contained in:
parent
028282f784
commit
09062d5f5d
@ -89,8 +89,8 @@ class HUD: GKEntity {
|
||||
text: "Done",
|
||||
isEnabled: true,
|
||||
position: CGPoint(
|
||||
x: EntityManager.gameEMInstance.scene.size.width * 0.1,
|
||||
y: EntityManager.gameEMInstance.scene.size.height * 0.1),
|
||||
x: EntityManager.gameEMInstance.scene.size.width * 0.15,
|
||||
y: EntityManager.gameEMInstance.scene.size.height * 0.06),
|
||||
onButtonPress: { }
|
||||
)
|
||||
finishButton.size = CGSize(width: 80, height: 40)
|
||||
|
@ -22,6 +22,8 @@ struct State: Codable {
|
||||
// 1 RemotePlayerInit fertig
|
||||
// 2 Peer hat Map erhalten
|
||||
// 3 Host hat Spiel gestartet
|
||||
// 4 Peer hat verloren
|
||||
// 5 Peer hat gewonnen
|
||||
let state: Int
|
||||
}
|
||||
|
||||
@ -92,7 +94,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
|
||||
achievement.percentComplete += increasePercentComplete
|
||||
achievement.showsCompletionBanner = true
|
||||
GKAchievement.report([achievement]) { (error) in
|
||||
print(error?.localizedDescription ?? "")
|
||||
print(error?.localizedDescription ?? "")
|
||||
}
|
||||
}
|
||||
})
|
||||
@ -101,7 +103,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
|
||||
newAchievement.showsCompletionBanner = true
|
||||
newAchievement.percentComplete = increasePercentComplete
|
||||
GKAchievement.report([newAchievement]) { (error) in
|
||||
print(error?.localizedDescription ?? "")
|
||||
print(error?.localizedDescription ?? "")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -129,29 +131,35 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
|
||||
}
|
||||
if let state = try? jsonDecoder.decode(State.self, from: data) {
|
||||
switch state.state {
|
||||
case 0:
|
||||
os_log("State 0 erhalten", log: LOG, type: .info)
|
||||
sendStateToPeers(state: State(state: 1))
|
||||
case 1:
|
||||
os_log("State 1 erhalten", log: LOG, type: .info)
|
||||
|
||||
if isServer {
|
||||
os_log("Peer hat Player initialisiert", log: LOG, type: .info)
|
||||
initAndSendMap()
|
||||
}else {
|
||||
os_log("Host hat Player initialisiert", log: LOG, type: .info)
|
||||
case 0:
|
||||
os_log("State 0 erhalten", log: LOG, type: .info)
|
||||
sendStateToPeers(state: State(state: 1))
|
||||
case 1:
|
||||
os_log("State 1 erhalten", log: LOG, type: .info)
|
||||
|
||||
if isServer {
|
||||
os_log("Peer hat Player initialisiert", log: LOG, type: .info)
|
||||
initAndSendMap()
|
||||
}else {
|
||||
os_log("Host hat Player initialisiert", log: LOG, type: .info)
|
||||
}
|
||||
case 2:
|
||||
os_log("State 2 erhalten", log: LOG, type: .info)
|
||||
sendStateToPeers(state: State(state: 3))
|
||||
initIsFinish = true
|
||||
os_log("Spiel startet", log: LOG, type: .info)
|
||||
case 3:
|
||||
os_log("State 3 erhalten", log: LOG, type: .info)
|
||||
initIsFinish = true
|
||||
os_log("Spiel startet", log: LOG, type: .info)
|
||||
default:
|
||||
break
|
||||
case 2:
|
||||
os_log("State 2 erhalten", log: LOG, type: .info)
|
||||
sendStateToPeers(state: State(state: 3))
|
||||
initIsFinish = true
|
||||
os_log("Spiel startet", log: LOG, type: .info)
|
||||
case 3:
|
||||
os_log("State 3 erhalten", log: LOG, type: .info)
|
||||
initIsFinish = true
|
||||
os_log("Spiel startet", log: LOG, type: .info)
|
||||
case 4:
|
||||
os_log("State 4 erhalten, Peer hat verloren", log: LOG, type: .info)
|
||||
// TODO: Trigger Loser Scene
|
||||
case 5:
|
||||
os_log("State 5 erhalten, Peer hat gewonnen", log: LOG, type: .info)
|
||||
// TODO: Trigger Winner Scene
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
if let roundData = try? jsonDecoder.decode(LocalRoundData.self, from: data) {
|
||||
|
@ -148,6 +148,18 @@ class RoundCalculatorService {
|
||||
DataService.sharedInstance.localRoundData.hasAttackBoost = false
|
||||
DataService.sharedInstance.localRoundData.hasDefenceBoost = false
|
||||
|
||||
if isGameOver() {
|
||||
let winner: String?
|
||||
if MAX_ROUNDS == currentRound {
|
||||
os_log("Game is over by rounds", log: RoundCalculatorService.LOG, type: .info)
|
||||
winner = determineWinner(by: "rounds")
|
||||
} else {
|
||||
os_log("Game is over by capture", log: RoundCalculatorService.LOG, type: .info)
|
||||
winner = determineWinner(by: "capture")
|
||||
}
|
||||
winner == GameCenterManager.sharedInstance.hostingPlayer?.displayName ? GameCenterManager.sharedInstance.sendStateToPeers(state: State(state: 4)) : GameCenterManager.sharedInstance.sendStateToPeers(state: State(state: 5))
|
||||
return
|
||||
}
|
||||
currentRound += 1
|
||||
entityManager.getHUD()?.setCurrentRound(round: currentRound)
|
||||
|
||||
@ -171,7 +183,7 @@ class RoundCalculatorService {
|
||||
DataService.sharedInstance.remotePlayerMoves[GameCenterManager.sharedInstance.peerPlayer!.displayName]?.hasAttackBoost ?? false,
|
||||
DataService.sharedInstance.remotePlayerMoves[GameCenterManager.sharedInstance.peerPlayer!.displayName]?.hasDefenceBoost ?? false
|
||||
)
|
||||
|
||||
|
||||
allPlayerMoves[GKLocalPlayer.local.displayName] = DataService.sharedInstance.localRoundData.localPlayerMoves
|
||||
|
||||
var baseSpecificMoves: [Int: [String: [PlayerMove]]] = [:]
|
||||
@ -209,4 +221,33 @@ class RoundCalculatorService {
|
||||
self.numberOfAttacks += 1
|
||||
}
|
||||
}
|
||||
|
||||
func isGameOver() -> Bool {
|
||||
let remoteplayerBasesCount = entityManager.getBasesByPlayer(for: GameCenterManager.sharedInstance.peerPlayer!).count
|
||||
let localplayerBasesCount = entityManager.getBasesByPlayer(for: GameCenterManager.sharedInstance.hostingPlayer!).count
|
||||
let onePlayerLoseAllBases = remoteplayerBasesCount == 0 || localplayerBasesCount == 0
|
||||
let reachMaxRounds = MAX_ROUNDS == currentRound
|
||||
return onePlayerLoseAllBases || reachMaxRounds
|
||||
}
|
||||
|
||||
func determineWinner(by criteria: String) -> String {
|
||||
var winner: String?
|
||||
switch criteria {
|
||||
case "rounds":
|
||||
let peerPlayerBasesCount = entityManager.getBasesByPlayer(for: GameCenterManager.sharedInstance.peerPlayer!).count
|
||||
let hostingPlayerBasesCount = entityManager.getBasesByPlayer(for: GameCenterManager.sharedInstance.hostingPlayer!).count
|
||||
if peerPlayerBasesCount == hostingPlayerBasesCount {
|
||||
let hostingPlayerUnitCount = entityManager.getUnitSum(by: GameCenterManager.sharedInstance.hostingPlayer!)
|
||||
let peerPlayerUnitCount = entityManager.getUnitSum(by: GameCenterManager.sharedInstance.peerPlayer!)
|
||||
winner = hostingPlayerUnitCount > peerPlayerUnitCount ? GameCenterManager.sharedInstance.hostingPlayer?.displayName : GameCenterManager.sharedInstance.peerPlayer?.displayName
|
||||
} else {
|
||||
winner = hostingPlayerBasesCount > peerPlayerBasesCount ? GameCenterManager.sharedInstance.hostingPlayer?.displayName : GameCenterManager.sharedInstance.peerPlayer?.displayName
|
||||
}
|
||||
case "capture":
|
||||
winner = entityManager.getBasesByPlayer(for: GameCenterManager.sharedInstance.hostingPlayer!).count == 0 ? GameCenterManager.sharedInstance.peerPlayer?.displayName : GameCenterManager.sharedInstance.hostingPlayer?.displayName
|
||||
default:
|
||||
break
|
||||
}
|
||||
return winner!
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user