comparison src/libmlk-rpg/rpg/battle-entity-state-moving.c @ 383:b944cd41e8f9

rpg: do the same for battle entities
author David Demelier <markand@malikania.fr>
date Sun, 13 Feb 2022 11:13:17 +0100
parents 460c78706989
children c458441ff472
comparison
equal deleted inserted replaced
382:43d155668a55 383:b944cd41e8f9
23 #include <core/alloc.h> 23 #include <core/alloc.h>
24 #include <core/panic.h> 24 #include <core/panic.h>
25 25
26 #include "battle-entity.h" 26 #include "battle-entity.h"
27 #include "battle-entity-state.h" 27 #include "battle-entity-state.h"
28 #include "battle-entity-state-moving.h"
28 #include "character.h" 29 #include "character.h"
29 #include "walksprite.h" 30 #include "walksprite.h"
30 31
31 #define SPEED 800 32 #define SPEED 800
32 #define SEC 1000 33 #define SEC 1000
33 #define WALK 40 34 #define WALK 40
34 35
35 struct position { 36 struct self {
37 struct battle_entity_state_moving data;
36 struct battle_entity_state state; 38 struct battle_entity_state state;
37 struct walksprite ws;
38
39 /* Destination. */
40 int x;
41 int y;
42 }; 39 };
43 40
44 static inline unsigned int 41 static inline unsigned int
45 orientation(const struct position *pos, const struct battle_entity *et) 42 orientation(const struct battle_entity_state_moving *mv, const struct battle_entity *et)
46 { 43 {
47 /* TODO: support diagonal. */ 44 /* TODO: support diagonal. */
48 /* See: walksprite definitions. */ 45 /* See: walksprite definitions. */
49 return pos->x < et->x ? 6 : 2; 46 return mv->x < et->x ? 6 : 2;
50 } 47 }
51 48
52 static int 49 static int
53 update(struct battle_entity_state *st, struct battle_entity *et, unsigned int ticks) 50 update(struct battle_entity_state *st, struct battle_entity *et, unsigned int ticks)
54 { 51 {
55 struct position *pos = st->data; 52 return battle_entity_state_moving_update(st->data, et, ticks);
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 } 53 }
73 54
74 static void 55 static void
75 draw(const struct battle_entity_state *st, const struct battle_entity *et) 56 draw(const struct battle_entity_state *st, const struct battle_entity *et)
76 { 57 {
77 /* TODO: compute orientation. */ 58 battle_entity_state_moving_draw(st->data, et);
78 struct position *pos = st->data;
79
80 walksprite_draw(&pos->ws, orientation(pos, et), et->x, et->y);
81 } 59 }
82 60
83 static void 61 static void
84 finish(struct battle_entity_state *st, struct battle_entity *et) 62 finish(struct battle_entity_state *st, struct battle_entity *et)
85 { 63 {
87 65
88 free(st->data); 66 free(st->data);
89 } 67 }
90 68
91 void 69 void
92 battle_entity_state_moving(struct battle_entity *et, int destx, int desty) 70 battle_entity_state_moving_init(struct battle_entity_state_moving *mv, struct battle_entity *et, int dstx, int dsty)
71 {
72 assert(mv);
73 assert(et);
74
75 walksprite_init(&mv->ws, et->ch->sprites[CHARACTER_SPRITE_NORMAL], 40);
76 mv->x = dstx;
77 mv->y = dsty;
78 }
79
80 int
81 battle_entity_state_moving_update(struct battle_entity_state_moving *mv, struct battle_entity *et, unsigned int ticks)
82 {
83 assert(mv);
84 assert(et);
85
86 int step_x, step_y, delta_x, delta_y;
87
88 delta_x = mv->x < et->x ? -1 : +1;
89 delta_y = mv->y < et->y ? -1 : +1;
90 step_x = fmin(SPEED * ticks / SEC, abs(et->x - mv->x));
91 step_y = fmin(SPEED * ticks / SEC, abs(et->y - mv->y));
92
93 et->x += delta_x * step_x;
94 et->y += delta_y * step_y;
95
96 if (et->x != mv->x || et->y != mv->y)
97 walksprite_update(&mv->ws, ticks);
98 else
99 walksprite_reset(&mv->ws);
100
101 return et->x == mv->x && et->y == mv->y;
102 }
103
104 void
105 battle_entity_state_moving_draw(const struct battle_entity_state_moving *mv, const struct battle_entity *et)
106 {
107 assert(mv);
108 assert(battle_entity_ok(et));
109
110 /* TODO: compute orientation. */
111 walksprite_draw(&mv->ws, orientation(mv, et), et->x, et->y);
112 }
113
114 void
115 battle_entity_state_moving(struct battle_entity *et, int dstx, int dsty)
93 { 116 {
94 assert(et); 117 assert(et);
95 118
96 struct position *pos; 119 struct self *self;
97 120
98 if (!(pos = alloc_new0(sizeof (*pos)))) 121 self = alloc_new0(sizeof (*self));
99 panic(); 122 self->state.data = self;
123 self->state.update = update;
124 self->state.draw = draw;
125 self->state.finish = finish;
100 126
101 walksprite_init(&pos->ws, et->ch->sprites[CHARACTER_SPRITE_NORMAL], 40); 127 battle_entity_state_moving_init(&self->data, et, dstx, dsty);
102 pos->x = destx; 128 battle_entity_switch(et, &self->state);
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 } 129 }