parent
f935f39523
commit
2f98c1007a
@ -0,0 +1,36 @@
|
|||||||
|
//
|
||||||
|
// UIViewArKit.swift
|
||||||
|
// ArkitDoushiQi
|
||||||
|
//
|
||||||
|
// Created by Enzo JOLYS on 12/06/2024.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
import DouShouQiModel
|
||||||
|
|
||||||
|
struct ContentArkit: View {
|
||||||
|
|
||||||
|
var vm:VMArkit = VMArkit(try! Game(withRules: ClassicRules(), andPlayer1: RandomPlayer(withName: "Bot1", andId: .player1)!, andPlayer2: RandomPlayer(withName: "Bot2", andId: .player2)!))
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack {
|
||||||
|
Text("toto")
|
||||||
|
/*
|
||||||
|
vm.view
|
||||||
|
.ignoresSafeArea()
|
||||||
|
.navigationBarBackButtonHidden(true)
|
||||||
|
.task {
|
||||||
|
try! await vm.game.start()
|
||||||
|
}*/
|
||||||
|
ArKitViewRepresentable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct UIViewArKit_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
ContentArkit()
|
||||||
|
}
|
||||||
|
}
|
@ -1,73 +0,0 @@
|
|||||||
//
|
|
||||||
// UIViewArKit.swift
|
|
||||||
// ArkitDoushiQi
|
|
||||||
//
|
|
||||||
// Created by Enzo JOLYS on 12/06/2024.
|
|
||||||
//
|
|
||||||
|
|
||||||
import SwiftUI
|
|
||||||
import DouShouQiModel
|
|
||||||
|
|
||||||
struct UIViewArKit: View {
|
|
||||||
|
|
||||||
var game:Game = try! Game(withRules: ClassicRules(), andPlayer1: HumanPlayer(withName: "Bot1", andId: .player1)!, andPlayer2: RandomPlayer(withName: "Bot2", andId: .player2)!)
|
|
||||||
|
|
||||||
|
|
||||||
@State var msg:String = "Msg"
|
|
||||||
|
|
||||||
var body: some View {
|
|
||||||
VStack {
|
|
||||||
Text(msg)
|
|
||||||
ArKitViewRepresentable(game)
|
|
||||||
.ignoresSafeArea()
|
|
||||||
.navigationBarBackButtonHidden(true)
|
|
||||||
.task {
|
|
||||||
game.addGameStartedListener { board in startGame()}
|
|
||||||
game.addGameOverListener { board, result, player in gameOver() }
|
|
||||||
game.addGameChangedListener { game in gameChange() }
|
|
||||||
|
|
||||||
game.addMoveChosenCallbacksListener { board, move, player in moveChose(board: board, move: move, player: player) }
|
|
||||||
|
|
||||||
game.addPieceRemovedListener { _,_,piece in removePiece(piece: piece) }
|
|
||||||
|
|
||||||
game.addPlayerNotifiedListener { board, player in
|
|
||||||
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: game.rules)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//try! await game.start()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------ Listener -------- //
|
|
||||||
|
|
||||||
func startGame() { print("Start game !")
|
|
||||||
msg = "Start !!"
|
|
||||||
}
|
|
||||||
func gameOver() { print("Game over !")
|
|
||||||
msg = "Game over !!"
|
|
||||||
}
|
|
||||||
func gameChange() { print("Game change !") }
|
|
||||||
|
|
||||||
func moveChose(board:Board,move:Move,player:Player) {
|
|
||||||
}
|
|
||||||
|
|
||||||
func removePiece(piece:Piece){
|
|
||||||
msg = "Piece manger !!"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ------------------------- //
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct UIViewArKit_Previews: PreviewProvider {
|
|
||||||
static var previews: some View {
|
|
||||||
UIViewArKit()
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,185 @@
|
|||||||
|
//
|
||||||
|
// VMArkit.swift
|
||||||
|
// ArkitDoushiQi
|
||||||
|
//
|
||||||
|
// Created by Enzo JOLYS on 14/06/2024.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import DouShouQiModel
|
||||||
|
import ARKit
|
||||||
|
import RealityKit
|
||||||
|
import UIKit
|
||||||
|
|
||||||
|
struct VMArkit {
|
||||||
|
|
||||||
|
static let offset = CGPoint(x: -400, y: -300 )
|
||||||
|
static let direction = CGVector(dx: 100, dy: 100)
|
||||||
|
|
||||||
|
var msg:String = ""
|
||||||
|
|
||||||
|
var game:Game
|
||||||
|
|
||||||
|
var view:ArKitViewRepresentable = ArKitViewRepresentable()
|
||||||
|
|
||||||
|
var pieces: [Owner : [ Animal : Entity?]] = [.player1: [.cat:nil,
|
||||||
|
.elephant:nil,
|
||||||
|
.dog:nil,
|
||||||
|
.leopard:nil,
|
||||||
|
.lion:nil,
|
||||||
|
.rat:nil,
|
||||||
|
.tiger:nil,
|
||||||
|
.wolf:nil],
|
||||||
|
.player2: [.cat:nil,
|
||||||
|
.elephant:nil,
|
||||||
|
.dog:nil,
|
||||||
|
.leopard:nil,
|
||||||
|
.lion:nil,
|
||||||
|
.rat:nil,
|
||||||
|
.tiger:nil,
|
||||||
|
.wolf:nil]]
|
||||||
|
|
||||||
|
init(_ game:Game){
|
||||||
|
self.game = game
|
||||||
|
|
||||||
|
defineListeners()
|
||||||
|
}
|
||||||
|
|
||||||
|
func addBoard(anchor:AnchorEntity){
|
||||||
|
let board = try? Entity.load(named: "board")
|
||||||
|
if let board {
|
||||||
|
anchor.addChild(board)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- Crée les pieces
|
||||||
|
func generatePieces(anchor:AnchorEntity) {
|
||||||
|
// Position - X,Y,Z
|
||||||
|
for c in pieces.flatMap({ animal,values in return values })
|
||||||
|
{
|
||||||
|
var entity:Entity?
|
||||||
|
switch c.self.key {
|
||||||
|
case .cat :
|
||||||
|
entity = try? Entity.load(named: "CatPiece")
|
||||||
|
case .elephant :
|
||||||
|
entity = try? Entity.load(named: "CatPiece")
|
||||||
|
case .dog :
|
||||||
|
entity = try? Entity.load(named: "CatPiece")
|
||||||
|
case .leopard :
|
||||||
|
entity = try? Entity.load(named: "CatPiece")
|
||||||
|
case .lion :
|
||||||
|
entity = try? Entity.load(named: "CatPiece")
|
||||||
|
case .rat :
|
||||||
|
entity = try? Entity.load(named: "CatPiece")
|
||||||
|
case .tiger :
|
||||||
|
entity = try? Entity.load(named: "CatPiece")
|
||||||
|
case .wolf :
|
||||||
|
entity = try? Entity.load(named: "CatPiece")
|
||||||
|
|
||||||
|
default:
|
||||||
|
fatalError("Animal non compris")
|
||||||
|
}
|
||||||
|
|
||||||
|
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 !")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -- Affiche les pieces à la bonne positions
|
||||||
|
func displayBoard(board:Board) {
|
||||||
|
for ligne in 0..<board.grid.count {
|
||||||
|
for col in 0..<board.grid[ligne].count {
|
||||||
|
if let piece = board.grid[ligne][col].piece {
|
||||||
|
if let element = pieces[piece.owner]![piece.animal]{
|
||||||
|
if (element != nil ){
|
||||||
|
element!.position = convertPosModeleIntoWorldPos(pos:CGPoint(x: ligne, y: col))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//print("WARNING !!! -> Element vide ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertPosModeleIntoWorldPos(pos:CGPoint) -> SIMD3<Float> {
|
||||||
|
return SIMD3<Float>(Float(VMArkit.offset.x + VMArkit.direction.dx * pos.x),Float(VMArkit.offset.y + VMArkit.direction.dy * pos.y),10)
|
||||||
|
}
|
||||||
|
func converWorldPosIntoPosModele(pos:SIMD3<Float>) -> CGPoint {
|
||||||
|
let posX = Int(round((CGFloat(pos.x) - (VMArkit.offset.x)) / VMArkit.direction.dx))
|
||||||
|
let posY = Int(round((CGFloat(pos.y) - (VMArkit.offset.y)) / VMArkit.direction.dy))
|
||||||
|
return CGPoint(x: posX, y: posY)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------ Listener -------- //
|
||||||
|
func defineListeners(){
|
||||||
|
self.game.addGameStartedListener { board in startGame()}
|
||||||
|
self.game.addGameOverListener { board, result, player in gameOver()}
|
||||||
|
self.game.addGameChangedListener { game in gameChange() }
|
||||||
|
self.game.addBoardChangedListener { board in boardChange() }
|
||||||
|
self.game.addMoveChosenCallbacksListener { board, move, player in moveChose(board: board, move: move, player: player) }
|
||||||
|
self.game.addInvalidMoveCallbacksListener { board,move,player,bool in invalidMove(board: board, move: move, player:player, bool:bool)}
|
||||||
|
self.game.addPieceRemovedListener { _,_,piece in removePiece(piece: piece) }
|
||||||
|
|
||||||
|
self.game.addPlayerNotifiedListener { board, player in
|
||||||
|
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: game.rules)
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startGame() { print("Start game !")
|
||||||
|
//self.msg = "Start !!"
|
||||||
|
}
|
||||||
|
func gameOver() { print("Game over !")
|
||||||
|
//msg = "Game over !!"
|
||||||
|
}
|
||||||
|
func gameChange() { print("Game change !") }
|
||||||
|
func boardChange() {
|
||||||
|
print("Board change !")
|
||||||
|
self.displayBoard(board: self.game.board)
|
||||||
|
}
|
||||||
|
|
||||||
|
func moveChose(board:Board,move:Move,player:Player) {
|
||||||
|
}
|
||||||
|
|
||||||
|
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)")
|
||||||
|
self.displayBoard(board: self.game.board)
|
||||||
|
}
|
||||||
|
print("------------")
|
||||||
|
}
|
||||||
|
|
||||||
|
func removePiece(piece:Piece){
|
||||||
|
print("Remove piece")
|
||||||
|
if let entity = pieces[piece.owner]![piece.animal] {
|
||||||
|
if let x = entity {
|
||||||
|
x.removeFromParent()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------- //
|
||||||
|
}
|
@ -1,5 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
<plist version="1.0">
|
<plist version="1.0">
|
||||||
<dict/>
|
<dict>
|
||||||
|
<key>NSCameraPortraitEffectEnabled</key>
|
||||||
|
<true/>
|
||||||
|
</dict>
|
||||||
</plist>
|
</plist>
|
||||||
|
Loading…
Reference in new issue