| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- ===== OLD =====
- private onMouseDown(event: EventMouse) {
- if (this.state !== 'playing') return;
- this.unlockAudio();
- this.handlePointerStart(-1, event);
- }
- ===== NEW =====
- private onMouseDown(event: EventMouse) {
- if (event.getButton() !== EventMouse.BUTTON_LEFT) return;
- if (this.state !== 'playing') return;
- this.unlockAudio();
- this.handlePointerStart(-1, event);
- }
- ===== OLD =====
- private resolveMobileActionFromEvent(event: EventTouch | EventMouse): MobileAction | null {
- const point = this.pointerToWorldPoint(event);
- if (this.spriteEllipseHit(point.x, point.y, this.touchFireSprite)) return 'fire';
- if (this.spriteEllipseHit(point.x, point.y, this.touchSwitchSprite)) return 'switch';
- return this.resolveJoystickActionFromEvent(event);
- }
- private resolveJoystickActionFromEvent(event: EventTouch | EventMouse): Extract<MobileAction, 'left' | 'right' | 'up'> | null {
- const point = this.pointerToWorldPoint(event);
- const box = this.spriteWorldBox(this.touchJoystickSprite);
- if (!box) return null;
- const rx = Math.max(1, box.width / 2);
- const ry = Math.max(1, box.height / 2);
- const nx = (point.x - box.cx) / rx;
- const ny = (point.y - box.cy) / ry;
- if (nx * nx + ny * ny > 1.12) return null;
- if (ny < -0.32 || (Math.abs(nx) < 0.18 && Math.abs(ny) < 0.18)) return null;
- if (ny > 0.30 && Math.abs(ny) >= Math.abs(nx) * 0.58) return 'up';
- if (nx < -0.18) return 'left';
- if (nx > 0.18) return 'right';
- return null;
- }
- private pointerToWorldPoint(event: EventTouch | EventMouse) {
- const location = event.getLocation();
- const camera = this.cameraNode.getComponent(Camera);
- if (!camera) return { x: location.x, y: location.y };
- const world = camera.screenToWorld(new Vec3(location.x, location.y, 0));
- return { x: world.x, y: world.y };
- }
- private spriteEllipseHit(x: number, y: number, sprite: Sprite | null) {
- const box = this.spriteWorldBox(sprite);
- if (!box) return false;
- const dx = (x - box.cx) / Math.max(1, box.width / 2);
- const dy = (y - box.cy) / Math.max(1, box.height / 2);
- return dx * dx + dy * dy <= 1;
- }
- private spriteWorldBox(sprite: Sprite | null) {
- const rect = sprite?.node.getComponent(UITransform)?.getBoundingBoxToWorld();
- if (!rect) return null;
- return {
- x: rect.x,
- y: rect.y,
- width: rect.width,
- height: rect.height,
- cx: rect.x + rect.width / 2,
- cy: rect.y + rect.height / 2
- };
- }
- ===== NEW =====
- private resolveMobileActionFromEvent(event: EventTouch | EventMouse): MobileAction | null {
- const point = this.pointerToGamePoint(event);
- if (this.controlEllipseHit(point.x, point.y, this.fireButtonCenter(), 118, 118)) return 'fire';
- if (this.controlEllipseHit(point.x, point.y, this.switchButtonCenter(), 90, 90)) return 'switch';
- return this.resolveJoystickActionAt(point.x, point.y);
- }
- private resolveJoystickActionFromEvent(event: EventTouch | EventMouse): Extract<MobileAction, 'left' | 'right' | 'up'> | null {
- const point = this.pointerToGamePoint(event);
- return this.resolveJoystickActionAt(point.x, point.y);
- }
- private resolveJoystickActionAt(x: number, y: number): Extract<MobileAction, 'left' | 'right' | 'up'> | null {
- const center = this.joystickCenter();
- const nx = (x - center.x) / 128;
- const ny = (y - center.y) / 128;
- if (nx * nx + ny * ny > 1.2) return null;
- if (ny < -0.28 || (Math.abs(nx) < 0.14 && Math.abs(ny) < 0.14)) return null;
- if (ny > 0.24 && Math.abs(ny) >= Math.abs(nx) * 0.5) return 'up';
- if (nx < -0.14) return 'left';
- if (nx > 0.14) return 'right';
- return null;
- }
- private pointerToGamePoint(event: EventTouch | EventMouse) {
- const location = event.getUILocation();
- const visible = view.getVisibleSize();
- const sourceWidth = Math.max(1, visible.width);
- const sourceHeight = Math.max(1, visible.height);
- const worldWidth = this.visibleWorldWidth();
- return {
- x: (location.x / sourceWidth - 0.5) * worldWidth,
- y: (location.y / sourceHeight - 0.5) * H
- };
- }
- private controlEllipseHit(x: number, y: number, center: { x: number; y: number }, radiusX: number, radiusY: number) {
- const dx = (x - center.x) / Math.max(1, radiusX);
- const dy = (y - center.y) / Math.max(1, radiusY);
- return dx * dx + dy * dy <= 1;
- }
|