|
|
@ -6,43 +6,43 @@
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
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: [
|
|
|
|
.rat: SpriteMeeple(imageNamed: AppImages.Rat, 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!),
|
|
|
|
.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!),
|
|
|
|
.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!),
|
|
|
|
.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!),
|
|
|
|
.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!),
|
|
|
|
.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!),
|
|
|
|
.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!),
|
|
|
|
.elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!, imageRotation: 180),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
.player2: [
|
|
|
|
.player2: [
|
|
|
|
.rat: SpriteMeeple(imageNamed: AppImages.Rat, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
|
|
|
|
.rat: SpriteMeeple(imageNamed: AppImages.Rat, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP2!),
|
|
|
|
.cat: SpriteMeeple(imageNamed: AppImages.Cat, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
|
|
|
|
.cat: SpriteMeeple(imageNamed: AppImages.Cat, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP2!),
|
|
|
|
.dog: SpriteMeeple(imageNamed: AppImages.Dog, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
|
|
|
|
.dog: SpriteMeeple(imageNamed: AppImages.Dog, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP2!),
|
|
|
|
.wolf: SpriteMeeple(imageNamed: AppImages.Wolf, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
|
|
|
|
.wolf: SpriteMeeple(imageNamed: AppImages.Wolf, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP2!),
|
|
|
|
.leopard: SpriteMeeple(imageNamed: AppImages.Leopard, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
|
|
|
|
.leopard: SpriteMeeple(imageNamed: AppImages.Leopard, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP2!),
|
|
|
|
.lion: SpriteMeeple(imageNamed: AppImages.Lion, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
|
|
|
|
.lion: SpriteMeeple(imageNamed: AppImages.Lion, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP2!),
|
|
|
|
.tiger: SpriteMeeple(imageNamed: AppImages.Tigger, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
|
|
|
|
.tiger: SpriteMeeple(imageNamed: AppImages.Tigger, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP2!),
|
|
|
|
.elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
|
|
|
|
.elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP2!),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
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,48 +50,41 @@ 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: gameVM.onMeepleMove)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
initializeBoard(game.board)
|
|
|
|
initializeBoard(game!.board)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gameVM.game.addPieceRemovedListener { _, _, piece in
|
|
|
|
|
|
|
|
self.pieces[piece.owner]![piece.animal]?.removeFromParent()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gameVM.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)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) 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()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|