Mercurial > molko
comparison src/libmlk-rpg/rpg/battle-entity-state-moving.c @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | libmlk-rpg/rpg/battle-entity-state-moving.c@d01e83210ca2 |
children | 460c78706989 |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * battle-entity-state-moving.c -- the entity is moving | |
3 * | |
4 * Copyright (c) 2020-2021 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 <math.h> | |
21 #include <stdlib.h> | |
22 | |
23 #include <core/alloc.h> | |
24 #include <core/panic.h> | |
25 | |
26 #include "battle-entity.h" | |
27 #include "battle-entity-state.h" | |
28 #include "character.h" | |
29 #include "walksprite.h" | |
30 | |
31 #define SPEED 800 | |
32 #define SEC 1000 | |
33 #define WALK 40 | |
34 | |
35 struct position { | |
36 struct battle_entity_state state; | |
37 struct walksprite ws; | |
38 | |
39 /* Destination. */ | |
40 int x; | |
41 int y; | |
42 }; | |
43 | |
44 static inline unsigned int | |
45 orientation(const struct position *pos, const struct battle_entity *et) | |
46 { | |
47 /* TODO: support diagonal. */ | |
48 /* See: walksprite definitions. */ | |
49 return pos->x < et->x ? 6 : 2; | |
50 } | |
51 | |
52 static int | |
53 update(struct battle_entity_state *st, struct battle_entity *et, unsigned int ticks) | |
54 { | |
55 struct position *pos = st->data; | |
56 int step_x, step_y, delta_x, delta_y; | |
57 | |
58 delta_x = pos->x < et->x ? -1 : +1; | |
59 delta_y = pos->y < et->y ? -1 : +1; | |
60 step_x = fmin(SPEED * ticks / SEC, abs(et->x - pos->x)); | |
61 step_y = fmin(SPEED * ticks / SEC, abs(et->y - pos->y)); | |
62 | |
63 et->x += delta_x * step_x; | |
64 et->y += delta_y * step_y; | |
65 | |
66 if (et->x != pos->x || et->y != pos->y) | |
67 walksprite_update(&pos->ws, ticks); | |
68 else | |
69 walksprite_reset(&pos->ws); | |
70 | |
71 return et->x == pos->x && et->y == pos->y; | |
72 } | |
73 | |
74 static void | |
75 draw(const struct battle_entity_state *st, const struct battle_entity *et) | |
76 { | |
77 /* TODO: compute orientation. */ | |
78 struct position *pos = st->data; | |
79 | |
80 walksprite_draw(&pos->ws, orientation(pos, et), et->x, et->y); | |
81 } | |
82 | |
83 static void | |
84 finish(struct battle_entity_state *st, struct battle_entity *et) | |
85 { | |
86 (void)et; | |
87 | |
88 free(st->data); | |
89 } | |
90 | |
91 void | |
92 battle_entity_state_moving(struct battle_entity *et, int destx, int desty) | |
93 { | |
94 assert(et); | |
95 | |
96 struct position *pos; | |
97 | |
98 if (!(pos = alloc_new0(sizeof (*pos)))) | |
99 panic(); | |
100 | |
101 walksprite_init(&pos->ws, et->ch->sprites[CHARACTER_SPRITE_NORMAL], 40); | |
102 pos->x = destx; | |
103 pos->y = desty; | |
104 | |
105 pos->state.data = pos; | |
106 pos->state.update = update; | |
107 pos->state.draw = draw; | |
108 pos->state.finish = finish; | |
109 | |
110 battle_entity_switch(et, &pos->state); | |
111 } |