|
|
@ -6,15 +6,16 @@
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import SpriteKit
|
|
|
|
import SpriteKit
|
|
|
|
import DouShouQiModel
|
|
|
|
import DouShouQiModel
|
|
|
|
|
|
|
|
|
|
|
|
class GameScene : SKScene {
|
|
|
|
class GameScene : SKScene {
|
|
|
|
|
|
|
|
|
|
|
|
var player1: HumanPlayer = HumanPlayer(withName: "Rémi", andId: .player1)!
|
|
|
|
var player1: DouShouQiModel.Player?
|
|
|
|
var player2: HumanPlayer = HumanPlayer(withName: "Rayhan", andId: .player2)!
|
|
|
|
var player2: DouShouQiModel.Player?
|
|
|
|
|
|
|
|
|
|
|
|
var game: Game
|
|
|
|
var game: Game?
|
|
|
|
|
|
|
|
|
|
|
|
let pieces: [Owner : [Animal:SpriteMeeple]] = [
|
|
|
|
let pieces: [Owner : [Animal:SpriteMeeple]] = [
|
|
|
|
.player1: [
|
|
|
|
.player1: [
|
|
|
@ -42,7 +43,6 @@ class GameScene : SKScene {
|
|
|
|
let imageBoard: SKSpriteNode = SKSpriteNode(imageNamed: AppImages.boardGame)
|
|
|
|
let imageBoard: SKSpriteNode = SKSpriteNode(imageNamed: AppImages.boardGame)
|
|
|
|
|
|
|
|
|
|
|
|
override init(size: CGSize) {
|
|
|
|
override init(size: CGSize) {
|
|
|
|
game = try! Game(withRules: ClassicRules(), andPlayer1: player1, andPlayer2: player2)
|
|
|
|
|
|
|
|
super.init(size: size)
|
|
|
|
super.init(size: size)
|
|
|
|
imageBoard.size = size
|
|
|
|
imageBoard.size = size
|
|
|
|
//self.scaleMode = .aspectFit
|
|
|
|
//self.scaleMode = .aspectFit
|
|
|
@ -50,15 +50,22 @@ class GameScene : SKScene {
|
|
|
|
self.backgroundColor = .white
|
|
|
|
self.backgroundColor = .white
|
|
|
|
|
|
|
|
|
|
|
|
self.addChild(imageBoard)
|
|
|
|
self.addChild(imageBoard)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
convenience init(size: CGSize, gameVM: PlayingGameVM) {
|
|
|
|
|
|
|
|
self.init(size: size)
|
|
|
|
|
|
|
|
self.game = gameVM.game
|
|
|
|
|
|
|
|
self.player1 = self.game?.players[.player1]
|
|
|
|
|
|
|
|
self.player2 = self.game?.players[.player2]
|
|
|
|
|
|
|
|
|
|
|
|
for piece in pieces.flatMap({owner, pieces in pieces.values}) {
|
|
|
|
for piece in pieces.flatMap({owner, pieces in pieces.values}) {
|
|
|
|
self.addChild(piece)
|
|
|
|
self.addChild(piece)
|
|
|
|
piece.setOnMove(onMove: onMeepleMove)
|
|
|
|
piece.setOnMove(onMove: onMeepleMove)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
initializeBoard(game.board)
|
|
|
|
initializeBoard(game!.board)
|
|
|
|
|
|
|
|
|
|
|
|
game.addInvalidMoveCallbacksListener { _, move, player, result in
|
|
|
|
game?.addInvalidMoveCallbacksListener { _, move, player, result in
|
|
|
|
if result {
|
|
|
|
if result {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -69,33 +76,34 @@ class GameScene : SKScene {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Task {
|
|
|
|
Task {
|
|
|
|
try await game.start()
|
|
|
|
try await game?.start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func initializeBoard(_ board: Board) {
|
|
|
|
func initializeBoard(_ board: Board) {
|
|
|
|
for (lineIndex, currentLine) in game.board.grid.enumerated() {
|
|
|
|
for (lineIndex, currentLine) in game!.board.grid.enumerated() {
|
|
|
|
for (cellIndex, currentCell) in currentLine.enumerated() {
|
|
|
|
for (cellIndex, currentCell) in currentLine.enumerated() {
|
|
|
|
if let piece = currentCell.piece {
|
|
|
|
if let piece = currentCell.piece {
|
|
|
|
pieces[piece.owner]?[piece.animal]?.cellPosition = CGPoint(x: cellIndex-3, y: lineIndex-4)
|
|
|
|
pieces[piece.owner]?[piece.animal]?.cellPosition = CGPoint(x: cellIndex, y: lineIndex)
|
|
|
|
pieces[piece.owner]?[piece.animal]?.cellPosition = CGPoint(x: 0, y: 0)
|
|
|
|
pieces[piece.owner]?[piece.animal]?.cellPosition = CGPoint(x: 0, y: 0)
|
|
|
|
print("line :", lineIndex, " cologne: ", cellIndex)
|
|
|
|
print("line :", lineIndex, " column: ", cellIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
game = try! Game(withRules: ClassicRules(), andPlayer1: player1, andPlayer2: player2)
|
|
|
|
game = try! Game(withRules: ClassicRules(), andPlayer1: DouShouQiModel.Player(withName: "P1", andId: .player1)!, andPlayer2: DouShouQiModel.Player(withName: "P2", andId: .player2)!)
|
|
|
|
super.init(coder: aDecoder);
|
|
|
|
super.init(coder: aDecoder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func onMeepleMove(_ start: CGPoint, _ end: CGPoint) {
|
|
|
|
func onMeepleMove(_ start: CGPoint, _ end: CGPoint) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let game: Game = self.game {
|
|
|
|
let owner = game.rules.getNextPlayer()
|
|
|
|
let owner = game.rules.getNextPlayer()
|
|
|
|
let player = game.players[owner]
|
|
|
|
let player: DouShouQiModel.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))
|
|
|
|
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)
|
|
|
|
print("Meeple moved = ", start, " -> ", end)
|
|
|
|
|
|
|
|
|
|
|
@ -103,8 +111,9 @@ class GameScene : SKScene {
|
|
|
|
try! await (player as! HumanPlayer).chooseMove(move)
|
|
|
|
try! await (player as! HumanPlayer).chooseMove(move)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public func start() async throws {
|
|
|
|
public func start() async throws {
|
|
|
|
try await game.start()
|
|
|
|
try await game?.start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|