init
This commit is contained in:
58
client/assets/Scripts/UI/JoyStickManager.ts
Normal file
58
client/assets/Scripts/UI/JoyStickManager.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { _decorator, Component, Node, input, Input, EventTouch, Vec2, Vec3, UITransform } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('JoyStickManager')
|
||||
export class JoyStickManager extends Component {
|
||||
input: Vec2 = new Vec2(0, 0)
|
||||
|
||||
private body: Node
|
||||
private stick: Node
|
||||
private touchStartPos: Vec2
|
||||
private defaultPos: Vec2
|
||||
private radius: number = 0
|
||||
|
||||
async init() {
|
||||
this.body = this.node.getChildByName("Body")
|
||||
this.stick = this.body.getChildByName("Stick")
|
||||
const { x, y } = this.body.position
|
||||
this.defaultPos = new Vec2(x, y)
|
||||
this.radius = this.body.getComponent(UITransform).contentSize.x / 2
|
||||
|
||||
input.on(Input.EventType.TOUCH_START, this.onTouchMove, this);
|
||||
input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
||||
input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
|
||||
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
input.off(Input.EventType.TOUCH_START, this.onTouchMove, this);
|
||||
input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
||||
input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
|
||||
}
|
||||
|
||||
onTouchMove(e: EventTouch) {
|
||||
const touchPos = e.touch.getUILocation()
|
||||
if (!this.touchStartPos) {
|
||||
this.touchStartPos = touchPos.clone()
|
||||
this.body.setPosition(this.touchStartPos.x, this.touchStartPos.y);
|
||||
}
|
||||
|
||||
const stickPos = new Vec2(touchPos.x - this.touchStartPos.x, touchPos.y - this.touchStartPos.y)
|
||||
// const stickPos = new Vec2(0, touchPos.y - this.touchStartPos.y)
|
||||
const len = stickPos.length()
|
||||
if (len > this.radius) {
|
||||
stickPos.multiplyScalar(this.radius / len)
|
||||
}
|
||||
|
||||
this.stick.setPosition(stickPos.x, stickPos.y)
|
||||
this.input = stickPos.clone().normalize()
|
||||
}
|
||||
|
||||
onTouchEnd() {
|
||||
this.body.setPosition(this.defaultPos.x, this.defaultPos.y)
|
||||
this.stick.setPosition(0, 0)
|
||||
this.touchStartPos = undefined
|
||||
this.input = new Vec2(0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
9
client/assets/Scripts/UI/JoyStickManager.ts.meta
Normal file
9
client/assets/Scripts/UI/JoyStickManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bfc9a6e3-7ecf-4422-af5c-670bff4ec928",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
13
client/assets/Scripts/UI/ShootManager.ts
Normal file
13
client/assets/Scripts/UI/ShootManager.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { _decorator, Component, Node } from 'cc';
|
||||
import { EventEnum } from '../Enum';
|
||||
import EventManager from '../Global/EventManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('ShootManager')
|
||||
export class ShootManager extends Component {
|
||||
fire() {
|
||||
EventManager.Instance.emit(EventEnum.ShootBtnClick)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
9
client/assets/Scripts/UI/ShootManager.ts.meta
Normal file
9
client/assets/Scripts/UI/ShootManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "f4c4bbdf-77d3-44a3-88c2-29f40401dab7",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user