+ Component-based CodyNguyen Mobile Game Developer, Vinova VN firstname.lastname@example.org Entity system in Mobile Game Development
Start small: cute infinite fly game Gasboy
After that: a cooler (and bigger) game Skills God’s Rage Traps Stickman
As with any software getting more complex, architecture problems start to pop out
Game entities share capabilities among each others deal_damage_around deal_damage_around throwable activate_on_touch movable delay_on_activating deal_damage_around deal_damage_around throwable activate_on_touch movable movable delay_on_activating lie_on_ground lie_on_ground
Game entities share capabilities among each others Impossible to build a good OOP class hierarchy! natural relationships welcome changes code reuse
First solution comes to mind: mix-ins via multiple-inheritance Capability Moveable DealDamage ActivateOnTouch Throwable Thorn Cloud Bomb Bull 1. Just a small portion of the tree is a mess already 2. Multiple inheritance smells 3. Where to put logic that evolves object of different classes?
Need a better solution Component-based entity system has been a hot topic for years. It helps solve our problem in an elegant way!
Component-based entity system Multitude of ways to represent and implement This talk discusses one way which we found practical and easy to understand Three elements: Entity Component System
Component-based entity system Decompose game entities by capabilities into reusable components. Prefer aggregation over inheritance, i.e., entity has components, not is a type of some component.
Entity and Component Entity is: an unique ID + a list of components Component contains only data Velocity Damage Graphic ThrowAngle ActivateOnTouch Thorn x x x Cloud x x x Bomb x x x x Bull x x x x
Where does the logic go? The systems. M o v e m e n t S y s t e m Bul Thorn A n i m a t i o n S y s t e m Light l -ning P h y s i c s S y s t e m Stick -man Cloud C o l l i s i o n S y s t e m H e a l t h S y s t e m Game has a bunch of Entities are processed by entities systems
Entity system sum up Entity Is just a unique ID with a bunch of components Doesn’t need a separate Class for each entity Component Is the “ingredient” to make entities. Contains data only. System Contains game logic Run on every game loop, process entities who has a set of components it care about.
Other benefits of using entity system Goes hand-in-hand with Data-Driven development: Entities are defined as bags of components, in text file. “No engineer required”, game designer can create new or edit existing entities easily. Easy to change or fine-tune entities Promote multi-threading Systems are independent from one another, so they can run on separate threads