diff --git a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj index dc0afa3..b2dce8c 100644 --- a/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj +++ b/DouShouQi_App/DouShouQi_App.xcodeproj/project.pbxproj @@ -41,6 +41,7 @@ EC05BFC82C04D832000F7B19 /* CustomSwitchButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC05BFC72C04D832000F7B19 /* CustomSwitchButton.swift */; }; EC05BFCB2C05F470000F7B19 /* GameScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC05BFCA2C05F470000F7B19 /* GameScene.swift */; }; EC2DD03F2C0CDF0C00FC55C0 /* MainMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC2DD03E2C0CDF0C00FC55C0 /* MainMenuView.swift */; }; + EC2DD0412C1C203000FC55C0 /* ShowAllPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC2DD0402C1C203000FC55C0 /* ShowAllPlayer.swift */; }; EC3EE4B12C0CD46000710153 /* ButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC3EE4B02C0CD46000710153 /* ButtonView.swift */; }; EC3EE4B32C0CD47000710153 /* MainButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC3EE4B22C0CD47000710153 /* MainButton.swift */; }; EC62C4FB2C038BD20048CD0B /* PlayersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC62C4FA2C038BD20048CD0B /* PlayersView.swift */; }; @@ -123,6 +124,7 @@ EC05BFC72C04D832000F7B19 /* CustomSwitchButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSwitchButton.swift; sourceTree = ""; }; EC05BFCA2C05F470000F7B19 /* GameScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameScene.swift; sourceTree = ""; }; EC2DD03E2C0CDF0C00FC55C0 /* MainMenuView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainMenuView.swift; sourceTree = ""; }; + EC2DD0402C1C203000FC55C0 /* ShowAllPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowAllPlayer.swift; sourceTree = ""; }; EC3EE4B02C0CD46000710153 /* ButtonView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ButtonView.swift; sourceTree = ""; }; EC3EE4B22C0CD47000710153 /* MainButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainButton.swift; sourceTree = ""; }; EC62C4FA2C038BD20048CD0B /* PlayersView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayersView.swift; sourceTree = ""; }; @@ -251,6 +253,7 @@ 647D565B2BFD0212008D02EA /* PlayerResumeFrame.swift */, EC62C4FC2C0391D30048CD0B /* PlayerRow.swift */, EC62C50E2C05D06A0048CD0B /* AddPlayerView.swift */, + EC2DD0402C1C203000FC55C0 /* ShowAllPlayer.swift */, EC62C51A2C09D1790048CD0B /* PlayerStatView.swift */, ); path = Player; @@ -641,6 +644,7 @@ EC62C5092C0467240048CD0B /* SplashScreenView.swift in Sources */, EC2DD03F2C0CDF0C00FC55C0 /* MainMenuView.swift in Sources */, 64FC4D692C09C78000D08B8B /* SelectPlayerView.swift in Sources */, + EC2DD0412C1C203000FC55C0 /* ShowAllPlayer.swift in Sources */, EC62C4FD2C0391D30048CD0B /* PlayerRow.swift in Sources */, EC62C4FB2C038BD20048CD0B /* PlayersView.swift in Sources */, 6458345C2BF5F92300E18321 /* DouShouQi_AppApp.swift in Sources */, diff --git a/DouShouQi_App/DouShouQi_App/Components/Player/SelectPlayerButtonView.swift b/DouShouQi_App/DouShouQi_App/Components/Player/SelectPlayerButtonView.swift index 93bf37b..ce3ebb0 100644 --- a/DouShouQi_App/DouShouQi_App/Components/Player/SelectPlayerButtonView.swift +++ b/DouShouQi_App/DouShouQi_App/Components/Player/SelectPlayerButtonView.swift @@ -35,8 +35,13 @@ struct CustomShapeRightButton: Shape { struct SelectPlayerButtonView: View { - var player1:String = "IA" - var player2:String = "IA" + @State var player1:String = "IA" + @State var player2:String = "IA" + + @ObservedObject var playersVM: PlayersVM + + @State private var showingPopup = false + var body: some View { ZStack{ Image("vs-custom") @@ -51,6 +56,15 @@ struct SelectPlayerButtonView: View { .padding(3) Button(action: { print("Button j1 pressé!") + if (player1 == "IA"){ + if (player2 == "Player1"){ + player1="Player2" + }else{ + player1="Player1" + } + }else{ + player1="IA" + } }) { VStack { Text("+") @@ -62,9 +76,8 @@ struct SelectPlayerButtonView: View { } .frame(width: 150, height: 150) .padding() - .border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: 4) - .foregroundColor(.black) - .foregroundColor(.white) + .border(Colors.TitleText, width: 4) + .foregroundColor(Colors.TitleText) .cornerRadius(10) .clipShape(CustomShapeLeftButton()) } @@ -76,6 +89,7 @@ struct SelectPlayerButtonView: View { Button(action: { print("Button j2 pressé!") + showingPopup = true }) { VStack { Text("+") @@ -84,11 +98,13 @@ struct SelectPlayerButtonView: View { Text("Add a player") .font(.body) .bold() + .sheet(isPresented: $showingPopup) { + ShowAllPlayer(playersVM: playersVM) + } } .frame(width: 150, height: 150) .padding() - .foregroundColor(.black) - .border(/*@START_MENU_TOKEN@*/Color.black/*@END_MENU_TOKEN@*/, width: 4) - .foregroundColor(.white) + .border(Colors.TitleText, width: 4) + .foregroundColor(Colors.TitleText) .cornerRadius(10) .clipShape(CustomShapeRightButton()) } @@ -100,6 +116,7 @@ struct SelectPlayerButtonView: View { struct SelectPlayerButtonView_Previews: PreviewProvider { static var previews: some View { - SelectPlayerButtonView() + @StateObject var playersVM = PlayersVM() + SelectPlayerButtonView(playersVM: PlayersVM()) } } diff --git a/DouShouQi_App/DouShouQi_App/Components/Player/ShowAllPlayer.swift b/DouShouQi_App/DouShouQi_App/Components/Player/ShowAllPlayer.swift new file mode 100644 index 0000000..cf706fc --- /dev/null +++ b/DouShouQi_App/DouShouQi_App/Components/Player/ShowAllPlayer.swift @@ -0,0 +1,50 @@ +// +// ShowAllPlayer.swift +// DouShouQi_App +// +// Created by Nathan Verdier on 14/06/2024. +// + +import SwiftUI + +struct ShowAllPlayer: View { + @State private var isShowingSheet = false + @ObservedObject var playersVM: PlayersVM + + var body: some View { + VStack(alignment: .leading) { + Text("Liste des joueurs actuelle :") + List { + ForEach($playersVM.players, id: \.self) { playerVM in + Button(action: { + + }) { + HStack { + Image(systemName: "person.fill") + .resizable() + .frame(width: 30, height: 30) + Text(playerVM.player.name.wrappedValue) + } + } + } + } + Button(action: { + self.isShowingSheet = true + }) { + Text("Add a player") + .font(.headline) + .foregroundColor(.white) + .padding() + .frame(maxWidth: .infinity) + .background(Color.red) + .cornerRadius(10) + .padding(.horizontal) + } + .padding(.bottom) + .sheet(isPresented: $isShowingSheet) { + AddPlayerView(isPresented: $isShowingSheet, playersVM: playersVM) + } + } + } +} + diff --git a/DouShouQi_App/DouShouQi_App/Components/Scene/GameScene.swift b/DouShouQi_App/DouShouQi_App/Components/Scene/GameScene.swift index 474c9a5..6f955a2 100644 --- a/DouShouQi_App/DouShouQi_App/Components/Scene/GameScene.swift +++ b/DouShouQi_App/DouShouQi_App/Components/Scene/GameScene.swift @@ -10,7 +10,11 @@ import SpriteKit import DouShouQiModel class GameScene : SKScene { - var game: Game = try! Game(withRules: ClassicRules(), andPlayer1: RandomPlayer(withName: "Rayhan", andId: .player1)!, andPlayer2: RandomPlayer(withName: "Rémi", andId: .player2)!) + + var player1: HumanPlayer = HumanPlayer(withName: "Rémi", andId: .player1)! + var player2: HumanPlayer = HumanPlayer(withName: "Rayhan", andId: .player2)! + + var game: Game let pieces: [Owner : [Animal:SpriteMeeple]] = [ .player1: [ @@ -38,6 +42,7 @@ class GameScene : SKScene { let imageBoard: SKSpriteNode = SKSpriteNode(imageNamed: AppImages.boardGame) override init(size: CGSize) { + game = try! Game(withRules: ClassicRules(), andPlayer1: player1, andPlayer2: player2) super.init(size: size) imageBoard.size = size //self.scaleMode = .aspectFit @@ -48,13 +53,14 @@ class GameScene : SKScene { for piece in pieces.flatMap({owner, pieces in pieces.values}) { self.addChild(piece) + //piece.setOnMove(onMove: onMeepleMove) } initializeBoard(game.board) + } func initializeBoard(_ board: Board) { - for (lineIndex, currentLine) in game.board.grid.enumerated() { for (cellIndex, currentCell) in currentLine.enumerated() { if let piece = currentCell.piece { @@ -67,6 +73,25 @@ class GameScene : SKScene { } required init?(coder aDecoder: NSCoder) { + game = try! Game(withRules: ClassicRules(), andPlayer1: player1, andPlayer2: player2) super.init(coder: aDecoder); } + + func onMeepleMove(_ start: CGPoint, _ end: CGPoint) async { + + let owner = game.rules.getNextPlayer() + let player = game.players[owner] + + let move = Move(of: owner, fromRow: Int(start.x), andFromColumn: Int(start.y), toRow: Int(end.x), andToColumn: Int(end.y)) + + print("Meeple moved") + + try! await (player as! HumanPlayer).chooseMove(move) + + + } + + public func start() async throws { + try await game.start() + } } diff --git a/DouShouQi_App/DouShouQi_App/Components/Scene/SpriteMeeple.swift b/DouShouQi_App/DouShouQi_App/Components/Scene/SpriteMeeple.swift index f9e74c4..0a74775 100644 --- a/DouShouQi_App/DouShouQi_App/Components/Scene/SpriteMeeple.swift +++ b/DouShouQi_App/DouShouQi_App/Components/Scene/SpriteMeeple.swift @@ -19,6 +19,8 @@ class SpriteMeeple : SKNode { var originalSize: CGSize var originalEllipseSize: CGSize + var onMove: ((CGPoint, CGPoint) async -> ())? + var cellPosition: CGPoint{ didSet(cellPosition){ self.position.x = SpriteMeeple.offset.x + SpriteMeeple.direction.dx*cellPosition.x @@ -56,6 +58,8 @@ class SpriteMeeple : SKNode { originalEllipseSize = CGSize(width: 0, height: 0) super.init(coder: aDecoder) } + + public func setOnMove(onMove: @escaping (CGPoint, CGPoint) -> ()) { self.onMove = onMove } override var isUserInteractionEnabled: Bool{ set { } @@ -83,6 +87,10 @@ class SpriteMeeple : SKNode { imageNode.size = originalSize ellipseNode.path = SKShapeNode(ellipseOf: originalEllipseSize).path + + if let onMove: (CGPoint, CGPoint) async -> () = onMove { + //onMove(CGPoint(x: 100, y: 100), CGPoint(x: 200, y: 100)) + } self.zPosition = 0 } diff --git a/DouShouQi_App/DouShouQi_App/ViewModel/Players/PlayersVM.swift b/DouShouQi_App/DouShouQi_App/ViewModel/Players/PlayersVM.swift index 23ea32f..4f6a801 100644 --- a/DouShouQi_App/DouShouQi_App/ViewModel/Players/PlayersVM.swift +++ b/DouShouQi_App/DouShouQi_App/ViewModel/Players/PlayersVM.swift @@ -12,7 +12,6 @@ import Combine public class PlayersVM: ObservableObject{ - @Published var players: [PlayerVM] public func SavePlayer(player: PlayerVM){ diff --git a/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift b/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift index 159e899..2642ee8 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Menu/MainMenuView.swift @@ -37,8 +37,8 @@ struct MainMenuView: View { VStack { HStack { VStack(spacing: 25) { - MainMenuButton(text: "Play", destination: ScoreBoardView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) - MainMenuButton(text: "Historique", destination: HistoricView(historicVM: HistoricVM()), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) + MainMenuButton(text: "Play", destination: SelectPlayerView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) + //MainMenuButton(text: "Historique", destination: HistoricView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) MainMenuButton(text: "Best Scores", destination: ScoreBoardView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) MainMenuButton(text: "Players", destination: PlayersView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) MainMenuButton(text: "Settings", destination: SettingsView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10) diff --git a/DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift b/DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift index 26b6bc3..cc532b0 100644 --- a/DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift +++ b/DouShouQi_App/DouShouQi_App/Views/Player/SelectPlayerView.swift @@ -6,6 +6,7 @@ // import SwiftUI +import SpriteKit struct SelectPlayerView: View { var body: some View { @@ -14,14 +15,14 @@ struct SelectPlayerView: View { Spacer() - SelectPlayerButtonView() + //SelectPlayerButtonView() Spacer() HStack { VStack(alignment: .trailing) { - MainMenuButton(text: "Start", destination: ContentView(), sound: "TitleScreenButtonSound", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10) + MainMenuButton(text: "Start", destination: GameView(), sound: "TitleScreenButtonSound", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10) MainMenuButton(text: "Settings", destination: ContentView(), sound: "TitleScreenButtonSound", horizontalAlignment: .trailing, topLeftCorner: 10, bottomLeftCorner: 10) } }