Mirai's Miscellaneous Misadventures

M37 / core / sprites.c

1// copyright 2022 zamfofex
2// license: AGPLv3 or later
3
4#include <mimimi/sprites.h>
5#include <mimimi/compound-behaviors.h>
6#include <mimimi/allocators.h>
7#include <mimimi/geometry.h>
8#include <mimimi/ground.h>
9#include <mimimi/behaviors.h>
10
11struct mimimi_physics_data
12{
13	struct mimimi_physics *physics;
14	struct mimimi_position *position;
15	struct mimimi_ground *ground;
16	struct mimimi_behavior *compound;
17};
18
19#include "physics.c"
20
21static void mimimi_collision_physics_behave(void *data)
22{
23	struct mimimi_physics_data *physics_data = data;
24	struct mimimi_physics *physics = physics_data->physics;
25	
26	if (physics->airborne == 0)
27		mimimi_ground_collision_physics(physics_data);
28	else
29		mimimi_airborne_collision_physics(physics_data);
30}
31
32static void mimimi_dynamics_behave(void *data)
33{
34	struct mimimi_physics_data *physics_data = data;
35	struct mimimi_physics *physics = physics_data->physics;
36	
37	if (physics->airborne == 0)
38		mimimi_ground_physics(physics_data);
39	else
40		mimimi_airborne_physics(physics_data);
41}
42
43static void mimimi_physics_behave(void *data)
44{
45	mimimi_dynamics_behave(data);
46	mimimi_collision_physics_behave(data);
47}
48
49static struct mimimi_physics_data *mimimi_physics_data(struct mimimi_physics *physics, struct mimimi_position *position, struct mimimi_ground *ground, struct mimimi_allocator *allocator)
50{
51	physics->dx = 0;
52	physics->dy = 0;
53	physics->airborne = 1;
54	physics->width = 0;
55	physics->height = 0;
56	physics->gravity = 3;
57	
58	struct mimimi_behavior *compound = mimimi_compound_behavior(allocator);
59	struct mimimi_physics_data *data = mimimi_compound_allocate(compound, sizeof *data, allocator);
60	
61	data->physics = physics;
62	data->position = position;
63	data->ground = ground;
64	data->compound = compound;
65	
66	return data;
67}
68
69struct mimimi_behavior *mimimi_physics(struct mimimi_physics *physics, struct mimimi_position *position, struct mimimi_ground *ground, struct mimimi_allocator *allocator)
70{
71	struct mimimi_physics_data *data = mimimi_physics_data(physics, position, ground, allocator);
72	mimimi_compound(data->compound, mimimi_function(data, &mimimi_physics_behave, allocator));
73	return data->compound;
74}
75
76struct mimimi_behavior *mimimi_collision_physics(struct mimimi_physics *physics, struct mimimi_position *position, struct mimimi_ground *ground, struct mimimi_allocator *allocator)
77{
78	struct mimimi_physics_data *data = mimimi_physics_data(physics, position, ground, allocator);
79	mimimi_compound(data->compound, mimimi_function(data, &mimimi_collision_physics_behave, allocator));
80	return data->compound;
81}
82
83struct mimimi_behavior *mimimi_dynamics(struct mimimi_physics *physics, struct mimimi_position *position, struct mimimi_allocator *allocator)
84{
85	struct mimimi_physics_data *data = mimimi_physics_data(physics, position, allocator->null, allocator);
86	mimimi_compound(data->compound, mimimi_function(data, &mimimi_dynamics_behave, allocator));
87	return data->compound;
88}
89
90static void mimimi_live(void *data)
91{
92	struct mimimi_life *life = data;
93	
94	if (life->knocked_time != 0)
95	{
96		life->knocked_time--;
97		life->immunity_time = life->recovery_time;
98	}
99	
100	if (life->immunity_time != 0)
101	{
102		life->pristinity = life->max_pristinity;
103		life->immunity_time--;
104	}
105	
106	if (life->pristinity < life->max_pristinity)
107		life->pristinity++;
108}
109
110struct mimimi_behavior *mimimi_life(struct mimimi_life *life, struct mimimi_allocator *allocator)
111{
112	life->knocked_time = 0;
113	life->immunity_time = 0;
114	life->pristinity = 256;
115	life->max_pristinity = 256;
116	life->recovery_time = 64;
117	
118	return mimimi_function(life, &mimimi_live, allocator);
119}
120
121struct mimimi_fall_data
122{
123	struct mimimi_fall *fall;
124	struct mimimi_physics *physics;
125	struct mimimi_life *life;
126};
127
128static void mimimi_fall_behave(void *data)
129{
130	struct mimimi_fall_data *fall_data = data;
131	
132	if (fall_data->physics->dy > fall_data->fall->max_speed)
133	if (fall_data->life->knocked_time < fall_data->fall->knocked_time)
134		fall_data->life->knocked_time = fall_data->fall->knocked_time;
135}
136
137struct mimimi_behavior *mimimi_fall(struct mimimi_fall *fall, struct mimimi_physics *physics, struct mimimi_life *life, struct mimimi_allocator *allocator)
138{
139	fall->max_speed = 128;
140	fall->knocked_time = 64;
141	
142	struct mimimi_behavior *compound = mimimi_compound_behavior(allocator);
143	struct mimimi_fall_data *data = mimimi_compound_allocate(compound, sizeof *data, allocator);
144	mimimi_compound(compound, mimimi_function(data, &mimimi_fall_behave, allocator));
145	
146	data->fall = fall;
147	data->physics = physics;
148	data->life = life;
149	
150	return compound;
151}
152
153struct mimimi_walk_data
154{
155	struct mimimi_walk *walk;
156	struct mimimi_physics *physics;
157};
158
159static void mimimi_walk_behave(void *data)
160{
161	struct mimimi_walk_data *walk_data = data;
162	struct mimimi_walk *walk = walk_data->walk;
163	struct mimimi_physics *physics = walk_data->physics;
164	
165	if (walk->direction != 0)
166	{
167		int ax;
168		if (walk_data->physics->airborne)
169			ax = walk->airborne_speed;
170		else
171			ax = walk->ground_speed;
172		
173		if (walk->direction == 1)
174			physics->dx -= ax;
175		if (walk->direction == 2)
176			physics->dx += ax;
177	}
178}
179
180struct mimimi_behavior *mimimi_walk(struct mimimi_walk *walk, struct mimimi_physics *physics, struct mimimi_allocator *allocator)
181{
182	walk->ground_speed = 6;
183	walk->airborne_speed = 3;
184	walk->direction = 0;
185	
186	struct mimimi_behavior *compound = mimimi_compound_behavior(allocator);
187	struct mimimi_walk_data *data = mimimi_compound_allocate(compound, sizeof *data, allocator);
188	mimimi_compound(compound, mimimi_function(data, &mimimi_walk_behave, allocator));
189	
190	data->walk = walk;
191	data->physics = physics;
192	
193	return compound;
194}
195
196struct mimimi_sprite_data
197{
198	struct mimimi_behavior *walk_behavior;
199	struct mimimi_sprite *sprite;
200};
201
202static void mimimi_sprite_behave(void *data0)
203{
204	struct mimimi_sprite_data *data = data0;
205	struct mimimi_sprite *sprite = data->sprite;
206	struct mimimi_behavior *walk_behavior = data->walk_behavior;
207	
208	if (sprite->life->knocked_time == 0)
209		sprite->awake = 1,
210		(*walk_behavior->behave)(walk_behavior->data);
211	else
212		sprite->awake = 0;
213}
214
215struct mimimi_sprite *mimimi_sprite(struct mimimi_ground *ground, int x, int y, int width, int height, struct mimimi_allocator *allocator)
216{
217	struct mimimi_behavior *compound = mimimi_compound_behavior(allocator);
218	
219	struct mimimi_position *position = mimimi_compound_allocate(compound, sizeof *position, allocator);
220	
221	struct mimimi_physics *physics = mimimi_compound_allocate(compound, sizeof *physics, allocator);
222	struct mimimi_behavior *physics_behavior = mimimi_physics(physics, position, ground, allocator);
223	
224	struct mimimi_life *life = mimimi_compound_allocate(compound, sizeof *life, allocator);
225	struct mimimi_behavior *life_behavior = mimimi_life(life, allocator);
226	
227	struct mimimi_fall *fall = mimimi_compound_allocate(compound, sizeof *fall, allocator);
228	struct mimimi_behavior *fall_behavior = mimimi_fall(fall, physics, life, allocator);
229	
230	struct mimimi_walk *walk = mimimi_compound_allocate(compound, sizeof *walk, allocator);
231	struct mimimi_behavior *walk_behavior = mimimi_walk(walk, physics, allocator);
232	mimimi_compound(compound, mimimi_finalizer(walk_behavior->data, walk_behavior->finish, allocator));
233	
234	struct mimimi_sprite *sprite = mimimi_compound_allocate(compound, sizeof *sprite, allocator);
235	struct mimimi_sprite_data *sprite_data = mimimi_compound_allocate(compound, sizeof *sprite_data, allocator);
236	sprite->awake = 1;
237	
238	struct mimimi_behavior *sprite_behavior = mimimi_function(sprite_data, &mimimi_sprite_behave, allocator);
239	
240	position->x = x;
241	position->y = y;
242	
243	physics->width = width;
244	physics->height = height;
245	
246	sprite_data->walk_behavior = walk_behavior;
247	sprite_data->sprite = sprite;
248	
249	sprite->position = position;
250	sprite->physics = physics;
251	sprite->life = life;
252	sprite->fall = fall;
253	sprite->walk = walk;
254	sprite->behavior = compound;
255	
256	mimimi_compound(compound, sprite_behavior);
257	mimimi_compound(compound, physics_behavior);
258	mimimi_compound(compound, life_behavior);
259	mimimi_compound(compound, fall_behavior);
260	
261	return sprite;
262}
263
264struct mimimi_jump_data
265{
266	struct mimimi_allocator *allocator;
267	struct mimimi_behavior *behavior;
268	struct mimimi_jump *jump;
269	struct mimimi_physics *physics;
270};
271
272static void mimimi_jump_behave(void *data)
273{
274	struct mimimi_jump_data *jump_data = data;
275	jump_data->physics->airborne = 1;
276	jump_data->physics->dy = -jump_data->jump->strength;
277}
278
279static void mimimi_jump_finish(void *data)
280{
281	struct mimimi_jump_data *jump_data = data;
282	mimimi_deallocate(jump_data->allocator, jump_data->behavior);
283	mimimi_deallocate(jump_data->allocator, jump_data);
284}
285
286struct mimimi_behavior *mimimi_jump(struct mimimi_jump *jump, struct mimimi_physics *physics, struct mimimi_allocator *allocator)
287{
288	struct mimimi_jump_data *data = mimimi_allocate(allocator, sizeof *data);
289	data->allocator = allocator;
290	data->jump = jump;
291	data->physics = physics;
292	
293	jump->strength = 56;
294	
295	data->behavior = mimimi_allocate(allocator, sizeof *data->behavior);
296	data->behavior->behave = &mimimi_jump_behave;
297	data->behavior->finish = &mimimi_jump_finish;
298	data->behavior->data = data;
299	
300	return data->behavior;
301}
302
303struct mimimi_dash_data
304{
305	struct mimimi_allocator *allocator;
306	struct mimimi_behavior *behavior;
307	struct mimimi_dash *dash;
308	struct mimimi_physics *physics;
309	struct mimimi_walk *walk;
310};
311
312static void mimimi_dash_behave(void *data)
313{
314	struct mimimi_dash_data *dash_data = data;
315	if (dash_data->walk->direction == 1)
316		dash_data->physics->dx -= dash_data->dash->x_strength;
317	if (dash_data->walk->direction == 2)
318		dash_data->physics->dx += dash_data->dash->x_strength;
319	dash_data->physics->dy -= dash_data->dash->y_strength;
320}
321
322static void mimimi_dash_finish(void *data)
323{
324	struct mimimi_dash_data *dash_data = data;
325	mimimi_deallocate(dash_data->allocator, dash_data->behavior);
326	mimimi_deallocate(dash_data->allocator, dash_data);
327}
328
329struct mimimi_behavior *mimimi_dash(struct mimimi_dash *dash, struct mimimi_physics *physics, struct mimimi_walk *walk, struct mimimi_allocator *allocator)
330{
331	struct mimimi_dash_data *data = mimimi_allocate(allocator, sizeof *data);
332	data->allocator = allocator;
333	data->dash = dash;
334	data->physics = physics;
335	data->walk = walk;
336	
337	dash->x_strength = 32;
338	dash->y_strength = 16;
339	
340	data->behavior = mimimi_allocate(allocator, sizeof *data->behavior);
341	data->behavior->behave = &mimimi_dash_behave;
342	data->behavior->finish = &mimimi_dash_finish;
343	data->behavior->data = data;
344	
345	return data->behavior;
346}