diff --git a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj index ae2026d..f347419 100644 --- a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj +++ b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ 649B59AE2BF64EAB002BAE38 /* AppImages.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649B59AD2BF64EAB002BAE38 /* AppImages.swift */; }; 649B59B22BF65392002BAE38 /* TextStyles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649B59B12BF65392002BAE38 /* TextStyles.swift */; }; 649B59B42BF653E1002BAE38 /* ViewTitleTextStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 649B59B32BF653E1002BAE38 /* ViewTitleTextStyle.swift */; }; + 64D2D74E2C25D380009BD010 /* Animals.Symbols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64D2D74D2C25D380009BD010 /* Animals.Symbols.swift */; }; 64D992722C06281B002ACBC6 /* SystemIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64D992712C06281B002ACBC6 /* SystemIcons.swift */; }; 64FC4D692C09C78000D08B8B /* SelectPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64FC4D682C09C78000D08B8B /* SelectPlayerView.swift */; }; 64FC4D6B2C09C7C900D08B8B /* SelectPlayerButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64FC4D6A2C09C7C900D08B8B /* SelectPlayerButtonView.swift */; }; @@ -129,6 +130,7 @@ 649B59AD2BF64EAB002BAE38 /* AppImages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppImages.swift; sourceTree = ""; }; 649B59B12BF65392002BAE38 /* TextStyles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextStyles.swift; sourceTree = ""; }; 649B59B32BF653E1002BAE38 /* ViewTitleTextStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewTitleTextStyle.swift; sourceTree = ""; }; + 64D2D74D2C25D380009BD010 /* Animals.Symbols.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Animals.Symbols.swift; sourceTree = ""; }; 64D992712C06281B002ACBC6 /* SystemIcons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemIcons.swift; sourceTree = ""; }; 64FC4D682C09C78000D08B8B /* SelectPlayerView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectPlayerView.swift; sourceTree = ""; }; 64FC4D6A2C09C7C900D08B8B /* SelectPlayerButtonView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectPlayerButtonView.swift; sourceTree = ""; }; @@ -447,6 +449,7 @@ isa = PBXGroup; children = ( EC62C53E2C1C6D1A0048CD0B /* CDPlayerExtension.swift */, + 64D2D74D2C25D380009BD010 /* Animals.Symbols.swift */, ); path = Extensions; sourceTree = ""; @@ -637,6 +640,7 @@ 649B59B22BF65392002BAE38 /* TextStyles.swift in Sources */, EC99D5C12C089F6B00731B62 /* GameResumeFrame.swift in Sources */, EC05BFCB2C05F470000F7B19 /* GameScene.swift in Sources */, + 64D2D74E2C25D380009BD010 /* Animals.Symbols.swift in Sources */, 6458345C2BF5F92300E18321 /* DouShouQi_AppApp.swift in Sources */, ECB636512C046379007CD5E2 /* MusicPlayer.swift in Sources */, ECB6364E2C04628E007CD5E2 /* SpriteMeeple.swift in Sources */, diff --git a/DouShouQi_App/DouShouQi_App/AppModelAndExtension/Extensions/Animals.Symbols.swift b/DouShouQi_App/DouShouQi_App/AppModelAndExtension/Extensions/Animals.Symbols.swift new file mode 100644 index 0000000..4ddbdb3 --- /dev/null +++ b/DouShouQi_App/DouShouQi_App/AppModelAndExtension/Extensions/Animals.Symbols.swift @@ -0,0 +1,34 @@ +// +// Animals.Symbols.swift +// DouShouQi_App +// +// Created by Rémi REGNAULT on 21/06/2024. +// + +import Foundation +import DouShouQiModel + +extension Animal { + public var symbol: String { + switch self{ + case .rat: + return "🐭" + case .cat: + return "🐱" + case .dog: + return "🐶" + case .wolf: + return "🐺" + case .leopard: + return "🐆" + case .tiger: + return "🐯" + case .lion: + return "🦁" + case .elephant: + return "🐘" + @unknown default: + return "🐥" + } + } +} diff --git a/DouShouQi_App/DouShouQi_App/Components/Scene/GameScene.swift b/DouShouQi_App/DouShouQi_App/Components/Scene/GameScene.swift index 0ea5b8a..5c1b24f 100644 --- a/DouShouQi_App/DouShouQi_App/Components/Scene/GameScene.swift +++ b/DouShouQi_App/DouShouQi_App/Components/Scene/GameScene.swift @@ -19,24 +19,24 @@ class GameScene : SKScene { let pieces: [Owner : [Animal:SpriteMeeple]] = [ .player1: [ - .rat: SpriteMeeple(imageNamed: AppImages.Rat, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!), - .cat: SpriteMeeple(imageNamed: AppImages.Cat, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!), - .dog: SpriteMeeple(imageNamed: AppImages.Dog, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!), - .wolf: SpriteMeeple(imageNamed: AppImages.Wolf, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!), - .leopard: SpriteMeeple(imageNamed: AppImages.Leopard, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!), - .lion: SpriteMeeple(imageNamed: AppImages.Lion, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!), - .tiger: SpriteMeeple(imageNamed: AppImages.Tigger, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!), - .elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!), + .rat: SpriteMeeple(imageNamed: AppImages.Rat, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180), + .cat: SpriteMeeple(imageNamed: AppImages.Cat, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180), + .dog: SpriteMeeple(imageNamed: AppImages.Dog, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180), + .wolf: SpriteMeeple(imageNamed: AppImages.Wolf, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180), + .leopard: SpriteMeeple(imageNamed: AppImages.Leopard, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180), + .lion: SpriteMeeple(imageNamed: AppImages.Lion, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180), + .tiger: SpriteMeeple(imageNamed: AppImages.Tigger, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180), + .elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180), ], .player2: [ - .rat: SpriteMeeple(imageNamed: AppImages.Rat, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180), - .cat: SpriteMeeple(imageNamed: AppImages.Cat, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180), - .dog: SpriteMeeple(imageNamed: AppImages.Dog, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180), - .wolf: SpriteMeeple(imageNamed: AppImages.Wolf, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180), - .leopard: SpriteMeeple(imageNamed: AppImages.Leopard, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180), - .lion: SpriteMeeple(imageNamed: AppImages.Lion, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180), - .tiger: SpriteMeeple(imageNamed: AppImages.Tigger, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180), - .elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180), + .rat: SpriteMeeple(imageNamed: AppImages.Rat, size: CGSize(width: 100, height: 100), backgroundColor: .blue), + .cat: SpriteMeeple(imageNamed: AppImages.Cat, size: CGSize(width: 100, height: 100), backgroundColor: .blue), + .dog: SpriteMeeple(imageNamed: AppImages.Dog, size: CGSize(width: 100, height: 100), backgroundColor: .blue), + .wolf: SpriteMeeple(imageNamed: AppImages.Wolf, size: CGSize(width: 100, height: 100), backgroundColor: .blue), + .leopard: SpriteMeeple(imageNamed: AppImages.Leopard, size: CGSize(width: 100, height: 100), backgroundColor: .blue), + .lion: SpriteMeeple(imageNamed: AppImages.Lion, size: CGSize(width: 100, height: 100), backgroundColor: .blue), + .tiger: SpriteMeeple(imageNamed: AppImages.Tigger, size: CGSize(width: 100, height: 100), backgroundColor: .blue), + .elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: .blue), ], ] @@ -60,24 +60,12 @@ class GameScene : SKScene { for piece in pieces.flatMap({owner, pieces in pieces.values}) { self.addChild(piece) - piece.setOnMove(onMove: onMeepleMove) + piece.setOnMove(onMove: gameVM.onMeepleMove) } initializeBoard(game!.board) - game?.addInvalidMoveCallbacksListener { _, move, player, result in - if result { - return - } - print("**************************************") - print("⚠️⚠️⚠️⚠️ Invalid Move detected: \(move) by \(player.name) (\(player.id))") - print("**************************************") - - } - - Task { - try await game?.start() - } + gameVM.start() } func initializeBoard(_ board: Board) { @@ -86,7 +74,6 @@ class GameScene : SKScene { if let piece = currentCell.piece { pieces[piece.owner]?[piece.animal]?.cellPosition = CGPoint(x: cellIndex, y: lineIndex) pieces[piece.owner]?[piece.animal]?.cellPosition = CGPoint(x: 0, y: 0) - print("line :", lineIndex, " column: ", cellIndex) } } } @@ -96,24 +83,4 @@ class GameScene : SKScene { game = try! Game(withRules: ClassicRules(), andPlayer1: DouShouQiModel.Player(withName: "P1", andId: .player1)!, andPlayer2: DouShouQiModel.Player(withName: "P2", andId: .player2)!) super.init(coder: aDecoder); } - - func onMeepleMove(_ start: CGPoint, _ end: CGPoint) { - - if let game: Game = self.game { - let owner = game.rules.getNextPlayer() - let player: DouShouQiModel.Player = game.players[owner]! - - let move = Move(of: owner, fromRow: Int(start.y), andFromColumn: Int(start.x), toRow: Int(end.y), andToColumn: Int(end.x)) - - print("Meeple moved = ", start, " -> ", end) - - Task { - try! await (player as! HumanPlayer).chooseMove(move) - } - } - } - - public func start() async throws { - try await game?.start() - } } diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Game/PlayingGameVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Game/PlayingGameVM.swift index 1d24ad4..68b4961 100644 --- a/DouShouQi_App/DouShouQi_App/ViewModel/Game/PlayingGameVM.swift +++ b/DouShouQi_App/DouShouQi_App/ViewModel/Game/PlayingGameVM.swift @@ -18,14 +18,20 @@ class PlayingGameVM: ObservableObject { public var lastMove: String { if let move: Move = game.rules.historic.last { - return "\(move.rowDestination).\(move.columnDestination)" + return "\(move.columnDestination).\(move.rowDestination)" } else { return "" } } - public var lastPieceMoved: String { "oui" } + public var lastPieceMoved: String { + if let move: Move = game.rules.historic.last { + return game.board.grid[move.rowDestination][move.columnDestination].piece?.animal.symbol ?? "" + } else { + return "" + } + } // Inits init(withGame game: Game) { @@ -35,10 +41,42 @@ class PlayingGameVM: ObservableObject { init?(withRules rules: Rules, andPlayer1 p1: DouShouQiModel.Player, andPlayer2 p2: DouShouQiModel.Player) { do { self.game = try Game(withRules: rules, andPlayer1: p1, andPlayer2: p2) + + self.game.addInvalidMoveCallbacksListener { _, move, player, result in + if result { + return + } + print("**************************************") + print("⚠️⚠️⚠️⚠️ Invalid Move detected: \(move) by \(player.name) (\(player.id))") + print("**************************************") + + } + } catch { print("Error") return nil } } + // Customs funcs + func onMeepleMove(_ start: CGPoint, _ end: CGPoint) { + let owner = game.rules.getNextPlayer() + let player: DouShouQiModel.Player = game.players[owner]! + + let move = Move(of: owner, fromRow: Int(start.y), andFromColumn: Int(start.x), toRow: Int(end.y), andToColumn: Int(end.x)) + + print("Meeple moved = ", start, " -> ", end) + + Task { + try! await (player as! HumanPlayer).chooseMove(move) + } + self.objectWillChange.send() + } + + public func start() { + Task { + try await self.game.start() + } + } + }