comparison examples/example-battle/main.c @ 209:23a844fdc911

examples: move all into subdirectories, closes #2513
author David Demelier <markand@malikania.fr>
date Wed, 11 Nov 2020 17:10:40 +0100
parents examples/example-battle.c@133926e08d6e
children befa2e855d3b
comparison
equal deleted inserted replaced
208:c0e0d4accae8 209:23a844fdc911
1 /*
2 * example-battle.c -- show how to use battle
3 *
4 * Copyright (c) 2020 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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <core/alloc.h>
25 #include <core/core.h>
26 #include <core/event.h>
27 #include <core/game.h>
28 #include <core/image.h>
29 #include <core/painter.h>
30 #include <core/panic.h>
31 #include <core/sprite.h>
32 #include <core/state.h>
33 #include <core/sys.h>
34 #include <core/texture.h>
35 #include <core/util.h>
36 #include <core/window.h>
37
38 #include <ui/align.h>
39 #include <ui/label.h>
40 #include <ui/theme.h>
41 #include <ui/ui.h>
42
43 #include <rpg/character.h>
44 #include <rpg/battle.h>
45 #include <rpg/rpg.h>
46 #include <rpg/spell.h>
47
48 #include "registry.h"
49 #include "spell-fire.h"
50
51 #define W 1280
52 #define H 720
53
54 static void
55 adventurer_reset(struct character *ch)
56 {
57 /* TODO: this function should compute the attack thanks to the level. */
58 ch->hpmax = 120;
59 ch->mpmax = 50;
60 ch->atk = 50;
61 ch->def = 50;
62 ch->agt = 50;
63 ch->luck = 50;
64 }
65
66 static void
67 haunted_wood_reset(struct character *ch)
68 {
69 ch->hpmax = ch->hp = 2000;
70 ch->mpmax = ch->mp = 250;
71 ch->atk = 178;
72 ch->def = 80;
73 ch->agt = 80;
74 ch->luck = 100;
75 }
76
77 static void
78 black_cat_reset(struct character *ch)
79 {
80 ch->hpmax = ch->hp = 126;
81 ch->mpmax = ch->mp = 38;
82 ch->atk = 22;
83 ch->def = 19;
84 ch->agt = 21;
85 ch->luck = 14;
86 }
87
88 static struct character team[] = {
89 {
90 .name = "Molko",
91 .type = "Adventurer",
92 .level = 1,
93 .hp = 120,
94 .mp = 50,
95 .reset = adventurer_reset,
96 .sprite = &registry_sprites[REGISTRY_TEXTURE_JOHN],
97 .spells = {
98 &spell_fire
99 }
100 },
101 {
102 .name = "Fake Molko",
103 .type = "Adventurer",
104 .level = 1,
105 .hp = 120,
106 .mp = 50,
107 .reset = adventurer_reset,
108 .sprite = &registry_sprites[REGISTRY_TEXTURE_JOHN],
109 .spells = {
110 &spell_fire
111 }
112 }
113 };
114
115 static void
116 haunted_wood_strat(struct character *ch, struct battle *bt)
117 {
118 (void)ch;
119
120 /* TODO: Select randomly. */
121 battle_attack(bt, bt->order_cur->ch, bt->team[0].ch);
122 }
123
124 static void
125 black_cat_strat(struct character *ch, struct battle *bt)
126 {
127 (void)ch;
128
129 /* TODO: Select randomly. */
130 battle_attack(bt, bt->order_cur->ch, bt->team[0].ch);
131 }
132
133 static struct character haunted_wood = {
134 .name = "Haunted Wood",
135 .type = "Wood",
136 .level = 30,
137 .reset = haunted_wood_reset,
138 .sprite = &registry_sprites[REGISTRY_TEXTURE_HAUNTED_WOOD],
139 .exec = haunted_wood_strat
140 };
141
142 static struct character black_cat = {
143 .name = "Black Cat",
144 .type = "Cat",
145 .level = 6,
146 .reset = black_cat_reset,
147 .sprite = &registry_sprites[REGISTRY_TEXTURE_BLACK_CAT],
148 .exec = black_cat_strat
149 };
150
151 static void
152 init(void)
153 {
154 if (!core_init() || !ui_init() || !rpg_init())
155 panic();
156 if (!window_open("Example - Battle", W, H))
157 panic();
158
159 registry_init();
160
161 /* Set cursor in default theme. */
162 theme_default()->sprites[THEME_SPRITE_CURSOR] = &registry_sprites[REGISTRY_TEXTURE_CURSOR];
163 }
164
165 static struct state fight_state;
166
167 static void
168 prepare_to_fight(void)
169 {
170 struct battle *bt = alloc_zero(1, sizeof (*bt));
171
172 // bt->enemies[0].ch = &haunted_wood;
173 bt->team[0].ch = &team[0];
174 bt->team[1].ch = &team[1];
175
176 /* Positionate the single ennemy to the left. */
177 align(ALIGN_LEFT,
178 &bt->enemies[0].x, &bt->enemies[0].y, haunted_wood.sprite->cellw, haunted_wood.sprite->cellh,
179 0, 0, window.w, window.h);
180
181 /* Black cat is near the previous monster. */
182 bt->enemies[1].ch = &black_cat;
183 bt->enemies[1].x = 500;
184 bt->enemies[1].y = 100;
185
186 battle_start(bt);
187
188 fight_state.data = bt;
189 game_switch(&fight_state, false);
190 }
191
192
193 static void
194 empty_handle(struct state *st, const union event *ev)
195 {
196 (void)st;
197
198 switch (ev->type) {
199 case EVENT_QUIT:
200 game_quit();
201 break;
202 case EVENT_KEYDOWN:
203 if (ev->key.key == KEY_SPACE)
204 prepare_to_fight();
205 break;
206 default:
207 break;
208 }
209 }
210
211 static void
212 empty_draw(struct state *st)
213 {
214 (void)st;
215
216 static const struct label info = {
217 .text = "Press <Space> to start a battle.",
218 .x = 10,
219 .y = 10,
220 .flags = LABEL_FLAGS_SHADOW
221 };
222
223 painter_set_color(0x4f8fbaff);
224 painter_clear();
225 label_draw(&info);
226 painter_present();
227 }
228
229 static struct state empty_state = {
230 .handle = empty_handle,
231 .draw = empty_draw
232 };
233
234 static void
235 fight_handle(struct state *st, const union event *ev)
236 {
237 battle_handle(st->data, ev);
238 }
239
240 static void
241 fight_update(struct state *st, unsigned int ticks)
242 {
243 struct battle *bt = st->data;
244
245 if (battle_update(bt, ticks))
246 game_switch(&empty_state, false);
247 }
248
249 static void
250 fight_draw(struct state *st)
251 {
252 painter_set_color(0x000000ff);
253 painter_clear();
254 battle_draw(st->data);
255 painter_present();
256 }
257
258 static void
259 fight_finish(struct state *st)
260 {
261 battle_finish(st->data);
262 free(st->data);
263 }
264
265 static struct state fight_state = {
266 .handle = fight_handle,
267 .update = fight_update,
268 .draw = fight_draw,
269 .finish = fight_finish,
270 };
271
272 static void
273 run(void)
274 {
275 game_switch(&empty_state, true);
276 game_loop();
277 }
278
279 static void
280 quit(void)
281 {
282 registry_finish();
283 theme_finish();
284 window_finish();
285 sys_finish();
286 }
287
288 int
289 main(int argc, char **argv)
290 {
291 --argc;
292 ++argv;
293
294 init();
295 run();
296 quit();
297
298 return 0;
299 }