How to update entity states and animations in a component-based game
Posted
by
mivic
on Game Development
See other posts from Game Development
or by mivic
Published on 2012-10-26T20:26:21Z
Indexed on
2012/10/26
23:18 UTC
Read the original article
Hit count: 293
I'm trying to design a component-based entity system for learning purposes (and later use on some games) and I'm having some troubles when it comes to updating entity states.
I don't want to have an update() method inside the Component to prevent dependencies between Components.
What I currently have in mind is that components hold data and systems update components.
So, if I have a simple 2D game with some entities (e.g. player, enemy1, enemy 2) that have Transform, Movement, State, Animation and Rendering components I think I should have:
- A MovementSystem that moves all the Movement components and updates the State components
- And a RenderSystem that updates the Animation components (the animation component should have one animation (i.e. a set of frames/textures) for each state and updating it means selecting the animation corresponding to the current state (e.g. jumping, moving_left, etc), and updating the frame index). Then, the RenderSystem updates the Render components with the texture corresponding to the current frame of each entity's Animation and renders everything on screen.
I've seen some implementations like Artemis framework, but I don't know how to solve this situation:
Let's say that my game has the following entities. Each entity have a set of states and one animation for each state:
- player: "idle", "moving_right", "jumping"
- enemy1: "moving_up", "moving_down"
- enemy2: "moving_left", "moving_right"
What are the most accepted approaches in order to update the current state of each entity? The only thing that I can think of is having separate systems for each group of entities and separate State and Animation components so I would have PlayerState, PlayerAnimation, Enemy1State, Enemy1Animation... PlayerMovementSystem, PlayerRenderingSystem... but I think this is a bad solution and breaks the purpose of having a component-based system.
As you can see, I'm quite lost here, so I'd very much appreciate any help.
© Game Development or respective owner