diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift index db77584..44860c6 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift @@ -12,7 +12,7 @@ import DouShouQiModel class GameScene : SKScene { let imageBoard:SKSpriteNode = SKSpriteNode(imageNamed: "board") - var game:Game = try! Game(withRules: ClassicRules(), andPlayer1: RandomPlayer(withName: "Bot1", andId: .player1)!, andPlayer2: HumanPlayer(withName: "Bot2", andId: .player2)!) + var game:Game = try! Game(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: "Bot1", andId: .player1)!, andPlayer2: RandomPlayer(withName: "Bot2", andId: .player2)!) var pieces: [Owner : [ Animal : SpriteMoople]] = [ .player1 : [ .cat : SpriteMoople(nameImage: "catMeeple", couleur: .red), .dog : SpriteMoople(nameImage: "dogMeeple", couleur: .red), @@ -44,12 +44,14 @@ class GameScene : SKScene { // -- -- // scaleMode = .aspectFit anchorPoint = CGPoint(x: 0.5, y: 0.5) + imageBoard.zPosition = 0 self.addChild(imageBoard) // -- -- // for c in pieces.flatMap({ _,values in return values }) { c.self.value.refGameScene = self + c.self.value.zPosition = 2 self.addChild(c.self.value) } diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift index 6f26f29..f883dd3 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift @@ -13,13 +13,13 @@ struct GameView: View { var gameScene:GameScene = GameScene(size: CGSize(width: 940, height: 740)) - @State var msg:Text = Text("Msg") + @State var msg:String = "Msg" var body: some View { VStack(){ - Text($msg) - SpriteView(scene: game).task { + Text(msg) + SpriteView(scene: gameScene).task { gameScene.game.addGameStartedListener { board in startGame()} gameScene.game.addGameOverListener { board, result, player in gameOver() } @@ -28,9 +28,16 @@ struct GameView: View { 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)} + + gameScene.game.addPieceRemovedListener { _,_,piece in removePiece(piece: piece) } gameScene.game.addPlayerNotifiedListener { board, player in print("Player notif : \(player.id) à toi de jouer ") + msg = "Player notif : \(player.id) à toi de jouer !" + + if (player is IAPlayer ){ + try! await player.chooseMove(in: board, with: gameScene.game.rules) + } } try! await gameScene.game.start() @@ -40,10 +47,12 @@ struct GameView: View { // ------ Listener -------- // - mutating func startGame() { print("Start game !") + func startGame() { print("Start game !") msg = "Start !!" } - func gameOver() { print("Game over !") } + func gameOver() { print("Game over !") + msg = "Game over !!" + } func gameChange() { print("Game change !") } func boardChange() { print("Board change !") @@ -51,19 +60,31 @@ struct GameView: View { } 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))") // True == Valid / False == Invalid - print("Player : \(player.id)") - print("Move : \(move.description)") + + if (bool){ // Valid + print("Move valid de \(player.id)") + print("Move : \(move.description)") + } + else { // Invalid + print("Move invalid de \(player.id)") + print("Move : \(move.description)") + gameScene.displayBoard(board: gameScene.game.board) + } print("------------") } + func removePiece(piece:Piece){ + print("Remove piece") + + if let node = gameScene.pieces[piece.owner]![piece.animal] { + print("Remove piece from parent") + node.removeFromParent() + } + } + // ------------------------- // diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift index 3c204d4..a566948 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/SKNodes/SpriteMoople.swift @@ -43,11 +43,14 @@ 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()) + + let movePoss: [ Move ] = refGameScene!.game.rules.getMoves(in: refGameScene!.game.board,of:refGameScene!.game.rules.getNextPlayer(),fromRow: Int(self.cellPosition.x),andColumn: Int(self.cellPosition.y)) 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) + let rect = SKShapeNode(rectOf: CGSize(width: 90, height: 90)) + rect.position = returnPositionByCellPos(cellX:pos.rowDestination,cellY:pos.columnDestination) + rect.fillColor = .red + rect.zPosition = 1 + refGameScene!.deplacementPossible.append(rect) refGameScene!.addChild(rect) } } @@ -59,36 +62,34 @@ 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() - } + // Clean rect + refGameScene!.removeChildren(in: refGameScene!.deplacementPossible) if let localisation = localisation { - - if (localisation.x > 400 || localisation.x < -400 || localisation.y < -300 || localisation.y > 300){ + print(localisation) + if (localisation.x > 450 || localisation.x < -450 || localisation.y < -350 || localisation.y > 350){ print("Hors limite !") + self.position = returnPositionByCellPos(cellX:Int(self.cellPosition.x),cellY:Int(self.cellPosition.y)) return } let posX = Int(round((localisation.x - (-400)) / 100)) let posY = Int(round((localisation.y - (-300)) / 100)) // 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 ){ + let owner = refGameScene!.game.rules.getNextPlayer() + let player = refGameScene!.game.players[owner]! + + if ( owner == refGameScene!.game.board.grid[Int(self.cellPosition.x)][Int(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) + Task { + try! await (player as! HumanPlayer).chooseMove(Move(of: owner, fromRow:Int(self.cellPosition.x), andFromColumn: Int(self.cellPosition.y), toRow: posX, andToColumn: posY)) + } } - // 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) - } + self.position = returnPositionByCellPos(cellX:Int(self.cellPosition.x),cellY:Int(self.cellPosition.y)) + } return } } @@ -99,6 +100,6 @@ class SpriteMoople : SKNode { } 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) + return CGPoint(x: SpriteMoople.offset.x + SpriteMoople.direction.dx * CGFloat(cellX),y: SpriteMoople.offset.y + SpriteMoople.direction.dy * CGFloat(cellY)) } }