Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. (def world
  2. "The world as it is this frame"
  3. (atom {}))
  4.  
  5. (def old-world
  6. "The world as it was last frame"
  7. (atom {}))
  8.  
  9. (def world-fns
  10. "Vector of fns to run against the world before updates."
  11. (atom []))
  12.  
  13. (defn update-entity
  14. "Return new entity running all logic for given entity"
  15. [entity world]
  16. (reduce
  17. (fn [entity* logic]
  18. (logic entity* world))
  19. entity
  20. (or (entity :logic) [])))
  21.  
  22. (defn update-world
  23. "Return new world after running all logic on all entities"
  24. [world]
  25. (reduce
  26. (fn [world* [id entity]]
  27. (assoc world* id (update-entity entity world)))
  28. {}
  29. world))
  30.  
  31. (defcomponent World []
  32. (Update [this]
  33. (doseq [f @world-fns]
  34. (f @world))
  35. (reset! old-world @world)
  36. (swap! world update-world)))
  37.  
  38. (defn present!
  39. "Run all presentation logic for a given entity, new entity, and game object"
  40. [entity old-entity go]
  41. (doseq [f (or (entity :presentation) [])]
  42. (f entity old-entity go)))
  43.  
  44. (defcomponent Entity [id]
  45. (Update [this]
  46. (let [entity (@world id)
  47. old-entity (@old-world id)]
  48. (present! entity old-entity this))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement