import { _decorator, BoxCollider, Camera, CapsuleCharacterController, CapsuleCollider, CharacterController, Component, director, Event, EventMouse, EventTouch, find, geometry, input, Input, ITriggerEvent, Node, PhysicsSystem, Quat, RigidBody, UITransform, v3, v4, Vec3, Vec4 } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('f1')
export class f1 extends Component {
res1: Vec3 = v3(0, 0, 0)
cc1: Node = null
cc2: Node = null
private _ray: geometry.Ray = new geometry.Ray()
ff1: Node = null
ff2: Node = null
ff3: Node = null
ff4: Node = null
ff5: Node = null
ff6: Node = null
ff7: Node = null
ff8: Node = null
ff9: Node = null
ff10: Node = null
ff11: Node = null
ff12: Node = null
zz1: Vec3 = v3(0, 0, 0)
xx1: Vec3 = v3(0, 0, 0)
xx2: Vec3 = v3(0, 0, 0)
xx3: Vec3 = v3(0, 0, 0)
sp1: number = -2
sp2: number = 0
sp3: number = 0
start() {
this.ff1 = find('b3/cu1')
this.ff2 = find('b3/cu2')
this.ff3 = find('b3/cu3')
this.ff4 = find('b3/cu4')
this.ff5 = find('b3/cube4')
this.ff6 = find('b3/cube5')
this.ff7 = find('b3/cube')
this.ff8 = find('b3/cu5')
this.ff9 = find('b3/cu6')
this.ff10 = find('b3/cu7')
this.ff11 = find('b3/cu8')
this.ff12 = find('b3/cu9')
//this.cc1 = find('b3/c1')
this.cc2 = find('c1')
}
onEnable() {
input.on(Input.EventType.TOUCH_START, this.onTouchStart, this)
this.node.getComponents(BoxCollider)[1].on('onTriggerEnter', this.onTriggerEnter, this)
this.node.getComponents(BoxCollider)[1].on('onTriggerStay', this.onTriggerStay, this)
this.node.getComponents(BoxCollider)[1].on('onTriggerExit', this.onTriggerExit, this)
}
onDisable() {
input.off(Input.EventType.TOUCH_START, this.onTouchStart, this)
this.node.getComponents(BoxCollider)[1].off('onTriggerEnter', this.onTriggerEnter, this)
this.node.getComponents(BoxCollider)[1].off('onTriggerStay', this.onTriggerStay, this)
this.node.getComponents(BoxCollider)[1].off('onTriggerExit', this.onTriggerExit, this)
}
//触发时加入子级
onTriggerEnter(event: ITriggerEvent) {
if (event.otherCollider.node == this.cc2) {
this.cc2.getComponent(CharacterController).enabled = false
this.cc2.getComponent(CapsuleCollider).enabled = true
if (this.cc2.parent != this.node) {
this.xx1 = this.cc2.position
//克隆自身角度
this.xx3 = this.cc2.eulerAngles.clone()
this.node.inverseTransformPoint(this.zz1, this.xx1)
this.node.addChild(this.cc2)
}
if (this.cc2.parent == this.node) {
this.cc2.setPosition(this.zz1)
//加入时用自身角度减去父级以旋转角度以匹配本地坐标系下的角度
this.cc2.setRotationFromEuler(this.xx3.x, this.xx3.y - this.node.eulerAngles.y, this.xx3.z)
}
}
}
onTriggerStay(event: ITriggerEvent) {
if (event.otherCollider.node == this.cc2) {
this.cc2.getComponent(CharacterController).enabled = false
this.cc2.getComponent(CapsuleCollider).enabled = true
//this.node.addChild(this.cc2)
}
}
//触发时移出子级
onTriggerExit(event: ITriggerEvent) {
if (event.otherCollider.node == this.cc2) {
this.cc2.getComponent(CharacterController).enabled = true
this.cc2.getComponent(CapsuleCollider).enabled = false
if (this.cc2.parent != this.node.parent) {
//克隆世界坐标系的位置
this.xx2 = this.cc2.worldPosition.clone()
this.xx3 = this.cc2.eulerAngles.clone()
//将CC2从子节点移出到父级
this.node.parent.addChild(this.cc2)
}
if (this.cc2.parent == this.node.parent) {
//将克隆的世界坐标系位置赋值
this.cc2.setPosition(this.xx2)
//移出时用自身角度加上父级以旋转角度以匹配世界坐标系下的角度
this.cc2.setRotationFromEuler(this.xx3.x, this.xx3.y + this.node.eulerAngles.y, this.xx3.z)
}
}
}
//触摸3D物体检测
onTouchStart(event: EventTouch) {
const touch = event.touch!;
this.cc2.getComponent(Camera).screenPointToRay(touch.getLocationX(), touch.getLocationY(), this._ray);
if (PhysicsSystem.instance.raycast(this._ray)) {
const raycastResults = PhysicsSystem.instance.raycastResults;
for (let i = 0; i < raycastResults.length; i++) {
const item = raycastResults[i];
if (item.collider.node == this.ff2) {
this.sp1 = 2
console.log('raycast hit the target node !');
break;
}
if (item.collider.node == this.ff3) {
this.sp1 = 0
console.log('raycast hit the target node !');
break;
}
if (item.collider.node == this.ff4) {
this.sp1 = -2
console.log('raycast hit the target node !');
break;
}
if (item.collider.node == this.ff9) {
this.sp3 = 0
console.log('raycast hit the target node !');
break;
}
if (item.collider.node == this.ff8) {
this.sp3 = 0.5
break;
}
if (item.collider.node == this.ff10) {
this.sp3 = -0.5
break;
}
if (item.collider.node == this.ff11) {
this.sp2 = 2
break;
}
if (item.collider.node == this.ff12) {
this.sp2 = 0
break;
}
}
} else {
console.log('raycast does not hit the target node !');
}
}
update(deltaTime: number) {
//更新子碰撞体的位置
this.ff4.setPosition(this.ff4.position)
this.ff3.setPosition(this.ff3.position)
this.ff2.setPosition(this.ff2.position)
this.ff1.setPosition(this.ff1.position)
this.ff5.setPosition(this.ff5.position)
this.ff6.setPosition(this.ff6.position)
this.ff7.setPosition(this.ff7.position)
this.ff8.setPosition(this.ff8.position)
this.ff9.setPosition(this.ff9.position)
this.ff10.setPosition(this.ff10.position)
this.ff11.setPosition(this.ff11.position)
this.ff12.setPosition(this.ff12.position)
//载具的移动
// 根据自身方向,转化方向
this.res1 = new Vec3(0, 0, this.sp2);
//获取节点的本地化局部旋转方向,1:返回值,2:需要转换的方向向量,3:当前本地旋转
Vec3.transformQuat(this.res1, this.res1, this.node.getWorldRotation());
this.node.getComponent(RigidBody).setLinearVelocity(v3(this.res1.x, this.sp1, this.res1.z))
this.node.getComponent(RigidBody).setAngularVelocity(v3(0, this.sp3, 0))
}
}