tab -> spaces what about now github ?

This commit is contained in:
Greenlamp 2024-05-03 18:58:07 +02:00
parent 1d2bc88cab
commit 62e4ff7b33
3 changed files with 65 additions and 66 deletions

View File

@ -200,7 +200,7 @@ export default class BattleScene extends SceneBase {
this.load.atlas(key, `images/pokemon/${variant ? 'variant/' : ''}${experimental ? 'exp/' : ''}${atlasPath}.png`, `images/pokemon/${variant ? 'variant/' : ''}${experimental ? 'exp/' : ''}${atlasPath}.json`); this.load.atlas(key, `images/pokemon/${variant ? 'variant/' : ''}${experimental ? 'exp/' : ''}${atlasPath}.png`, `images/pokemon/${variant ? 'variant/' : ''}${experimental ? 'exp/' : ''}${atlasPath}.json`);
} }
async preload() { async preload() {
this.load.scenePlugin('inputController', InputsController); this.load.scenePlugin('inputController', InputsController);
this.load.scenePlugin('uiInputs', UiInputs); this.load.scenePlugin('uiInputs', UiInputs);
if (DEBUG_RNG) { if (DEBUG_RNG) {

View File

@ -1,4 +1,4 @@
import Phaser, { Time } from "phaser"; import Phaser, {Time} from "phaser";
import * as Utils from "./utils"; import * as Utils from "./utils";
import {initTouchControls} from './touch-controls'; import {initTouchControls} from './touch-controls';
import pad_generic from "#app/configs/pad_generic"; import pad_generic from "#app/configs/pad_generic";
@ -26,19 +26,20 @@ export enum Button {
SPEED_UP, SPEED_UP,
SLOW_DOWN SLOW_DOWN
} }
const repeatInputDelayMillis = 250; const repeatInputDelayMillis = 250;
export class InputsController extends Phaser.Plugins.ScenePlugin { export class InputsController extends Phaser.Plugins.ScenePlugin {
private game: Phaser.Game; private game: Phaser.Game;
private buttonKeys: Phaser.Input.Keyboard.Key[][]; private buttonKeys: Phaser.Input.Keyboard.Key[][];
private gamepads: Array<string> = new Array(); private gamepads: Array<string> = new Array();
private scene: Phaser.Scene; private scene: Phaser.Scene;
// buttonLock ensures only a single movement key is firing repeated inputs // buttonLock ensures only a single movement key is firing repeated inputs
// (i.e. by holding down a button) at a time // (i.e. by holding down a button) at a time
private buttonLock: Button; private buttonLock: Button;
private interactions: Map<Button, Map<string, boolean>> = new Map(); private interactions: Map<Button, Map<string, boolean>> = new Map();
private time: Time; private time: Time;
constructor(scene: Phaser.Scene, pluginManager: Phaser.Plugins.PluginManager, pluginKey: string) { constructor(scene: Phaser.Scene, pluginManager: Phaser.Plugins.PluginManager, pluginKey: string) {
super(scene, pluginManager, pluginKey); super(scene, pluginManager, pluginKey);
@ -57,7 +58,7 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
delete this.interactions[Button.MENU]; delete this.interactions[Button.MENU];
} }
boot() { boot() {
this.eventEmitter = this.systems.events; this.eventEmitter = this.systems.events;
this.events = new Phaser.Events.EventEmitter(); this.events = new Phaser.Events.EventEmitter();
this.game.events.on(Phaser.Core.Events.STEP, this.update, this); this.game.events.on(Phaser.Core.Events.STEP, this.update, this);
@ -79,13 +80,13 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
this.systems.input.gamepad.on('down', this.gamepadButtonDown, this); this.systems.input.gamepad.on('down', this.gamepadButtonDown, this);
this.systems.input.gamepad.on('up', this.gamepadButtonUp, this); this.systems.input.gamepad.on('up', this.gamepadButtonUp, this);
} }
// Keyboard // Keyboard
this.setupKeyboardControls(); this.setupKeyboardControls();
} }
update() { update() {
for (const b of Utils.getEnumValues(Button)) { for (const b of Utils.getEnumValues(Button)) {
if (!this.interactions.hasOwnProperty(b)) continue; if (!this.interactions.hasOwnProperty(b)) continue;
if (this.repeatInputDurationJustPassed(b)) { if (this.repeatInputDurationJustPassed(b)) {
@ -96,17 +97,17 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
this.setLastProcessedMovementTime(b); this.setLastProcessedMovementTime(b);
} }
} }
} }
setupGamepad(thisGamepad): void { setupGamepad(thisGamepad): void {
let gamepadID = thisGamepad.id.toLowerCase(); let gamepadID = thisGamepad.id.toLowerCase();
const mappedPad = this.mapGamepad(gamepadID); const mappedPad = this.mapGamepad(gamepadID);
this.player = { this.player = {
'mapping': mappedPad.gamepadMapping, 'mapping': mappedPad.gamepadMapping,
} }
} }
refreshGamepads() :void { refreshGamepads(): void {
// Sometimes, gamepads are undefined. For some reason. // Sometimes, gamepads are undefined. For some reason.
this.gamepads = this.systems.input.gamepad.gamepads.filter(function (el) { this.gamepads = this.systems.input.gamepad.gamepads.filter(function (el) {
return el != null; return el != null;
@ -115,9 +116,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
for (const [index, thisGamepad] of this.gamepads.entries()) { for (const [index, thisGamepad] of this.gamepads.entries()) {
thisGamepad.index = index; // Overwrite the gamepad index, in case we had undefined gamepads earlier thisGamepad.index = index; // Overwrite the gamepad index, in case we had undefined gamepads earlier
} }
} }
getActionGamepadMapping() { getActionGamepadMapping() {
const gamepadMapping = {}; const gamepadMapping = {};
gamepadMapping[this.player.mapping.LC_N] = Button.UP; gamepadMapping[this.player.mapping.LC_N] = Button.UP;
gamepadMapping[this.player.mapping.LC_S] = Button.DOWN; gamepadMapping[this.player.mapping.LC_S] = Button.DOWN;
@ -138,9 +139,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
gamepadMapping[this.player.mapping.RS] = Button.SLOW_DOWN; gamepadMapping[this.player.mapping.RS] = Button.SLOW_DOWN;
return gamepadMapping; return gamepadMapping;
} }
gamepadButtonDown(pad, button, value): void { gamepadButtonDown(pad, button, value): void {
const actionMapping = this.getActionGamepadMapping(); const actionMapping = this.getActionGamepadMapping();
const buttonDown = actionMapping.hasOwnProperty(button.index) && actionMapping[button.index]; const buttonDown = actionMapping.hasOwnProperty(button.index) && actionMapping[button.index];
if (buttonDown !== undefined) { if (buttonDown !== undefined) {
@ -150,9 +151,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
}); });
this.setLastProcessedMovementTime(buttonDown); this.setLastProcessedMovementTime(buttonDown);
} }
} }
gamepadButtonUp(pad, button, value): void { gamepadButtonUp(pad, button, value): void {
const actionMapping = this.getActionGamepadMapping(); const actionMapping = this.getActionGamepadMapping();
const buttonUp = actionMapping.hasOwnProperty(button.index) && actionMapping[button.index]; const buttonUp = actionMapping.hasOwnProperty(button.index) && actionMapping[button.index];
if (buttonUp !== undefined) { if (buttonUp !== undefined) {
@ -162,9 +163,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
}); });
this.delLastProcessedMovementTime(buttonUp); this.delLastProcessedMovementTime(buttonUp);
} }
} }
setupKeyboardControls(): void { setupKeyboardControls(): void {
const keyCodes = Phaser.Input.Keyboard.KeyCodes; const keyCodes = Phaser.Input.Keyboard.KeyCodes;
const keyConfig = { const keyConfig = {
[Button.UP]: [keyCodes.UP, keyCodes.W], [Button.UP]: [keyCodes.UP, keyCodes.W],
@ -198,9 +199,9 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
initTouchControls(mobileKeyConfig); initTouchControls(mobileKeyConfig);
this.listenInputKeyboard(); this.listenInputKeyboard();
} }
listenInputKeyboard(): void { listenInputKeyboard(): void {
this.buttonKeys.forEach((row, index) => { this.buttonKeys.forEach((row, index) => {
for (const key of row) { for (const key of row) {
key.on('down', () => { key.on('down', () => {
@ -219,7 +220,7 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
}); });
} }
}); });
} }
mapGamepad(id) { mapGamepad(id) {
id = id.toLowerCase(); id = id.toLowerCase();
@ -227,23 +228,21 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
if (id.includes('081f') && id.includes('e401')) { if (id.includes('081f') && id.includes('e401')) {
padConfig = pad_unlicensedSNES; padConfig = pad_unlicensedSNES;
} } else if (id.includes('xbox') && id.includes('360')) {
else if (id.includes('xbox') && id.includes('360')) {
padConfig = pad_xbox360; padConfig = pad_xbox360;
} } else if (id.includes('054c')) {
else if (id.includes('054c')) {
padConfig = pad_dualshock; padConfig = pad_dualshock;
} }
return padConfig; return padConfig;
} }
/** /**
* repeatInputDurationJustPassed returns true if @param button has been held down long * repeatInputDurationJustPassed returns true if @param button has been held down long
* enough to fire a repeated input. A button must claim the buttonLock before * enough to fire a repeated input. A button must claim the buttonLock before
* firing a repeated input - this is to prevent multiple buttons from firing repeatedly. * firing a repeated input - this is to prevent multiple buttons from firing repeatedly.
*/ */
repeatInputDurationJustPassed(button: Button): boolean { repeatInputDurationJustPassed(button: Button): boolean {
if (this.buttonLock === null || this.buttonLock !== button) { if (this.buttonLock === null || this.buttonLock !== button) {
return false; return false;
} }
@ -251,17 +250,17 @@ export class InputsController extends Phaser.Plugins.ScenePlugin {
this.buttonLock = null; this.buttonLock = null;
return true; return true;
} }
} }
setLastProcessedMovementTime(button: Button): void { setLastProcessedMovementTime(button: Button): void {
if (!this.interactions.hasOwnProperty(button)) return; if (!this.interactions.hasOwnProperty(button)) return;
this.buttonLock = button; this.buttonLock = button;
this.interactions[button].pressTime = this.time.now; this.interactions[button].pressTime = this.time.now;
} }
delLastProcessedMovementTime(button: Button): void { delLastProcessedMovementTime(button: Button): void {
if (!this.interactions.hasOwnProperty(button)) return; if (!this.interactions.hasOwnProperty(button)) return;
this.buttonLock = null; this.buttonLock = null;
this.interactions[button].pressTime = null; this.interactions[button].pressTime = null;
} }
} }

