Mercurial > molko
comparison libmlk-adventure/adventure/state/battle.c @ 286:3991779aaba9
adventure: initial test of spawn
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 23 Dec 2020 15:37:48 +0100 |
parents | |
children | 648f5f949afb |
comparison
equal
deleted
inserted
replaced
285:c43e39745cd8 | 286:3991779aaba9 |
---|---|
1 /* | |
2 * battle.c -- manage a 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 <stdlib.h> | |
21 | |
22 #include <core/alloc.h> | |
23 #include <core/game.h> | |
24 #include <core/painter.h> | |
25 #include <core/state.h> | |
26 | |
27 #include <rpg/battle.h> | |
28 | |
29 #include "battle.h" | |
30 | |
31 struct self { | |
32 struct battle *battle; | |
33 struct state state; | |
34 }; | |
35 | |
36 static void | |
37 start(struct state *state) | |
38 { | |
39 struct self *self = state->data; | |
40 | |
41 battle_start(self->battle); | |
42 } | |
43 | |
44 static void | |
45 handle(struct state *state, const union event *ev) | |
46 { | |
47 struct self *self = state->data; | |
48 | |
49 battle_handle(self->battle, ev); | |
50 } | |
51 | |
52 static void | |
53 update(struct state *state, unsigned int ticks) | |
54 { | |
55 struct self *self = state->data; | |
56 | |
57 /* TODO: once we have stacked states, pop it. */ | |
58 if (battle_update(self->battle, ticks)) | |
59 game_quit(); | |
60 } | |
61 | |
62 static void | |
63 draw(struct state *state) | |
64 { | |
65 struct self *self = state->data; | |
66 | |
67 painter_set_color(0xffffffff); | |
68 painter_clear(); | |
69 battle_draw(self->battle); | |
70 painter_present(); | |
71 } | |
72 | |
73 static void | |
74 finish(struct state *state) | |
75 { | |
76 struct self *self = state->data; | |
77 | |
78 battle_finish(self->battle); | |
79 | |
80 free(self->battle); | |
81 free(self); | |
82 } | |
83 | |
84 struct state * | |
85 state_battle_new(struct battle *bt) | |
86 { | |
87 assert(bt); | |
88 | |
89 struct self *self; | |
90 | |
91 self = alloc_new0(sizeof (*self)); | |
92 self->battle = bt; | |
93 self->state.data = self; | |
94 self->state.start = start; | |
95 self->state.handle = handle; | |
96 self->state.update = update; | |
97 self->state.draw = draw; | |
98 self->state.finish = finish; | |
99 | |
100 return &self->state; | |
101 } |