pull/24/head^2
Your Name 11 months ago
parent 48393b1772
commit 1a2752b4d0

@ -12,7 +12,7 @@ import DouShouQiModel
class GameScene : SKScene { class GameScene : SKScene {
let imageBoard:SKSpriteNode = SKSpriteNode(imageNamed: "board") 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), var pieces: [Owner : [ Animal : SpriteMoople]] = [ .player1 : [ .cat : SpriteMoople(nameImage: "catMeeple", couleur: .red),
.dog : SpriteMoople(nameImage: "dogMeeple", couleur: .red), .dog : SpriteMoople(nameImage: "dogMeeple", couleur: .red),
@ -44,12 +44,14 @@ class GameScene : SKScene {
// -- -- // // -- -- //
scaleMode = .aspectFit scaleMode = .aspectFit
anchorPoint = CGPoint(x: 0.5, y: 0.5) anchorPoint = CGPoint(x: 0.5, y: 0.5)
imageBoard.zPosition = 0
self.addChild(imageBoard) self.addChild(imageBoard)
// -- -- // // -- -- //
for c in pieces.flatMap({ _,values in return values }) for c in pieces.flatMap({ _,values in return values })
{ {
c.self.value.refGameScene = self c.self.value.refGameScene = self
c.self.value.zPosition = 2
self.addChild(c.self.value) self.addChild(c.self.value)
} }

@ -13,13 +13,13 @@ struct GameView: View {
var gameScene:GameScene = GameScene(size: CGSize(width: 940, height: 740)) 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 { var body: some View {
VStack(){ VStack(){
Text($msg) Text(msg)
SpriteView(scene: game).task { SpriteView(scene: gameScene).task {
gameScene.game.addGameStartedListener { board in startGame()} gameScene.game.addGameStartedListener { board in startGame()}
gameScene.game.addGameOverListener { board, result, player in gameOver() } gameScene.game.addGameOverListener { board, result, player in gameOver() }
@ -29,8 +29,15 @@ struct GameView: View {
gameScene.game.addMoveChosenCallbacksListener { board, move, player in moveChose(board: board, move: move, player: player) } 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.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 gameScene.game.addPlayerNotifiedListener { board, player in
print("Player notif : \(player.id) à toi de jouer ") 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() try! await gameScene.game.start()
@ -40,10 +47,12 @@ struct GameView: View {
// ------ Listener -------- // // ------ Listener -------- //
mutating func startGame() { print("Start game !") func startGame() { print("Start game !")
msg = "Start !!" msg = "Start !!"
} }
func gameOver() { print("Game over !") } func gameOver() { print("Game over !")
msg = "Game over !!"
}
func gameChange() { print("Game change !") } func gameChange() { print("Game change !") }
func boardChange() { func boardChange() {
print("Board change !") print("Board change !")
@ -51,19 +60,31 @@ struct GameView: View {
} }
func moveChose(board:Board,move:Move,player:Player) { 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) { func invalidMove(board:Board,move:Move,player:Player,bool:Bool) {
print("Move invalid ! (\(bool))") // True == Valid / False == Invalid
print("Player : \(player.id)") if (bool){ // Valid
print("Move : \(move.description)") 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("------------") print("------------")
} }
func removePiece(piece:Piece){
print("Remove piece")
if let node = gameScene.pieces[piece.owner]![piece.animal] {
print("Remove piece from parent")
node.removeFromParent()
}
}
// ------------------------- // // ------------------------- //

@ -43,11 +43,14 @@ 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())
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 { for pos in movePoss {
var rect = SKShapeNode(rectOf: CGSize(width: 90, height: 90)) let rect = SKShapeNode(rectOf: CGSize(width: 90, height: 90))
rect.position = returnPositionByCellPos(cellX:move.destinationRow,cellY:move.destinationColm) rect.position = returnPositionByCellPos(cellX:pos.rowDestination,cellY:pos.columnDestination)
refGameScene!.deplacementPossible.add(rect) rect.fillColor = .red
rect.zPosition = 1
refGameScene!.deplacementPossible.append(rect)
refGameScene!.addChild(rect) refGameScene!.addChild(rect)
} }
} }
@ -60,34 +63,32 @@ class SpriteMoople : SKNode {
let localisation = touches.first?.location(in: parent!) let localisation = touches.first?.location(in: parent!)
// Clean rect // Clean rect
for rect in refGameScene!.deplacementPossible { refGameScene!.removeChildren(in: refGameScene!.deplacementPossible)
rect.removeChild()
}
if let localisation = localisation { if let localisation = localisation {
print(localisation)
if (localisation.x > 400 || localisation.x < -400 || localisation.y < -300 || localisation.y > 300){ if (localisation.x > 450 || localisation.x < -450 || localisation.y < -350 || localisation.y > 350){
print("Hors limite !") print("Hors limite !")
self.position = returnPositionByCellPos(cellX:Int(self.cellPosition.x),cellY:Int(self.cellPosition.y))
return return
} }
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))
// Récupéré le joueur qui doit jouer // Récupéré le joueur qui doit jouer
let player = refGameScene!.game.rules.getNextPlayer() let owner = refGameScene!.game.rules.getNextPlayer()
if ( player == refGameScene!.game.board.grid[self.cellPosition.x][self.cellPosition.y].piece!.owner ){ 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 if player is HumanPlayer { // Player humain
try! (refGameScene!.game.players[player] as! HumanPlayer).chooseMove(x) Task {
} try! await (player as! HumanPlayer).chooseMove(Move(of: owner, fromRow:Int(self.cellPosition.x), andFromColumn: Int(self.cellPosition.y), toRow: posX, andToColumn: posY))
else { // IA }
try! await refGameScene!.game.players[player].chooseMove(in: refGameScene!.game.board, with: refGameScene!.game.rules)
} }
// Change position de la piece
self.cellPosition = CGPoint(x: posX, y: posY)
} }
else { else {
// Ce n'est pas la bonne personne, reset position // 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 return
} }
@ -99,6 +100,6 @@ class SpriteMoople : SKNode {
} }
func returnPositionByCellPos(cellX:Int,cellY:Int) -> CGPoint { 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))
} }
} }

Loading…
Cancel
Save