From 400568564a4fd12aa21931d778c13142b6f15927 Mon Sep 17 00:00:00 2001 From: "louis.dufour" Date: Mon, 24 Jun 2024 15:49:51 +0200 Subject: [PATCH] Add(Gestion joueur): affichage image et nom --- .../ArkitDoushiQi.xcodeproj/project.pbxproj | 52 ------------------ .../ArkitDoushiQi/Game/GameScene.swift | 1 - .../ArkitDoushiQi/Game/GameView.swift | 33 ++++++----- ArkitDoushiQi/ArkitDoushiQi/Game/VMGame.swift | 55 +++++++++++++------ .../Views/GameParametersMenuView.swift | 17 +++++- 5 files changed, 70 insertions(+), 88 deletions(-) diff --git a/ArkitDoushiQi/ArkitDoushiQi.xcodeproj/project.pbxproj b/ArkitDoushiQi/ArkitDoushiQi.xcodeproj/project.pbxproj index f1c9421..0524e85 100644 --- a/ArkitDoushiQi/ArkitDoushiQi.xcodeproj/project.pbxproj +++ b/ArkitDoushiQi/ArkitDoushiQi.xcodeproj/project.pbxproj @@ -24,15 +24,6 @@ 82CE59EB2C045E3800ADEE24 /* GameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82CE59EA2C045E3800ADEE24 /* GameView.swift */; }; 82CE59EF2C0460E500ADEE24 /* SpriteMoople.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82CE59EE2C0460E500ADEE24 /* SpriteMoople.swift */; }; 82D7BDA22C29A25500723ABA /* VMGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 82D7BDA12C29A25500723ABA /* VMGame.swift */; }; - 82FBE2F22C25811400672F32 /* lion.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2E92C25811400672F32 /* lion.usdz */; }; - 82FBE2F32C25811400672F32 /* dog.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2EA2C25811400672F32 /* dog.usdz */; }; - 82FBE2F42C25811400672F32 /* board.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2EB2C25811400672F32 /* board.usdz */; }; - 82FBE2F52C25811400672F32 /* tiger.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2EC2C25811400672F32 /* tiger.usdz */; }; - 82FBE2F62C25811400672F32 /* wolf.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2ED2C25811400672F32 /* wolf.usdz */; }; - 82FBE2F72C25811400672F32 /* leopard.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2EE2C25811400672F32 /* leopard.usdz */; }; - 82FBE2F82C25811400672F32 /* cat.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2EF2C25811400672F32 /* cat.usdz */; }; - 82FBE2F92C25811400672F32 /* elephant.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2F02C25811400672F32 /* elephant.usdz */; }; - 82FBE2FA2C25811400672F32 /* rat.usdz in Resources */ = {isa = PBXBuildFile; fileRef = 82FBE2F12C25811400672F32 /* rat.usdz */; }; C205A2B72BF373360097BD93 /* ArkitDoushiQiApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C205A2B62BF373360097BD93 /* ArkitDoushiQiApp.swift */; }; C205A2BB2BF373380097BD93 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C205A2BA2BF373380097BD93 /* Assets.xcassets */; }; C205A2BE2BF373380097BD93 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C205A2BD2BF373380097BD93 /* Preview Assets.xcassets */; }; @@ -115,15 +106,6 @@ 82CE59EA2C045E3800ADEE24 /* GameView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameView.swift; sourceTree = ""; }; 82CE59EE2C0460E500ADEE24 /* SpriteMoople.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpriteMoople.swift; sourceTree = ""; }; 82D7BDA12C29A25500723ABA /* VMGame.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VMGame.swift; sourceTree = ""; }; - 82FBE2E92C25811400672F32 /* lion.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = lion.usdz; path = Ressources/3DModel/lion.usdz; sourceTree = SOURCE_ROOT; }; - 82FBE2EA2C25811400672F32 /* dog.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = dog.usdz; path = Ressources/3DModel/dog.usdz; sourceTree = SOURCE_ROOT; }; - 82FBE2EB2C25811400672F32 /* board.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = board.usdz; path = Ressources/3DModel/board.usdz; sourceTree = SOURCE_ROOT; }; - 82FBE2EC2C25811400672F32 /* tiger.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = tiger.usdz; path = Ressources/3DModel/tiger.usdz; sourceTree = SOURCE_ROOT; }; - 82FBE2ED2C25811400672F32 /* wolf.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = wolf.usdz; path = Ressources/3DModel/wolf.usdz; sourceTree = SOURCE_ROOT; }; - 82FBE2EE2C25811400672F32 /* leopard.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = leopard.usdz; path = Ressources/3DModel/leopard.usdz; sourceTree = SOURCE_ROOT; }; - 82FBE2EF2C25811400672F32 /* cat.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = cat.usdz; path = Ressources/3DModel/cat.usdz; sourceTree = SOURCE_ROOT; }; - 82FBE2F02C25811400672F32 /* elephant.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = elephant.usdz; path = Ressources/3DModel/elephant.usdz; sourceTree = SOURCE_ROOT; }; - 82FBE2F12C25811400672F32 /* rat.usdz */ = {isa = PBXFileReference; lastKnownFileType = file.usdz; name = rat.usdz; path = Ressources/3DModel/rat.usdz; sourceTree = SOURCE_ROOT; }; C205A2B32BF373360097BD93 /* ArkitDoushiQi.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ArkitDoushiQi.app; sourceTree = BUILT_PRODUCTS_DIR; }; C205A2B62BF373360097BD93 /* ArkitDoushiQiApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkitDoushiQiApp.swift; sourceTree = ""; }; C205A2BA2BF373380097BD93 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -197,7 +179,6 @@ 165725EB2C1985920052D7EF /* Ressources */ = { isa = PBXGroup; children = ( - 82FBE2E82C2580D800672F32 /* Object */, 16FA058D2C1C14FD0042D688 /* fr.lproj */, 16FA05892C1C14F50042D688 /* en.lproj */, 165725EF2C1985920052D7EF /* Font */, @@ -269,22 +250,6 @@ path = SKNodes; sourceTree = ""; }; - 82FBE2E82C2580D800672F32 /* Object */ = { - isa = PBXGroup; - children = ( - 82FBE2EB2C25811400672F32 /* board.usdz */, - 82FBE2EF2C25811400672F32 /* cat.usdz */, - 82FBE2EA2C25811400672F32 /* dog.usdz */, - 82FBE2F02C25811400672F32 /* elephant.usdz */, - 82FBE2EE2C25811400672F32 /* leopard.usdz */, - 82FBE2E92C25811400672F32 /* lion.usdz */, - 82FBE2F12C25811400672F32 /* rat.usdz */, - 82FBE2EC2C25811400672F32 /* tiger.usdz */, - 82FBE2ED2C25811400672F32 /* wolf.usdz */, - ); - path = Object; - sourceTree = ""; - }; C205A2AA2BF373360097BD93 = { isa = PBXGroup; children = ( @@ -332,7 +297,6 @@ C2F015122C09D3E7000F7221 /* Components */, C2F0150D2C09D3C3000F7221 /* Enum */, C2F0150A2C09D3A4000F7221 /* EventTriggers */, - C25220EC2C00AC530026B71F /* GameParametersMenu */, C205A2BD2BF373380097BD93 /* Preview Assets.xcassets */, ); path = Views; @@ -355,13 +319,6 @@ path = ArkitDoushiQiUITests; sourceTree = ""; }; - C25220EC2C00AC530026B71F /* GameParametersMenu */ = { - isa = PBXGroup; - children = ( - ); - path = GameParametersMenu; - sourceTree = ""; - }; C2DF07592C206EEF00BEE0E1 /* Data */ = { isa = PBXGroup; children = ( @@ -561,22 +518,13 @@ buildActionMask = 2147483647; files = ( C205A2BE2BF373380097BD93 /* Preview Assets.xcassets in Resources */, - 82FBE2FA2C25811400672F32 /* rat.usdz in Resources */, - 82FBE2F92C25811400672F32 /* elephant.usdz in Resources */, 16DB00A42C257BA900BA190C /* nuku1.ttf in Resources */, C2DF07652C20719800BEE0E1 /* FireworkLaunch.sks in Resources */, - 82FBE2F22C25811400672F32 /* lion.usdz in Resources */, C2DF07642C20719800BEE0E1 /* Spark.sks in Resources */, C2DF07662C20719800BEE0E1 /* Fireworks.sks in Resources */, - 82FBE2F62C25811400672F32 /* wolf.usdz in Resources */, - 82FBE2F52C25811400672F32 /* tiger.usdz in Resources */, - 82FBE2F82C25811400672F32 /* cat.usdz in Resources */, C2DF07622C20719800BEE0E1 /* Magic.sks in Resources */, C2DF07632C20719800BEE0E1 /* Assets.xcassets in Resources */, - 82FBE2F32C25811400672F32 /* dog.usdz in Resources */, - 82FBE2F42C25811400672F32 /* board.usdz in Resources */, C205A2BB2BF373380097BD93 /* Assets.xcassets in Resources */, - 82FBE2F72C25811400672F32 /* leopard.usdz in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift index f915d80..1816764 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/GameScene.swift @@ -1,4 +1,3 @@ -// // GameScene.swift // ArkitDoushiQi // diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift index 759b0c0..ba55e8a 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/GameView.swift @@ -1,4 +1,3 @@ -// // GameView.swift // ArkitDoushiQi // @@ -10,12 +9,25 @@ import SpriteKit import DouShouQiModel struct GameView: View { - var player1Name: String = "toto" + var player1Name: String var player1Image: UIImage - var player2Name: String = "toto" + var isPlayer1AI: Bool + var player2Name: String var player2Image: UIImage + var isPlayer2AI: Bool - @ObservedObject var vm:VMGame = VMGame() + @ObservedObject var vm: VMGame + + init(playerName1: String, image1: UIImage, isPlayer1AI: Bool, player2Name: String, image2: UIImage, isPlayer2AI: Bool) { + self.player1Name = playerName1 + self.player1Image = image1 + self.isPlayer1AI = isPlayer1AI + self.player2Name = player2Name + self.player2Image = image2 + self.isPlayer2AI = isPlayer2AI + + self.vm = VMGame(player1Name: playerName1, player1Image: Image(uiImage: image1), isPlayer1AI: isPlayer1AI, player2Name: player2Name, player2Image: Image(uiImage: image2), isPlayer2AI: isPlayer2AI) + } var body: some View { ZStack { @@ -27,7 +39,7 @@ struct GameView: View { VStack { HStack { VStack { - Image("profil") + Image(uiImage: player1Image) .resizable() .aspectRatio(contentMode: .fill) .frame(width: 125, height: 125) @@ -40,7 +52,7 @@ struct GameView: View { } Spacer() VStack { - Image("profil") + Image(uiImage: player2Image) .resizable() .aspectRatio(contentMode: .fill) .frame(width: 125, height: 125) @@ -61,13 +73,4 @@ struct GameView: View { } } } - - init(playerName1:String,image1:UIImage,player2Name:String,image2:UIImage){ - - //self.player1Name = playerName1 - self.player1Image = image1 - //self.player2Name = player2Name - self.player2Image = image2 - } } - diff --git a/ArkitDoushiQi/ArkitDoushiQi/Game/VMGame.swift b/ArkitDoushiQi/ArkitDoushiQi/Game/VMGame.swift index a2640e8..4e239d7 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Game/VMGame.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Game/VMGame.swift @@ -1,23 +1,40 @@ - +// VMGame.swift +// ArkitDoushiQi +// +// Created by Enzo JOLYS, Louis DUFOUR on 27/05/2024. +// import Foundation import SwiftUI import DouShouQiModel class VMGame : ObservableObject { - @Published var msg: String = "" - var gameScene: GameScene = GameScene(size: CGSize(width: 940, height: 740),player1Name:"TOTO",player1Image: Image("profil"),player2Name:"toto2",player2Image: Image("profil")) + var gameScene: GameScene + var isPlayer1AI: Bool + var isPlayer2AI: Bool - func start() async { + init(player1Name: String, player1Image: Image, isPlayer1AI: Bool, player2Name: String, player2Image: Image, isPlayer2AI: Bool) { + self.isPlayer1AI = isPlayer1AI + self.isPlayer2AI = isPlayer2AI + + let player1: Player = isPlayer1AI ? IAPlayer(withName: player1Name, andId: .player1)! : HumanPlayer(withName: player1Name, andId: .player1)! + let player2: Player = isPlayer2AI ? IAPlayer(withName: player2Name, andId: .player2)! : HumanPlayer(withName: player2Name, andId: .player2)! + + self.gameScene = GameScene(size: CGSize(width: 940, height: 740), player1Name: player1Name, player1Image: player1Image, player2Name: player2Name, player2Image: player2Image) + self.gameScene.game = try! Game(withRules: ClassicRules(), andPlayer1: player1, andPlayer2: player2) + defineListener() + } + + func start() async { try! await gameScene.game.start() } - func defineListener(){ + func defineListener() { gameScene.game.addGameStartedListener { board in self.startGame() } - gameScene.game.addGameOverListener { board, result, player in self.gameOver(result : result) } + gameScene.game.addGameOverListener { board, result, player in self.gameOver(result: result) } gameScene.game.addGameChangedListener { game in self.gameChange() } gameScene.game.addBoardChangedListener { board in self.boardChange() } @@ -27,11 +44,15 @@ class VMGame : ObservableObject { gameScene.game.addPieceRemovedListener { _, _, piece in self.removePiece(piece: piece) } gameScene.game.addPlayerNotifiedListener { board, player in - print("Player notif : \(player.id) à toi de jouer ") - self.msg = "Player notif : \(player.id) à toi de jouer !" - - if player is IAPlayer { - try! await player.chooseMove(in: board, with: self.gameScene.game.rules) + DispatchQueue.main.async { + print("Player notif : \(player.id) à toi de jouer ") + self.msg = "Player notif : \(player.id) à toi de jouer !" + + if let iaPlayer = player as? IAPlayer { + Task { + try await iaPlayer.chooseMove(in: board, with: self.gameScene.game.rules) + } + } } } } @@ -41,10 +62,10 @@ class VMGame : ObservableObject { msg = "Start !!" } - func gameOver(result : Result) { print("Game over !") + func gameOver(result: Result) { + print("Game over !") msg = "Game over ! " - switch result - { + switch result { case .winner(let winner, _): switch winner { case .player1: @@ -55,11 +76,11 @@ class VMGame : ObservableObject { msg = msg + "equality !" } default: - msg="Game over !" + msg = "Game over !" } gameScene.isGameOver = true - actionEndGame(scene: gameScene,message: msg) + actionEndGame(scene: gameScene, message: msg) } func gameChange() { @@ -86,7 +107,7 @@ class VMGame : ObservableObject { print("------------") } - func removePiece(piece:Piece){ + func removePiece(piece: Piece) { print("Remove piece") if let node = gameScene.pieces[piece.owner]![piece.animal] { actionRemove(size: gameScene.size, node: node, scene: gameScene) diff --git a/ArkitDoushiQi/ArkitDoushiQi/Views/GameParametersMenuView.swift b/ArkitDoushiQi/ArkitDoushiQi/Views/GameParametersMenuView.swift index b3597e1..cebd466 100644 --- a/ArkitDoushiQi/ArkitDoushiQi/Views/GameParametersMenuView.swift +++ b/ArkitDoushiQi/ArkitDoushiQi/Views/GameParametersMenuView.swift @@ -1,4 +1,3 @@ -// // GameParametersMenuView.swift // ArkitDoushiQi // @@ -16,6 +15,8 @@ struct GameParametersMenuView: View, KeyboardReadable { @State private var playerName2 = NSLocalizedString("Nom du Joueur 2", comment: "") @State private var playerImage1: UIImage = UIImage(named: "profil")! @State private var playerImage2: UIImage = UIImage(named: "profil")! + @State private var isPlayer1AI = false + @State private var isPlayer2AI = false @State private var keyboardHeight: CGFloat = 0 var body: some View { @@ -34,15 +35,25 @@ struct GameParametersMenuView: View, KeyboardReadable { PickerComponent(title: "Sélectionne une IA :", selectedOption: $selectedAIOption, options: AI.allCases) } + Toggle(isOn: $isPlayer1AI) { + Text("Joueur 1 est une IA") + } ProfileEdit(color: Color(.red), profileWidth: 100, profileHeight: 100, defaultImage: Image("profil"), imageTextChange: "Changer l'avatar du joueur 1", playerNameKey: "Nom du Joueur 1", playerName: $playerName1, playerImage: $playerImage1) if selectedGameType == .PvP { + Toggle(isOn: $isPlayer2AI) { + Text("Joueur 2 est une IA") + } ProfileEdit(color: Color(.blue), profileWidth: 100, profileHeight: 100, defaultImage: Image("profil"), imageTextChange: "Changer l'avatar du joueur 2", playerNameKey: "Nom du Joueur 2", playerName: $playerName2, playerImage: $playerImage2) } if !isKeyboardVisible { - ButtonComponent(title: "Lancer la partie") { - GameView(playerName1: playerName1, image1: playerImage1, player2Name: playerName2, image2: playerImage2) + NavigationLink(destination: GameView(playerName1: playerName1, image1: playerImage1, isPlayer1AI: isPlayer1AI, player2Name: playerName2, image2: playerImage2, isPlayer2AI: isPlayer2AI)) { + Text("Lancer la partie") + .foregroundColor(.white) + .padding() + .background(Color.blue) + .cornerRadius(10) } .padding(EdgeInsets(top: 10, leading: 32, bottom: 10, trailing: 32)) }