parent
5b73c5584f
commit
cefc96e854
@ -0,0 +1,139 @@
|
||||
//
|
||||
// ArKitView.swift
|
||||
// ArkitDoushiQi
|
||||
//
|
||||
// Created by Enzo JOLYS on 12/06/2024.
|
||||
//
|
||||
|
||||
import DouShouQiModel
|
||||
import Foundation
|
||||
import ARKit
|
||||
import RealityKit
|
||||
import UIKit
|
||||
|
||||
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 pieces: [Owner : [ Animal : Entity?]]
|
||||
|
||||
required init(frame frameRect: CGRect) {
|
||||
pieces = [.noOne:[.cat:nil]]
|
||||
super.init(frame: frameRect)
|
||||
}
|
||||
|
||||
required init?(coder decoder: NSCoder) {
|
||||
fatalError("init(coder:) not implemented")
|
||||
}
|
||||
|
||||
convenience init() {
|
||||
self.init(frame: UIScreen.main.bounds)
|
||||
let anchor = defineAnchors()
|
||||
addBoard(anchor: anchor)
|
||||
generatePieces(anchor:anchor)
|
||||
}
|
||||
|
||||
func applyConfiguration() {
|
||||
let configuration = ARWorldTrackingConfiguration()
|
||||
session.run(configuration)
|
||||
}
|
||||
|
||||
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")
|
||||
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: "ArKitCat")
|
||||
case .elephant :
|
||||
entity = try? Entity.load(named: "ArKitElephant")
|
||||
case .dog :
|
||||
entity = try? Entity.load(named: "ArKitDog")
|
||||
case .leopard :
|
||||
entity = try? Entity.load(named: "ArKitLeopard")
|
||||
case .lion :
|
||||
entity = try? Entity.load(named: "ArKitLion")
|
||||
case .rat :
|
||||
entity = try? Entity.load(named: "ArKitRat")
|
||||
case .tiger :
|
||||
entity = try? Entity.load(named: "ArKitTiger")
|
||||
case .wolf :
|
||||
entity = try? Entity.load(named: "ArKitWolf")
|
||||
|
||||
default:
|
||||
fatalError("Animal non compris")
|
||||
}
|
||||
|
||||
if let entityNotNull = entity {
|
||||
anchor.addChild(entityNotNull)
|
||||
entityNotNull.position = SIMD3<Float>(0,0,10)
|
||||
}
|
||||
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]{
|
||||
element!.position = convertPosModeleIntoWorldPos(pos:CGPoint(x: ligne, y: col))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func convertPosModeleIntoWorldPos(pos:CGPoint) -> SIMD3<Float> {
|
||||
return SIMD3<Float>(Float(ArKitView.offset.x + ArKitView.direction.dx * pos.x),Float(ArKitView.offset.y + ArKitView.direction.dy * pos.y),10)
|
||||
}
|
||||
func converWorldPosIntoPosModele(pos:SIMD3<Float>) -> CGPoint {
|
||||
let posX = Int(round((pos.x - (-400)) / 100))
|
||||
let posY = Int(round((pos.y - (-300)) / 100))
|
||||
return CGPoint(x: posX, y: posY)
|
||||
}
|
||||
|
||||
|
||||
// -- Pour les mouvement -- //
|
||||
var initialTransform: Transform = Transform()
|
||||
|
||||
@objc private func handleGesture(_ recognizer: UIGestureRecognizer) {
|
||||
guard let translationGesture = recognizer as? EntityTranslationGestureRecognizer, let entity = translationGesture.entity else { return }
|
||||
|
||||
switch translationGesture.state {
|
||||
case .began:
|
||||
self.initialTransform = entity.transform
|
||||
case .ended:
|
||||
entity.move(to: initialTransform, relativeTo: entity.parent, duration: 1)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
//self.installGestures([.all], for: robot as Entity & HasCollision).forEach { gestureRecognizer in
|
||||
// gestureRecognizer.addTarget(self, action: #selector(handleGesture(_:)))
|
||||
// -- //
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
//
|
||||
// ArKitViewRepresentable.swift
|
||||
// ArkitDoushiQi
|
||||
//
|
||||
// Created by Enzo JOLYS on 12/06/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct ArKitViewRepresentable : UIViewRepresentable {
|
||||
|
||||
func makeUIView(context: Context) -> ArKitView {
|
||||
return ArKitView()
|
||||
}
|
||||
|
||||
func updateUIView(_ uiView: UIViewType, context: Context) { }
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
//
|
||||
// UIViewArKit.swift
|
||||
// ArkitDoushiQi
|
||||
//
|
||||
// Created by Enzo JOLYS on 12/06/2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct UIViewArKit: View {
|
||||
var body: some View {
|
||||
ZStack {
|
||||
ArKitViewRepresentable()
|
||||
.ignoresSafeArea()
|
||||
.navigationBarBackButtonHidden(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
struct UIViewArKit_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
UIViewArKit()
|
||||
}
|
||||
}
|
Loading…
Reference in new issue