pinball/assets/_Game/Scripts/GamePlay/Bar.ts

60 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-07-25 20:07:42 -07:00
import {
_decorator,
Camera,
CCFloat,
clamp,
Component,
EventMouse,
Input,
input,
IVec2Like,
Node,
Vec2,
Vec3,
} from 'cc';
import P4PSDK, { EventType } from '../P4PSDK';
const { ccclass, property } = _decorator;
@ccclass('Bar')
export default class Bar extends Component {
@property(CCFloat)
private yOffset: number = 350;
@property(CCFloat)
private minX: number = 0;
@property(CCFloat)
private maxX: number = 100;
@property(Camera)
private camera: Camera;
private _screenPoint: Vec2 = new Vec2();
private _worldPoint: Vec3 = new Vec3();
protected onLoad(): void {
input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
P4PSDK.broadCast.on(EventType.OnMouse, this.onMouse, this);
}
private onMouseMove(event: EventMouse): void {
event.getLocation(this._screenPoint);
this.updateWorldPoint();
}
private onMouse(position: IVec2Like): void {
this._screenPoint = new Vec2(position.x, position.y);
this.updateWorldPoint();
}
private updateWorldPoint(): Vec3 {
this.camera.screenToWorld(this._screenPoint.toVec3(), this._worldPoint);
this._worldPoint.x = clamp(this._worldPoint.x, this.minX, this.maxX);
this._worldPoint.y = this.yOffset;
this._worldPoint.z = 0;
return this._worldPoint;
}
protected update(dt: number): void {
this.node.setWorldPosition(this.node.getWorldPosition().lerp(this._worldPoint, 30 * dt));
}
}