From d4302bdb58e94bdda3a36923ad3478529e704286 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 31 May 2024 12:06:22 +0200 Subject: [PATCH] listener game --- .../ArkitDoushiQi/Game/GameScene.swift | 17 ++++-- .../ArkitDoushiQi/Game/GameView.swift | 59 ++++++++++++++++++- .../Game/SKNodes/SpriteMoople.swift | 2 + 3 files changed, 71 insertions(+), 7 deletions(-) diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift index 503d60b..21b9bbe 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift @@ -12,8 +12,9 @@ import DouShouQiModel class GameScene : SKScene { let imageBoard:SKSpriteNode = SKSpriteNode(imageNamed: "board") - var game:Game = try! Game(withRules: ClassicRules(), andPlayer1: Player(withName: "x", andId: .player1)!, andPlayer2: Player(withName: "y", andId: .player2)!) + var game:Game = try! Game(withRules: ClassicRules(), andPlayer1: RandomPlayer(withName: "Bot1", andId: .player1)!, andPlayer2: HumanPlayer(withName: "Bot2", andId: .player2,andInputMethod:)!) + static var lastMove:Move? = nil var pieces: [Owner : [ Animal : SpriteMoople]] = [ .player1 : [ .cat : SpriteMoople(nameImage: "catMeeple", couleur: .red), .dog : SpriteMoople(nameImage: "dogMeeple", couleur: .red), @@ -33,6 +34,10 @@ class GameScene : SKScene { .elephant : SpriteMoople(nameImage: "elephantMeeple", couleur: .blue), .tiger : SpriteMoople(nameImage: "tigerMeeple", couleur: .blue)]] + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + } + override init(size: CGSize) { super.init(size: size) @@ -63,11 +68,11 @@ class GameScene : SKScene { } } - - - - required init?(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) + func humanChoseMove(player:HumanPlayer){ + + + return Move(of: player.id, fromRow: <#T##Int#>, andFromColumn: <#T##Int#>, toRow: <#T##Int#>, andToColumn: <#T##Int#>) } + } diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift index 427396b..c735a65 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift @@ -7,14 +7,71 @@ import SwiftUI import SpriteKit +import DouShouQiModel struct GameView: View { var game:GameScene = GameScene(size: CGSize(width: 940, height: 740)) + + var body: some View { - SpriteView(scene: game) + //Text("Msg à modifié !") + SpriteView(scene: game).task { + + game.game.addGameStartedListener { board in startGame()} + game.game.addGameOverListener { board, result, player in gameOver() } + game.game.addGameChangedListener { game in gameChange() } + game.game.addBoardChangedListener { board in boardChange() } + + game.game.addMoveChosenCallbacksListener { board, move, player in moveChose(board: board, move: move, player: player) } + game.game.addInvalidMoveCallbacksListener { board,move,player,bool in invalidMove(board: board, move: move, player:player, bool:bool)} + + game.game.addPlayerNotifiedListener { board, player in + print("Player notif : \(player.id)") + + + try await player.chooseMove(in: board, with: game.game.rules) + + + print("------------") + } + + + + + try! await game.game.start() + } + } + + // ------ Listener -------- // + + func startGame() { print("Start game !") } + func gameOver() { print("Game over !") } + func gameChange() { print("Game change !") } + func boardChange() { + print("Board change !") + game.displayBoard(board: game.game.board) + print("Last : \(String(describing: GameScene.lastMove?.description))") } + + func moveChose(board:Board,move:Move,player:Player) { + print("Move choisi !") + print("joué par \(player.id)") + print("Move : \(move.description)") + print("------------") + } + + func invalidMove(board:Board,move:Move,player:Player,bool:Bool) { + print("Move invalid ! (\(bool))") + print("Player : \(player.id)") + print("Move : \(move.description)") + print("------------") + } + + + // ------------------------- // + } struct GameView_Previews: PreviewProvider { diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift index b37112c..88c7dc3 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift @@ -7,6 +7,7 @@ import Foundation import SpriteKit +import DouShouQiModel class SpriteMoople : SKNode { @@ -58,6 +59,7 @@ class SpriteMoople : SKNode { let posX = Int(round((localisation.x - (-400)) / 100)) let posY = Int(round((localisation.y - (-300)) / 100)) + //GameScene.lastMove = Move(of:owner, fromRow:Int(self.cellPosition.x),andFromColumn:Int(self.cellPosition.y),toRow:posX, andToColumn:posY) self.cellPosition = CGPoint(x: posX, y: posY) return }