Files
asteroids/game.c

482 lines
11 KiB
C

#include "game.h"
#include "input.h"
#include "renderer.h"
#include "rng.h"
#include <assert.h>
#include <linux/input-event-codes.h>
#include <math.h>
#include <string.h>
#define SHIP_COLLIDE_R 0.05
#define FIRE_MEAN -0.15
#define FIRE_JITTER 0.01
#define ASTEROID_MIN_VERTS 5
#define ASTEROID_MAX_VERTS 8
#define ASTEROID_VERT_RANGE (ASTEROID_MAX_VERTS - ASTEROID_MIN_VERTS)
#define ASTEROID_A_JITTER 0.8
#define ASTEROID_VEL_JITTER 0.001
#define ASTEROID_OMG_JITTER 0.005
#define INIT_ASTEROIDS 8
#define ASTEROID_SMALL 0.05
#define ASTEROID_MEDIUM 0.1
#define ASTEROID_LARGE 0.15
#define ASTEROID_R_JITTER 0.02
#define ASTEROID_MIN_DIST 0.8
#define REPLACE_MIN 2
#define REPLACE_MAX 4
#define REPLACE_RANGE (REPLACE_MAX - REPLACE_MIN)
#define REPLACE_R_COEFF 0.5
#define REPLACE_R_JITTER 0.02
#define REPLACE_A_JITTER 0.4
#define REPLACE_V_JITTER 0.001
#define REPLACE_RADIAL_V_COEFF 0.005
#define REPLACE_OMG_JITTER 0.01
#define LIN_PWR 0.0001
#define ROT_PWR 0.002
#define SHOT_VEL 0.04
#define SHOT_COLLIDE_R 0.005
#define SHOT_MASS 0.005
#define MAX_SHAPES 256U
#define MAX_ENTITIES 128U
#define MAX_SHAPES_PER_ENTITY 2
#define MAX_COLLISIONS 128U
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
typedef enum {
COLLISION_SHIP,
COLLISION_SHOT,
COLLISION_ASTEROID,
} collision_tag_t;
typedef struct {
vec2_t pos;
vec2_t vel;
mat2_t dir;
float omg;
unsigned shapes[MAX_SHAPES_PER_ENTITY];
unsigned shape_count;
float radius;
collision_tag_t tag;
bool wrap;
bool dead;
} entity_t;
typedef struct {
bool visible;
int entity;
unsigned vert_count;
vec2_t verts[MAX_VERTS];
uint8_t flags;
} shape_t;
typedef struct {
unsigned entities[2];
float vs[2];
vec2_t normal;
} collision_t;
static const vec2_t ship_verts[] = {
{ 0.0, 0.11 },
{ 0.05, -0.07 },
{ 0.0, -0.04 },
{ -0.05, -0.07 },
};
static const vec2_t fire_verts[] = {
{ 0.0, -0.15 },
{ 0.015, -0.07 },
{ -0.015, -0.07 },
};
static const vec2_t shot_verts[] = { { 0.0, -0.02 }, { 0.0, 0.02 } };
static entity_t entities[MAX_ENTITIES];
static mat3_t transforms[MAX_ENTITIES];
static shape_t shapes[MAX_SHAPES];
static unsigned entity_count;
static unsigned shape_count;
static bool dead;
static float aspect;
static unsigned ship_entity_id;
static unsigned ship_shape_id;
static unsigned fire_shape_id;
static entity_t *add_entity(unsigned *id_out)
{
const unsigned id = entity_count++;
memset(entities + id, 0, sizeof(entity_t));
entities[id].dir = (mat2_t) { { 1, 0 }, { 0, 1 } };
if (id_out != nullptr)
*id_out = id;
return entities + id;
}
static shape_t *add_shape(unsigned entity, unsigned *id_out)
{
assert(entities[entity].shape_count < MAX_SHAPES_PER_ENTITY);
const int id = shape_count++;
memset(shapes + id, 0, sizeof(shape_t));
shapes[id].entity = entity;
entities[entity].shapes[entities[entity].shape_count++] = id;
if (id_out != nullptr)
*id_out = id;
return shapes + id;
}
static void shoot()
{
if (entity_count >= MAX_ENTITIES || shape_count >= MAX_SHAPES)
return;
const entity_t *ship = entities + ship_entity_id;
const shape_t *ship_shape = shapes + ship_shape_id;
unsigned id;
entity_t *e = add_entity(&id);
e->dir = ship->dir;
e->pos = vec2_add(
ship->pos, mat2_mul_vec2(ship->dir, ship_shape->verts[0]));
e->vel = vec2_add(
ship->vel, mat2_mul_vec2(ship->dir, (vec2_t) { 0, SHOT_VEL }));
e->radius = SHIP_COLLIDE_R;
e->tag = COLLISION_SHOT;
shape_t *s = add_shape(id, nullptr);
s->visible = true;
s->vert_count = NELEMS(shot_verts);
memcpy(s->verts, shot_verts, sizeof(shot_verts));
}
static entity_t *gen_asteroid(float r_mean, unsigned *id_out)
{
unsigned id;
entity_t *e = add_entity(&id);
e->wrap = true;
e->radius = r_mean;
e->tag = COLLISION_ASTEROID;
shape_t *s = add_shape(id, nullptr);
s->visible = true;
s->flags = WRAP | CONNECT;
const unsigned n
= ASTEROID_MIN_VERTS + rng_uint32() % ASTEROID_VERT_RANGE;
s->vert_count = n;
const float da = 2.0f * PI / n;
for (unsigned i = 0; i < n; ++i) {
const float r = r_mean + rng_plusminus() * ASTEROID_R_JITTER;
const float a
= i * da + ASTEROID_A_JITTER * rng_plusminus() * da / 2;
s->verts[i] = (vec2_t) { r * cosf(a), r * sinf(a) };
}
if (id_out)
*id_out = id;
return e;
}
static bool intersecting(unsigned a, unsigned b)
{
const float sep = vec2_len(vec2_sub(entities[b].pos, entities[a].pos));
return sep <= entities[a].radius + entities[b].radius;
}
static bool intersecting_any(unsigned id)
{
for (unsigned i = 0; i < entity_count; ++i) {
if (i == id)
continue;
if (intersecting(id, i))
return true;
}
return false;
}
static void spawn_asteroid()
{
float r;
const float rnd = rng_canon();
if (rnd < 0.2)
r = ASTEROID_SMALL;
else if (rnd < 0.6)
r = ASTEROID_MEDIUM;
else
r = ASTEROID_LARGE;
unsigned id;
entity_t *e = gen_asteroid(r, &id);
do {
e->pos.y = rng_plusminus();
e->pos.x = aspect * rng_plusminus();
} while (vec2_len(e->pos) < ASTEROID_MIN_DIST || intersecting_any(id));
e->vel = (vec2_t) {
ASTEROID_VEL_JITTER * rng_plusminus(),
ASTEROID_VEL_JITTER * rng_plusminus(),
};
e->omg = ASTEROID_OMG_JITTER * rng_plusminus();
}
static void remove_shape(unsigned id)
{
if (id < shape_count - 1) {
const shape_t *last = shapes + shape_count - 1;
memcpy(shapes + id, last, sizeof(shape_t));
for (unsigned i = 0; i < entities[last->entity].shape_count; ++i) {
if (entities[last->entity].shapes[i] == shape_count - 1) {
entities[last->entity].shapes[i] = id;
break;
}
}
}
--shape_count;
}
static void remove_entity(unsigned id)
{
assert(id != ship_entity_id);
entity_t *e = entities + id;
for (unsigned i = 0; i < e->shape_count; ++i)
remove_shape(e->shapes[i]);
if (id < entity_count - 1) {
const entity_t *last = entities + entity_count - 1;
memcpy(e, last, sizeof(entity_t));
for (unsigned i = 0; i < e->shape_count; ++i)
shapes[e->shapes[i]].entity = id;
}
--entity_count;
}
static void ship_update()
{
entity_t *ship = entities + ship_entity_id;
ship->omg += ROT_PWR * (float)input.spin;
const vec2_t thrust = { 0, (float)input.thrust * LIN_PWR };
const vec2_t acc = mat2_mul_vec2(ship->dir, thrust);
ship->vel = vec2_add(ship->vel, acc);
shape_t *fire = shapes + fire_shape_id;
fire->visible = input.thrust != 0;
fire->verts[0].y = FIRE_MEAN + FIRE_JITTER * rng_plusminus();
}
static unsigned check_collisions(collision_t *out)
{
unsigned count = 0;
for (unsigned i = 0; i < entity_count; ++i) {
for (unsigned j = i + 1; j < entity_count; ++j) {
if (intersecting(i, j)) {
const vec2_t n
= vec2_norm(vec2_sub(entities[j].pos, entities[i].pos));
const float vi = vec2_dot(entities[i].vel, n);
const float vj = vec2_dot(entities[j].vel, n);
if (vi < 0 && vj > 0)
continue;
assert(count < MAX_COLLISIONS);
out[count].entities[0] = i;
out[count].entities[1] = j;
out[count].vs[0] = vi;
out[count].vs[1] = vj;
out[count].normal = n;
++count;
}
}
}
return count;
}
static void bounce(collision_t c)
{
entity_t *a = entities + c.entities[0];
entity_t *b = entities + c.entities[1];
const float ma = a->radius * a->radius;
const float mb = b->radius * b->radius;
const float m = ma + mb;
const vec2_t n = c.normal;
const float va1 = c.vs[0];
const float vb1 = c.vs[1];
const float va2 = (va1 * (ma - mb) + 2 * mb * vb1) / m;
const float vb2 = (vb1 * (mb - ma) + 2 * ma * va1) / m;
a->vel = vec2_add(a->vel, vec2_scale(n, va2 - va1));
b->vel = vec2_add(b->vel, vec2_scale(n, vb2 - vb1));
}
static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
{
a->dead = true;
float r;
if (fabs(a->radius - ASTEROID_LARGE) < 1e-3)
r = ASTEROID_MEDIUM;
else if (fabs(a->radius - ASTEROID_MEDIUM) < 1e-3)
r = ASTEROID_SMALL;
else
return;
const unsigned n = REPLACE_MIN + rng_uint32() % REPLACE_RANGE;
const float da = 2 * PI / n;
const float m = a->radius * a->radius;
const vec2_t p
= vec2_add(vec2_scale(a->vel, m), vec2_scale(shot_vel, SHOT_MASS));
const vec2_t inherit_v = vec2_scale(p, 1 / n * (m + SHOT_MASS));
for (unsigned i = 0; i < n; ++i) {
const float disp_r = REPLACE_R_COEFF * a->radius
+ REPLACE_R_JITTER * rng_plusminus();
const float disp_a = i * da + REPLACE_A_JITTER * rng_plusminus();
const vec2_t disp = { disp_r * cosf(disp_a), disp_r * sinf(disp_a) };
const vec2_t radial_v = vec2_scale(
vec2_norm(disp), REPLACE_RADIAL_V_COEFF * rng_canon());
const vec2_t jitter_v = {
REPLACE_V_JITTER * rng_plusminus(),
REPLACE_V_JITTER * rng_plusminus(),
};
entity_t *e = gen_asteroid(r, nullptr);
e->pos = vec2_add(a->pos, disp);
e->vel = vec2_add(inherit_v, vec2_add(radial_v, jitter_v));
e->omg = a->omg / n + REPLACE_OMG_JITTER * rng_plusminus();
}
}
static void handle_collisions(const collision_t *collisions, unsigned count)
{
for (unsigned i = 0; i < count; ++i) {
const collision_t c = collisions[i];
entity_t *a = entities + c.entities[0];
entity_t *b = entities + c.entities[1];
if (a->tag == COLLISION_SHIP || b->tag == COLLISION_SHIP) {
dead = true;
continue;
}
if (a->tag == COLLISION_SHOT && b->tag == COLLISION_SHOT)
continue;
if (a->tag == COLLISION_ASTEROID && b->tag == COLLISION_ASTEROID) {
bounce(c);
continue;
}
if (a->tag == COLLISION_SHOT) {
a->dead = true;
destroy_asteroid(b, a->vel);
}
if (b->tag == COLLISION_SHOT) {
b->dead = true;
destroy_asteroid(a, b->vel);
}
}
for (unsigned i = 0; i < entity_count; ++i) {
if (entities[i].dead)
remove_entity(i--);
}
}
void game_init(float _aspect)
{
input_on_shoot(shoot);
aspect = _aspect;
dead = false;
entity_count = shape_count = 0;
entity_t *ship = add_entity(&ship_entity_id);
ship->wrap = true;
ship->radius = SHIP_COLLIDE_R;
ship->tag = COLLISION_SHIP;
shape_t *ship_shape = add_shape(ship_entity_id, &ship_shape_id);
ship_shape->visible = true;
ship_shape->flags = WRAP | CONNECT;
ship_shape->vert_count = NELEMS(ship_verts);
memcpy(ship_shape->verts, ship_verts, sizeof(ship_verts));
shape_t *fire = add_shape(ship_entity_id, &fire_shape_id);
fire->visible = true;
fire->flags = WRAP | CONNECT;
fire->vert_count = NELEMS(fire_verts);
memcpy(fire->verts, fire_verts, sizeof(fire_verts));
for (unsigned i = 0; i < INIT_ASTEROIDS; ++i)
spawn_asteroid();
}
void game_update()
{
if (dead)
return;
ship_update();
for (unsigned i = 0; i < entity_count; ++i) {
entity_t *e = entities + i;
e->dir = mat2_mul_mat2(mat2_rotation(e->omg), e->dir);
e->pos = vec2_add(e->pos, e->vel);
if (e->wrap) {
if (e->pos.y > 1)
e->pos.y -= 2;
else if (e->pos.y <= -1)
e->pos.y += 2;
if (e->pos.x >= aspect)
e->pos.x -= 2 * aspect;
else if (e->pos.x < -aspect)
e->pos.x += 2 * aspect;
} else if (
e->pos.y > 1 || e->pos.y <= -1 || e->pos.x >= aspect
|| e->pos.x < -aspect) {
remove_entity(i--);
continue;
}
transforms[i]
= mat3_mul_mat3(mat3_translation(e->pos), mat2_extend(e->dir));
}
static collision_t collisions[MAX_COLLISIONS];
const unsigned ncols = check_collisions(collisions);
handle_collisions(collisions, ncols);
}
void game_draw()
{
renderer_clear();
for (unsigned i = 0; i < shape_count; ++i) {
if (!shapes[i].visible)
continue;
const mat3_t transform = transforms[shapes[i].entity];
renderer_draw(
shapes[i].verts, shapes[i].vert_count, transform,
shapes[i].flags);
}
}