summaryrefslogtreecommitdiff
path: root/sys/src/games/sokoban/animation.c
blob: 147012b2f77880fb742cd7bea78958cdc18d07cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <u.h>
#include <libc.h>
#include <draw.h>

#include "sokoban.h"

void
initanimation(Animation *a)
{
	if (a == nil)
		return;

	memset(a, 0, sizeof(Animation));
}

void
setupanimation(Animation *a, Route *r)
{
	if (a == nil || r == nil || r->step == nil)
		return;

	a->route = r;
	a->step = r->step;
	if (a->step < a->route->step + a->route->nstep)
		a->count = a->step->count;
	else
		stopanimation(a);
}

int
onestep(Animation *a)
{
	if (a == nil)
		return 0;

	if (a->count > 0 && a->step != nil && a->route != nil) {
		move(a->step->dir);
		a->count--;
		if (a->count == 0) {
			a->step++;
			if (a->step < a->route->step + a->route->nstep)
				a->count = a->step->count;
			else
				stopanimation(a);
		}
	} else if (a->count > 0 && (a->step == nil || a->route == nil))
		stopanimation(a);
	return (a->count > 0);
}

void
stopanimation(Animation *a)
{
	if (a == nil)
		return;

	if (a->route != nil)
		freeroute(a->route);
	memset(a, 0, sizeof(Animation));
}