phoboslab / Impact

HTML5 Game Engine
MIT License
2k stars 201 forks source link

Weltmeister: New entities being spawned way off-screen #97

Closed Joncom closed 1 year ago

Joncom commented 1 year ago

There is a bug in Weltmeister, where sometimes, when adding a new entity by clicking an entity name from the entity spawn menu, it spawns in way off-screen. It's a jarring experience because you have to look at the x/y coordinates in the properties menu, and then hunt around until you find the entity, then bring it back to where you tried to spawn it.

How to reproduce this:

  1. Create an entity with a relatively large width or height (ex. 1000 px).
  2. Click on it to select it (it's properties should appear in the right side menu).
  3. Then click on some empty area to unselect it.
  4. Now try spawning a new, small, regular size entity.
  5. Your new entity just spawned in way over to the left or above.

Why does this happen? Because when selecting an existing entity (via the selectEntityAt function), selector.offset gets set to the difference between where you clicked and the top-left corner of the entity that you clicked.

https://github.com/phoboslab/Impact/blob/38465b77248696aa7ed173a38e6a00027157eaba/lib/weltmeister/edit-entities.js#L170-L173

This difference gets used to keep the entity under the cursor while dragging it around in the moveSelectedEntity function.

https://github.com/phoboslab/Impact/blob/38465b77248696aa7ed173a38e6a00027157eaba/lib/weltmeister/edit-entities.js#L268-L274

The problem is however, that this offset gets re-used when spawning a new entity, despite the fact that it corresponds to a different, previous selected entity. We can fix this issue by simply resetting selector.offset to 0 when spawning new entities.