Mirai's Miscellaneous Misadventures
M7 / game.c
#include "game.h"
#include "math.c"
struct mirai_animation
{
int count;
struct mirai_image *images;
};
struct mirai_direction_animation_set
{
struct mirai_animation idling;
struct mirai_animation moving;
struct mirai_animation walking;
struct mirai_animation sprinting;
};
struct mirai_animation_set
{
struct mirai_direction_animation_set left, right;
};
struct mirai_sprite_type
{
struct mirai_animation_set animations;
int width;
int height;
};
struct mirai_sprite
{
struct mirai_sprite_type *type;
int x, y;
int dx, dy;
int ax;
char airborne;
char moving_direction;
char last_moving_direction;
int attack_y;
int attack_width;
int attack_height;
int attack_time;
void (*behave)(struct mirai_game *game, struct mirai_sprite *sprite);
unsigned char animation_time;
};
struct mirai_rotating_image_row
{
int size;
unsigned char count;
unsigned char colors[256];
};
struct mirai_rotating_image
{
int count;
struct mirai_rotating_image_row rows[];
};
#include "assets.c"
static int mirai_gravity = 4;
static unsigned char mirai_ground_colors[16 * 16];
static unsigned char mirai_colors[8 * 8 * 26 * 36];
static struct mirai_image mirai_images[66] =
{
{16, 16, mirai_ground_colors},
};
static struct mirai_image *mirai_ground_image = mirai_images;
static struct mirai_sprite_type mirai_mirai = {{}, 80, 544};
struct mirai_game
{
struct mirai_engine engine;
struct mirai_sprite sprites[256];
unsigned char sprite_count;
unsigned int left_history:16;
unsigned int right_history:16;
struct mirai_sprite *camera_sprite;
int camera_x;
int camera_y;
};
static void mirai_stamp(struct mirai_game *game, int x, int y, struct mirai_image *image)
{
x -= game->camera_x;
x += mirai_width * 4;
y -= game->camera_y;
y += mirai_height * 4;
(*game->engine.stamp)(game->engine.data, x / 8, y / 8, image);
}
static void mirai_physics(struct mirai_sprite *sprite)
{
sprite->dx += sprite->ax;
sprite->x += sprite->dx;
sprite->y += sprite->dy;
int x0 = sprite->x;
int x1 = sprite->type->width / 2;
int y0 = sprite->y;
int y1 = sprite->type->height;
if (mirai_ground[(x0 + x1) / 128][(y0 - 127) / 128] != 0)
{
if (mirai_ground[(x0 + x1) / 128][(y0 - y1 / 2) / 128] == 0)
{
sprite->y = (y0 / 128 - 1) * 128;
}
else
{
sprite->dx = 0;
sprite->x = (x0 + x1) / 128 * 128 - x1;
}
}
if (mirai_ground[(x0 - x1) / 128][(y0 - 127) / 128] != 0)
{
if (mirai_ground[(x0 - x1) / 128][(y0 - y1 / 2) / 128] == 0)
{
sprite->y = (y0 / 128 - 1) * 128;
}
else
{
sprite->dx = 0;
sprite->x = ((x0 - x1) / 128 + 1) * 128 + x1;
}
}
x0 = sprite->x;
y0 = sprite->y;
if (mirai_ground[(x0 - x1) / 128][y0 / 128] != 0 || mirai_ground[(x0 + x1 - 1) / 128][y0 / 128] != 0)
{
sprite->y = y0 / 128 * 128;
sprite->dy = 0;
sprite->airborne = 0;
}
else if (!sprite->airborne)
{
if (mirai_ground[x0 / 128][y0 / 128 + 1] == 0)
sprite->airborne = 1;
else
sprite->y = (y0 / 128 + 1) * 128;
}
}
static void mirai_ground_physics(struct mirai_sprite *sprite)
{
sprite->dx *= 7;
sprite->dx /= 8;
sprite->dy = 0;
mirai_physics(sprite);
}
static void mirai_airborne_physics(struct mirai_sprite *sprite)
{
sprite->dx *= 15;
sprite->dx /= 16;
sprite->dy += mirai_gravity;
mirai_physics(sprite);
}
static void mirai_jump(struct mirai_sprite *sprite)
{
if (sprite->airborne) return;
sprite->airborne = 1;
sprite->dy = -64;
}
static void mirai_damage(struct mirai_game *game, struct mirai_sprite *sprite)
{
struct mirai_image area = {sprite->attack_width / 16, sprite->attack_height / 8};
int x = sprite->x + sprite->attack_width / 4;
int y = (sprite->y + sprite->attack_y);
if (sprite->moving_direction == 1) x -= sprite->attack_width / 2;
}
static void mirai_attack(struct mirai_game *game, struct mirai_sprite *sprite)
{
sprite->attack_y = -16;
sprite->attack_width = 512;
sprite->attack_height = 256;
mirai_damage(game, sprite);
}
static void mirai_rapid_attack(struct mirai_game *game, struct mirai_sprite *sprite)
{
sprite->attack_y = -128;
sprite->attack_width = sprite->attack_time * 16 + 64;
sprite->attack_height = 64;
if (sprite->attack_time != 0x7F) sprite->attack_time++;
mirai_damage(game, sprite);
}
static unsigned char mirai_count_oscillations(unsigned int history)
{
unsigned char count = 0;
unsigned char prev = history&1;
for (int i = 0 ; i < 16 ; i++)
{
if ((history&1) != prev) count++;
prev = history&1;
history >>= 1;
}
return count;
}
static void mirai_controls(struct mirai_game *game, struct mirai_sprite *sprite)
{
unsigned char left_oscillations = mirai_count_oscillations(game->left_history);
unsigned char right_oscillations = mirai_count_oscillations(game->right_history);
switch (sprite->moving_direction)
{
case 0:
if ((game->left_history&1) != 0)
sprite->moving_direction = 1;
if ((game->right_history&1) != 0)
sprite->moving_direction = 2;
break;
case 1:
if ((game->left_history&1) == 0)
sprite->moving_direction = 0;
else if (left_oscillations > 1)
mirai_jump(sprite);
if ((game->right_history&1) != 0)
{
if (right_oscillations == 0)
mirai_rapid_attack(game, sprite);
else
sprite->attack_time = 0;
}
if ((game->right_history&1) != 0 && (game->right_history&2) == 0)
mirai_attack(game, sprite);
break;
case 2:
if ((game->right_history&1) == 0)
sprite->moving_direction = 0;
else if (right_oscillations > 1)
mirai_jump(sprite);
if ((game->left_history&1) != 0)
{
if (left_oscillations == 0)
mirai_rapid_attack(game, sprite);
else
sprite->attack_time = 0;
}
if ((game->left_history&1) != 0 && (game->left_history&2) == 0)
mirai_attack(game, sprite);
break;
}
}
static void mirai_spawn(struct mirai_game *game, int x, int y, struct mirai_sprite_type *type, void (*behave)(struct mirai_game *game, struct mirai_sprite *sprite))
{
if (game->sprite_count == 0xFF) return;
struct mirai_sprite *sprite = game->sprites + game->sprite_count;
game->sprite_count++;
sprite->type = type;
sprite->x = x;
sprite->y = y;
sprite->dx = 0;
sprite->dy = 0;
sprite->ax = 0;
sprite->airborne = 1;
sprite->moving_direction = 0;
sprite->last_moving_direction = 2;
sprite->attack_time = 0;
sprite->animation_time = 0;
sprite->behave = behave;
}
static void mirai_image_rotation(struct mirai_image *image, struct mirai_rotating_image *rotating_image, unsigned char angle, int x0, int y0, int tilt, int tilt_origin, char flip)
{
if (flip != 0) angle = 0x100 - angle;
int width = image->width;
int height = image->height;
for (int y = 0 ; y < rotating_image->count ; y++)
for (int x = 0 ; x < rotating_image->rows[y].size ; x++)
{
int count = rotating_image->rows[y].count;
int w = rotating_image->rows[y].size;
int h = rotating_image->count;
unsigned int a = angle;
a *= count;
a += 128;
a /= 256;
a += x;
a %= count;
if (flip) a = count - a - 1;
int x1 = x + x0 + (width - w) / 2 + tilt * (tilt_origin - y) / 256;
int y1 = y - y0 + height - h;
unsigned char color = rotating_image->rows[y].colors[a];
if (color == 0) continue;
image->colors[x1 + y1 * width] = color;
}
}
static void mirai_mirai_animation_frame(struct mirai_image *image, unsigned char **colors_pointer, int angle, int arm_tilt)
{
unsigned char *colors = *colors_pointer;
image->colors = colors;
image->width = 26;
image->height = 36;
(*colors_pointer) += image->width * image->height;
for (int x = 0 ; x < image->width ; x++)
for (int y = 0 ; y < image->height ; y++)
image->colors[x + y * image->width] = 0;
int tilt = (64 - angle);
if (tilt < 0) tilt += 12;
else tilt -= 12;
int x = mirai_sine[angle];
x *= 5;
x /= 128;
int left_x = x + (mirai_mirai_left_arm.count + 10) * tilt / 256;
int right_x = x - (mirai_mirai_right_arm.count + 10) * tilt / 256;
int height = mirai_mirai_torso.count;
mirai_image_rotation(image, &mirai_mirai_hair, angle, 0, 7, tilt, height, 1);
if ((angle + 64) % 256 < 128)
{
mirai_image_rotation(image, &mirai_mirai_left_arm, angle, left_x, 10, tilt - arm_tilt, 0, 0);
mirai_image_rotation(image, &mirai_mirai_torso, angle, 0, 0, tilt, height, 0);
mirai_image_rotation(image, &mirai_mirai_right_arm, angle, -right_x, 10, tilt + arm_tilt, 0, 0);
}
else
{
mirai_image_rotation(image, &mirai_mirai_right_arm, angle, -right_x, 10, tilt + arm_tilt, 0, 0);
mirai_image_rotation(image, &mirai_mirai_torso, angle, 0, 0, tilt, height, 0);
mirai_image_rotation(image, &mirai_mirai_left_arm, angle, left_x, 10, tilt - arm_tilt, 0, 0);
}
mirai_image_rotation(image, &mirai_mirai_hair, angle, 0, 7, tilt, height, 0);
}
static void mirai_mirai_animation(struct mirai_animation *animation, struct mirai_image **images, unsigned char **colors, int angle)
{
animation->count = 8;
animation->images = *images;
int tilt = (64 - angle);
if (tilt < 0) tilt += 12;
else tilt -= 12;
tilt *= 4;
mirai_mirai_animation_frame((*images)++, colors, angle, 0);
mirai_mirai_animation_frame((*images)++, colors, angle, tilt / 2);
mirai_mirai_animation_frame((*images)++, colors, angle, tilt);
mirai_mirai_animation_frame((*images)++, colors, angle, tilt / 2);
mirai_mirai_animation_frame((*images)++, colors, angle, 0);
mirai_mirai_animation_frame((*images)++, colors, angle, -tilt / 2);
mirai_mirai_animation_frame((*images)++, colors, angle, -tilt);
mirai_mirai_animation_frame((*images)++, colors, angle, -tilt / 2);
}
int mirai_game_size = sizeof (struct mirai_game);
int mirai_width = 512;
int mirai_height = 256;
void mirai_start(struct mirai_game *game, struct mirai_engine *engine)
{
game->engine = *engine;
game->engine.stamp = engine->stamp;
game->left_history = 0;
game->right_history = 0;
game->sprite_count = 0;
mirai_spawn(game, 500, 1500, &mirai_mirai, &mirai_controls);
game->camera_x = 500;
game->camera_y = 1500;
game->camera_sprite = game->sprites;
mirai_assets();
}
void mirai_step(struct mirai_game *game, struct mirai_keys keys)
{
game->left_history <<= 1;
game->left_history |= keys.left;
game->right_history <<= 1;
game->right_history |= keys.right;
game->camera_x *= 3;
game->camera_y *= 3;
game->camera_x += game->camera_sprite->x;
game->camera_y += game->camera_sprite->y - 512;
game->camera_x /= 4;
game->camera_y /= 4;
for (int i = 0 ; i < sizeof mirai_ground / sizeof *mirai_ground ; i++)
for (int j = 0 ; j < sizeof *mirai_ground / sizeof **mirai_ground ; j++)
{
if (mirai_ground[i][j])
mirai_stamp(game, i * 128 + 64, j * 128 + 128, mirai_ground_image);
}
for (int i = 0 ; i < game->sprite_count ; i++)
{
struct mirai_sprite *sprite = game->sprites + i;
if (game->sprites->airborne == 0)
mirai_ground_physics(sprite);
else
mirai_airborne_physics(sprite);
(*sprite->behave)(game, sprite);
if (sprite->moving_direction != 0)
sprite->last_moving_direction = sprite->moving_direction;
game->sprites[i].ax = 0;
if (sprite->airborne)
{
if (sprite->moving_direction == 1)
sprite->ax = -3;
if (sprite->moving_direction == 2)
sprite->ax = 3;
}
else
{
if (sprite->moving_direction == 1)
sprite->ax = -4;
if (sprite->moving_direction == 2)
sprite->ax = 4;
}
int dx;
struct mirai_direction_animation_set *animations;
if (sprite->last_moving_direction == 1)
{
dx = -sprite->dx;
animations = &sprite->type->animations.left;
}
if (sprite->last_moving_direction == 2)
{
dx = sprite->dx;
animations = &sprite->type->animations.right;
}
struct mirai_animation *animation;
animation = &animations->sprinting;
if (dx < 16) animation = &animations->walking;
if (dx < 12) animation = &animations->moving;
if (dx < 8) animation = &animations->idling;
mirai_stamp(game, sprite->x, sprite->y, animation->images + sprite->animation_time * animation->count / 256);
sprite->animation_time += 8;
}
}
struct mirai_image *mirai_assets(void)
{
static char done = 0;
if (done != 0) return mirai_images;
int image_count = sizeof mirai_images / sizeof *mirai_images;
struct mirai_image *last_image = mirai_images + image_count;
int color_count = sizeof mirai_colors / sizeof *mirai_colors;
unsigned char *last_color = mirai_colors + color_count;
last_image->width = 0;
last_image->height = 0;
for (int i = 0 ; i < 256 ; i++)
mirai_ground_colors[i] = 0x3B;
struct mirai_image *images = mirai_images + 1;
unsigned char *colors = mirai_colors;
mirai_mirai_animation(&mirai_mirai.animations.left.idling, &images, &colors, 64 + 12);
mirai_mirai_animation(&mirai_mirai.animations.left.moving, &images, &colors, 64 + 16);
mirai_mirai_animation(&mirai_mirai.animations.left.walking, &images, &colors, 64 + 32);
mirai_mirai_animation(&mirai_mirai.animations.left.sprinting, &images, &colors, 64 + 48);
mirai_mirai_animation(&mirai_mirai.animations.right.idling, &images, &colors, 64 - 12);
mirai_mirai_animation(&mirai_mirai.animations.right.moving, &images, &colors, 64 - 16);
mirai_mirai_animation(&mirai_mirai.animations.right.walking, &images, &colors, 64 - 32);
mirai_mirai_animation(&mirai_mirai.animations.right.sprinting, &images, &colors, 64 - 48);
if (images != last_image - 1) return 0;
if (colors != last_color) return 0;
done = 1;
return mirai_images;
}