2021-04-20 11:40:39 +02:00
|
|
|
import VirtualJoystick from 'phaser3-rex-plugins/plugins/virtualjoystick.js';
|
2021-05-04 11:29:37 +02:00
|
|
|
import {waScaleManager} from "../Services/WaScaleManager";
|
2021-05-10 17:10:41 +02:00
|
|
|
import {DEPTH_INGAME_TEXT_INDEX} from "../Game/DepthIndexes";
|
2021-04-20 11:40:39 +02:00
|
|
|
|
|
|
|
//the assets were found here: https://hannemann.itch.io/virtual-joystick-pack-free
|
|
|
|
export const joystickBaseKey = 'joystickBase';
|
|
|
|
export const joystickBaseImg = 'resources/objects/joystickSplitted.png';
|
|
|
|
export const joystickThumbKey = 'joystickThumb';
|
|
|
|
export const joystickThumbImg = 'resources/objects/smallHandleFilledGrey.png';
|
|
|
|
|
2021-05-05 09:35:24 +02:00
|
|
|
const baseSize = 50;
|
|
|
|
const thumbSize = 25;
|
|
|
|
const radius = 17.5;
|
|
|
|
|
2021-04-20 11:40:39 +02:00
|
|
|
export class MobileJoystick extends VirtualJoystick {
|
2021-05-04 11:29:37 +02:00
|
|
|
private resizeCallback: () => void;
|
|
|
|
|
2021-04-20 11:40:39 +02:00
|
|
|
constructor(scene: Phaser.Scene) {
|
|
|
|
super(scene, {
|
2021-05-05 09:35:24 +02:00
|
|
|
x: -1000,
|
|
|
|
y: -1000,
|
|
|
|
radius: radius * window.devicePixelRatio,
|
2021-05-10 17:10:41 +02:00
|
|
|
base: scene.add.image(0, 0, joystickBaseKey).setDisplaySize(baseSize * window.devicePixelRatio, baseSize * window.devicePixelRatio).setDepth(DEPTH_INGAME_TEXT_INDEX),
|
|
|
|
thumb: scene.add.image(0, 0, joystickThumbKey).setDisplaySize(thumbSize * window.devicePixelRatio, thumbSize * window.devicePixelRatio).setDepth(DEPTH_INGAME_TEXT_INDEX),
|
2021-04-20 11:40:39 +02:00
|
|
|
enable: true,
|
|
|
|
dir: "8dir",
|
|
|
|
});
|
2021-05-05 09:35:24 +02:00
|
|
|
this.visible = false;
|
|
|
|
this.enable = false;
|
|
|
|
|
|
|
|
this.scene.input.on('pointerdown', (pointer: { x: number; y: number; wasTouch: boolean; event: TouchEvent }) => {
|
|
|
|
if (!pointer.wasTouch) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-20 11:40:39 +02:00
|
|
|
|
2021-05-05 09:35:24 +02:00
|
|
|
// Let's only display the joystick if there is one finger on the screen
|
|
|
|
if (pointer.event.touches.length === 1) {
|
|
|
|
this.x = pointer.x;
|
|
|
|
this.y = pointer.y;
|
|
|
|
this.visible = true;
|
|
|
|
this.enable = true;
|
|
|
|
} else {
|
|
|
|
this.visible = false;
|
|
|
|
this.enable = false;
|
|
|
|
}
|
2021-04-20 11:40:39 +02:00
|
|
|
});
|
|
|
|
this.scene.input.on('pointerup', () => {
|
2021-05-05 09:35:24 +02:00
|
|
|
this.visible = false;
|
|
|
|
this.enable = false;
|
2021-04-20 11:40:39 +02:00
|
|
|
});
|
2021-05-04 11:29:37 +02:00
|
|
|
this.resizeCallback = this.resize.bind(this);
|
|
|
|
this.scene.scale.on(Phaser.Scale.Events.RESIZE, this.resizeCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
private resize() {
|
2021-05-05 09:35:24 +02:00
|
|
|
this.base.setDisplaySize(baseSize / waScaleManager.zoomModifier * window.devicePixelRatio, baseSize / waScaleManager.zoomModifier * window.devicePixelRatio);
|
|
|
|
this.thumb.setDisplaySize(thumbSize / waScaleManager.zoomModifier * window.devicePixelRatio, thumbSize / waScaleManager.zoomModifier * window.devicePixelRatio);
|
|
|
|
this.setRadius(radius / waScaleManager.zoomModifier * window.devicePixelRatio);
|
2021-05-04 11:29:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public destroy() {
|
|
|
|
super.destroy();
|
|
|
|
this.scene.scale.removeListener(Phaser.Scale.Events.RESIZE, this.resizeCallback);
|
2021-04-20 11:40:39 +02:00
|
|
|
}
|
2021-05-04 11:29:37 +02:00
|
|
|
}
|