comparison examples/battle/spell-fire.c @ 192:4ad7420ab678

rpg: add minimalist battle system, continue #2477 @60h - Implement battle as states, - Add basic support for spells (no calculation yet), - Add won/lost state, - Add animations and messages, - Add order.
author David Demelier <markand@malikania.fr>
date Sat, 07 Nov 2020 16:00:39 +0100
parents
children d3ef968745f5
comparison
equal deleted inserted replaced
191:633a25df450e 192:4ad7420ab678
1 /*
2 * spell-fire.c -- example of spell: fire
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 <stdlib.h>
20
21 #include <core/action.h>
22 #include <core/animation.h>
23 #include <core/alloc.h>
24
25 #include <ui/align.h>
26
27 #include <rpg/battle.h>
28 #include <rpg/character.h>
29 #include <rpg/spell.h>
30
31 #include "registry.h"
32 #include "spell-fire.h"
33
34 struct data {
35 struct battle *battle;
36 struct animation animation;
37 struct action action;
38 unsigned int selection;
39 };
40
41 static bool
42 update(struct action *act, unsigned int ticks)
43 {
44 struct data *data = act->data;
45
46 return animation_update(&data->animation, ticks);
47 }
48
49 static void
50 draw(struct action *act)
51 {
52 const struct data *data = act->data;
53 const struct battle_entity *et = &data->battle->enemies[data->selection];
54 int x, y;
55
56 align(ALIGN_CENTER,
57 &x, &y, data->animation.sprite->cellw, data->animation.sprite->cellh,
58 et->x, et->y, et->ch->sprite->cellw, et->ch->sprite->cellh);
59
60 animation_draw(&data->animation, x, y);
61 }
62
63 static void
64 end(struct action *act)
65 {
66 struct data *data = act->data;
67 struct character *ch = data->battle->enemies[data->selection].ch;
68
69 /* TODO: compute damage. */
70 const unsigned int damage = 100;
71 if ((unsigned int)ch->hp < damage)
72 ch->hp = 0;
73 else
74 ch->hp -= damage;
75
76 battle_indicator_hp(data->battle, data->battle->enemies[data->selection].ch, 100);
77 }
78
79 static void
80 finish(struct action *act)
81 {
82 free(act->data);
83 }
84
85 static void
86 fire_action(struct battle *bt, struct character *owner, unsigned int selection)
87 {
88 struct data *data;
89
90 (void)owner;
91
92 data = alloc_zero(1, sizeof (*data));
93 data->battle = bt;
94 data->selection = selection;
95 data->action.data = data;
96 data->action.update = update;
97 data->action.draw = draw;
98 data->action.finish = finish;
99 data->action.end = end;
100
101 animation_init(&data->animation, &registry_sprites[REGISTRY_TEXTURE_EXPLOSION], 12);
102 animation_start(&data->animation);
103
104 sound_play(&registry_sounds[REGISTRY_SOUND_FIRE]);
105
106 action_stack_add(&bt->actions[0], &data->action);
107 }
108
109 const struct spell spell_fire = {
110 .name = "Fire",
111 .description = "A delicate fire.",
112 .mp = 5,
113 .type = SPELL_TYPE_FIRE,
114 .selection = SELECTION_ENEMY_ONE,
115 .action = fire_action
116 };