Merge branch '72-spielabbruch-button' into 'development'

Resolve "Spielabbruch Button"

Closes #72

See merge request marcel.schwarz/software-projekt-2!106
This commit is contained in:
Aldin Duraki 2020-06-25 17:33:15 +00:00
commit 207315af30
7 changed files with 123 additions and 11 deletions

View File

@ -44,6 +44,10 @@ class ButtonNode: SKSpriteNode {
isUserInteractionEnabled = true isUserInteractionEnabled = true
} }
func setTexture(textureName: String) {
super.texture = SKTexture(imageNamed: textureName)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if isEnabled { if isEnabled {
let action = SKAction.sequence( let action = SKAction.sequence(

View File

@ -48,6 +48,7 @@ class EntityManager {
scene.addChild(hudEntitiy.hostUnitsLabel) scene.addChild(hudEntitiy.hostUnitsLabel)
scene.addChild(hudEntitiy.peerLabel) scene.addChild(hudEntitiy.peerLabel)
scene.addChild(hudEntitiy.peerUnitsLabel) scene.addChild(hudEntitiy.peerUnitsLabel)
scene.addChild(hudEntitiy.leaveGame)
scene.addChild(hudEntitiy.defSkill) scene.addChild(hudEntitiy.defSkill)
scene.addChild(hudEntitiy.atkSkill) scene.addChild(hudEntitiy.atkSkill)
scene.addChild(hudEntitiy.spySkill) scene.addChild(hudEntitiy.spySkill)
@ -357,6 +358,15 @@ class EntityManager {
}) })
} }
func exitToMenu() {
GameCenterManager.sharedInstance.reset()
EntityManager.gameEMInstance.getTimer()?.stopTimer()
RoundCalculatorService.sharedInstance.currentRound = 1
EntityManager.gameEMInstance.scene.view?.presentScene(EntityManager.menuEMInstance.scene)
EntityManager.gameEMInstance.entities.removeAll()
EntityManager.gameEMInstance.scene.removeFromParent()
}
func getTimer() -> RoundTimer? { func getTimer() -> RoundTimer? {
return getHUD()?.roundTimer return getHUD()?.roundTimer
} }

View File

@ -18,6 +18,8 @@ class HUD: GKEntity {
var peerLabel:SKLabelNode var peerLabel:SKLabelNode
var peerUnitsLabel:SKLabelNode var peerUnitsLabel:SKLabelNode
var leaveGame: ButtonNode
var spySkill: SkillButtonNode var spySkill: SkillButtonNode
var defSkill: SkillButtonNode var defSkill: SkillButtonNode
var atkSkill: SkillButtonNode var atkSkill: SkillButtonNode
@ -50,6 +52,11 @@ class HUD: GKEntity {
self.roundTimer = RoundTimer() self.roundTimer = RoundTimer()
leaveGame = ButtonNode(textureName: "yellow_button04", text: "Verlassen" , isEnabled: true, position: CGPoint(x: EntityManager.gameEMInstance.scene.size.width * 0.17, y: EntityManager.gameEMInstance.scene.size.height * 0.04), onButtonPress: {
EntityManager.gameEMInstance.add(Modal(modaltype: .QuitGame, base: nil, anchorPoint: CGPoint(x: EntityManager.gameEMInstance.scene.size.width / 2 , y: EntityManager.gameEMInstance.scene.size.height / 2), gameScene: EntityManager.gameEMInstance.scene, currentDraggedBase: nil, touchLocation: nil, collisionBase: nil))
})
leaveGame.setScale(0.6)
spySkill = SkillButtonNode( spySkill = SkillButtonNode(
textureName: "spy_button", textureName: "spy_button",
text: "Spy", text: "Spy",
@ -96,10 +103,11 @@ class HUD: GKEntity {
isEnabled: true, isEnabled: true,
position: CGPoint( position: CGPoint(
x: EntityManager.gameEMInstance.scene.size.width * 0.17, x: EntityManager.gameEMInstance.scene.size.width * 0.17,
y: EntityManager.gameEMInstance.scene.size.height * 0.06), y: EntityManager.gameEMInstance.scene.size.height * 0.09),
onButtonPress: { } onButtonPress: { }
) )
finishButton.size = CGSize(width: 125, height: 40) finishButton.size = CGSize(width: 125, height: 40)
finishButton.setScale(0.9)
finishButton.zPosition = 2 finishButton.zPosition = 2
backgroundRoundCounter = SKSpriteNode(texture: SKTexture(imageNamed: "roundInfo_texture")) backgroundRoundCounter = SKSpriteNode(texture: SKTexture(imageNamed: "roundInfo_texture"))

View File

@ -12,6 +12,7 @@ enum ModalType: String {
case BaseAttack case BaseAttack
case BaseMoveOwnUnits case BaseMoveOwnUnits
case PauseGame case PauseGame
case QuitGame
} }
class Modal: GKEntity{ class Modal: GKEntity{
@ -26,7 +27,7 @@ class Modal: GKEntity{
var overlay: SKSpriteNode var overlay: SKSpriteNode
var type: ModalType var type: ModalType
init(modaltype: ModalType, base: Base?, anchorPoint: CGPoint, gameScene: GameScene, currentDraggedBase: Base?, touchLocation: CGPoint?, collisionBase: Base?) { init(modaltype: ModalType, base: Base?, anchorPoint: CGPoint, gameScene: SKScene, currentDraggedBase: Base?, touchLocation: CGPoint?, collisionBase: Base?) {
self.type = modaltype self.type = modaltype
unitCount = 0 unitCount = 0
if base != nil { if base != nil {
@ -64,6 +65,10 @@ class Modal: GKEntity{
body = SKLabelNode(text: "Warte auf Gegner...") body = SKLabelNode(text: "Warte auf Gegner...")
footer = SKLabelNode() footer = SKLabelNode()
closeButton.zPosition = -1 closeButton.zPosition = -1
case .QuitGame:
header = SKLabelNode(text: "Spiel verlassen")
body = SKLabelNode(text: "Sicher verlassen?")
footer = SKLabelNode()
} }
self.header.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y + 90) self.header.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y + 90)
@ -86,13 +91,25 @@ class Modal: GKEntity{
super.init() super.init()
switch modaltype {
case .BaseAttack, .BaseMoveOwnUnits:
let text = (modaltype == .BaseAttack) ? "Angriff" : "Senden"
addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 15)))
addComponent(ButtonComponent(textureName: "yellow_button04", text: text, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: {
self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation!, gameScene: gameScene, collisionBase: collisionBase)
EntityManager.gameEMInstance.removeModal()
}))
case .QuitGame:
addComponent(ButtonComponent(textureName: "yellow_button04", text: "Verlassen", position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: {
EntityManager.gameEMInstance.removeModal()
GameCenterManager.sharedInstance.sendStateToPeers(state: State(state: 6))
GameCenterManager.sharedInstance.opponentQuit = false
GameCenterManager.sharedInstance.quitGame = true
}))
case .PauseGame:
break
}
let text = (modaltype == .BaseAttack) ? "Angriff" : "Senden"
addComponent(SliderComponent(width: 300, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 15)))
addComponent(ButtonComponent(textureName: "yellow_button04", text: text, position: CGPoint(x: anchorPoint.x , y: anchorPoint.y - 95), isEnabled: true, onButtonPress: {
self.sendUnits(currentDraggedBase: currentDraggedBase, touchLocation: touchLocation!, gameScene: gameScene, collisionBase: collisionBase)
EntityManager.gameEMInstance.removeModal()
}))
} }
@ -108,7 +125,7 @@ class Modal: GKEntity{
} }
} }
func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: GameScene, collisionBase: Base?){ func sendUnits(currentDraggedBase: Base?, touchLocation: CGPoint, gameScene: SKScene, collisionBase: Base?){
for base in currentDraggedBase!.adjacencyList { for base in currentDraggedBase!.adjacencyList {
if base == collisionBase { if base == collisionBase {
RoundCalculatorService.sharedInstance.increaseMoveCounter(ownBase: currentDraggedBase?.ownershipPlayer == base.ownershipPlayer) RoundCalculatorService.sharedInstance.increaseMoveCounter(ownBase: currentDraggedBase?.ownershipPlayer == base.ownershipPlayer)

View File

@ -24,6 +24,7 @@ struct State: Codable {
// 3 Host hat Spiel gestartet // 3 Host hat Spiel gestartet
// 4 Peer hat verloren // 4 Peer hat verloren
// 5 Peer hat gewonnen // 5 Peer hat gewonnen
// 6 Peer hat Spiel verlassen
let state: Int let state: Int
} }
@ -47,6 +48,8 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
var gameEnded = false var gameEnded = false
var winner: String? var winner: String?
var gameScene: GameScene? var gameScene: GameScene?
var quitGame: Bool = false
var opponentQuit: Bool = false
static var isAuthenticated: Bool { static var isAuthenticated: Bool {
return GKLocalPlayer.local.isAuthenticated return GKLocalPlayer.local.isAuthenticated
} }
@ -171,6 +174,10 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG
os_log("State 5 erhalten, Peer hat gewonnen", log: LOG, type: .info) os_log("State 5 erhalten, Peer hat gewonnen", log: LOG, type: .info)
winner = peerPlayer?.displayName winner = peerPlayer?.displayName
gameEnded = true gameEnded = true
case 6:
os_log("State 6 erhalten, Peer hat Spiel verlassen ", log: LOG, type: .info)
opponentQuit = true
quitGame = true
default: default:
break break
} }

View File

@ -81,9 +81,12 @@ class GameScene: SKScene{
if GameCenterManager.sharedInstance.gameEnded && !gameEndEffects { if GameCenterManager.sharedInstance.gameEnded && !gameEndEffects {
gameEnd() gameEnd()
} }
if GameCenterManager.sharedInstance.quitGame {
gameQuit()
}
} }
func addAttackDetails(touchLocation: CGPoint){ func addAttackDetails(touchLocation: CGPoint){
for base in currentDraggedBase!.adjacencyList { for base in currentDraggedBase!.adjacencyList {
if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode { if atPoint(touchLocation) == base.component(ofType: DefaultBaseComponent.self)?.spriteNode {
@ -167,6 +170,66 @@ class GameScene: SKScene{
} }
} }
func gameQuit() {
entityManager.getHUD()?.blockWholeScreenPane.isHidden = true
gameEndEffects = true
GameCenterManager.sharedInstance.quitGame = false
let move = SKAction.move(to: CGPoint(x: self.size.width / 2, y: self.size.height / 2), duration: 1)
let removeParticle = SKAction.removeFromParent()
let sequence = SKAction.sequence([move, removeParticle])
var actionAdded = false
for nodeChild in children {
if nodeChild.name == "peerLabel" || nodeChild.name == "hostLabel"{
nodeChild.removeFromParent()
}
if nodeChild.name == "way" {
nodeChild.run(SKAction.removeFromParent())
continue
}
if nodeChild.name != "clouds"{
nodeChild.run(sequence) {
if !actionAdded {
let explosion = self.getFinalExplosion()
self.addChild(explosion)
let action = SKAction.afterDelay(2) {
let node = ButtonNode(textureName: "yellow_button05", text: "Menü", isEnabled: true, position: CGPoint(x: self.size.width / 2, y: self.size.height / 2 - 300), onButtonPress: {
self.backToMenuAction()
})
node.name = "BackButton"
self.addChild(node)
}
explosion.run(action)
actionAdded = true
}
}
}
}
let disconnectLabel = SKLabelNode.init(fontNamed: "Courier-Bold")
disconnectLabel.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
disconnectLabel.horizontalAlignmentMode = .center
disconnectLabel.fontColor = UIColor.black
disconnectLabel.alpha = 0
disconnectLabel.setScale(0.1)
if GameCenterManager.sharedInstance.opponentQuit {
if GameCenterManager.sharedInstance.localPlayer == GameCenterManager.sharedInstance.hostingPlayer {
disconnectLabel.text = GameCenterManager.sharedInstance.peerPlayer?.displayName
} else {
disconnectLabel.text = GameCenterManager.sharedInstance.hostingPlayer?.displayName
}
disconnectLabel.text?.append(" hat das Spiel verlassen!")
} else {
disconnectLabel.text = "Du hast das Spiel verlassen!"
}
let showUpAnimation = SKAction.sequence([
SKAction.fadeAlpha(by: 1, duration: 2.5),
SKAction.scale(by: 10, duration: 1),
])
disconnectLabel.run(showUpAnimation)
self.addChild(disconnectLabel)
}
func initGameEndIcons() { func initGameEndIcons() {
let iclonSize = CGSize(width: 400, height: 400) let iclonSize = CGSize(width: 400, height: 400)
let winnerIcon = SKSpriteNode(texture: SKTexture(imageNamed: "winner")); let winnerIcon = SKSpriteNode(texture: SKTexture(imageNamed: "winner"));
@ -270,6 +333,8 @@ class GameScene: SKScene{
return collisionBase?.ownershipPlayer != currentDraggedBase?.ownershipPlayer && !(collisionBase?.changeOwnership ?? false) return collisionBase?.ownershipPlayer != currentDraggedBase?.ownershipPlayer && !(collisionBase?.changeOwnership ?? false)
} }
@objc func pauseGame() -> Void { @objc func pauseGame() -> Void {
entityManager.removeModal() entityManager.removeModal()
entityManager.getHUD()?.roundTimer.stopTimer() entityManager.getHUD()?.roundTimer.stopTimer()
@ -282,6 +347,7 @@ class GameScene: SKScene{
collisionBase: nil collisionBase: nil
)) ))
} }
@objc func resumeGame() -> Void { @objc func resumeGame() -> Void {
entityManager.removeModal() entityManager.removeModal()
entityManager.getHUD()?.roundTimer.resumeTimer() entityManager.getHUD()?.roundTimer.resumeTimer()

View File

@ -18,7 +18,7 @@ class SettingsScene: SKScene {
entityManager.setScene(scene: self) entityManager.setScene(scene: self)
let positionX = self.size.width * 0.1 let positionX = self.size.width * 0.1
let positionY = self.size.height * 0.05 let positionY = self.size.height * 0.05
isMusicDeactivatedByUserDefault ? setMusicButtonTextByUserDefault(text: "Aus") : setMusicButtonTextByUserDefault(text: "Ein") isMusicDeactivatedByUserDefault ? setMusicButtonTextByUserDefault(text: "Aus") : setMusicButtonTextByUserDefault(text: "An")
entityManager.add(Button(name: "backToMenuScene", entityManager.add(Button(name: "backToMenuScene",
textureName: "yellow_button04", textureName: "yellow_button04",
text: "Zurück", text: "Zurück",