Mirai's Miscellaneous Misadventures
M19 / core / physics.c
static void mimimi_wall_physics(struct mimimi_physics_data *data)
{
struct mimimi_ground *ground = data->ground;
struct mimimi_position *position = data->position;
struct mimimi_physics *physics = data->physics;
int x0 = position->x;
int y0 = position->y;
int x1 = physics->width / 2;
int y1 = physics->height;
int top = (y0 - y1) / 128;
int top2 = top - 1;
int bottom = (y0 - 127) / 128;
int bottom2 = bottom - 1;
int left = (x0 - x1) / 128;
int right = (x0 + x1) / 128;
if (
ground->tiles[left + bottom * ground->width] != 0 &&
ground->tiles[left + bottom2 * ground->width] != 0 ||
ground->tiles[left + top * ground->width] != 0 &&
ground->tiles[left + top2 * ground->width]
)
{
if (physics->dx < 0) physics->dx = 0;
position->x = (left + 1) * 128 + x1;
}
if (
ground->tiles[right + bottom * ground->width] != 0 &&
ground->tiles[right + bottom2 * ground->width] != 0 ||
ground->tiles[right + top * ground->width] != 0 &&
ground->tiles[right + top2 * ground->width]
)
{
if (physics->dx > 0) physics->dx = 0;
position->x = right * 128 - x1 - 1;
}
}
static void mimimi_step_physics(struct mimimi_physics_data *data)
{
struct mimimi_ground *ground = data->ground;
struct mimimi_position *position = data->position;
struct mimimi_physics *physics = data->physics;
int x0 = position->x;
int y0 = position->y;
int x1 = physics->width / 2;
int y1 = physics->height;
int top = (y0 - y1) / 128;
int bottom = (y0 - 127) / 128;
int left = (x0 - x1) / 128;
int right = (x0 + x1) / 128;
if (ground->tiles[left + bottom * ground->width] != 0 && ground->tiles[left + top * ground->width] == 0)
position->y = bottom * 128;
if (ground->tiles[right + bottom * ground->width] != 0 && ground->tiles[right + top * ground->width] == 0)
position->y = bottom * 128;
}
static void mimimi_ceiling_physics(struct mimimi_physics_data *data)
{
struct mimimi_ground *ground = data->ground;
struct mimimi_position *position = data->position;
struct mimimi_physics *physics = data->physics;
int x0 = position->x;
int y0 = position->y;
int x1 = physics->width / 2;
int y1 = physics->height;
int top = (y0 - y1) / 128;
int top2 = top - 1;
int left = (x0 - x1) / 128;
int right = (x0 + x1) / 128;
if (physics->dy > 0) return;
if (ground->tiles[left + top * ground->width] != 0)
if (ground->tiles[right + top * ground->width] != 0)
if (ground->tiles[left + top2 * ground->width] != 0 || ground->tiles[right + top2 * ground->width] != 0)
{
physics->dy = 0;
position->y = (top + 1) * 128 + y1;
}
}
static void mimimi_landing_physics(struct mimimi_physics_data *data)
{
struct mimimi_ground *ground = data->ground;
struct mimimi_position *position = data->position;
struct mimimi_physics *physics = data->physics;
int x0 = position->x;
int y0 = position->y;
int x1 = physics->width / 2;
int bottom = y0 / 128;
int left = (x0 - x1) / 128;
int right = (x0 + x1) / 128;
if (physics->dy < 0) return;
if (ground->tiles[left + bottom * ground->width] != 0 || ground->tiles[right + bottom * ground->width] != 0)
{
physics->airborne = 0;
physics->dy = 0;
position->y = bottom * 128;
}
}
static void mimimi_fall_physics(struct mimimi_physics_data *data)
{
struct mimimi_ground *ground = data->ground;
struct mimimi_position *position = data->position;
struct mimimi_physics *physics = data->physics;
int x0 = position->x;
int y0 = position->y;
int x1 = physics->width / 2;
int bottom = y0 / 128;
int center = x0 / 128;
int left = (x0 - x1) / 128;
int right = (x0 + x1) / 128;
if (ground->tiles[left + bottom * ground->width] == 0 && ground->tiles[right + bottom * ground->width] == 0)
{
if (ground->tiles[center + (bottom + 1) * ground->width] == 0)
physics->airborne = 1;
else
position->y = (bottom + 1) * 128;
}
}
static void mimimi_physics_dynamics(struct mimimi_physics_data *data)
{
struct mimimi_position *position = data->position;
struct mimimi_physics *physics = data->physics;
position->x += physics->dx;
position->y += physics->dy;
}
static void mimimi_ground_physics(struct mimimi_physics_data *data)
{
struct mimimi_physics *physics = data->physics;
physics->dx *= 7;
physics->dx /= 8;
physics->dy = 0;
mimimi_physics_dynamics(data);
mimimi_step_physics(data);
mimimi_wall_physics(data);
mimimi_fall_physics(data);
}
static void mimimi_airborne_physics(struct mimimi_physics_data *data)
{
struct mimimi_physics *physics = data->physics;
physics->dx *= 15;
physics->dx /= 16;
physics->dy += physics->gravity;
mimimi_physics_dynamics(data);
mimimi_ceiling_physics(data);
mimimi_wall_physics(data);
mimimi_landing_physics(data);
}