From 48393b1772d88849610376d1db5ad1ac4660e24f Mon Sep 17 00:00:00 2001 From: Enzo Date: Tue, 4 Jun 2024 23:02:08 +0200 Subject: [PATCH] =?UTF-8?q?finish=20=3F=20j'esp=C3=A8re?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArkitDoushiQi/Game/GameScene.swift | 2 + .../ArkitDoushiQi/Game/GameView.swift | 49 ++++++++----------- .../Game/SKNodes/SpriteMoople.swift | 42 +++++++++++----- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift index df6da33..db77584 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift @@ -32,6 +32,8 @@ class GameScene : SKScene { .elephant : SpriteMoople(nameImage: "elephantMeeple", couleur: .blue), .tiger : SpriteMoople(nameImage: "tigerMeeple", couleur: .blue)]] + var deplacementPossible: [SKShapeNode] = [] + required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift index 2e83081..6f26f29 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift @@ -11,50 +11,43 @@ import DouShouQiModel struct GameView: View { - var game:GameScene = GameScene(size: CGSize(width: 940, height: 740)) + var gameScene:GameScene = GameScene(size: CGSize(width: 940, height: 740)) + + @State var msg:Text = Text("Msg") var body: some View { - //Text("Msg à modifié !") - SpriteView(scene: game).task { + + VStack(){ + Text($msg) + 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() } + gameScene.game.addGameStartedListener { board in startGame()} + gameScene.game.addGameOverListener { board, result, player in gameOver() } + gameScene.game.addGameChangedListener { game in gameChange() } + gameScene.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)} + gameScene.game.addMoveChosenCallbacksListener { board, move, player in moveChose(board: board, move: move, player: player) } + gameScene.game.addInvalidMoveCallbacksListener { board,move,player,bool in invalidMove(board: board, move: move, player:player, bool:bool)} - game.game.addPlayerNotifiedListener { board, player in + gameScene.game.addPlayerNotifiedListener { board, player in print("Player notif : \(player.id) à toi de jouer ") - - /* - if player is HumanPlayer { - var x:Move - //let newThread = Thread { x = game.humanChoseMove(player: player as! HumanPlayer) } - //newThread.start() - - //let move = await game.humanChoseMove(player: player as! HumanPlayer) - try! await (player as! HumanPlayer).chooseMove(x) - } - else { - try await player.chooseMove(in: board, with: game.game.rules) - }*/ - print("------------") } - try! await game.game.start() + try! await gameScene.game.start() + } } } // ------ Listener -------- // - func startGame() { print("Start game !") } + mutating func startGame() { print("Start game !") + msg = "Start !!" + } func gameOver() { print("Game over !") } func gameChange() { print("Game change !") } func boardChange() { print("Board change !") - game.displayBoard(board: game.game.board) + gameScene.displayBoard(board: gameScene.game.board) } func moveChose(board:Board,move:Move,player:Player) { @@ -65,7 +58,7 @@ struct GameView: View { } func invalidMove(board:Board,move:Move,player:Player,bool:Bool) { - print("Move invalid ! (\(bool))") + print("Move invalid ! (\(bool))") // True == Valid / False == Invalid print("Player : \(player.id)") print("Move : \(move.description)") print("------------") diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift index 4d0c83b..3c204d4 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift @@ -43,6 +43,13 @@ class SpriteMoople : SKNode { } override func touchesBegan(_ touches: Set, with event: UIEvent?) { + var movePoss: [ move ] = refGameScene!.game.rules.getMoves(board: refGameScene!.game.board,owner:refGameScene!.game.rules.getNextPlayer()) + for pos in movePoss { + var rect = SKShapeNode(rectOf: CGSize(width: 90, height: 90)) + rect.position = returnPositionByCellPos(cellX:move.destinationRow,cellY:move.destinationColm) + refGameScene!.deplacementPossible.add(rect) + refGameScene!.addChild(rect) + } } override func touchesMoved(_ touches: Set, with event: UIEvent?) { @@ -51,6 +58,11 @@ class SpriteMoople : SKNode { override func touchesEnded(_ touches: Set, with event: UIEvent?) { let localisation = touches.first?.location(in: parent!) + + // Clean rect + for rect in refGameScene!.deplacementPossible { + rect.removeChild() + } if let localisation = localisation { @@ -61,15 +73,22 @@ class SpriteMoople : SKNode { let posX = Int(round((localisation.x - (-400)) / 100)) let posY = Int(round((localisation.y - (-300)) / 100)) - //GameScene.lastMove = Move(of:.noOne, fromRow:Int(self.cellPosition.x),andFromColumn:Int(self.cellPosition.y),toRow:posX, andToColumn:posY) - //print("Change lastMove -> \(GameScene.lastMove)"); - self.cellPosition = CGPoint(x: posX, y: posY) - - // Get player - let x = refGameScene?.game.rules.getNextPlayer() - //try! refGameScene?.game.players. - - // Faire un retour en arrière si lastMove != nil + // Récupéré le joueur qui doit jouer + let player = refGameScene!.game.rules.getNextPlayer() + if ( player == refGameScene!.game.board.grid[self.cellPosition.x][self.cellPosition.y].piece!.owner ){ + if player is HumanPlayer { // Player humain + try! (refGameScene!.game.players[player] as! HumanPlayer).chooseMove(x) + } + else { // IA + try! await refGameScene!.game.players[player].chooseMove(in: refGameScene!.game.board, with: refGameScene!.game.rules) + } + // Change position de la piece + self.cellPosition = CGPoint(x: posX, y: posY) + } + else { + // Ce n'est pas la bonne personne, reset position + self.position = returnPositionByCellPos(cellX:self.cellPosition.x,cellY:self.cellPosition.y) + } return } } @@ -79,6 +98,7 @@ class SpriteMoople : SKNode { fatalError("init(coder:) has not been implemented") } - - + func returnPositionByCellPos(cellX:Int,cellY:Int) -> CGPoint { + return CGPoint(x: SpriteMoople.offset.x + SpriteMoople.direction.dx * cellX,y: self.position.y = SpriteMoople.offset.y + SpriteMoople.direction.dy * cellY) + } }