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