Mercurial > molko
comparison src/examples/example-battle/main.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | examples/example-battle/main.c@d01e83210ca2 |
children |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * example-battle.c -- show how to use battle | |
3 * | |
4 * Copyright (c) 2020-2021 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 = 11; | |
85 ch->luck = 14; | |
86 } | |
87 | |
88 static struct character team[] = { | |
89 { | |
90 .name = "Molko", | |
91 .level = 1, | |
92 .hp = 120, | |
93 .mp = 50, | |
94 .reset = adventurer_reset, | |
95 .sprites = { | |
96 [CHARACTER_SPRITE_NORMAL] = ®istry_sprites[REGISTRY_TEXTURE_JOHN_WALK], | |
97 [CHARACTER_SPRITE_SWORD] = ®istry_sprites[REGISTRY_TEXTURE_JOHN_SWORD], | |
98 }, | |
99 .spells = { | |
100 &spell_fire | |
101 } | |
102 }, | |
103 }; | |
104 | |
105 static void | |
106 haunted_wood_strat(struct character *ch, struct battle *bt) | |
107 { | |
108 (void)ch; | |
109 | |
110 /* TODO: Select randomly. */ | |
111 battle_attack(bt, bt->order_cur->ch, bt->team[0].ch); | |
112 } | |
113 | |
114 static void | |
115 black_cat_strat(struct character *ch, struct battle *bt) | |
116 { | |
117 (void)ch; | |
118 | |
119 /* TODO: Select randomly. */ | |
120 battle_attack(bt, bt->order_cur->ch, bt->team[0].ch); | |
121 } | |
122 | |
123 static struct character haunted_wood = { | |
124 .name = "Haunted Wood", | |
125 .level = 30, | |
126 .reset = haunted_wood_reset, | |
127 .sprites = { | |
128 [CHARACTER_SPRITE_NORMAL] = ®istry_sprites[REGISTRY_TEXTURE_HAUNTED_WOOD], | |
129 }, | |
130 .exec = haunted_wood_strat | |
131 }; | |
132 | |
133 static struct character black_cat = { | |
134 .name = "Black Cat", | |
135 .level = 6, | |
136 .reset = black_cat_reset, | |
137 .sprites = { | |
138 [CHARACTER_SPRITE_NORMAL] = ®istry_sprites[REGISTRY_TEXTURE_BLACK_CAT], | |
139 }, | |
140 .exec = black_cat_strat | |
141 }; | |
142 | |
143 static void | |
144 init(void) | |
145 { | |
146 if (core_init("fr.malikania", "battle") < 0 || ui_init() < 0 || rpg_init() < 0) | |
147 panic(); | |
148 if (window_open("Example - Battle", W, H) < 0) | |
149 panic(); | |
150 | |
151 registry_init(); | |
152 | |
153 /* Set cursor in default theme. */ | |
154 theme_default()->sprites[THEME_SPRITE_CURSOR] = ®istry_sprites[REGISTRY_TEXTURE_CURSOR]; | |
155 } | |
156 | |
157 static struct state fight_state; | |
158 | |
159 static void | |
160 prepare_to_fight(void) | |
161 { | |
162 struct battle *bt = alloc_new0(sizeof (*bt)); | |
163 | |
164 // bt->enemies[0].ch = &haunted_wood; | |
165 bt->team[0].ch = &team[0]; | |
166 //bt->team[1].ch = &team[1]; | |
167 | |
168 /* Positionate the single ennemy to the left. */ | |
169 align(ALIGN_LEFT, | |
170 &bt->enemies[0].x, &bt->enemies[0].y, | |
171 haunted_wood.sprites[CHARACTER_SPRITE_NORMAL]->cellw, | |
172 haunted_wood.sprites[CHARACTER_SPRITE_NORMAL]->cellh, | |
173 0, 0, window.w, window.h); | |
174 | |
175 /* Black cat is near the previous monster. */ | |
176 bt->enemies[1].ch = &black_cat; | |
177 bt->enemies[1].x = 500; | |
178 bt->enemies[1].y = 100; | |
179 | |
180 bt->background = ®istry_images[REGISTRY_IMAGE_BATTLE_BACKGROUND]; | |
181 | |
182 battle_start(bt); | |
183 | |
184 fight_state.data = bt; | |
185 game_push(&fight_state); | |
186 } | |
187 | |
188 | |
189 static void | |
190 empty_handle(struct state *st, const union event *ev) | |
191 { | |
192 (void)st; | |
193 | |
194 switch (ev->type) { | |
195 case EVENT_QUIT: | |
196 game_quit(); | |
197 break; | |
198 case EVENT_KEYDOWN: | |
199 if (ev->key.key == KEY_SPACE) | |
200 prepare_to_fight(); | |
201 break; | |
202 default: | |
203 break; | |
204 } | |
205 } | |
206 | |
207 static void | |
208 empty_draw(struct state *st) | |
209 { | |
210 (void)st; | |
211 | |
212 static const struct label info = { | |
213 .text = "Press <Space> to start a battle.", | |
214 .x = 10, | |
215 .y = 10, | |
216 .flags = LABEL_FLAGS_SHADOW | |
217 }; | |
218 | |
219 painter_set_color(0x4f8fbaff); | |
220 painter_clear(); | |
221 label_draw(&info); | |
222 painter_present(); | |
223 } | |
224 | |
225 static struct state empty_state = { | |
226 .handle = empty_handle, | |
227 .draw = empty_draw | |
228 }; | |
229 | |
230 static void | |
231 fight_handle(struct state *st, const union event *ev) | |
232 { | |
233 battle_handle(st->data, ev); | |
234 } | |
235 | |
236 static void | |
237 fight_update(struct state *st, unsigned int ticks) | |
238 { | |
239 struct battle *bt = st->data; | |
240 | |
241 if (battle_update(bt, ticks)) | |
242 game_push(&empty_state); | |
243 } | |
244 | |
245 static void | |
246 fight_draw(struct state *st) | |
247 { | |
248 painter_set_color(0x000000ff); | |
249 painter_clear(); | |
250 battle_draw(st->data); | |
251 painter_present(); | |
252 } | |
253 | |
254 static void | |
255 fight_finish(struct state *st) | |
256 { | |
257 battle_finish(st->data); | |
258 free(st->data); | |
259 } | |
260 | |
261 static struct state fight_state = { | |
262 .handle = fight_handle, | |
263 .update = fight_update, | |
264 .draw = fight_draw, | |
265 .finish = fight_finish, | |
266 }; | |
267 | |
268 static void | |
269 run(void) | |
270 { | |
271 game_push(&empty_state); | |
272 game_loop(); | |
273 } | |
274 | |
275 static void | |
276 quit(void) | |
277 { | |
278 registry_finish(); | |
279 theme_finish(); | |
280 window_finish(); | |
281 sys_finish(); | |
282 } | |
283 | |
284 int | |
285 main(int argc, char **argv) | |
286 { | |
287 --argc; | |
288 ++argv; | |
289 | |
290 init(); | |
291 run(); | |
292 quit(); | |
293 | |
294 return 0; | |
295 } |