super-hero/assets/cc-game/Test/JoyStick.ts

88 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-04-19 03:49:41 -07:00
import { _decorator, Component, Node } from 'cc';
import { NodeBase } from '../scripts/base/NodeBase';
import { Vec3, EventTouch } from 'cc';
import { UmUtil } from '../../cc-common/cc-util/UmUtil';
import { HeroMove } from '../scripts/game_play/hero/HeroMove';
import { GameGlobalData } from '../scripts/global/GameGlobalData';
import { UmLog } from '../../cc-common/cc-util/UmLog';
import { UmClientEvent } from '../../cc-common/cc-util/UmOneToMultiListener';
import { GameDefine } from '../scripts/config/GameDefine';
const { ccclass, property } = _decorator;
@ccclass('JoyStick')
export class JoyStick extends NodeBase {
@property(HeroMove) objectMove: HeroMove = null!;
@property(Node) handle: Node = null!;
originPoint: Vec3 = Vec3.ZERO;
joyStickSpeed: Number = 5;
handleMaxDistance: number;
start() {
this.enableTouch();
this.handleMaxDistance = (this.node.getNodeTransform().width - this.handle.getNodeTransform().width) / 2;
}
protected onTouchStart(event: EventTouch) {
if (!GameGlobalData.Instance.isStatePlay()) return;
this.originPoint = new Vec3(this.node.position);
this.joyStickSpeed = GameGlobalData.Instance.heroControlConfig.joyStickSpeed;
let newPoint = this.getTouchPointInParentNode(event.getUILocation());
let direction = UmUtil.subtractTwoVector3(newPoint, this.originPoint);
this.setHandlePosition(newPoint, new Vec3(direction));
UmClientEvent.dispatchEvent(GameDefine.EVENT_START_JOYSTICK);
}
protected onTouchEnd(event: EventTouch) {
if (!GameGlobalData.Instance.isStatePlay()) return;
this.objectMove.isTouchEnable = false;
this.handle.position = Vec3.ZERO;
this.objectMove.directionVec3 = Vec3.ZERO;
if (GameGlobalData.Instance.isStatePlay())
UmClientEvent.dispatchEvent(GameDefine.EVENT_END_JOYSTICK);
}
protected onTouchCancel(event: EventTouch) {
if (!GameGlobalData.Instance.isStatePlay()) return;
this.objectMove.isTouchEnable = false;
this.handle.position = Vec3.ZERO;
this.objectMove.directionVec3 = Vec3.ZERO;
if (GameGlobalData.Instance.isStatePlay())
UmClientEvent.dispatchEvent(GameDefine.EVENT_END_JOYSTICK);
}
protected onTouchMove(event: EventTouch) {
if (!GameGlobalData.Instance.isStatePlay()) return;
let newPoint = this.getTouchPointInParentNode(event.getUILocation());
let direction = UmUtil.subtractTwoVector3(newPoint, this.originPoint);
direction.normalize();
direction = UmUtil.scaleVector3(direction, 5);
this.objectMove.isTouchEnable = true;
this.objectMove.directionVec3 = direction;
GameGlobalData.Instance.lastHeroMoveDirection = direction;
// UmLog.log(newPoint.toString());
// this.currentPoint = new Vec3(newPoint);
this.setHandlePosition(newPoint, new Vec3(direction));
}
setHandlePosition(newPoint, direction) {
var distance = Vec3.distance(newPoint, this.originPoint);
if (distance > this.handleMaxDistance) {
direction = UmUtil.scaleVector3(direction.normalize(), this.handleMaxDistance);
newPoint = UmUtil.plusTwoVector3(this.originPoint, direction);
}
this.handle.worldPosition = this.node?.parent.getNodeTransform().convertToWorldSpaceAR(newPoint);
}
getTouchPointInParentNode(touchPoint): Vec3 {
return this.node.parent?.getNodeTransform().convertToNodeSpaceAR(touchPoint.toVec3());
}
}