GaryStanton / phaser3-merged-input

A Phaser 3 plugin to map input from keyboard & gamepad to player actions
MIT License
34 stars 5 forks source link

Not Handling Gamepad Inputs Correctly? #43

Closed xanmankey closed 6 months ago

xanmankey commented 1 year ago

First of all, cool plugin! It's nice to have the support for gamepads, keyboards, and pointers all in one place. I don't think I'm using the plugin right though, as although key inputs are being registered, gamepad inputs are not. I tested my controller using https://hardwaretester.com/gamepad, so it's not that. Here's my code being called in update:

function updatePlayer(scene: CustomScene): void {
    // Handle movement and animation
    // Interaction is blocking
    if (scene.player.isInteracting) {
        scene.player.anims.stop();
        return;
    }

    if (scene.player.playerInput.direction.UP && !scene.player.playerInput.direction.DOWN) {
        // Move player up
        scene.player.changePos({ y: -3 });
        // Animate player
        if (scene.player.playerInput.direction.LEFT && !scene.player.playerInput.direction.RIGHT) {
            scene.player.changePos({ x: -3 });
            scene.player.anims.play("walkForwardLeft", true);
        } else if (scene.player.playerInput.direction.RIGHT && !scene.player.playerInput.direction.LEFT) {
            scene.player.changePos({ x: 3 });
            scene.player.anims.play("walkForwardRight", true);
        } else {
            scene.player.anims.play("walkForward", true);
        }
    } else if (scene.player.playerInput.direction.DOWN && !scene.player.playerInput.direction.UP) {
        // Move player down
        scene.player.changePos({ y: 3 });
        // Animate player
        if (scene.player.playerInput.direction.LEFT && !scene.player.playerInput.direction.RIGHT) {
            scene.player.changePos({ x: -3 });
            scene.player.anims.play("walkBackwardLeft", true);
        } else if (scene.player.playerInput.direction.RIGHT && !scene.player.playerInput.direction.LEFT) {
            scene.player.changePos({ x: 3 });
            scene.player.anims.play("walkBackwardRight", true);
        } else {
            scene.player.anims.play("walkBackward", true);
        }
    } else if (scene.player.playerInput.direction.LEFT && !scene.player.playerInput.direction.RIGHT) {
        // Move player left
        scene.player.changePos({ x: -3 });
        // Animate player
        scene.player.anims.play("walkLeft", true);
    } else if (scene.player.playerInput.direction.RIGHT && !scene.player.playerInput.direction.LEFT) {
        // Move player right
        scene.player.changePos({ x: 3 });
        // Animate player
        scene.player.anims.play("walkRight", true);
    } else {
        // Stop player animation when no keys are pressed
        scene.player.anims.stop();
    }
    // Instead of a listener, add a manual check for handling interactions here
    if (scene.player.playerInput.interaction_mapped.isPressed("RC_S")) {
        handleInteraction(scene);
    }
}

Where each scene contains a custom Player object player and that player object has a Player member (aliased as PlayerInput) named playerInput. The plugin is initialized in preload and the inputs for the MergedInput member mergedInputs of the current scene are set in create.

Here's the code used to set the inputs as well:

function initializeKeys(scene: CustomScene, playerIndex: number) {
  const mergedInputPlugin = scene.plugins.get("mergedInput");
  if (mergedInputPlugin instanceof MergedInput) {
    scene.mergedInput = mergedInputPlugin;
  }
  // Supporting multiple different potential inputs for both gamepad and controller
  scene.mergedInput.defineKey(playerIndex, "UP", "W", true);
  scene.mergedInput.defineKey(playerIndex, "UP", "UP", true);
  scene.mergedInput.defineKey(playerIndex, "LEFT", "A", true);
  scene.mergedInput.defineKey(playerIndex, "LEFT", "LEFT", true);
  scene.mergedInput.defineKey(playerIndex, "DOWN", "S", true);
  scene.mergedInput.defineKey(playerIndex, "DOWN", "DOWN", true);
  scene.mergedInput.defineKey(playerIndex, "RIGHT", "D", true);
  scene.mergedInput.defineKey(playerIndex, "RIGHT", "RIGHT", true);
  scene.mergedInput.defineKey(playerIndex, "RC_S", "F", true);
  scene.mergedInput.defineKey(playerIndex, "RC_S", "E", true);
}

It's not a super pressing issue, it's likely that I just configured something wrong regarding gamepad inputs, but I figured I'd reach out and ask all the same.

Also I cloned the repository and then tried to Install with npm install, then use npm run dev to spin up a development server and run the demo scene in an attempt to see if my controller was working with the demo scene, but that errored out with the following error


> phaser3-merged-input@1.6.1 dev
> webpack --config webpack.demo.config.js

webpack is watching the files…

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/util/createHash.js:90:53)
    at NormalModule._initBuildHash (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/NormalModule.js:402:16)
    at handleParseError (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/NormalModule.js:450:10)
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/NormalModule.js:482:5
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/NormalModule.js:343:12
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:373:3
    at iterateNormalLoaders (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
    at iterateNormalLoaders (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:221:10)
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:236:3
    at runSyncOrAsync (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:130:11)
    at iterateNormalLoaders (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:232:2)
    at Array.<anonymous> (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
    at Storage.finished (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:43:16)
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:79:9
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/graceful-fs/graceful-fs.js:90:16
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)
/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:133
                if(isError) throw e;
                            ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/util/createHash.js:90:53)
    at NormalModule._initBuildHash (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/NormalModule.js:402:16)
    at handleParseError (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/NormalModule.js:450:10)
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/NormalModule.js:482:5
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/webpack/lib/NormalModule.js:343:12
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:373:3
    at iterateNormalLoaders (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
    at iterateNormalLoaders (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:221:10)
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:236:3
    at context.callback (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
    at module.exports (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/babel-loader/lib/index.js:182:8)
    at LOADER_EXECUTION (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:119:14)
    at runSyncOrAsync (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:120:4)
    at iterateNormalLoaders (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:232:2)
    at Array.<anonymous> (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
    at Storage.finished (/home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:43:16)
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:79:9
    at /home/xanmankey/Programming/Web/temp/phaser3-merged-input/node_modules/graceful-fs/graceful-fs.js:90:16
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3) {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

Node.js v18.16.0

It's very possible I have something configured differently on my end or there's an environmental difference, but I just wanted to bring it up all the same.

GaryStanton commented 1 year ago

Hi, glad you like the plugin!

Sorry you're having trouble getting things working. I've seen the error you're getting when running the demo, but my understanding of node, webpack, et al, is limited... It seems related to an SSL issue that has been patched in newer versions of Node. I found a post on StackOverflow that talks about this: https://stackoverflow.com/questions/69692842/error-message-error0308010cdigital-envelope-routinesunsupported

Chances are you're using a newer version of node... lots of people suggest downgrading, but you'd probably be better off updating packages. Let me know how you get on!

With regard to your issue of not being able to pick up gamepad input, it's tough to diagnose without being able to do a bit of debugging. Have you set up your player objects by calling addPlayer()? If you can get a demo up and running, I'd be happy to take a look.