Mirai's Miscellaneous Misadventures

M25 / core / physics.c

// copyright 2022 zamfofex
// license: AGPLv3 or later

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_slope_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)
			// fall
			physics->airborne = 1;
		else
			// step down
			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 *= 5;
	physics->dx /= 6;
	physics->dy = 0;
	
	mimimi_physics_dynamics(data);
	
	mimimi_slope_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 *= 17;
	physics->dx /= 18;
	physics->dy += physics->gravity;
	
	mimimi_physics_dynamics(data);
	
	mimimi_ceiling_physics(data);
	mimimi_wall_physics(data);
	mimimi_landing_physics(data);
}