Mercurial > molko
comparison libmlk-adventure/adventure/action/spawner.c @ 273:e28429dbdaaf
adventure: uniform actions
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 12 Dec 2020 12:31:23 +0100 |
parents | a49ae1b6ea4f |
children | f89a53abb314 |
comparison
equal
deleted
inserted
replaced
272:a49ae1b6ea4f | 273:e28429dbdaaf |
---|---|
26 | 26 |
27 #include <rpg/map.h> | 27 #include <rpg/map.h> |
28 | 28 |
29 #include "spawner.h" | 29 #include "spawner.h" |
30 | 30 |
31 struct self { | 31 static inline unsigned int |
32 struct action action; | 32 distance(const struct spawner *s) |
33 struct map *map; | 33 { |
34 int last_x; | 34 unsigned int gap_x = fmax(s->last_x, s->map->player_x) - |
35 int last_y; | 35 fmin(s->last_x, s->map->player_x); |
36 unsigned int steps; | 36 unsigned int gap_y = fmax(s->last_y, s->map->player_y) - |
37 }; | 37 fmin(s->last_y, s->map->player_y); |
38 | 38 |
39 static inline unsigned int | 39 return fmin(s->steps, gap_x + gap_y); |
40 distance(const struct self *self) | |
41 { | |
42 unsigned int gap_x = fmax(self->last_x, self->map->player_x) - | |
43 fmin(self->last_x, self->map->player_x); | |
44 unsigned int gap_y = fmax(self->last_y, self->map->player_y) - | |
45 fmin(self->last_y, self->map->player_y); | |
46 | |
47 return fmin(self->steps, gap_x + gap_y); | |
48 } | 40 } |
49 | 41 |
50 static bool | 42 static bool |
51 update(struct action *act, unsigned int ticks) | 43 update(struct action *act, unsigned int ticks) |
52 { | 44 { |
53 (void)ticks; | 45 (void)ticks; |
54 | 46 |
55 struct self *self = act->data; | 47 struct spawner *s = act->data; |
56 | 48 |
57 if (self->map->player_movement) { | 49 if (s->map->player_movement) { |
58 self->steps -= distance(self); | 50 s->steps -= distance(s); |
59 self->last_x = self->map->player_x; | 51 s->last_x = s->map->player_x; |
60 self->last_y = self->map->player_y; | 52 s->last_y = s->map->player_y; |
61 | 53 |
62 if (self->steps == 0) { | 54 if (s->steps == 0) { |
55 s->steps = util_nrand(s->low, s->high); | |
56 | |
63 /* TODO: start battle here. */ | 57 /* TODO: start battle here. */ |
64 return false; | 58 return false; |
65 } | 59 } |
66 } | 60 } |
67 | 61 |
68 return false; | 62 return false; |
69 } | 63 } |
70 | 64 |
71 static void | 65 void |
72 finish(struct action *act) | 66 spawner_init(struct spawner *s) |
73 { | 67 { |
74 free(act->data); | 68 assert(s); |
69 | |
70 s->last_x = s->map->player_x; | |
71 s->last_y = s->map->player_y; | |
72 s->steps = util_nrand(s->low, s->high); | |
75 } | 73 } |
76 | 74 |
77 struct action * | 75 struct action * |
78 spawner_new(struct map *map, unsigned int low, unsigned int high) | 76 spawner_new(struct spawner *s) |
79 { | 77 { |
80 assert(map); | 78 assert(s); |
81 | 79 |
82 struct self *self; | 80 s->action.data = s; |
81 s->action.update = update; | |
83 | 82 |
84 self = alloc_new0(sizeof (*self)); | 83 return &s->action; |
85 self->map = map; | |
86 self->last_x = map->player_x; | |
87 self->last_y = map->player_y; | |
88 self->steps = util_nrand(low, high); | |
89 | |
90 self->action.data = self; | |
91 self->action.update = update; | |
92 self->action.finish = finish; | |
93 | |
94 return &self->action; | |
95 } | 84 } |