import { _decorator, Animation, AnimationClip, CharacterController, Component, EventKeyboard, EventMouse, Input, input, KeyCode, Node, RigidBody, v2, v3, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('c1')
export class c1 extends Component {
sp1: number = 2
res1: Vec3 = v3(0, 0, 0)
x1: number = 0
z1: number = 0
characterController: CharacterController = null
g1:Animation = null
g2:AnimationClip = null
g3:AnimationClip = null
g4:AnimationClip = null
g5:AnimationClip = null
g6:AnimationClip = null
start() {
this.characterController = this.node.getComponent(CharacterController)
//旋转视角
input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this)
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this)
input.on(Input.EventType.KEY_UP, this.onKeyUp, this)
this.g1 = this.node.getChildByName('c1').getComponent(Animation)
this.g2 = this.g1.clips[7]
this.g3 = this.g1.clips[5]
this.g4 = this.g1.clips[2]
this.g5 = this.g1.clips[3]
this.g6 = this.g1.clips[4]
}
onKeyDown(event: EventKeyboard) {
if (event.keyCode == KeyCode.KEY_W) {
this.z1 = 1
this.g1.getState(this.g2.name).play()
}
if (event.keyCode == KeyCode.KEY_A) {
this.x1 = 1
this.g1.getState(this.g4.name).play()
}
if (event.keyCode == KeyCode.KEY_S) {
this.z1 = -1
this.g1.getState(this.g6.name).play()
}
if (event.keyCode == KeyCode.KEY_D) {
this.x1 = -1
this.g1.getState(this.g5.name).play()
}
}
onKeyUp(event: EventKeyboard) {
if (event.keyCode == KeyCode.KEY_W) {
this.z1 = 0
this.g1.getState(this.g3.name).play()
if(this.x1 != 1 ){
this.g1.getState(this.g4.name).stop()
}
if(this.x1 != -1 ){
this.g1.getState(this.g5.name).stop()
}
this.g1.getState(this.g6.name).stop()
this.g1.getState(this.g2.name).stop()
}
if (event.keyCode == KeyCode.KEY_A && this.x1 == 1) {
this.x1 = 0
this.g1.getState(this.g3.name).play()
this.g1.getState(this.g4.name).stop()
this.g1.getState(this.g5.name).stop()
if(this.z1 != -1 ){
this.g1.getState(this.g6.name).stop()
}
if(this.z1 != 1 ){
this.g1.getState(this.g2.name).stop()
}
}
if (event.keyCode == KeyCode.KEY_S) {
this.z1 = 0
this.g1.getState(this.g3.name).play()
if(this.x1 != 1 ){
this.g1.getState(this.g4.name).stop()
}
if(this.x1 != -1 ){
this.g1.getState(this.g5.name).stop()
}
this.g1.getState(this.g6.name).stop()
this.g1.getState(this.g2.name).stop()
}
if (event.keyCode == KeyCode.KEY_D && this.x1 == -1) {
this.x1 = 0
this.g1.getState(this.g3.name).play()
this.g1.getState(this.g4.name).stop()
this.g1.getState(this.g5.name).stop()
if(this.z1 != -1 ){
this.g1.getState(this.g6.name).stop()
}
if(this.z1 != 1 ){
this.g1.getState(this.g2.name).stop()
}
}
}
//旋转视角
onMouseMove(event: EventMouse) {
if (event.getButton() == 2) {
//获取本地坐标系下的旋转,用欧拉角表示
let fe1 = this.node.getChildByName('n2').eulerAngles.x
let fe2 = this.node.getChildByName('n2').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('n2').setRotationFromEuler(fe1, fe2, 0)
}
}
}
update(deltaTime: number) {
// 根据自身方向,转化方向
this.res1 = new Vec3(this.x1 * this.sp1 * deltaTime, 0, this.z1 * this.sp1 * deltaTime);
//获取节点的本地化局部旋转方向,1:返回值,2:需要转换的方向向量,3:当前本地旋转
Vec3.transformQuat(this.res1, this.res1, this.node.getChildByName('c1').getRotation());
this.characterController.move(v3(this.res1.x, -0.2, this.res1.z));
if(this.z1 !=0 || this.x1 != 0){
let h1 = this.node.getChildByName('n2')
let h2 = this.node.getChildByName('c1')
h2.setRotationFromEuler(h2.eulerAngles.x,h1.eulerAngles.y+180,h2.eulerAngles.z)
}
}
}