120 lines
4.6 KiB
TypeScript
120 lines
4.6 KiB
TypeScript
|
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 { 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';
|
||
|
import { director } from 'cc';
|
||
|
const { ccclass, property } = _decorator;
|
||
|
|
||
|
@ccclass('JoyStick')
|
||
|
export class JoyStick extends NodeBase {
|
||
|
@property(Node) background: Node = null!;
|
||
|
@property(Node) handle: Node = null!;
|
||
|
|
||
|
radiusDistance: number;
|
||
|
public static IS_TOUCH_ENABLE: boolean = false;
|
||
|
public static DIRECTION: Vec3 = new Vec3();
|
||
|
public static Instance: JoyStick = null;
|
||
|
|
||
|
startBgPoint: Vec3 = Vec3.ZERO;
|
||
|
|
||
|
protected onLoad(): void {
|
||
|
JoyStick.Instance = this;
|
||
|
}
|
||
|
|
||
|
start() {
|
||
|
this.startBgPoint = new Vec3(this.background.position);
|
||
|
this.enableTouch();
|
||
|
this.radiusDistance = (this.background.getNodeTransform().width) / 2;//differentPoint
|
||
|
}
|
||
|
|
||
|
showJoystick()
|
||
|
{
|
||
|
this.background.setNodeActive(true);
|
||
|
}
|
||
|
|
||
|
hideJoySitck()
|
||
|
{
|
||
|
this.background.setNodeActive(false);
|
||
|
}
|
||
|
|
||
|
resetBgPosition()
|
||
|
{
|
||
|
this.background.position = new Vec3(this.startBgPoint);
|
||
|
}
|
||
|
|
||
|
protected onTouchStart(event: EventTouch) {
|
||
|
if (!GameGlobalData.Instance.isStatePlay()) return;
|
||
|
JoyStick.IS_TOUCH_ENABLE = true;
|
||
|
var touchPoint = this.getTouchPointInParentNode(event.getUILocation());
|
||
|
this.background.position = new Vec3(touchPoint);
|
||
|
|
||
|
// {
|
||
|
// //OLD version
|
||
|
// // this.startPoint = new Vec3(this.node.position);
|
||
|
// // 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 onTouchMove(event: EventTouch) {
|
||
|
if (!GameGlobalData.Instance.isStatePlay() || !JoyStick.IS_TOUCH_ENABLE) return;
|
||
|
var movePoint = this.getTouchPointInParentNode(event.getUILocation());
|
||
|
// this.handle.worldPosition = this.node?.parent.getNodeTransform().convertToWorldSpaceAR(movePoint);
|
||
|
|
||
|
var currentBgPoint = new Vec3(this.background.worldPosition);
|
||
|
var newHandlePoint = this.node?.parent.getNodeTransform().convertToWorldSpaceAR(movePoint);//new Vec3(this.handle.worldPosition);
|
||
|
var direction: Vec3 = UmUtil.subtractTwoVector3(newHandlePoint, currentBgPoint).normalize();
|
||
|
if (Vec3.distance(newHandlePoint, currentBgPoint) > this.radiusDistance)
|
||
|
{
|
||
|
var differentPoint = UmUtil.scaleVector3(direction, this.radiusDistance);
|
||
|
this.background.worldPosition = UmUtil.subtractTwoVector3(newHandlePoint, differentPoint);
|
||
|
}
|
||
|
|
||
|
this.handle.worldPosition = newHandlePoint;
|
||
|
JoyStick.IS_TOUCH_ENABLE = true;
|
||
|
JoyStick.DIRECTION = direction;
|
||
|
GameGlobalData.Instance.lastHeroMoveDirection = direction;
|
||
|
|
||
|
// {
|
||
|
// //OLD version
|
||
|
// // let newPoint = this.getTouchPointInParentNode(event.getUILocation());
|
||
|
// // let direction = UmUtil.subtractTwoVector3(newPoint, this.originPoint);
|
||
|
// // direction.normalize();
|
||
|
// // // direction = UmUtil.scaleVector3(direction, this.heroMoveSpeed);
|
||
|
// // JoyStick.IS_TOUCH_ENABLE = true;
|
||
|
// // JoyStick.DIRECTION = direction;
|
||
|
// // GameGlobalData.Instance.lastHeroMoveDirection = direction;
|
||
|
// // // UmLog.log(newPoint.toString());
|
||
|
// // // this.currentPoint = new Vec3(newPoint);
|
||
|
// // this.setHandlePosition(newPoint, new Vec3(direction));
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
protected onTouchEnd(event: EventTouch) {
|
||
|
JoyStick.IS_TOUCH_ENABLE = false;
|
||
|
JoyStick.DIRECTION = Vec3.ZERO;
|
||
|
this.handle.position = Vec3.ZERO;
|
||
|
this.resetBgPosition();
|
||
|
UmClientEvent.dispatchEvent(GameDefine.EVENT_END_JOYSTICK);
|
||
|
}
|
||
|
|
||
|
protected onTouchCancel(event: EventTouch) {
|
||
|
JoyStick.IS_TOUCH_ENABLE = false;
|
||
|
JoyStick.DIRECTION = Vec3.ZERO;
|
||
|
this.handle.position = Vec3.ZERO;
|
||
|
this.resetBgPosition();
|
||
|
UmClientEvent.dispatchEvent(GameDefine.EVENT_END_JOYSTICK);
|
||
|
}
|
||
|
|
||
|
getTouchPointInParentNode(touchPoint): Vec3 {
|
||
|
return this.node.parent?.getNodeTransform().convertToNodeSpaceAR(touchPoint.toVec3());
|
||
|
}
|
||
|
}
|
||
|
|