Add experimental vibration touch feedback on mobile

pull/14/head
Flashfyre 2023-12-25 15:48:16 -05:00
parent 77e70a6540
commit 1e4e0d013e
2 changed files with 30 additions and 13 deletions

View File

@ -77,6 +77,8 @@ export default class BattleScene extends Phaser.Scene {
public gameSpeed: integer = 1; public gameSpeed: integer = 1;
public showLevelUpStats: boolean = true; public showLevelUpStats: boolean = true;
public windowType: integer = 1; public windowType: integer = 1;
public enableTouchControls: boolean = false;
public enableVibration: boolean = false;
public quickStart: boolean = quickStart; public quickStart: boolean = quickStart;
public finalWave: integer = 200; public finalWave: integer = 200;
@ -910,19 +912,24 @@ export default class BattleScene extends Phaser.Scene {
if (this.blockInput) if (this.blockInput)
return; return;
let inputSuccess = false; let inputSuccess = false;
if (this.isButtonPressed(Button.UP)) let vibrationLength = 0;
if (this.isButtonPressed(Button.UP)) {
inputSuccess = this.ui.processInput(Button.UP); inputSuccess = this.ui.processInput(Button.UP);
else if (this.isButtonPressed(Button.DOWN)) vibrationLength = 5;
} else if (this.isButtonPressed(Button.DOWN)) {
inputSuccess = this.ui.processInput(Button.DOWN); inputSuccess = this.ui.processInput(Button.DOWN);
else if (this.isButtonPressed(Button.LEFT)) vibrationLength = 5;
} else if (this.isButtonPressed(Button.LEFT)) {
inputSuccess = this.ui.processInput(Button.LEFT); inputSuccess = this.ui.processInput(Button.LEFT);
else if (this.isButtonPressed(Button.RIGHT)) vibrationLength = 5;
} else if (this.isButtonPressed(Button.RIGHT)) {
inputSuccess = this.ui.processInput(Button.RIGHT); inputSuccess = this.ui.processInput(Button.RIGHT);
else if (this.isButtonPressed(Button.ACTION)) vibrationLength = 5;
} else if (this.isButtonPressed(Button.ACTION))
inputSuccess = this.ui.processInput(Button.ACTION); inputSuccess = this.ui.processInput(Button.ACTION);
else if (this.isButtonPressed(Button.CANCEL)) else if (this.isButtonPressed(Button.CANCEL)) {
inputSuccess = this.ui.processInput(Button.CANCEL); inputSuccess = this.ui.processInput(Button.CANCEL);
else if (this.isButtonPressed(Button.MENU)) { } else if (this.isButtonPressed(Button.MENU)) {
switch (this.ui.getMode()) { switch (this.ui.getMode()) {
case Mode.MESSAGE: case Mode.MESSAGE:
if (!(this.ui.getHandler() as MessageUiHandler).pendingPrompt) if (!(this.ui.getHandler() as MessageUiHandler).pendingPrompt)
@ -938,12 +945,14 @@ export default class BattleScene extends Phaser.Scene {
case Mode.CONFIRM: case Mode.CONFIRM:
case Mode.GAME_MODE_SELECT: case Mode.GAME_MODE_SELECT:
this.ui.setOverlayMode(Mode.MENU); this.ui.setOverlayMode(Mode.MENU);
inputSuccess = true;
break; break;
case Mode.MENU: case Mode.MENU:
case Mode.SETTINGS: case Mode.SETTINGS:
case Mode.ACHIEVEMENTS: case Mode.ACHIEVEMENTS:
this.ui.revertMode(); this.ui.revertMode();
this.playSound('select'); this.playSound('select');
inputSuccess = true;
break; break;
default: default:
return; return;
@ -959,8 +968,7 @@ export default class BattleScene extends Phaser.Scene {
inputSuccess = this.ui.processInput(Button.CYCLE_ABILITY); inputSuccess = this.ui.processInput(Button.CYCLE_ABILITY);
else else
return; return;
} } else if (this.isButtonPressed(Button.SPEED_UP)) {
else if (this.isButtonPressed(Button.SPEED_UP)) {
if (this.gameSpeed < 5) { if (this.gameSpeed < 5) {
this.gameData.saveSetting(Setting.Game_Speed, settingOptions[Setting.Game_Speed].indexOf(`${this.gameSpeed}x`) + 1); this.gameData.saveSetting(Setting.Game_Speed, settingOptions[Setting.Game_Speed].indexOf(`${this.gameSpeed}x`) + 1);
if (this.ui.getMode() === Mode.SETTINGS) if (this.ui.getMode() === Mode.SETTINGS)
@ -983,6 +991,8 @@ export default class BattleScene extends Phaser.Scene {
return; return;
} else } else
return; return;
if (inputSuccess && this.enableVibration)
navigator.vibrate(vibrationLength || 10);
this.blockInput = true; this.blockInput = true;
this.time.delayedCall(Utils.fixedInt(250), () => this.blockInput = false); this.time.delayedCall(Utils.fixedInt(250), () => this.blockInput = false);
} }

View File

@ -8,7 +8,8 @@ export enum Setting {
SE_Volume = "SE_VOLUME", SE_Volume = "SE_VOLUME",
Show_Stats_on_Level_Up = "SHOW_LEVEL_UP_STATS", Show_Stats_on_Level_Up = "SHOW_LEVEL_UP_STATS",
Window_Type = "WINDOW_TYPE", Window_Type = "WINDOW_TYPE",
Touch_Controls = "TOUCH_CONTROLS" Touch_Controls = "TOUCH_CONTROLS",
Vibration = "VIBRATION"
} }
export interface SettingOptions { export interface SettingOptions {
@ -26,7 +27,8 @@ export const settingOptions: SettingOptions = {
[Setting.SE_Volume]: new Array(11).fill(null).map((_, i) => i ? (i * 10).toString() : 'Mute'), [Setting.SE_Volume]: new Array(11).fill(null).map((_, i) => i ? (i * 10).toString() : 'Mute'),
[Setting.Show_Stats_on_Level_Up]: [ 'Off', 'On' ], [Setting.Show_Stats_on_Level_Up]: [ 'Off', 'On' ],
[Setting.Window_Type]: new Array(4).fill(null).map((_, i) => (i + 1).toString()), [Setting.Window_Type]: new Array(4).fill(null).map((_, i) => (i + 1).toString()),
[Setting.Touch_Controls]: [ 'Auto', 'Disabled' ] [Setting.Touch_Controls]: [ 'Auto', 'Disabled' ],
[Setting.Vibration]: [ 'Auto', 'Disabled' ]
}; };
export const settingDefaults: SettingDefaults = { export const settingDefaults: SettingDefaults = {
@ -36,7 +38,8 @@ export const settingDefaults: SettingDefaults = {
[Setting.SE_Volume]: 10, [Setting.SE_Volume]: 10,
[Setting.Show_Stats_on_Level_Up]: 1, [Setting.Show_Stats_on_Level_Up]: 1,
[Setting.Window_Type]: 0, [Setting.Window_Type]: 0,
[Setting.Touch_Controls]: 0 [Setting.Touch_Controls]: 0,
[Setting.Vibration]: 0
}; };
export function setSetting(scene: BattleScene, setting: Setting, value: integer): boolean { export function setSetting(scene: BattleScene, setting: Setting, value: integer): boolean {
@ -63,9 +66,13 @@ export function setSetting(scene: BattleScene, setting: Setting, value: integer)
updateWindowType(scene, parseInt(settingOptions[setting][value])); updateWindowType(scene, parseInt(settingOptions[setting][value]));
break; break;
case Setting.Touch_Controls: case Setting.Touch_Controls:
scene.enableTouchControls = settingOptions[setting][value] !== 'Disabled' && hasTouchscreen();
const touchControls = document.getElementById('touchControls'); const touchControls = document.getElementById('touchControls');
if (touchControls) if (touchControls)
touchControls.classList.toggle('visible', settingOptions[setting][value] !== 'Disabled' && hasTouchscreen()); touchControls.classList.toggle('visible', scene.enableTouchControls);
break;
case Setting.Vibration:
scene.enableVibration = settingOptions[setting][value] !== 'Disabled' && hasTouchscreen();
break; break;
} }