Advertisement
Drakim

Component Example

Feb 22nd, 2012
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. Mission: We want entities to be rendered to show up on the screen.
  2.  
  3. The simplest way to code this would be to first make a EntityListComponent that holds all the entities, and have it call the event 'render' on each entity it holds, every frame. We would then make a RenderComponent that has a drawing-method tied to 'render' on each entity, that simply draws the pixelsoup specified in the RenderData at the PositionData coordinates.
  4.  
  5. On constructing the EntityListComponent
  6. *Make a list that can hold entities
  7. *Add the event 'render' on the world that invokes EntityListComponent.renderentities()
  8. *renderentities() loops over entity list and calls 'render' on each
  9.  
  10. On constructing the Entity
  11. *Add entity to EntityListComponent's list
  12.  
  13. On constructing the RenderComponent and giving it to an Entity
  14. *Find the PositionData and RenderData on this entity and cache them
  15. *Add the event 'render' on the entity that invokes RenderComponent.render()
  16.  
  17. It would work, but it wouldn't be the fastest system, as EntityListComponent would dispatch an 'render' for each entity, which would look up the methods associated with 'render' and call them. For 100 entities that's 100 Hash.get() and 100 method() calls.
  18.  
  19. However, with the newfound flexibility we can structure this differently. We don't even need a EntityListComponent anymore (we might need it for other things later, but let's drop it for now).
  20.  
  21. On constructing the Entity
  22. *Nothing
  23.  
  24. On constructing the RenderComponent and giving it to an Entity
  25. *Add rendercomponent to global static rendercomponent list
  26. *Add the event 'render' to the world that invokes a static global RenderComponent.renderall() method
  27. *renderall() loops the static rendercomponent list and draws their pixelsoups
  28.  
  29. Now rendering all 100 has turned into 1 Hash.get(), 1 method(), and 1 while() loop. We were able to break out of the "per entity" thinking.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement