master ohmu/planport / sokoban / animation.c
ohmu  ·  2026-09-07
 1#include <u.h>
 2#include <libc.h>
 3#include <draw.h>
 4
 5#include "sokoban.h"
 6
 7void
 8initanimation(Animation *a)
 9{
10	if (a == nil)
11		return;
12
13	memset(a, 0, sizeof(Animation));
14}
15
16void
17setupanimation(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
30int
31onestep(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
51void
52stopanimation(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}