import { _decorator, Animation, animation, AnimationClip, Collider, color, Color, ColorKey, Component, EventKeyboard, EventTouch, find, geometry, ICollisionEvent, input, Input, KeyCode, Node, physics, PhysicsSystem, Quat, RigidBody, Sprite, v2, v3, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('pc2')
export class pc2 extends Component {
private pc1:Node = null
private pc2:Node = null
private pc3:Node = null
private ku3:Node = null
private ku4:Node = null
private ku5:Node = null
private ku6:Node = null
private co1:Collider = null
private ri1:RigidBody = null
public pb1:Vec2 = v2(0,0)
public pb2:Vec2 = v2(0,0)
public w1:Vec3 = v3(0,0,0)
public w2:Vec3 = v3(0,0,0)
private max1:number = 50
private sp1:number = 5
private sp3:number = 5
private pg1:boolean = false
private pg2:boolean = false
private pg3:boolean = false
private pg4:boolean = false
private mask:number = 0xffffffff;
private maxDistance:number = 10000000;
private queryTrigger:boolean = true
public onLoad(): void {
this.pc1 = find('c1/n1/b1')
this.pc2 = find('c1/n1/b2')
this.pc3 = find('c1/b3')
this.ku3 = find('c1/b4')
this.ku4 = find('c1/b5')
this.ku5 = find('c1/b6')
this.ku6 = find('c1/b7')
this.co1 = this.node.getParent().getComponent(Collider)
this.ri1 = this.node.getParent().getComponent(RigidBody)
this.pc1.on(Input.EventType.TOUCH_START,this.onTouchMove,this)
this.pc1.on(Input.EventType.TOUCH_MOVE,this.onTouchMove,this)
this.pc1.on(Input.EventType.TOUCH_CANCEL,this.onTouchEnd,this)
this.pc1.on(Input.EventType.TOUCH_END,this.onTouchEnd,this)
this.pc3.on(Node.EventType.TOUCH_MOVE,this.onTouchMove2,this)
this.ku3.on(Node.EventType.TOUCH_START,this.onTouchStartK3,this)
this.ku4.on(Node.EventType.TOUCH_START,this.onTouchStartK4,this)
this.ku5.on(Node.EventType.TOUCH_START,this.onTouchStartK5,this)
}
start() {
this.co1.on('onCollisionEnter',this.onCollisEnter1,this)
this.co1.on('onCollisionExit',this.onCollisExit1,this)
}
onTouchStartK5(event:EventTouch){
if(this.pg1 && !this.pg2 && !this.pg3){
this.sp1 = 2
this.ku5.getComponent(Sprite).color = color(200,200,200,255)
this.getComponent(Animation).play('am1')
this.pg2 = true
}
else if(this.pg2 && !this.pg3){
this.sp1 = 5
this.ku5.getComponent(Sprite).color = color(255,255,255,255)
this.getComponent(Animation).play('am2')
this.pg2 = false
}
}
onCollisEnter1(event:ICollisionEvent){
this.pg1 = true
this.pg4 = true
}
//下坠
onCollisExit1(event:ICollisionEvent){
if(this.pg4 && this.pg1){
this.ri1.getLinearVelocity(this.w1)
this.w1.y = -5
this.ri1.setLinearVelocity(this.w1)
this.pg4 =false
}
this.w1.y = 0
}
//跳
onTouchStartK4(event:EventTouch){
if(this.pg1 && !this.pg2){
//获取当前线性速度
this.ri1.getLinearVelocity(this.w1)
this.w1.y = this.sp3
//设置Y轴速度
this.ri1.setLinearVelocity(this.w1)
this.pg1 = false
}
this.w1.y = 0
}
//跑
onTouchStartK3(event:EventTouch){
if(this.pg1 && !this.pg2 && !this.pg3){
this.ku3.getComponent(Sprite).color = color(200,200,200,255)
this.sp1 = 10
this.pg3 = true
}
else if(this.pg1 && !this.pg2 && this.pg3){
this.sp1 = 5
this.ku3.getComponent(Sprite).color = color(255,255,255,255)
this.pg3 = false
}
}
onTouchMove(event:EventTouch){
let sc1:Vec2 = event.getUILocation()
let sc2 = v3(sc1.x,sc1.y,0)
//转到屏幕坐标
let sc3 = v3()
this.pc1.inverseTransformPoint(sc3,sc2)
let la1 = sc3.length()
//范围
if(la1 > this.max1){
sc3.x = sc3.x * this.max1 / la1
sc3.y = sc3.y * this.max1 / la1
la1 = this.max1
}
//方向向量
this.pb1.x = sc3.x / la1 //cos角度
this.pb1.y = sc3.y / la1 //sin角度
this.pc2.setPosition(sc3)
}
onTouchMove2(event:EventTouch){
//获取本地坐标系下的旋转,用欧拉角表示
let fe1 = this.node.eulerAngles.x
let fe2 = this.node.eulerAngles.y
//鼠标在UI坐标系下的移动距离
let x1 = event.getDeltaX()
let y1 = event.getDeltaY()
//用欧拉角设置世界坐标系下的旋转
this.node.setRotationFromEuler(fe1+=y1,fe2+=x1*-1,0)
}
onTouchEnd(event:EventTouch){
this.pb1.x = this.pb1.y = 0
this.pc2.setPosition(0,0,0)
}
update(deltaTime: number) {
// 根据自身方向,转化方向
const res1 = new Vec3(this.pb1.x,0,-this.pb1.y);
//获取节点的本地化局部旋转方向,1:返回值,2:需要转换的方向向量,3:当前本地旋转
Vec3.transformQuat(res1, res1, this.node.getRotation());
//射线检测
let js1 = new geometry.Ray(this.node.worldPosition.x,this.node.worldPosition.y,this.node.worldPosition.z,res1.x,res1.y,res1.z)
if(PhysicsSystem.instance.raycastClosest(js1,this.mask,this.maxDistance,this.queryTrigger)){
const js2 = PhysicsSystem.instance.raycastClosestResult
const js3 = js2.distance
//最小距离3
if(js3<3){
this.ri1.setLinearVelocity(v3(0,0,0))
return
}
}
//动力学移动
if(this.pg1 && this.pg4){
this.ri1.setLinearVelocity(v3(res1.x*this.sp1,0,res1.z*this.sp1))
}
}
}