animation.c 973 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include "sokoban.h"
  5. void
  6. initanimation(Animation *a)
  7. {
  8. if (a == nil)
  9. return;
  10. memset(a, 0, sizeof(Animation));
  11. }
  12. void
  13. setupanimation(Animation *a, Route *r)
  14. {
  15. if (a == nil || r == nil || r->step == nil)
  16. return;
  17. a->route = r;
  18. a->step = r->step;
  19. if (a->step < a->route->step + a->route->nstep)
  20. a->count = a->step->count;
  21. else
  22. stopanimation(a);
  23. }
  24. int
  25. onestep(Animation *a)
  26. {
  27. if (a == nil)
  28. return 0;
  29. if (a->count > 0 && a->step != nil && a->route != nil) {
  30. move(a->step->dir);
  31. a->count--;
  32. if (a->count == 0) {
  33. a->step++;
  34. if (a->step < a->route->step + a->route->nstep)
  35. a->count = a->step->count;
  36. else
  37. stopanimation(a);
  38. }
  39. } else if (a->count > 0 && (a->step == nil || a->route == nil))
  40. stopanimation(a);
  41. return (a->count > 0);
  42. }
  43. void
  44. stopanimation(Animation *a)
  45. {
  46. if (a == nil)
  47. return;
  48. if (a->route != nil)
  49. freeroute(a->route);
  50. memset(a, 0, sizeof(Animation));
  51. }