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

120 lines
4.6 KiB
TypeScript
Raw Permalink 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 { 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';
2024-05-08 02:31:19 -07:00
import { director } from 'cc';
2024-04-19 03:49:41 -07:00
const { ccclass, property } = _decorator;
@ccclass('JoyStick')
export class JoyStick extends NodeBase {
2024-05-08 02:31:19 -07:00
@property(Node) background: Node = null!;
2024-04-19 03:49:41 -07:00
@property(Node) handle: Node = null!;
2024-05-08 02:31:19 -07:00
radiusDistance: number;
2024-04-22 01:32:16 -07:00
public static IS_TOUCH_ENABLE: boolean = false;
public static DIRECTION: Vec3 = new Vec3();
2024-05-08 02:31:19 -07:00
public static Instance: JoyStick = null;
startBgPoint: Vec3 = Vec3.ZERO;
protected onLoad(): void {
JoyStick.Instance = this;
}
2024-04-22 01:32:16 -07:00
2024-04-19 03:49:41 -07:00
start() {
2024-05-08 02:31:19 -07:00
this.startBgPoint = new Vec3(this.background.position);
2024-04-19 03:49:41 -07:00
this.enableTouch();
2024-05-08 02:31:19 -07:00
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);
2024-04-19 03:49:41 -07:00
}
protected onTouchStart(event: EventTouch) {
if (!GameGlobalData.Instance.isStatePlay()) return;
2024-05-08 02:31:19 -07:00
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);
// }
2024-04-19 03:49:41 -07:00
}
2024-05-08 02:31:19 -07:00
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));
// }
}
2024-04-19 03:49:41 -07:00
protected onTouchEnd(event: EventTouch) {
2024-04-22 01:32:16 -07:00
JoyStick.IS_TOUCH_ENABLE = false;
JoyStick.DIRECTION = Vec3.ZERO;
2024-04-19 03:49:41 -07:00
this.handle.position = Vec3.ZERO;
2024-05-08 02:31:19 -07:00
this.resetBgPosition();
2024-04-22 01:32:16 -07:00
UmClientEvent.dispatchEvent(GameDefine.EVENT_END_JOYSTICK);
2024-04-19 03:49:41 -07:00
}
protected onTouchCancel(event: EventTouch) {
2024-04-22 01:32:16 -07:00
JoyStick.IS_TOUCH_ENABLE = false;
JoyStick.DIRECTION = Vec3.ZERO;
2024-04-19 03:49:41 -07:00
this.handle.position = Vec3.ZERO;
2024-05-08 02:31:19 -07:00
this.resetBgPosition();
2024-04-22 01:32:16 -07:00
UmClientEvent.dispatchEvent(GameDefine.EVENT_END_JOYSTICK);
2024-04-19 03:49:41 -07:00
}
getTouchPointInParentNode(touchPoint): Vec3 {
return this.node.parent?.getNodeTransform().convertToNodeSpaceAR(touchPoint.toVec3());
}
}