Guest User

Untitled

a guest
Jan 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. import system
  2.  
  3. # Entities that need animation get an AnimationComponent
  4. class AnimationComponent(object):
  5.  
  6. __slots__ = ('animations', 'enabled')
  7.  
  8. def __init__(self, *, animations=[]):
  9.  
  10. # List of currently applied (not necessarily running!) animations
  11. self.animations = animations
  12.  
  13. # Enable/disable animations with a single flag (i.e. for pausing the game)
  14. self.enabled = True
  15.  
  16. class AnimationSystem(system.System):
  17.  
  18. def __init__(self, mgr):
  19.  
  20. super().__init__(mgr)
  21.  
  22. # Boilerplate for the main Entity Manager to know how to interact with the AnimationSystem
  23. self.component = AnimationComponent
  24. self.component_name = 'Animation'
  25.  
  26. # Specifies the priority it gets to run.
  27. # Different Systems run on different tick rates, but if they occur on the frame update,
  28. self.priority = 25
  29.  
  30. def startup(self):
  31.  
  32. super().startup()
  33.  
  34. def shutdown(self):
  35.  
  36. super().shutdown()
  37.  
  38. def update(self, tick):
  39.  
  40. # Receive a "tick" which is a time delta from the last game loop
  41. super().update(tick)
  42.  
  43. # For each AnimationComponent and each active animation, update it.
  44. # self._data is an entity->component lookup table for every System
  45. entities = [ (entity, self._mgr.get_component(entity,'Render'), animation_comp.animations)
  46. for entity, animation_comp in self._data.items() if animation_comp.enabled ]
  47.  
  48. # Loop over the active animation entities
  49. for entity, render, animations in entities:
  50.  
  51. # Entities may have multiple simultaneous animations, run them in order
  52. for animation in animations:
  53. animation.update(tick)
  54.  
  55. def handle_event(self, event):
  56. pass
Add Comment
Please, Sign In to add comment