comparison libmlk-rpg/mlk/rpg/battle-entity-state-attacking.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-state-attacking.c@8f59201dc76b
children 25a56ca53ac2
comparison
equal deleted inserted replaced
433:862b15c3a3ae 434:4e78f045e8c0
1 /*
2 * battle-entity-state-attacking.h -- the entity is attacking
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 #include <stdlib.h>
21
22 #include <mlk/core/alloc.h>
23 #include <mlk/core/animation.h>
24 #include <mlk/core/panic.h>
25 #include <mlk/core/sprite.h>
26
27 #include "battle-entity-state-attacking.h"
28 #include "battle-entity-state.h"
29 #include "battle-entity.h"
30
31 struct self {
32 struct battle_entity_state_attacking data;
33 struct battle_entity_state state;
34 };
35
36 static int
37 update(struct battle_entity_state *st, struct battle_entity *et, unsigned int ticks)
38 {
39 (void)et;
40
41 return battle_entity_state_attacking_update(st->data, ticks);
42 }
43
44 static void
45 draw(const struct battle_entity_state *st, const struct battle_entity *et)
46 {
47 battle_entity_state_attacking_draw(st->data, et);
48 }
49
50 static void
51 finish(struct battle_entity_state *st, struct battle_entity *et)
52 {
53 (void)et;
54
55 free(st->data);
56 }
57
58 void
59 battle_entity_state_attacking_init(struct battle_entity_state_attacking *atk, const struct sprite *which)
60 {
61 assert(atk);
62 assert(sprite_ok(which));
63
64 animation_init(&atk->anim, which, 100);
65 animation_start(&atk->anim);
66 }
67
68 int
69 battle_entity_state_attacking_update(struct battle_entity_state_attacking *atk, unsigned int ticks)
70 {
71 assert(atk);
72
73 return animation_update(&atk->anim, ticks);
74 }
75
76 void
77 battle_entity_state_attacking_draw(const struct battle_entity_state_attacking *atk, const struct battle_entity *et)
78 {
79 assert(atk);
80 assert(battle_entity_ok(et));
81
82 animation_draw(&atk->anim, et->x, et->y);
83 }
84
85 void
86 battle_entity_state_attacking(struct battle_entity *et, const struct sprite *which)
87 {
88 assert(battle_entity_ok(et));
89 assert(sprite_ok(which));
90
91 struct self *self;
92
93 self = alloc_new0(sizeof (*self));
94 self->state.data = self;
95 self->state.update = update;
96 self->state.draw = draw;
97 self->state.finish = finish;
98
99 battle_entity_state_attacking_init(&self->data, which);
100 battle_entity_switch(et, &self->state);
101 }