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

8-direction move #15

Closed zewa666 closed 2 years ago

zewa666 commented 2 years ago

hi, let me first say its a great plugin. I'm using it together with the phaser-grid-engine plugin to build a simple top down rpg. Now it would be great if your plugin could also handle diagonal movement.

GaryStanton commented 2 years ago

I’m thrilled to hear you’re using the plugin for your game. I’m not sure what you’re asking for however, diagonal movement is already supported! Can you tell me a bit more about what you’re trying to do? Perhaps show some code?

zewa666 commented 2 years ago

well I didnt see any diagonal example in your demo so I honestly have no clue how its supposed to work. Does it mean that in case of diagonal DOWN + RIGHT both directions would be TRUE?

GaryStanton commented 2 years ago

Yes, that’s one way of handling it. It depends how your game interprets what comes back in the data. There’s also a direction property that shows the compass points, that might be more helpful for you. I’m on the road at the moment so I can’t into the code right now, but dump out the player object and you should see a few different options. If you’re after an explicit keybinding for diagonals, that might be a bit more tricky… I could look at adding that if that’s what you’re after.

zewa666 commented 2 years ago

Holy moly that worked like a charm. Thanks @GaryStanton for the hint with the compass points, the BEARING was what I've been missing. The final code looks something like this:

    if (this.playerControls.direction.DOWN) {
      this.gridEngine.move(PLAYER_KEY, Direction.DOWN);
    }
    if (this.playerControls.direction.UP) {
      this.gridEngine.move(PLAYER_KEY, Direction.UP);
    }
    if (this.playerControls.direction.LEFT) {
      this.gridEngine.move(PLAYER_KEY, Direction.LEFT);
    }
    if (this.playerControls.direction.RIGHT) {
      this.gridEngine.move(PLAYER_KEY, Direction.RIGHT);
    }
    switch (this.playerControls.direction.BEARING) {
      case "SE":
        this.gridEngine.move(PLAYER_KEY, Direction.DOWN_RIGHT);
        break;
      case "NE":
        this.gridEngine.move(PLAYER_KEY, Direction.UP_RIGHT);
        break;
      case "SW":
        this.gridEngine.move(PLAYER_KEY, Direction.DOWN_LEFT);
        break;
      case "NW":
        this.gridEngine.move(PLAYER_KEY, Direction.UP_LEFT);
        break;
    }