Best way to transform this OO design into a functional one?


So I'm working on a small web-based game (with a rust backend and websockets) and I am having some issue doing a clean design (well, I have one that would work in an OOP language).

Basically, I've got a bunch of units, and those units might fire projectiles. For now, I have only one type of unit, but I wanted to implement a more generic form of my projectile logic (to avoid refactoring all of it later).

In OOP, I would have simply an abstract class Projectile, with a bunch of attributes common to all children (damage, coordinates, speed, id...). And then a bunch of methods that might (or might not) be specific for each children ( canHitAllies(), getBoudingBox() etc).

Is the only way to rethink this design in rust to create a trait ProjectileTrait which I'll reimplement for each Projectile struct ? While it would work (with a bunch of Box everywhere, but I can live with that), I'll have to basically implement most of my attributes into getters because I can't link my attributes with my data type.

What are your thoughts on this ? Am I too stuck with the OO approach ?

Some games adapt a so called entity component system, for managing objects in a game.

There are a couple of libraries in rust, which can make your life easier like a
entity component system framework. It is also used within the amethyst game engine project.

Basically ECS separates your game object into called components. A entity car might consist out of the components:

  • Driveable
  • Damage
  • Storage

It is possible to attach whatever components you like to a entity. The components just contain the data relevant to it (Damage might just track taken damage in % with a uint8).

The actual behavior is defined within systems, which go trough every entity and each component of them, and progress the state of the game forward.

Specs makes this really efficient, and in general the pattern is really pleasant to use / extend / refactor.