View File

@ -19,7 +19,7 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin {
this.events = this.scene.inputController.events this.events = this.scene.inputController.events
} }
boot() { boot() {
this.listenInputs(); this.listenInputs();
} }
@ -41,7 +41,7 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin {
}, this); }, this);
} }
getActionsKeyDown() { getActionsKeyDown() {
const actions = {}; const actions = {};
actions[Button.UP] = () => this.buttonDirection(Button.UP); actions[Button.UP] = () => this.buttonDirection(Button.UP);
actions[Button.DOWN] = () => this.buttonDirection(Button.DOWN); actions[Button.DOWN] = () => this.buttonDirection(Button.DOWN);
@ -61,31 +61,31 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin {
actions[Button.SPEED_UP] = () => this.buttonSpeedChange(); actions[Button.SPEED_UP] = () => this.buttonSpeedChange();
actions[Button.SLOW_DOWN] = () => this.buttonSpeedChange(false); actions[Button.SLOW_DOWN] = () => this.buttonSpeedChange(false);
return actions; return actions;
} }
getActionsKeyUp() { getActionsKeyUp() {
const actions = {}; const actions = {};
actions[Button.STATS] = () => this.buttonStats(false); actions[Button.STATS] = () => this.buttonStats(false);
return actions; return actions;
} }
buttonDirection(direction): Array<boolean | number> { buttonDirection(direction): Array<boolean | number> {
const inputSuccess = this.scene.ui.processInput(direction); const inputSuccess = this.scene.ui.processInput(direction);
const vibrationLength = 5; const vibrationLength = 5;
return [inputSuccess, vibrationLength]; return [inputSuccess, vibrationLength];
} }
buttonAb(button): Array<boolean | number> { buttonAb(button): Array<boolean | number> {
const inputSuccess = this.scene.ui.processInput(button); const inputSuccess = this.scene.ui.processInput(button);
return [inputSuccess, 0]; return [inputSuccess, 0];
} }
buttonTouch(): Array<boolean | number> { buttonTouch(): Array<boolean | number> {
const inputSuccess = this.scene.ui.processInput(Button.SUBMIT) || this.scene.ui.processInput(Button.ACTION); const inputSuccess = this.scene.ui.processInput(Button.SUBMIT) || this.scene.ui.processInput(Button.ACTION);
return [inputSuccess, 0]; return [inputSuccess, 0];
} }
buttonStats(pressed = true): Array<boolean | number> { buttonStats(pressed = true): Array<boolean | number> {
if (pressed) { if (pressed) {
for (let p of this.scene.getField().filter(p => p?.isActive(true))) for (let p of this.scene.getField().filter(p => p?.isActive(true)))
p.toggleStats(true); p.toggleStats(true);
@ -94,9 +94,9 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin {
p.toggleStats(false); p.toggleStats(false);
} }
return [true, 0]; return [true, 0];
} }
buttonMenu(): Array<boolean | number> { buttonMenu(): Array<boolean | number> {
let inputSuccess; let inputSuccess;
if (this.scene.disableMenu) if (this.scene.disableMenu)
return [true, 0]; return [true, 0];
@ -129,17 +129,17 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin {
return [true, 0]; return [true, 0];
} }
return [inputSuccess, 0]; return [inputSuccess, 0];
} }
buttonCycleOption(button): Array<boolean | number> { buttonCycleOption(button): Array<boolean | number> {
let inputSuccess; let inputSuccess;
if (this.scene.ui?.getHandler() instanceof StarterSelectUiHandler) { if (this.scene.ui?.getHandler() instanceof StarterSelectUiHandler) {
inputSuccess = this.scene.ui.processInput(button); inputSuccess = this.scene.ui.processInput(button);
} }
return [inputSuccess, 0]; return [inputSuccess, 0];
} }
buttonSpeedChange(up= true): Array<boolean | number> { buttonSpeedChange(up = true): Array<boolean | number> {
if (up) { if (up) {
if (this.scene.gameSpeed < 5) { if (this.scene.gameSpeed < 5) {
this.scene.gameData.saveSetting(Setting.Game_Speed, settingOptions[Setting.Game_Speed].indexOf(`${this.scene.gameSpeed}x`) + 1); this.scene.gameData.saveSetting(Setting.Game_Speed, settingOptions[Setting.Game_Speed].indexOf(`${this.scene.gameSpeed}x`) + 1);
@ -154,6 +154,6 @@ export class UiInputs extends Phaser.Plugins.ScenePlugin {
(this.scene.ui.getHandler() as SettingsUiHandler).show([]); (this.scene.ui.getHandler() as SettingsUiHandler).show([]);
} }
return [0, 0]; return [0, 0];
} }
} }