flame-engine / flame

A Flutter based game engine.
https://flame-engine.org
MIT License
9.19k stars 899 forks source link

Make SpriteComponent respect anchor #3208

Closed livtanong closed 3 months ago

livtanong commented 3 months ago

What could be improved

Make SpriteComponent's render method respect the anchor.

SpriteComponent.render already makes use of sprite.render, which respects anchor. All that needs to be done is to pass anchor down to the sprite.render call.

Why should this be improved

It makes intuitive sense. I had to dig into the source to figure out why it wasn't behaving like I expected. I imagine I'm not alone.

Risks

  1. Behavior will change, and any project that uses SpriteComponent with an anchor that is not Anchor.topLeft will break.
  2. debugMode = true will look off because PositionComponent (which SpriteComponent inherits from) does not respect anchor either, and so renderDebugMode will still render a rectangle as if the anchor were at (0, 0).
  3. Likewise, containsLocalPoint will behave unexpectedly, as it also does not respect the anchor.

While (2) and (3) are risks associated with PositionComponent's behavior, I believe PositionComponent itself can stand to also update its behavior to respect anchor.

More information

For PositionComponent to respect anchor, what makes sense to me is to implement a bound or boundingBox getter, that returns a Rect that takes anchor into consideration. This bound can then be used by both containsLocalPoint and renderDebugMode in their respective implementations.

To mitigate the breaking change, a boolean flag can be set up for users to opt-in to this new behavior, along with a migration warning.

Other

spydon commented 3 months ago

The anchor already works like it should for SpriteComponent it is handled by the PositionComponent which SpriteComponent is a subclass of.

spydon commented 3 months ago

Read this section, it might help you understand how the anchor works in Flame components: https://docs.flame-engine.org/latest/flame/components.html#anchor

And otherwise, feel free to join our Discord if you have any further questions about it. :) https://discord.com/invite/pxrBmy4