import { _decorator, CapsuleCharacterController, CapsuleCollider, Component, Director, director, EventKeyboard, EventMouse, find, Input, input, ITriggerEvent, KeyCode, Node, PhysicsSystem, RigidBody, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('t1')
export class t1 extends Component {
x1:number = 0
z1:number = 0
s1:number = 5
n1:number = 0
n2:number = 0
g1:number = 0
g2:number = 0
k1:number = 0
k2:number = 0
o1:Node = null
o2:Node = null
w1:boolean = false
p1 = v3()
u1:boolean = false
r1 = v3()
start() {
this.o1 = find('e1')
}
onLoad (){
input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this)
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this)
input.on(Input.EventType.KEY_PRESSING, this.onKeyDown, this)
input.on(Input.EventType.KEY_UP, this.onKeyUp, this)
}
onDestroy (){
input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this)
input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this)
input.off(Input.EventType.KEY_PRESSING, this.onKeyDown, this)
input.off(Input.EventType.KEY_UP, this.onKeyUp, this)
}
onKeyDown(event: EventKeyboard) {
if (event.keyCode == KeyCode.KEY_W) {
this.z1 = -1
}
if (event.keyCode == KeyCode.KEY_A) {
this.x1 = -1
}
if (event.keyCode == KeyCode.KEY_S) {
this.z1 = 1
}
if (event.keyCode == KeyCode.KEY_D) {
this.x1 = 1
}
}
onKeyUp(event: EventKeyboard) {
if (event.keyCode == KeyCode.KEY_W) {
this.z1 = 0
this.k1 = this.node.position.y
if(this.node.position.y < 5002){this.k1 = 5002}
this.node.setPosition(0,this.k1,0)
this.node.setRotationFromEuler(0,0,0)
this.o1.setRotationFromEuler(this.o1.eulerAngles.x + -this.n2,0,this.o1.eulerAngles.z + -this.n1)
}
if (event.keyCode == KeyCode.KEY_A && this.x1 == -1) {
this.x1 = 0
}
if (event.keyCode == KeyCode.KEY_S) {
this.z1 = 0
}
if (event.keyCode == KeyCode.KEY_D && this.x1 == 1) {
this.x1 = 0
}
}
onMouseMove(event: EventMouse) {
if (event.getButton() == 2) {
//获取本地坐标系下的旋转,用欧拉角表示
let fe1 = this.node.getChildByName('c2').eulerAngles.x
let fe2 = this.node.getChildByName('c2').eulerAngles.y
//鼠标在UI坐标系下的移动距离
let x1 = event.movementX * 0.5
let y1 = event.movementY * 0.5
//用欧拉角设置世界坐标系下的旋转
fe1 += -y1
fe2 += -x1
if (fe1 > -80 && fe1 < 80) {
this.node.getChildByName('c2').setRotationFromEuler(fe1, fe2,0)
}
}
}
update(deltaTime: number) {
this.r1 = new Vec3(this.x1 * this.s1 * deltaTime, 0, this.z1 * this.s1 * deltaTime)
Vec3.transformQuat(this.r1, this.r1, this.node.getChildByName('c2').getRotation())
this.p1 = v3(this.r1.x, -0.2,this.r1.z)
this.node.getComponent(CapsuleCharacterController).move(this.p1)
//this.node.getComponent(RigidBody).setLinearVelocity(this.p1)
//给Z轴
this.g1 = Math.atan2(this.node.position.x,this.node.position.y)
this.n1 = this.g1 * 180 / Math.PI * -1
//给X轴
this.g2 = Math.atan2(this.node.position.z,this.node.position.y)
this.n2 = this.g2 * 180 / Math.PI
this.node.setRotationFromEuler(this.n2,0,this.n1)
//console.log(g1 + ' aaa ')
//console.log(this.n1 + ' aaa ')
//console.log(g2 + ' aaa ')
//console.log(this.n2 + ' aaa ')
}
}