[add] first
This commit is contained in:
158
assets/script/ui/easy_touch/easy_touch.ts
Normal file
158
assets/script/ui/easy_touch/easy_touch.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
|
||||
import { _decorator, Component, Node, CCInteger, Vec3 } from 'cc';
|
||||
import { Constant } from '../../framework/constant';
|
||||
import { Util } from '../../framework/util';
|
||||
import { PlayerData } from '../../framework/playerData';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* Predefined variables
|
||||
* Name = easyTouch
|
||||
* DateTime = Wed May 11 2022 14:12:44 GMT+0800 (中国标准时间)
|
||||
* Author = yu_meng123
|
||||
* FileBasename = easyTouch.ts
|
||||
* FileBasenameNoExtension = easyTouch
|
||||
* URL = db://assets/script/game/easyTouch.ts
|
||||
* ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
|
||||
*
|
||||
*/
|
||||
@ccclass('EasyTouch')
|
||||
export class EasyTouch extends Component {
|
||||
@property(Node)
|
||||
centerCircleNode: Node = null!;
|
||||
|
||||
@property(Node)
|
||||
directionNode: Node = null!;
|
||||
|
||||
@property(CCInteger)
|
||||
bgLength: number = 0;
|
||||
|
||||
@property
|
||||
isLeftTouch: boolean = false;
|
||||
|
||||
private _vec3_1: Vec3 = new Vec3();
|
||||
|
||||
private _lastPos: Vec3 = new Vec3();
|
||||
|
||||
private _vec3_Angle: Vec3 = new Vec3(0, 0, 0);
|
||||
|
||||
private _lastX: number = 0;
|
||||
private _lastY: number = 0;
|
||||
|
||||
start () {
|
||||
this.directionNode.active = false;
|
||||
|
||||
setTimeout(() => {
|
||||
this._lastPos.set(this.node.position);
|
||||
});
|
||||
}
|
||||
|
||||
public startTouch (x: number, y: number) {
|
||||
this.node.setPosition(new Vec3(x, y));
|
||||
this.directionNode.active = false;
|
||||
}
|
||||
|
||||
public endTouch () {
|
||||
this.node.setPosition(this._lastPos);
|
||||
this.directionNode.active = false;
|
||||
|
||||
// 左手
|
||||
if (this.isLeftTouch) {
|
||||
PlayerData.instance.updateEasyTouchInfo(Constant.EASY_TOUCH.TOUCH_LEFT_X, 0);
|
||||
PlayerData.instance.updateEasyTouchInfo(Constant.EASY_TOUCH.TOUCH_LEFT_Y, 0);
|
||||
}
|
||||
// 右手
|
||||
else {
|
||||
PlayerData.instance.updateEasyTouchInfo(Constant.EASY_TOUCH.TOUCH_RIGHT_X, 0);
|
||||
PlayerData.instance.updateEasyTouchInfo(Constant.EASY_TOUCH.TOUCH_RIGHT_Y, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
public moveTouch (x: number, y: number) {
|
||||
var dis = Math.sqrt(x * x + y * y);
|
||||
if (dis > this.bgLength) {
|
||||
var per: number = this.bgLength / dis;
|
||||
x = per * x;
|
||||
y = per * y;
|
||||
this._vec3_1.set(x, y, 0);
|
||||
this.centerCircleNode.setPosition(this._vec3_1);
|
||||
}
|
||||
else {
|
||||
this._vec3_1.set(x, y, 0);
|
||||
this.centerCircleNode.setPosition(this._vec3_1);
|
||||
}
|
||||
|
||||
var z: number = Math.atan(x / y) * 180 / Math.PI;
|
||||
if (y < 0) {
|
||||
z -= 180;
|
||||
}
|
||||
|
||||
this._vec3_Angle.set(0, 0, z * -1);
|
||||
this.directionNode.eulerAngles = this._vec3_Angle;
|
||||
this.directionNode.active = true;
|
||||
|
||||
// 左手
|
||||
if (this.isLeftTouch) {
|
||||
PlayerData.instance.updateEasyTouchInfo(Constant.EASY_TOUCH.TOUCH_LEFT_X, x / this.bgLength);
|
||||
PlayerData.instance.updateEasyTouchInfo(Constant.EASY_TOUCH.TOUCH_LEFT_Y, y / this.bgLength);
|
||||
}
|
||||
// 右手
|
||||
else {
|
||||
PlayerData.instance.updateEasyTouchInfo(Constant.EASY_TOUCH.TOUCH_RIGHT_X, x / this.bgLength);
|
||||
PlayerData.instance.updateEasyTouchInfo(Constant.EASY_TOUCH.TOUCH_RIGHT_Y, y / this.bgLength);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 上次便移的点
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
public setLastXY (x: number, y: number) {
|
||||
this._lastX = x;
|
||||
this._lastY = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过数值更改位置
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
public moveTouchByXY (x: number, y: number) {
|
||||
x = Math.ceil(x * 10) * 0.1;
|
||||
y = Math.ceil(y * 10) * 0.1;
|
||||
|
||||
this._lastX = Util.lerp(this._lastX, x, 0.1);
|
||||
this._lastY = Util.lerp(this._lastY, y, 0.1);
|
||||
|
||||
this._vec3_1.set(this._lastX * this.bgLength, this._lastY * this.bgLength, 0);
|
||||
this.centerCircleNode.setPosition(this._vec3_1);
|
||||
|
||||
var z: number = Math.atan(x / y) * 180 / Math.PI;
|
||||
if (y < 0) {
|
||||
z -= 180;
|
||||
}
|
||||
|
||||
this._vec3_Angle.set(0, 0, z * -1);
|
||||
this.directionNode.eulerAngles = this._vec3_Angle;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* [1] Class member could be defined like this.
|
||||
* [2] Use `property` decorator if your want the member to be serializable.
|
||||
* [3] Your initialization goes here.
|
||||
* [4] Your update function goes here.
|
||||
*
|
||||
* Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
|
||||
* Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
|
||||
* Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
|
||||
*/
|
9
assets/script/ui/easy_touch/easy_touch.ts.meta
Normal file
9
assets/script/ui/easy_touch/easy_touch.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "2ab5d37b-dce3-45ce-b8c6-247477bb702f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
140
assets/script/ui/easy_touch/easy_touch_panel.ts
Normal file
140
assets/script/ui/easy_touch/easy_touch_panel.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
|
||||
import { _decorator, Touch, input, Input, view, EventTouch, Vec2, Component } from 'cc';
|
||||
import { Constant } from '../../framework/constant';
|
||||
import { EasyTouch } from './easy_touch';
|
||||
import { ClientEvent } from '../../framework/clientEvent';
|
||||
const { ccclass, property } = _decorator;
|
||||
@ccclass('EasyTouchPanel')
|
||||
export class EasyTouchPanel extends Component {
|
||||
// 左 横杆
|
||||
@property(EasyTouch)
|
||||
leftEasyTouch: EasyTouch = null!;
|
||||
|
||||
@property(EasyTouch)
|
||||
rightEasyTouch: EasyTouch = null!;
|
||||
|
||||
// 移动位置
|
||||
private _startLeftMoveX: number = 0;
|
||||
|
||||
// 移动坐标
|
||||
private _startLeftMoveY: number = 0;
|
||||
|
||||
// 移动位置
|
||||
private _startRightMoveX: number = 0;
|
||||
|
||||
// 移动坐标
|
||||
private _startRightMoveY: number = 0;
|
||||
|
||||
// 屏幕宽
|
||||
private _canvasMidWidth: number = 0;
|
||||
|
||||
// 屏幕高
|
||||
private _canvasMidHeight: number = 0;
|
||||
|
||||
private _leftTouchId: number = -1;
|
||||
|
||||
private _rightTouchId: number = -1;
|
||||
|
||||
start () {
|
||||
let size = view.getVisibleSize();
|
||||
let width = Math.round(size.width);
|
||||
let height = Math.round(size.height);
|
||||
this._canvasMidWidth = width * 0.5;
|
||||
this._canvasMidHeight = height * 0.5;
|
||||
}
|
||||
|
||||
onEnable () {
|
||||
ClientEvent.on(Constant.EVENT_NAME.GAME_INIT, this._init, this);
|
||||
input.on(Input.EventType.TOUCH_START, this._onTouchStart, this);
|
||||
input.on(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
|
||||
input.on(Input.EventType.TOUCH_END, this._onTouchEnd, this);
|
||||
}
|
||||
|
||||
onDisable () {
|
||||
input.off(Input.EventType.TOUCH_START, this._onTouchStart, this);
|
||||
input.off(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
|
||||
input.off(Input.EventType.TOUCH_END, this._onTouchEnd, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 鼠标触摸
|
||||
* @param event
|
||||
*/
|
||||
private _onTouchStart (event: EventTouch) {
|
||||
var touch: Touch = event.touch;
|
||||
var touchX: number = touch.getUIStartLocation().x;
|
||||
var touchY: number = touch.getUIStartLocation().y;
|
||||
|
||||
// 屏幕左边
|
||||
if (touchX < this._canvasMidWidth) {
|
||||
this._startLeftMoveX = touchX;
|
||||
this._startLeftMoveY = touchY;
|
||||
this._leftTouchId = touch.getID();
|
||||
|
||||
this.leftEasyTouch.startTouch(touchX - this._canvasMidWidth, touchY - this._canvasMidHeight);
|
||||
}
|
||||
else {
|
||||
this._startRightMoveX = touchX;
|
||||
this._startRightMoveY = touchY;
|
||||
this._rightTouchId = touch.getID();
|
||||
|
||||
this.rightEasyTouch.startTouch(touchX - this._canvasMidWidth, touchY - this._canvasMidHeight);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 鼠标触摸
|
||||
* @param event
|
||||
*/
|
||||
private _onTouchMove (event: EventTouch) {
|
||||
var touchs: Touch[] = event.getTouches();
|
||||
for (var index: number = 0; index < touchs.length; index++) {
|
||||
var touch: Touch = touchs[index];
|
||||
var vec2: Vec2 = touch.getUILocation();
|
||||
if (touch.getID() == this._leftTouchId) {
|
||||
this.leftEasyTouch.moveTouch(touch.getUILocation().x - this._startLeftMoveX, touch.getUILocation().y - this._startLeftMoveY);
|
||||
}
|
||||
else if (touch.getID() == this._rightTouchId) {
|
||||
this.rightEasyTouch.moveTouch(vec2.x - this._startRightMoveX, vec2.y - this._startRightMoveY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _init(){
|
||||
this._leftTouchId = -1;
|
||||
this.leftEasyTouch.endTouch();
|
||||
this.leftEasyTouch.moveTouchByXY(
|
||||
0,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 鼠标触摸
|
||||
* @param event
|
||||
*/
|
||||
private _onTouchEnd (event: EventTouch) {
|
||||
var touch: Touch = event.touch;
|
||||
var touchId: number = touch.getID();
|
||||
if (touchId == this._leftTouchId) {
|
||||
this._leftTouchId = -1;
|
||||
this.leftEasyTouch.endTouch();
|
||||
this.leftEasyTouch.moveTouchByXY(
|
||||
0,
|
||||
0
|
||||
);
|
||||
}
|
||||
else {
|
||||
this._rightTouchId = -1;
|
||||
this.rightEasyTouch.endTouch();
|
||||
this.rightEasyTouch.moveTouchByXY(
|
||||
0,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
if (this._leftTouchId == -1 && this._rightTouchId == -1) {
|
||||
// 双手都移开屏幕
|
||||
}
|
||||
}
|
||||
}
|
9
assets/script/ui/easy_touch/easy_touch_panel.ts.meta
Normal file
9
assets/script/ui/easy_touch/easy_touch_panel.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "e1f2a28c-3bf2-49c7-91b1-c5c641bb0edd",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user