import { _decorator, Animation, animation, AnimationClip, Collider, color, Color, ColorKey, Component, EventTouch, find, ICollisionEvent, Input, Node, 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 pc4:Node = null
private ku1:Node = null
private ku2: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 = 150
private sp1:number = 5
private sp2:number = 5
private sp3:number = 10
private pg1:boolean = false
private pg2:boolean = false
public onLoad(): void {
this.pc1 = find('cn2/cn1/bc1')
this.pc2 = find('cn2/cn1/bc1/bc2')
this.pc3 = find('cn2/cn1/bc3')
this.pc4 = find('cn2/cn1/bc3/bc4')
this.ku1 = find('cn2/cn1/la1')
this.ku2 = find('cn2/cn1/la2')
this.ku3 = find('cn2/cn1/la3')
this.ku4 = find('cn2/cn1/la4')
this.ku5 = find('cn2/cn1/la5')
this.ku6 = find('cn2/cn1/la6')
this.co1 = this.node.getParent().getComponent(Collider)
this.ri1 = this.node.getParent().getComponent(RigidBody)
this.ku1.on(Input.EventType.TOUCH_START,this.onTouchMove,this)
this.ku1.on(Input.EventType.TOUCH_MOVE,this.onTouchMove,this)
this.ku1.on(Input.EventType.TOUCH_CANCEL,this.onTouchEnd,this)
this.ku1.on(Input.EventType.TOUCH_END,this.onTouchEnd,this)
this.ku2.on(Node.EventType.TOUCH_START,this.onTouchMove1,this)
this.ku2.on(Node.EventType.TOUCH_MOVE,this.onTouchMove1,this)
this.ku2.on(Node.EventType.TOUCH_CANCEL,this.onTouchEnd,this)
this.ku2.on(Node.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)
}
onTouchStartK5(event:EventTouch){
if(this.pg2){
this.sp1 = 5
this.ku5.getComponent(Sprite).color = color(255,255,255,255)
this.getComponent(Animation).play('an2')
this.pg2 = false
}else if(this.pg1){
this.sp1 = 2
this.ku5.getComponent(Sprite).color = color(200,200,200,255)
this.getComponent(Animation).play('an1')
this.ku3.getComponent(Sprite).color = color(255,255,255,255)
this.pg2 = true
}
}
onCollisEnter1(event:ICollisionEvent){
this.pg1 = true
}
onTouchStartK4(event:EventTouch){
if(this.pg1){
this.w1.y = this.sp3
this.ri1.setLinearVelocity(this.w1)
this.pg1 = false
}
this.w1.y = 0
}
onTouchStartK3(event:EventTouch){
if(this.sp1 == 5){
this.ku3.getComponent(Sprite).color = color(200,200,200,255)
this.sp1 = 10
}
else if(this.sp1 == 10){
this.sp1 = 5
this.ku3.getComponent(Sprite).color = color(255,255,255,255)
}
else if(this.sp1 == 2){
this.ku3.getComponent(Sprite).color = color(200,200,200,255)
this.sp1 = 10
this.ku5.getComponent(Sprite).color = color(255,255,255,255)
this.getComponent(Animation).play('an2')
this.pg2 = 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)
}
onTouchMove1(event:EventTouch){
let sc1:Vec2 = event.getUILocation()
let sc2 = v3(sc1.x,sc1.y,0)
//转到屏幕坐标
let sc3 = v3()
this.pc3.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.pb2.x = sc3.x / la1 //cos角度
this.pb2.y = sc3.y / la1 //sin角度
this.pc4.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*0.5,fe2+=x1*0.5*-1,0)
}
onTouchEnd(event:EventTouch){
this.pb1.x = this.pb1.y = 0
this.pb2.x = this.pb2.y = 0
this.pc2.setPosition(0,0,0)
this.pc4.setPosition(0,0,0)
}
// 根据自身方向,转化方向
getDirection (x: number, y: number, z: number) {
const result = new Vec3(x, y, z);
//向量四元数乘法,获取节点的旋转方向
Vec3.transformQuat(result, result, this.node.getRotation());
return result;
}
update(deltaTime: number) {
const position = this.node.getPosition()
//逐元素向量乘加: A + B * scale
Vec3.scaleAndAdd(position, position, this.getDirection(this.pb1.x,0,-this.pb1.y), deltaTime * this.sp1)
this.node.setPosition(position.x,this.node.position.y,position.z)
//获取本地坐标系下的旋转,用欧拉角表示
let fe1 = this.node.eulerAngles.x
let fe2 = this.node.eulerAngles.y
//鼠标在UI坐标系下的移动距离
let x1 = this.pb2.x
let y1 = this.pb2.y
//用欧拉角设置世界坐标系下的旋转
this.node.setRotationFromEuler(fe1+=y1*deltaTime*200,fe2+=x1*deltaTime*200*-1,0)
}
}