I'm writing a game using the Java Artemis-odb ECS library, and have been stuck on this design question for a few days: what is the proper way to store a function/command in an ECS?
The use-case is that pressing a key should activate the currently held item in the player's Equipment component. This would have an entityId linking it to an entity with a Useable component. The question I have is what should be stored in Usable that could preform a variety of effects (shooting a gun, gaining health etc.) Another consideration is that the component should ideally be serializable.
Right now, my current thought is to use the event system, and have Usable return an EventFactory which EquipmentSystem will call to get a new event & send it. This would be listened to by GunSystem or HealthSystem etc.
It seems a little gross though, and making an ItemEvent subclass, ItemEventFactory subclass, and ItemEventListener function seems more heavyweight than necessary.
Is there a better way to store an action/function inside a component?
Thanks!