comparison examples/example/spell-fire.c @ 414:6947c1fefe5c

misc: extreme cleanup - Remove of Javascript binding for many reasons. - Put examples and tests in GNU make. - Create a library with shared example code.
author David Demelier <markand@malikania.fr>
date Sun, 09 Oct 2022 13:51:03 +0200
parents examples/example-battle/spell-fire.c@14ce7c4871e3
children 8f59201dc76b
comparison
equal deleted inserted replaced
413:222045c513ec 414:6947c1fefe5c
1 /*
2 * spell-fire.c -- example of spell: fire
3 *
4 * Copyright (c) 2020-2022 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/drawable.h>
24 #include <core/alloc.h>
25
26 #include <ui/align.h>
27
28 #include <rpg/battle-state-check.h>
29 #include <rpg/battle-state-rendering.h>
30 #include <rpg/battle.h>
31 #include <rpg/character.h>
32 #include <rpg/spell.h>
33
34 #include "registry.h"
35 #include "spell-fire.h"
36
37 struct self {
38 struct battle *battle;
39 struct character *target;
40 struct animation animation;
41 struct drawable drawable;
42 unsigned int selection;
43 };
44
45 static int
46 update(struct drawable *dw, unsigned int ticks)
47 {
48 struct self *self = dw->data;
49
50 return animation_update(&self->animation, ticks);
51 }
52
53 static void
54 draw(struct drawable *dw)
55 {
56 const struct self *self = dw->data;
57 const struct battle_entity *et = self->battle->enemies[self->selection];
58 const struct sprite *sprite = et->ch->sprites[CHARACTER_SPRITE_NORMAL];
59 int x, y;
60
61 align(ALIGN_CENTER,
62 &x, &y, self->animation.sprite->cellw, self->animation.sprite->cellh,
63 et->x, et->y, sprite->cellw, sprite->cellh);
64
65 animation_draw(&self->animation, x, y);
66 }
67
68 static void
69 end(struct drawable *dw)
70 {
71 struct self *self = dw->data;
72 struct character *ch = self->battle->enemies[self->selection]->ch;
73
74 /* TODO: compute damage. */
75 const unsigned int damage = 100;
76
77 if ((unsigned int)ch->hp < damage)
78 ch->hp = 0;
79 else
80 ch->hp -= damage;
81
82 battle_indicator_hp(self->battle, self->battle->enemies[self->selection]->ch, 100);
83 battle_state_check(self->battle);
84 }
85
86 static void
87 finish(struct drawable *dw)
88 {
89 free(dw->data);
90 }
91
92 static void
93 fire_select(const struct battle *bt, struct selection *slt)
94 {
95 slt->index_side = 0;
96
97 selection_first(slt, bt);
98 }
99
100 static void
101 fire_action(struct battle *bt, struct character *owner, const struct selection *slt)
102 {
103 struct self *self;
104
105 (void)owner;
106
107 self = alloc_new0(sizeof (*self));
108 self->selection = slt->index_character;
109 self->battle = bt;
110 self->drawable.data = self;
111 self->drawable.update = update;
112 self->drawable.draw = draw;
113 self->drawable.finish = finish;
114 self->drawable.end = end;
115
116 animation_init(&self->animation, &registry_sprites[REGISTRY_TEXTURE_EXPLOSION], 12);
117 animation_start(&self->animation);
118
119 sound_play(&registry_sounds[REGISTRY_SOUND_FIRE]);
120 battle_state_rendering(bt, &self->drawable);
121 }
122
123 const struct spell spell_fire = {
124 .name = "Fire",
125 .description = "A delicate fire.",
126 .mp = 5,
127 .type = SPELL_TYPE_FIRE,
128 .select = fire_select,
129 .select_kind = SELECTION_KIND_ONE,
130 .select_side = SELECTION_SIDE_ENEMY,
131 .action = fire_action
132 };