Annoraaq / grid-engine

A grid based movement engine compatible with Phaser3.
https://annoraaq.github.io/grid-engine/
Apache License 2.0
227 stars 19 forks source link

Grid engine failing to snap to grid after version >= 2.17.0 #306

Closed jonit-dev closed 1 year ago

jonit-dev commented 1 year ago

Hey folks!

I'm creating a MMORPG here and this lib is one of my critical libs. My character suddenly stopped to snap to the grid after I was updating my dependencies about 2 days ago. It took me almost 8 hours to realize that this was because I had a:

    "grid-engine": "^2.12.0",

On my package.json, so yarn was automatically updating it to the latest version (2.19.0)

Once I figured out the problem, I noticed that it started from 2.17.0 above.

Screenshot (1)

Screenshot (13)

Here's my implementation code:

  import { FromGridX, FromGridY, ToGridX, ToGridY } from '@rpg-engine/shared';
import { IComponent } from '../../abstractions/ComponentService';
import { Entity } from '../../abstractions/Entity';

export class EntityGrid implements IComponent {
  private gameObject: Entity;

  public init(targetObject: Entity) {
    this.gameObject = targetObject;
  }

  public awake() {
    // this whole offset and displayOrigin adjustments are required because our entity sprites are 32x32 while our tileset is 16x16

    this.gameObject.scene.grid.addCharacter({
      id: this.gameObject.id,
      sprite: this.gameObject,
      // walkingAnimationMapping: 6,
      startPosition: {
        x: ToGridX(this.gameObject.x),
        y: ToGridY(this.gameObject.y),
      },
      speed: this.gameObject.speed,
      charLayer: 'character',
      collides: false,
    });

    // make sure our sprite position and grid position are the same, to avoid inconsistencies
    const { x: gx, y: gy } = this.gameObject.scene.grid.getPosition(this.gameObject.id);
    this.gameObject.x = FromGridX(gx);
    this.gameObject.y = FromGridY(gy);
  }
}

My fix was rolling back to the 2.16.0 version.

Annoraaq commented 1 year ago

Is the issue, that the character is rendered with an offset? Or in other words: is only the pixel position wrong or also the tile position? And is the pixel position wrong by a constant value or does it change over time?

My first shot would be that there is something wrong with the offset.

If that is true, you could as a workaround (and alternative to falling back to an old version), play around with the custom character offset values.

jonit-dev commented 1 year ago

It's weird, because it's only a character related bug. And I didn't specify an offset anywhere. Do the grid engine automatically specify one?

My items are aligning correctly, so I assume the tiles are being rendered fine

Annoraaq commented 1 year ago

There is indeed a default offset given by GridEngine. However, it should not be different in the new version, so it seems to be a bug. I will have a closer look at this.

Thanks for pointing it out!

jonit-dev commented 1 year ago

Cool, thanks for being proactive.

Annoraaq commented 1 year ago

I just patched a bug related to the offset in v2.19.1. Can you check, if that solves your problem?

jonit-dev commented 1 year ago

@Annoraaq it seems to have solved. Thank you!