import { EVENT_TYPE, JOY_STATE } from "../../utrl/gameEnum"; import gameEventManager from "../../utrl/gameEventManager"; const { ccclass, requireComponent, property } = cc._decorator; /**节点移动控制类 */ @ccclass @requireComponent(cc.RigidBody) export default class NodeMove extends cc.Component { @property /**移动速度 */ move_speed = 200; @property /**使用力度 */ use_power = false; /**当前遥感状态 */ state = JOY_STATE.end; /**当前遥感角度 */ angle = 0; /**当前遥感力度 */ power = 0; /**本身刚体 */ private rigid: cc.RigidBody = null; onEnable() { this.state = JOY_STATE.end; this.angle = 0; this.power = 0; this.rigid = this.node.getComponent(cc.RigidBody); this.rigid.gravityScale = 0; this.rigid.linearDamping = 5; this.rigid.fixedRotation = true; gameEventManager.on(EVENT_TYPE.joy_stick, this.onJoyStick, this); } onDisable() { gameEventManager.off(EVENT_TYPE.joy_stick, this.onJoyStick, this); } update() { this.refreshLinearVelocity(); } /** * 遥感回调 * @param state 当前遥感状态 * @param angle 当前遥感角度 * @param power 当前遥感力度 */ private onJoyStick(state: JOY_STATE, angle: number, power: number) { this.state = state; this.angle = angle; this.power = power; } /**刷新刚体线性速度 */ private refreshLinearVelocity() { if (this.state == JOY_STATE.move) { let rad = this.angle * Math.PI / 180; this.rigid.linearVelocity = cc.v2(Math.cos(rad), Math.sin(rad)).mul(this.power * this.move_speed); } } }