Mercurial > molko
comparison libmlk-rpg/mlk/rpg/battle-state-check.c @ 434:4e78f045e8c0
rpg: cleanup hierarchy
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 15 Oct 2022 21:24:17 +0200 |
parents | src/libmlk-rpg/rpg/battle-state-check.c@8f59201dc76b |
children | 25a56ca53ac2 |
comparison
equal
deleted
inserted
replaced
433:862b15c3a3ae | 434:4e78f045e8c0 |
---|---|
1 /* | |
2 * battle-state-check.c -- battle state (check status) | |
3 * | |
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <assert.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 | |
23 #include <mlk/core/alloc.h> | |
24 #include <mlk/core/panic.h> | |
25 #include <mlk/core/sprite.h> | |
26 #include <mlk/core/texture.h> | |
27 #include <mlk/core/trace.h> | |
28 | |
29 #include "battle-state-check.h" | |
30 #include "battle-state-lost.h" | |
31 #include "battle-state-victory.h" | |
32 #include "battle-state.h" | |
33 #include "battle.h" | |
34 #include "character.h" | |
35 | |
36 struct fadeout { | |
37 struct character *ch; | |
38 int x; | |
39 int y; | |
40 struct drawable dw; | |
41 unsigned int alpha; | |
42 unsigned int elapsed; | |
43 }; | |
44 | |
45 static int | |
46 fadeout_update(struct drawable *dw, unsigned int ticks) | |
47 { | |
48 struct fadeout *fade = dw->data; | |
49 | |
50 fade->elapsed += ticks; | |
51 | |
52 if (fade->elapsed >= 8) { | |
53 fade->elapsed = 0; | |
54 | |
55 if (fade->alpha == 0) | |
56 return 1; | |
57 | |
58 fade->alpha -= 10; | |
59 } | |
60 | |
61 return 0; | |
62 } | |
63 | |
64 static void | |
65 fadeout_draw(struct drawable *dw) | |
66 { | |
67 const struct fadeout *fade = dw->data; | |
68 struct sprite *sprite = fade->ch->sprites[CHARACTER_SPRITE_NORMAL]; | |
69 | |
70 texture_set_alpha_mod(sprite->texture, fade->alpha); | |
71 sprite_draw(sprite, 0, 0, fade->x, fade->y); | |
72 texture_set_alpha_mod(sprite->texture, 255); | |
73 } | |
74 | |
75 static void | |
76 fadeout_finish(struct drawable *dw) | |
77 { | |
78 free(dw->data); | |
79 } | |
80 | |
81 static void | |
82 fadeout(struct battle *bt, struct battle_entity *et) | |
83 { | |
84 struct fadeout *fade; | |
85 | |
86 if (!bt->effects) { | |
87 tracef("can't create a fadeout effect without a drawable_stack"); | |
88 return; | |
89 } | |
90 | |
91 fade = alloc_new0(sizeof (*fade)); | |
92 fade->ch = et->ch; | |
93 fade->x = et->x; | |
94 fade->y = et->y; | |
95 fade->alpha = 250; | |
96 fade->dw.data = fade; | |
97 fade->dw.draw = fadeout_draw; | |
98 fade->dw.update = fadeout_update; | |
99 fade->dw.finish = fadeout_finish; | |
100 | |
101 if (drawable_stack_add(bt->effects, &fade->dw) < 0) | |
102 free(fade); | |
103 } | |
104 | |
105 static int | |
106 is_dead(const struct battle *bt) | |
107 { | |
108 const struct battle_entity *et; | |
109 | |
110 BATTLE_TEAM_FOREACH(bt, et) { | |
111 if (!character_ok(et->ch)) | |
112 continue; | |
113 if (et->ch->hp > 0) | |
114 return 0; | |
115 } | |
116 | |
117 return 1; | |
118 } | |
119 | |
120 static int | |
121 is_won(const struct battle *bt) | |
122 { | |
123 const struct battle_entity *et; | |
124 | |
125 BATTLE_ENEMY_FOREACH(bt, et) | |
126 if (character_ok(et->ch)) | |
127 return 0; | |
128 | |
129 return 1; | |
130 } | |
131 | |
132 static void | |
133 clean(struct battle *bt) | |
134 { | |
135 for (size_t i = 0; i < bt->enemiesz; ++i) { | |
136 if (bt->enemies[i] && character_ok(bt->enemies[i]->ch) && bt->enemies[i]->ch->hp == 0) { | |
137 fadeout(bt, bt->enemies[i]); | |
138 bt->enemies[i] = NULL; | |
139 } | |
140 } | |
141 } | |
142 | |
143 static int | |
144 update(struct battle_state *st, struct battle *bt, unsigned int ticks) | |
145 { | |
146 (void)st; | |
147 (void)ticks; | |
148 | |
149 battle_state_check_update(bt); | |
150 | |
151 return 0; | |
152 } | |
153 | |
154 static void | |
155 draw(const struct battle_state *st, const struct battle *bt) | |
156 { | |
157 (void)st; | |
158 | |
159 battle_state_check_draw(bt); | |
160 } | |
161 | |
162 static void | |
163 finish(struct battle_state *st, struct battle *bt) | |
164 { | |
165 (void)bt; | |
166 | |
167 free(st); | |
168 } | |
169 | |
170 void | |
171 battle_state_check_update(struct battle *bt) | |
172 { | |
173 assert(battle_ok(bt)); | |
174 | |
175 clean(bt); | |
176 | |
177 if (is_dead(bt)) | |
178 battle_state_lost(bt); | |
179 else if (is_won(bt)) | |
180 battle_state_victory(bt); | |
181 else | |
182 battle_next(bt); | |
183 } | |
184 | |
185 void | |
186 battle_state_check_draw(const struct battle *bt) | |
187 { | |
188 assert(battle_ok(bt)); | |
189 | |
190 battle_draw_component(bt, BATTLE_COMPONENT_ALL); | |
191 } | |
192 | |
193 void | |
194 battle_state_check(struct battle *bt) | |
195 { | |
196 assert(battle_ok(bt)); | |
197 | |
198 struct battle_state *self; | |
199 | |
200 self = alloc_new0(sizeof (*self)); | |
201 self->data = bt; | |
202 self->update = update; | |
203 self->draw = draw; | |
204 self->finish = finish; | |
205 | |
206 battle_switch(bt, self); | |
207 } |