finish ? j'espère

pull/24/head^2
Enzo 11 months ago
parent 011f741e73
commit 48393b1772

@ -32,6 +32,8 @@ class GameScene : SKScene {
.elephant : SpriteMoople(nameImage: "elephantMeeple", couleur: .blue), .elephant : SpriteMoople(nameImage: "elephantMeeple", couleur: .blue),
.tiger : SpriteMoople(nameImage: "tigerMeeple", couleur: .blue)]] .tiger : SpriteMoople(nameImage: "tigerMeeple", couleur: .blue)]]
var deplacementPossible: [SKShapeNode] = []
required init?(coder aDecoder: NSCoder) { required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder) super.init(coder: aDecoder)
} }

@ -11,50 +11,43 @@ import DouShouQiModel
struct GameView: View { 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 { var body: some View {
//Text("Msg à modifié !")
SpriteView(scene: game).task {
game.game.addGameStartedListener { board in startGame()} VStack(){
game.game.addGameOverListener { board, result, player in gameOver() } Text($msg)
game.game.addGameChangedListener { game in gameChange() } SpriteView(scene: game).task {
game.game.addBoardChangedListener { board in boardChange() }
game.game.addMoveChosenCallbacksListener { board, move, player in moveChose(board: board, move: move, player: player) } gameScene.game.addGameStartedListener { board in startGame()}
game.game.addInvalidMoveCallbacksListener { board,move,player,bool in invalidMove(board: board, move: move, player:player, bool:bool)} gameScene.game.addGameOverListener { board, result, player in gameOver() }
gameScene.game.addGameChangedListener { game in gameChange() }
gameScene.game.addBoardChangedListener { board in boardChange() }
game.game.addPlayerNotifiedListener { board, player in gameScene.game.addMoveChosenCallbacksListener { board, move, player in moveChose(board: board, move: move, player: player) }
print("Player notif : \(player.id) à toi de jouer ") gameScene.game.addInvalidMoveCallbacksListener { board,move,player,bool in invalidMove(board: board, move: move, player:player, bool:bool)}
/* gameScene.game.addPlayerNotifiedListener { board, player in
if player is HumanPlayer { print("Player notif : \(player.id) à toi de jouer ")
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 -------- // // ------ Listener -------- //
func startGame() { print("Start game !") } mutating func startGame() { print("Start game !")
msg = "Start !!"
}
func gameOver() { print("Game over !") } func gameOver() { print("Game over !") }
func gameChange() { print("Game change !") } func gameChange() { print("Game change !") }
func boardChange() { func boardChange() {
print("Board change !") print("Board change !")
game.displayBoard(board: game.game.board) gameScene.displayBoard(board: gameScene.game.board)
} }
func moveChose(board:Board,move:Move,player:Player) { 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) { 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("Player : \(player.id)")
print("Move : \(move.description)") print("Move : \(move.description)")
print("------------") print("------------")

@ -43,6 +43,13 @@ class SpriteMoople : SKNode {
} }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesBegan(_ touches: Set<UITouch>, 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<UITouch>, with event: UIEvent?) { override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
@ -52,6 +59,11 @@ class SpriteMoople : SKNode {
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let localisation = touches.first?.location(in: parent!) let localisation = touches.first?.location(in: parent!)
// Clean rect
for rect in refGameScene!.deplacementPossible {
rect.removeChild()
}
if let localisation = localisation { if let localisation = localisation {
if (localisation.x > 400 || localisation.x < -400 || localisation.y < -300 || localisation.y > 300){ if (localisation.x > 400 || localisation.x < -400 || localisation.y < -300 || localisation.y > 300){
@ -61,15 +73,22 @@ class SpriteMoople : SKNode {
let posX = Int(round((localisation.x - (-400)) / 100)) let posX = Int(round((localisation.x - (-400)) / 100))
let posY = Int(round((localisation.y - (-300)) / 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) // Récupéré le joueur qui doit jouer
//print("Change lastMove -> \(GameScene.lastMove)"); let player = refGameScene!.game.rules.getNextPlayer()
self.cellPosition = CGPoint(x: posX, y: posY) if ( player == refGameScene!.game.board.grid[self.cellPosition.x][self.cellPosition.y].piece!.owner ){
if player is HumanPlayer { // Player humain
// Get player try! (refGameScene!.game.players[player] as! HumanPlayer).chooseMove(x)
let x = refGameScene?.game.rules.getNextPlayer() }
//try! refGameScene?.game.players. else { // IA
try! await refGameScene!.game.players[player].chooseMove(in: refGameScene!.game.board, with: refGameScene!.game.rules)
// Faire un retour en arrière si lastMove != nil }
// 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 return
} }
} }
@ -79,6 +98,7 @@ class SpriteMoople : SKNode {
fatalError("init(coder:) has not been implemented") 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)
}
} }

Loading…
Cancel
Save