comparison libmlk-rpg/mlk/rpg/battle-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-state-attacking.c@8f59201dc76b
children 25a56ca53ac2
comparison
equal deleted inserted replaced
433:862b15c3a3ae 434:4e78f045e8c0
1 /*
2 * battle-state-attacking.c -- battle state (entity is moving for 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/panic.h>
24 #include <mlk/core/sprite.h>
25
26 #include "battle-entity-state-attacking.h"
27 #include "battle-entity-state-blinking.h"
28 #include "battle-entity-state-moving.h"
29 #include "battle-entity-state-normal.h"
30 #include "battle-entity-state.h"
31 #include "battle-state-attacking.h"
32 #include "battle-state-check.h"
33 #include "battle-state.h"
34 #include "battle.h"
35 #include "character.h"
36
37 struct self {
38 struct battle_state_attacking data;
39 struct battle_state state;
40 };
41
42 static void
43 damage(const struct battle_entity *source, struct battle_entity *target, struct battle *bt)
44 {
45 (void)source;
46
47 /* TODO: math calculation here.*/
48 target->ch->hp -= 50;
49
50 if (target->ch->hp < 0)
51 target->ch->hp = 0;
52
53 battle_indicator_hp(bt, target->ch, 50);
54 }
55
56 static int
57 update(struct battle_state *st, struct battle *bt, unsigned int ticks)
58 {
59 battle_state_attacking_update(st->data, bt, ticks);
60
61 return 0;
62 }
63
64 static void
65 draw(const struct battle_state *st, const struct battle *bt)
66 {
67 battle_state_attacking_draw(st->data, bt);
68 }
69
70 static void
71 finish(struct battle_state *st, struct battle *bt)
72 {
73 (void)bt;
74
75 free(st->data);
76 }
77
78 void
79 battle_state_attacking_init(struct battle_state_attacking *atk,
80 struct battle_entity *source,
81 struct battle_entity *target)
82 {
83 assert(atk);
84 assert(battle_entity_ok(source));
85 assert(battle_entity_ok(target));
86
87 int x, y;
88
89 /* Starts this state with advancing. */
90 atk->source = source;
91 atk->target = target;
92 atk->origin_x = source->x;
93 atk->origin_y = source->y;
94
95 /* We go to the enemy. */
96 x = target->x + target->ch->sprites[CHARACTER_SPRITE_NORMAL]->cellw;
97 y = target->y;
98
99 /* If it is an enemy we don't move it but blink instead. */
100 if (source->ch->exec) {
101 atk->status = BATTLE_STATE_ATTACKING_BLINKING;
102 battle_entity_state_blinking(source);
103 } else
104 battle_entity_state_moving(source, x, y);
105 }
106
107 void
108 battle_state_attacking_update(struct battle_state_attacking *atk, struct battle *bt, unsigned int ticks)
109 {
110 assert(atk);
111 assert(bt);
112
113 battle_update_component(bt, ticks, BATTLE_COMPONENT_ALL);
114
115 if (!battle_entity_update(atk->source, 0))
116 return;
117
118 switch (atk->status) {
119 case BATTLE_STATE_ATTACKING_ADVANCING:
120 /*
121 * Current entity state is battle-entity-state-moving but it is
122 * already updated from the game itself so pass 0 just to check
123 * if it has finished moving.
124 */
125 atk->status = BATTLE_STATE_ATTACKING_DAMAGING;
126
127 /* TODO: determine sprite to use about equipment. */
128 battle_entity_state_attacking(atk->source, atk->source->ch->sprites[CHARACTER_SPRITE_SWORD]);
129 break;
130 case BATTLE_STATE_ATTACKING_DAMAGING:
131 /* Move back to original position. */
132 atk->status = BATTLE_STATE_ATTACKING_RETURNING;
133 damage(atk->source, atk->target, bt);
134 battle_entity_state_moving(atk->source, atk->origin_x, atk->origin_y);
135 break;
136 case BATTLE_STATE_ATTACKING_RETURNING:
137 case BATTLE_STATE_ATTACKING_BLINKING:
138 /* Just wait. */
139 battle_entity_state_normal(atk->source);
140 damage(atk->source, atk->target, bt);
141 battle_state_check(bt);
142 break;
143 default:
144 break;
145 }
146 }
147
148 void
149 battle_state_attacking_draw(const struct battle_state_attacking *atk, const struct battle *bt)
150 {
151 assert(atk);
152 assert(battle_ok(bt));
153
154 (void)atk;
155
156 battle_draw_component(bt, BATTLE_COMPONENT_ALL);
157 }
158
159 void
160 battle_state_attacking(struct battle_entity *source, struct battle_entity *target, struct battle *bt)
161 {
162 assert(battle_entity_ok(source));
163 assert(battle_entity_ok(target));
164 assert(bt);
165
166 struct self *self;
167
168 self = alloc_new0(sizeof (*self));
169 self->state.data = self;
170 self->state.update = update;
171 self->state.draw = draw;
172 self->state.finish = finish;
173
174 battle_state_attacking_init(&self->data, source, target);
175 battle_switch(bt, &self->state);
176 }