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",
|
text: "Done",
|
||||||
isEnabled: true,
|
isEnabled: true,
|
||||||
position: CGPoint(
|
position: CGPoint(
|
||||||
x: EntityManager.gameEMInstance.scene.size.width * 0.1,
|
x: EntityManager.gameEMInstance.scene.size.width * 0.15,
|
||||||
y: EntityManager.gameEMInstance.scene.size.height * 0.1),
|
y: EntityManager.gameEMInstance.scene.size.height * 0.06),
|
||||||
onButtonPress: { }
|
onButtonPress: { }
|
||||||
)
|
)
|
||||||
finishButton.size = CGSize(width: 80, height: 40)
|
finishButton.size = CGSize(width: 80, height: 40)
|
||||||
|
@ -22,6 +22,8 @@ struct State: Codable {
|
|||||||
// 1 RemotePlayerInit fertig
|
// 1 RemotePlayerInit fertig
|
||||||
// 2 Peer hat Map erhalten
|
// 2 Peer hat Map erhalten
|
||||||
// 3 Host hat Spiel gestartet
|
// 3 Host hat Spiel gestartet
|
||||||
|
// 4 Peer hat verloren
|
||||||
|
// 5 Peer hat gewonnen
|
||||||
let state: Int
|
let state: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,6 +152,12 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
|
|||||||
os_log("State 3 erhalten", log: LOG, type: .info)
|
os_log("State 3 erhalten", log: LOG, type: .info)
|
||||||
initIsFinish = true
|
initIsFinish = true
|
||||||
os_log("Spiel startet", log: LOG, type: .info)
|
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:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -148,6 +148,18 @@ class RoundCalculatorService {
|
|||||||
DataService.sharedInstance.localRoundData.hasAttackBoost = false
|
DataService.sharedInstance.localRoundData.hasAttackBoost = false
|
||||||
DataService.sharedInstance.localRoundData.hasDefenceBoost = 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
|
currentRound += 1
|
||||||
entityManager.getHUD()?.setCurrentRound(round: currentRound)
|
entityManager.getHUD()?.setCurrentRound(round: currentRound)
|
||||||
|
|
||||||
@ -209,4 +221,33 @@ class RoundCalculatorService {
|
|||||||
self.numberOfAttacks += 1
|
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