Mercurial > molko
comparison libmlk-rpg/mlk/rpg/battle-entity.c @ 434:4e78f045e8c0
rpg: cleanup hierarchy
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 15 Oct 2022 21:24:17 +0200 |
parents | src/libmlk-rpg/rpg/battle-entity.c@862b15c3a3ae |
children | 773a082f0b91 |
comparison
equal
deleted
inserted
replaced
433:862b15c3a3ae | 434:4e78f045e8c0 |
---|---|
1 /* | |
2 * battle-entity.c -- in game battle entity | |
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 <assert.h> | |
20 | |
21 #include <mlk/core/sprite.h> | |
22 #include <mlk/core/texture.h> | |
23 | |
24 #include <mlk/ui/theme.h> | |
25 | |
26 #include "battle.h" | |
27 #include "battle-entity.h" | |
28 #include "battle-entity-state.h" | |
29 #include "battle-entity-state-normal.h" | |
30 #include "character.h" | |
31 | |
32 static void | |
33 draw_name(const struct battle_entity *et, const struct battle *bt) | |
34 { | |
35 struct label label = et->name; | |
36 | |
37 label.theme = BATTLE_THEME(bt); | |
38 | |
39 if (et == battle_current(bt)) | |
40 label.flags |= LABEL_FLAGS_SELECTED; | |
41 else | |
42 label.flags &= ~LABEL_FLAGS_SELECTED; | |
43 | |
44 label_draw(&label); | |
45 } | |
46 | |
47 void | |
48 battle_entity_init(struct battle_entity *et) | |
49 { | |
50 assert(et); | |
51 | |
52 character_reset(et->ch); | |
53 texture_set_alpha_mod(et->ch->sprites[CHARACTER_SPRITE_NORMAL]->texture, 255); | |
54 | |
55 battle_entity_state_normal(et); | |
56 } | |
57 | |
58 int | |
59 battle_entity_ok(const struct battle_entity *et) | |
60 { | |
61 return et && character_ok(et->ch); | |
62 } | |
63 | |
64 void | |
65 battle_entity_switch(struct battle_entity *et, struct battle_entity_state *st) | |
66 { | |
67 assert(et); | |
68 assert(st); | |
69 | |
70 if (et->state) | |
71 battle_entity_state_finish(et->state, et); | |
72 | |
73 et->state = st; | |
74 } | |
75 | |
76 int | |
77 battle_entity_update(struct battle_entity *et, unsigned int ticks) | |
78 { | |
79 assert(et); | |
80 | |
81 return battle_entity_state_update(et->state, et, ticks); | |
82 } | |
83 | |
84 void | |
85 battle_entity_draw_sprite(const struct battle_entity *et) | |
86 { | |
87 struct sprite *sprite = et->ch->sprites[CHARACTER_SPRITE_NORMAL]; | |
88 int row; | |
89 | |
90 /* | |
91 * Ennemies are usually defined with a single image as such the | |
92 * sprite may contain only one cell/row. Otherwise if the user | |
93 * have provided a structured sprite, use appropriate row. | |
94 */ | |
95 if (sprite->nrows >= 6) | |
96 row = 6; | |
97 else | |
98 row = 0; | |
99 | |
100 sprite_draw(sprite, row, 0, et->x, et->y); | |
101 } | |
102 | |
103 void | |
104 battle_entity_draw(const struct battle_entity *et, const struct battle *bt) | |
105 { | |
106 assert(et); | |
107 assert(bt); | |
108 | |
109 draw_name(et, bt); | |
110 battle_entity_state_draw(et->state, et); | |
111 } | |
112 | |
113 void | |
114 battle_entity_finish(struct battle_entity *et) | |
115 { | |
116 assert(et); | |
117 | |
118 battle_entity_state_finish(et->state, et); | |
119 } |