comparison libmlk-rpg/rpg/battle.h @ 286:3991779aaba9

adventure: initial test of spawn
author David Demelier <markand@malikania.fr>
date Wed, 23 Dec 2020 15:37:48 +0100
parents 71b3b7036de7
children 63d9fb56c609
comparison
equal deleted inserted replaced
285:c43e39745cd8 286:3991779aaba9
34 #include "spell.h" 34 #include "spell.h"
35 35
36 union event; 36 union event;
37 37
38 struct character; 38 struct character;
39 struct inventory;
39 struct music; 40 struct music;
40 struct spell; 41 struct spell;
41 struct theme; 42 struct theme;
42 43
43 #define BATTLE_TEAM_MAX (4) /*!< Maximum team members. */ 44 #define BATTLE_TEAM_MAX (4)
44 #define BATTLE_ENEMY_MAX (8) /*!< Maximum enemies. */ 45 #define BATTLE_ENEMY_MAX (8)
45 #define BATTLE_ENTITY_MAX \ 46 #define BATTLE_ENTITY_MAX (BATTLE_TEAM_MAX + BATTLE_ENEMY_MAX)
46 (BATTLE_TEAM_MAX + BATTLE_ENEMY_MAX)
47 47
48 #define BATTLE_TEAM_FOREACH(bt, iter) \ 48 #define BATTLE_TEAM_FOREACH(bt, iter) \
49 for (size_t i = 0; i < BATTLE_TEAM_MAX && ((iter) = &(bt)->team[i]); ++i) 49 for (size_t i = 0; i < BATTLE_TEAM_MAX && ((iter) = &(bt)->team[i]); ++i)
50 #define BATTLE_ENEMY_FOREACH(bt, iter) \ 50 #define BATTLE_ENEMY_FOREACH(bt, iter) \
51 for (size_t i = 0; i < BATTLE_ENEMY_MAX && ((iter) = &(bt)->enemies[i]); ++i) 51 for (size_t i = 0; i < BATTLE_ENEMY_MAX && ((iter) = &(bt)->enemies[i]); ++i)
52 52
53 #define BATTLE_THEME(bt) ((bt)->theme ? (bt)->theme : theme_default()) 53 #define BATTLE_THEME(bt) ((bt)->theme ? (bt)->theme : theme_default())
54 54
55 /**
56 * \brief Generic battle status.
57 */
58 enum battle_status { 55 enum battle_status {
59 BATTLE_STATUS_NONE, /*!< Battle is not even started. */ 56 BATTLE_STATUS_NONE,
60 BATTLE_STATUS_RUNNING, /*!< Battle is running. */ 57 BATTLE_STATUS_RUNNING,
61 BATTLE_STATUS_WON, /*!< Battle has been won. */ 58 BATTLE_STATUS_WON,
62 BATTLE_STATUS_LOST, /*!< Battle has been lost. */ 59 BATTLE_STATUS_LOST,
63 }; 60 };
64 61
65 /**
66 * \brief Battle structure.
67 */
68 struct battle { 62 struct battle {
69 /**
70 * (+&?) Battle state.
71 */
72 struct battle_state *state; 63 struct battle_state *state;
73
74 /**
75 * (-) Battle status.
76 *
77 * This information is provided as final result once the battle has
78 * completed, modifying by the user won't change the battle routines.
79 */
80 enum battle_status status; 64 enum battle_status status;
81
82 /**
83 * (+) Member of team.
84 */
85 struct battle_entity team[BATTLE_TEAM_MAX]; 65 struct battle_entity team[BATTLE_TEAM_MAX];
86
87 /**
88 * (+) Ennemies to fight.
89 */
90 struct battle_entity enemies[BATTLE_ENEMY_MAX]; 66 struct battle_entity enemies[BATTLE_ENEMY_MAX];
91
92 /**
93 * (+&?) Order of playing.
94 */
95 struct battle_entity *order[BATTLE_ENTITY_MAX]; 67 struct battle_entity *order[BATTLE_ENTITY_MAX];
96
97 /**
98 * (-&?) Current entity playing.
99 */
100 struct battle_entity *order_cur; 68 struct battle_entity *order_cur;
101
102 /**
103 * (-) Current entity playing index.
104 */
105 size_t order_curindex; 69 size_t order_curindex;
106
107 /**
108 * (+&?) An optional background.
109 */
110 struct texture *background; 70 struct texture *background;
111
112 /**
113 * (+&?) A music to play.
114 *
115 * Music to play in the battle:
116 *
117 * - [0]: while the battle is running,
118 * - [1]: in case of victory,
119 * - [2]: in case of lost.
120 */
121 struct music *music[3]; 71 struct music *music[3];
122
123 /**
124 * (+&?) Theme to use.
125 */
126 struct theme *theme; 72 struct theme *theme;
127
128 /**
129 * (+) Stack of animations.
130 *
131 * The drawing animations are ran in parallel.
132 */
133 struct drawable_stack effects; 73 struct drawable_stack effects;
134
135 /**
136 * (+) Stack of actions.
137 *
138 * The array [0] contains actions that must complete to continue the
139 * battle, it can be used to write some messages while blocking the
140 * battle.
141 *
142 * The array [1] is a set of actions that run in "parallel" and don't
143 * prevent the battle from continuing.
144 */
145 struct action_stack actions[2]; 74 struct action_stack actions[2];
146 75 struct inventory *inventory;
147 /**
148 * (-) Bottom bar.
149 */
150 struct battle_bar bar; 76 struct battle_bar bar;
151 }; 77 };
152 78
153 /**
154 * Call this function before using the battle but after calling \a battle_init.
155 *
156 * If the team entities are positioned to 0, 0 they will be placed
157 * automatically.
158 *
159 * \pre bt != NULL
160 * \param bt the battle object
161 */
162 void 79 void
163 battle_start(struct battle *bt); 80 battle_start(struct battle *bt);
164 81
165 /**
166 * Select next member in battle.
167 *
168 * \pre bt != NULL
169 * \param bt the battle object
170 */
171 void 82 void
172 battle_next(struct battle *bt); 83 battle_next(struct battle *bt);
173 84
174 struct battle_entity * 85 struct battle_entity *
175 battle_find(struct battle *bt, const struct character *ch); 86 battle_find(struct battle *bt, const struct character *ch);
176 87
177 /**
178 * Change battle state.
179 *
180 * \pre bt != NULL
181 * \pre st != NULL
182 * \param bt the battle object
183 * \param st the state (referenced)
184 * \warning This will immediately close the current state and call finish field
185 * function if defined.
186 */
187 void 88 void
188 battle_switch(struct battle *bt, struct battle_state *st); 89 battle_switch(struct battle *bt, struct battle_state *st);
189 90
190 /**
191 * Compute battle ordering.
192 *
193 * \pre bt != NULL
194 * \param bt the battle object
195 */
196 void 91 void
197 battle_order(struct battle *bt); 92 battle_order(struct battle *bt);
198 93
199 /**
200 * All in one function to animate and compute damage.
201 *
202 * Use this function from the character AI or when a character from the team
203 * should attack an enemy.
204 *
205 * This will do the following in order:
206 *
207 * 1. Change battle state to "attacking"
208 * 2. Change battle entity state to "moving" or "blinking" if source is member of
209 * team or enemies respectively.
210 * 3. Wait until animations complete.
211 * 4. Compute and produce damage.
212 * 5. Change battle state to "check"
213 *
214 * \pre bt != NULL
215 * \pre source != NULL
216 * \pre target != NULL
217 * \param bt the battle object
218 * \param source the entity attacking
219 * \param target the target entity
220 */
221 void 94 void
222 battle_attack(struct battle *bt, 95 battle_attack(struct battle *bt,
223 struct character *source, 96 struct character *source,
224 struct character *target); 97 struct character *target);
225 98
226 /**
227 * Default function to cast a spell.
228 *
229 * Prefer to use this function to cast a spell because it performs some checks
230 * and hooks before casting the spell.
231 *
232 * \pre bt != NULL
233 * \pre source != NULL
234 * \pre spell != NULL
235 * \param bt the current battle
236 * \param source the entity casting a spell
237 * \param spell the spell used
238 * \param selection the selection
239 */
240 void 99 void
241 battle_cast(struct battle *bt, 100 battle_cast(struct battle *bt,
242 struct character *source, 101 struct character *source,
243 const struct spell *spell, 102 const struct spell *spell,
244 unsigned int selection); 103 unsigned int selection);
245 104
246 /**
247 * Spawn an indicator drawable to show damage.
248 *
249 * The drawable will draw the amount near the entity and fade away after
250 * several seconds.
251 *
252 * \pre bt != NULL
253 * \pre target != NULL
254 * \param bt the battle object
255 * \param target the target entity
256 * \param amount the amount of damage
257 */
258 void 105 void
259 battle_indicator_hp(struct battle *bt, const struct character *target, unsigned int amount); 106 battle_indicator_hp(struct battle *bt, const struct character *target, unsigned int amount);
260 107
261 /**
262 * Handle an event.
263 *
264 * \pre bt != NULL
265 * \pre ev != NULL
266 * \param bt the battle object
267 * \param ev the event
268 */
269 void 108 void
270 battle_handle(struct battle *bt, const union event *ev); 109 battle_handle(struct battle *bt, const union event *ev);
271 110
272 /**
273 * Update the battle.
274 *
275 * \pre bt != NULL
276 * \param bt the battle object
277 * \param ticks the number of milliseconds since last frame
278 */
279 bool 111 bool
280 battle_update(struct battle *bt, unsigned int ticks); 112 battle_update(struct battle *bt, unsigned int ticks);
281 113
282 /**
283 * Draw the battle.
284 *
285 * \pre bt != NULL
286 * \param bt the battle object
287 */
288 void 114 void
289 battle_draw(struct battle *bt); 115 battle_draw(struct battle *bt);
290 116
291 /**
292 * Finish and dispose resources if necessary.
293 *
294 * \pre bt != NULL
295 * \param bt the battle object
296 */
297 void 117 void
298 battle_finish(struct battle *bt); 118 battle_finish(struct battle *bt);
299 119
300 #endif /* MOLKO_RPG_BATTLE_H */ 120 #endif /* MOLKO_RPG_BATTLE_H */