|
|
|
@ -16,12 +16,13 @@ class ArKitView : ARView {
|
|
|
|
|
static let offset = CGPoint(x: -400, y: -300 )
|
|
|
|
|
static let direction = CGVector(dx: 100, dy: 100)
|
|
|
|
|
|
|
|
|
|
var game:Game = try! Game(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: "Bot1", andId: .player1)!, andPlayer2: RandomPlayer(withName: "Bot2", andId: .player2)!)
|
|
|
|
|
var game:Game
|
|
|
|
|
|
|
|
|
|
var pieces: [Owner : [ Animal : Entity?]]
|
|
|
|
|
|
|
|
|
|
required init(frame frameRect: CGRect) {
|
|
|
|
|
pieces = [.noOne:[.cat:nil]]
|
|
|
|
|
game = try! Game(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: "Bot1", andId: .player1)!, andPlayer2: RandomPlayer(withName: "Bot2", andId: .player2)!)
|
|
|
|
|
super.init(frame: frameRect)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -29,12 +30,47 @@ class ArKitView : ARView {
|
|
|
|
|
fatalError("init(coder:) not implemented")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
|
convenience init(_ game:Game) {
|
|
|
|
|
self.init(frame: UIScreen.main.bounds)
|
|
|
|
|
self.game = game
|
|
|
|
|
let anchor = defineAnchors()
|
|
|
|
|
addBoard(anchor: anchor)
|
|
|
|
|
generatePieces(anchor:anchor)
|
|
|
|
|
|
|
|
|
|
self.game.addBoardChangedListener { board in self.boardChange() }
|
|
|
|
|
self.game.addPieceRemovedListener { _,_,piece in self.removePiece(piece: piece) }
|
|
|
|
|
self.game.addInvalidMoveCallbacksListener { board,move,player,bool in self.invalidMove(board: board, move: move, player:player, bool:bool)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ------ Listener -------- //
|
|
|
|
|
func boardChange() {
|
|
|
|
|
print("Board change !")
|
|
|
|
|
displayBoard(board: game.board)
|
|
|
|
|
}
|
|
|
|
|
func removePiece(piece:Piece){
|
|
|
|
|
print("Remove piece")
|
|
|
|
|
|
|
|
|
|
if let entity = pieces[piece.owner]![piece.animal] {
|
|
|
|
|
if let x = entity {
|
|
|
|
|
x.removeFromParent()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func invalidMove(board:Board,move:Move,player:Player,bool:Bool) {
|
|
|
|
|
|
|
|
|
|
if (bool){ // Valid
|
|
|
|
|
print("Move valid de \(player.id)")
|
|
|
|
|
print("Move : \(move.description)")
|
|
|
|
|
}
|
|
|
|
|
else { // Invalid
|
|
|
|
|
print("Move invalid de \(player.id)")
|
|
|
|
|
print("Move : \(move.description)")
|
|
|
|
|
displayBoard(board: game.board)
|
|
|
|
|
}
|
|
|
|
|
print("------------")
|
|
|
|
|
}
|
|
|
|
|
// ------------------------ //
|
|
|
|
|
|
|
|
|
|
func applyConfiguration() {
|
|
|
|
|
let configuration = ARWorldTrackingConfiguration()
|
|
|
|
@ -42,14 +78,13 @@ class ArKitView : ARView {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func defineAnchors() -> AnchorEntity {
|
|
|
|
|
//let anchor = AnchorEntity(world: .zero)
|
|
|
|
|
let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2)))
|
|
|
|
|
scene.addAnchor(anchor)
|
|
|
|
|
return anchor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addBoard(anchor:AnchorEntity){
|
|
|
|
|
let board = try? Entity.load(named: "test")
|
|
|
|
|
let board = try? Entity.load(named: "board")
|
|
|
|
|
if let board {
|
|
|
|
|
anchor.addChild(board)
|
|
|
|
|
}
|
|
|
|
@ -63,21 +98,21 @@ class ArKitView : ARView {
|
|
|
|
|
var entity:Entity?
|
|
|
|
|
switch c.self.key {
|
|
|
|
|
case .cat :
|
|
|
|
|
entity = try? Entity.load(named: "ArKitCat")
|
|
|
|
|
entity = try? Entity.load(named: "CatPiece")
|
|
|
|
|
case .elephant :
|
|
|
|
|
entity = try? Entity.load(named: "ArKitElephant")
|
|
|
|
|
entity = try? Entity.load(named: "CatPiece")
|
|
|
|
|
case .dog :
|
|
|
|
|
entity = try? Entity.load(named: "ArKitDog")
|
|
|
|
|
entity = try? Entity.load(named: "CatPiece")
|
|
|
|
|
case .leopard :
|
|
|
|
|
entity = try? Entity.load(named: "ArKitLeopard")
|
|
|
|
|
entity = try? Entity.load(named: "CatPiece")
|
|
|
|
|
case .lion :
|
|
|
|
|
entity = try? Entity.load(named: "ArKitLion")
|
|
|
|
|
entity = try? Entity.load(named: "CatPiece")
|
|
|
|
|
case .rat :
|
|
|
|
|
entity = try? Entity.load(named: "ArKitRat")
|
|
|
|
|
entity = try? Entity.load(named: "CatPiece")
|
|
|
|
|
case .tiger :
|
|
|
|
|
entity = try? Entity.load(named: "ArKitTiger")
|
|
|
|
|
entity = try? Entity.load(named: "CatPiece")
|
|
|
|
|
case .wolf :
|
|
|
|
|
entity = try? Entity.load(named: "ArKitWolf")
|
|
|
|
|
entity = try? Entity.load(named: "CatPiece")
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
fatalError("Animal non compris")
|
|
|
|
@ -86,6 +121,12 @@ class ArKitView : ARView {
|
|
|
|
|
if let entityNotNull = entity {
|
|
|
|
|
anchor.addChild(entityNotNull)
|
|
|
|
|
entityNotNull.position = SIMD3<Float>(0,0,10)
|
|
|
|
|
entityNotNull.generateCollisionShapes(recursive:true)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
self.installGestures([.all], for: entityNotNull as! Entity & HasCollision).forEach { gestureRecognizer in
|
|
|
|
|
gestureRecognizer.addTarget(self, action: #selector(handleGesture(_:)))}*/
|
|
|
|
|
print("Piece \(c.self.key) a été crée !")
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
print("La pièce \(c.self.key) n'a pas été crée !")
|
|
|
|
@ -132,8 +173,6 @@ class ArKitView : ARView {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//self.installGestures([.all], for: robot as Entity & HasCollision).forEach { gestureRecognizer in
|
|
|
|
|
// gestureRecognizer.addTarget(self, action: #selector(handleGesture(_:)))
|
|
|
|
|
// -- //
|
|
|
|
|
//-------------------------//
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|