Refactor into more modular architecture
This commit is contained in:
183
asteroids.c
Normal file
183
asteroids.c
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "asteroids.h"
|
||||
|
||||
#include "collisions.h"
|
||||
#include "entity.h"
|
||||
#include "renderer.h"
|
||||
#include "rng.h"
|
||||
#include "scene.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MIN_VERTS 5
|
||||
#define VERT_RANGE (MAX_VERTS - MIN_VERTS)
|
||||
#define A_JITTER 0.8
|
||||
#define V_JITTER 0.001
|
||||
#define ROT_JITTER 0.005
|
||||
#define R_JITTER 0.02
|
||||
#define MIN_DIST 0.8
|
||||
|
||||
#define REPLACE_MIN 3
|
||||
#define REPLACE_MAX 5
|
||||
#define REPLACE_RANGE (REPLACE_MAX - REPLACE_MIN)
|
||||
#define REPLACE_R_COEFF 0.8
|
||||
#define REPLACE_R_JITTER 0.02
|
||||
#define REPLACE_A_JITTER 0.4
|
||||
#define REPLACE_V_JITTER 0.0005
|
||||
#define REPLACE_RADIAL_V_COEFF 0.005
|
||||
#define REPLACE_ROT_JITTER 0.02
|
||||
|
||||
#define COLLIDE_PRIOR 0
|
||||
|
||||
typedef struct {
|
||||
bool valid;
|
||||
unsigned component_id;
|
||||
asteroid_size_t size;
|
||||
} entry_t;
|
||||
|
||||
static asteroids_cb_t clear;
|
||||
static unsigned count;
|
||||
static entry_t entries[MAX_ENTITIES];
|
||||
|
||||
static float radii[] = {
|
||||
[ASTEROID_SMALL] = 0.05f,
|
||||
[ASTEROID_MEDIUM] = 0.1f,
|
||||
[ASTEROID_LARGE] = 0.2f,
|
||||
[ASTEROID_HUGE] = 0.4f,
|
||||
};
|
||||
|
||||
static void update(unsigned new_entity_id, void *ref)
|
||||
{
|
||||
entry_t *old_e = (entry_t *)ref;
|
||||
entry_t *new_e = entries + new_entity_id;
|
||||
memcpy(new_e, old_e, sizeof(entry_t));
|
||||
old_e->valid = false;
|
||||
entity_update_component(new_entity_id, new_e->component_id, new_e);
|
||||
}
|
||||
|
||||
static void remove(void *ref)
|
||||
{
|
||||
entry_t *e = (entry_t *)ref;
|
||||
e->valid = false;
|
||||
--count;
|
||||
if (count == 0)
|
||||
clear();
|
||||
}
|
||||
|
||||
static void
|
||||
create_asteroid(vec2_t pos, vec2_t vel, float rot, asteroid_size_t size)
|
||||
{
|
||||
const float r_mean = radii[size];
|
||||
const unsigned n = MIN_VERTS + rng_uint32() % VERT_RANGE;
|
||||
|
||||
const float da = 2.0f * PI / n;
|
||||
vec2_t verts[n];
|
||||
for (unsigned i = 0; i < n; ++i) {
|
||||
const float r = r_mean + rng_plusminus() * R_JITTER;
|
||||
const float a_jitter = A_JITTER * rng_plusminus() * da / 2;
|
||||
const float a = i * da + a_jitter;
|
||||
verts[i] = (vec2_t) { r * cosf(a), r * sinf(a) };
|
||||
}
|
||||
|
||||
const unsigned id = entity_add();
|
||||
scene_add(id, verts, n, true);
|
||||
physics_add(id, pos, MAT2_ID, vel, rot, r_mean * r_mean);
|
||||
collisions_add(id, r_mean, COLLIDE_PRIOR, physics_bounce);
|
||||
|
||||
entry_t *entry = entries + id;
|
||||
entry->valid = true;
|
||||
entry->component_id = entity_add_component(id, update, remove, entry);
|
||||
entry->size = size;
|
||||
|
||||
++count;
|
||||
}
|
||||
|
||||
static asteroid_size_t draw_size(const asteroid_size_dist_entry_t *dist)
|
||||
{
|
||||
const float r = rng_canon();
|
||||
float acc = 0;
|
||||
for (unsigned i = 0; dist[i].probability != 0.0f; ++i) {
|
||||
acc += dist[i].probability;
|
||||
if (r < acc)
|
||||
return dist[i].size;
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
|
||||
void asteroids_clear()
|
||||
{
|
||||
count = 0;
|
||||
memset(entries, 0, sizeof(entries));
|
||||
}
|
||||
|
||||
void asteroids_on_clear(asteroids_cb_t cb)
|
||||
{
|
||||
clear = cb;
|
||||
}
|
||||
|
||||
void asteroids_add(const asteroid_size_dist_entry_t *dist)
|
||||
{
|
||||
vec2_t pos;
|
||||
do {
|
||||
pos.y = rng_plusminus();
|
||||
pos.x = aspect * rng_plusminus();
|
||||
} while (vec2_len(pos) < MIN_DIST);
|
||||
const vec2_t vel = {
|
||||
V_JITTER * rng_plusminus(),
|
||||
V_JITTER * rng_plusminus(),
|
||||
};
|
||||
const float rot = ROT_JITTER * rng_plusminus();
|
||||
const asteroid_size_t size = draw_size(dist);
|
||||
|
||||
create_asteroid(pos, vel, rot, size);
|
||||
}
|
||||
|
||||
void asteroids_hit(unsigned shot, unsigned asteroid, physics_sep_t)
|
||||
{
|
||||
assert(entries[asteroid].valid);
|
||||
entity_mark(asteroid);
|
||||
|
||||
const asteroid_size_t old_size = entries[asteroid].size;
|
||||
if (old_size == ASTEROID_SMALL)
|
||||
return;
|
||||
const asteroid_size_t size = old_size - 1;
|
||||
const float mass = radii[size] * radii[size];
|
||||
|
||||
const physics_t *phys_shot = physics_get(shot);
|
||||
const physics_t *phys_ast = physics_get(asteroid);
|
||||
const vec2_t p_shot = vec2_scale(phys_shot->vel, phys_shot->mass);
|
||||
const vec2_t p_ast = vec2_scale(phys_ast->vel, phys_ast->mass);
|
||||
|
||||
const unsigned n = REPLACE_MIN + rng_uint32() % REPLACE_RANGE;
|
||||
const float da = 2 * PI / n;
|
||||
|
||||
const vec2_t p = vec2_add(p_shot, p_ast);
|
||||
const vec2_t inherit_v = vec2_scale(p, 1 / (n * mass));
|
||||
|
||||
for (unsigned i = 0; i < n; ++i) {
|
||||
const float r = REPLACE_R_COEFF * radii[old_size]
|
||||
+ REPLACE_R_JITTER * rng_plusminus();
|
||||
const float a = i * da + REPLACE_A_JITTER * rng_plusminus();
|
||||
const vec2_t disp = { r * cosf(a), r * sinf(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(),
|
||||
};
|
||||
|
||||
const vec2_t pos = vec2_add(phys_ast->pos, disp);
|
||||
const vec2_t vel = vec2_add(inherit_v, vec2_add(radial_v, jitter_v));
|
||||
const float rot
|
||||
= phys_ast->rot / n + REPLACE_ROT_JITTER * rng_plusminus();
|
||||
|
||||
create_asteroid(pos, vel, rot, size);
|
||||
}
|
||||
}
|
||||
|
||||
bool asteroids_check(unsigned id)
|
||||
{
|
||||
return entries[id].valid;
|
||||
}
|
||||
27
asteroids.h
Normal file
27
asteroids.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef ASTEROIDS_H
|
||||
#define ASTEROIDS_H
|
||||
|
||||
#include "physics.h"
|
||||
|
||||
typedef void (*asteroids_cb_t)();
|
||||
|
||||
typedef enum {
|
||||
ASTEROID_SMALL,
|
||||
ASTEROID_MEDIUM,
|
||||
ASTEROID_LARGE,
|
||||
ASTEROID_HUGE,
|
||||
} asteroid_size_t;
|
||||
|
||||
typedef struct {
|
||||
float probability;
|
||||
asteroid_size_t size;
|
||||
} asteroid_size_dist_entry_t;
|
||||
|
||||
void asteroids_clear();
|
||||
void asteroids_on_clear(asteroids_cb_t cb);
|
||||
|
||||
void asteroids_add(const asteroid_size_dist_entry_t *dist);
|
||||
void asteroids_hit(unsigned shot, unsigned asteroid, physics_sep_t sep);
|
||||
bool asteroids_check(unsigned id);
|
||||
|
||||
#endif
|
||||
3
build.sh
3
build.sh
@@ -8,4 +8,5 @@ defs="-D_POSIX_C_SOURCE=200809L"
|
||||
|
||||
$cc $warn $flags $libs $defs \
|
||||
-o asteroids \
|
||||
fb.c game.c input.c main.c maths.c renderer.c rng.c text.c
|
||||
asteroids.c collisions.c entity.c fb.c game.c input.c \
|
||||
main.c maths.c physics.c scene.c renderer.c rng.c text.c
|
||||
|
||||
87
collisions.c
Normal file
87
collisions.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "collisions.h"
|
||||
|
||||
#include "entity.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
bool valid;
|
||||
unsigned component_id;
|
||||
float r;
|
||||
unsigned priority;
|
||||
collision_cb_t cb;
|
||||
} entry_t;
|
||||
|
||||
static unsigned max;
|
||||
static entry_t entries[MAX_ENTITIES];
|
||||
|
||||
static void update(unsigned new_entity_id, void *ref)
|
||||
{
|
||||
entry_t *old_e = (entry_t *)ref;
|
||||
entry_t *new_e = entries + new_entity_id;
|
||||
memcpy(new_e, old_e, sizeof(entry_t));
|
||||
old_e->valid = false;
|
||||
entity_update_component(new_entity_id, new_e->component_id, new_e);
|
||||
}
|
||||
|
||||
static void remove(void *ref)
|
||||
{
|
||||
entry_t *e = (entry_t *)ref;
|
||||
e->valid = false;
|
||||
}
|
||||
|
||||
void collisions_init()
|
||||
{
|
||||
max = 0;
|
||||
}
|
||||
|
||||
void collisions_update()
|
||||
{
|
||||
for (unsigned i = 0; i < max; ++i) {
|
||||
if (!entries[i].valid)
|
||||
continue;
|
||||
|
||||
for (unsigned j = i + 1; j < max; ++j) {
|
||||
if (!entries[j].valid)
|
||||
continue;
|
||||
|
||||
const physics_sep_t sep = physics_separation(i, j);
|
||||
if (sep.dist > entries[i].r + entries[j].r)
|
||||
continue;
|
||||
if (sep.va - sep.vb <= 0)
|
||||
continue;
|
||||
|
||||
if (entries[i].priority >= entries[j].priority) {
|
||||
entries[i].cb(i, j, sep);
|
||||
} else {
|
||||
const physics_sep_t flip_sep = {
|
||||
.dist = sep.dist,
|
||||
.norm = vec2_scale(sep.norm, -1),
|
||||
.va = -sep.vb,
|
||||
.vb = -sep.va,
|
||||
};
|
||||
entries[j].cb(j, i, flip_sep);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void collisions_add(
|
||||
unsigned entity, float radius, unsigned priority, collision_cb_t cb)
|
||||
{
|
||||
assert(entity < MAX_ENTITIES);
|
||||
if (entity >= max)
|
||||
max = entity + 1;
|
||||
else
|
||||
assert(!entries[entity].valid);
|
||||
|
||||
entry_t *entry = entries + entity;
|
||||
entry->valid = true;
|
||||
entry->component_id
|
||||
= entity_add_component(entity, update, remove, entry);
|
||||
|
||||
entry->r = radius;
|
||||
entry->priority = priority;
|
||||
entry->cb = cb;
|
||||
}
|
||||
14
collisions.h
Normal file
14
collisions.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef COLLISIONS_H
|
||||
#define COLLISIONS_H
|
||||
|
||||
#include "physics.h"
|
||||
|
||||
typedef void (*collision_cb_t)(unsigned a, unsigned b, physics_sep_t sep);
|
||||
|
||||
void collisions_init();
|
||||
void collisions_update();
|
||||
|
||||
void collisions_add(
|
||||
unsigned entity, float radius, unsigned priority, collision_cb_t cb);
|
||||
|
||||
#endif
|
||||
88
entity.c
Normal file
88
entity.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include "entity.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_COMPONENTS_PER_ENTITY 4U
|
||||
|
||||
typedef struct {
|
||||
update_cb_t update;
|
||||
remove_cb_t remove;
|
||||
void *ref;
|
||||
} component_t;
|
||||
|
||||
typedef struct {
|
||||
bool marked;
|
||||
unsigned count;
|
||||
component_t components[MAX_COMPONENTS_PER_ENTITY];
|
||||
} entity_t;
|
||||
|
||||
static unsigned count;
|
||||
static entity_t entities[MAX_ENTITIES];
|
||||
|
||||
void entities_clear()
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
void entities_purge()
|
||||
{
|
||||
for (unsigned id = count - 1; id < count; --id) {
|
||||
entity_t *e = entities + id;
|
||||
if (!e->marked)
|
||||
continue;
|
||||
|
||||
for (unsigned i = 0; i < e->count; ++i)
|
||||
e->components[i].remove(e->components[i].ref);
|
||||
|
||||
const unsigned last = count - 1;
|
||||
if (id != last) {
|
||||
memcpy(e, entities + last, sizeof(entity_t));
|
||||
for (unsigned i = 0; i < e->count; ++i)
|
||||
e->components[i].update(id, e->components[i].ref);
|
||||
}
|
||||
|
||||
--count;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned entity_add()
|
||||
{
|
||||
assert(count < MAX_ENTITIES);
|
||||
const unsigned id = count++;
|
||||
memset(entities + id, 0, sizeof(entity_t));
|
||||
return id;
|
||||
}
|
||||
|
||||
void entity_mark(unsigned id)
|
||||
{
|
||||
assert(id < count);
|
||||
entities[id].marked = true;
|
||||
}
|
||||
|
||||
unsigned entity_add_component(
|
||||
unsigned id, update_cb_t update, remove_cb_t remove, void *ref)
|
||||
{
|
||||
assert(id < count);
|
||||
entity_t *e = entities + id;
|
||||
|
||||
assert(e->count < MAX_COMPONENTS_PER_ENTITY);
|
||||
const unsigned component_id = e->count++;
|
||||
e->components[component_id] = (component_t) {
|
||||
.update = update,
|
||||
.remove = remove,
|
||||
.ref = ref,
|
||||
};
|
||||
|
||||
return component_id;
|
||||
}
|
||||
|
||||
void entity_update_component(
|
||||
unsigned entity_id, unsigned component_id, void *new_ref)
|
||||
{
|
||||
assert(entity_id < count);
|
||||
entity_t *e = entities + entity_id;
|
||||
|
||||
assert(component_id < e->count);
|
||||
e->components[component_id].ref = new_ref;
|
||||
}
|
||||
20
entity.h
Normal file
20
entity.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
|
||||
#define MAX_ENTITIES 256U
|
||||
|
||||
typedef void (*update_cb_t)(unsigned new_entity_id, void *ref);
|
||||
typedef void (*remove_cb_t)(void *ref);
|
||||
|
||||
void entities_clear();
|
||||
void entities_purge();
|
||||
|
||||
unsigned entity_add();
|
||||
void entity_mark(unsigned id);
|
||||
|
||||
unsigned entity_add_component(
|
||||
unsigned id, update_cb_t update, remove_cb_t remove, void *ref);
|
||||
void entity_update_component(
|
||||
unsigned entity_id, unsigned component_id, void *new_ref);
|
||||
|
||||
#endif
|
||||
536
game.c
536
game.c
@@ -1,8 +1,13 @@
|
||||
#include "game.h"
|
||||
|
||||
#include "asteroids.h"
|
||||
#include "collisions.h"
|
||||
#include "entity.h"
|
||||
#include "input.h"
|
||||
#include "physics.h"
|
||||
#include "renderer.h"
|
||||
#include "rng.h"
|
||||
#include "scene.h"
|
||||
#include "text.h"
|
||||
|
||||
#include <assert.h>
|
||||
@@ -10,88 +15,32 @@
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#define INIT_LEVEL 1
|
||||
|
||||
#define SHIP_COLLIDE_R 0.05
|
||||
#define SHIP_MASS 0.5
|
||||
#define SHIP_MASS 0.02
|
||||
|
||||
#define FIRE_MEAN -0.15
|
||||
#define FIRE_JITTER 0.01
|
||||
|
||||
#define INIT_LEVEL 1
|
||||
|
||||
#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 ASTEROID_SMALL 0.05f
|
||||
#define ASTEROID_MEDIUM 0.1f
|
||||
#define ASTEROID_LARGE 0.2f
|
||||
#define ASTEROID_HUGE 0.4f
|
||||
#define ASTEROID_R_JITTER 0.02
|
||||
#define ASTEROID_MIN_DIST 0.8
|
||||
|
||||
#define REPLACE_MIN 3
|
||||
#define REPLACE_MAX 5
|
||||
#define REPLACE_RANGE (REPLACE_MAX - REPLACE_MIN)
|
||||
#define REPLACE_R_COEFF 0.8
|
||||
#define REPLACE_R_JITTER 0.02
|
||||
#define REPLACE_A_JITTER 0.4
|
||||
#define REPLACE_V_JITTER 0.0005
|
||||
#define REPLACE_RADIAL_V_COEFF 0.005
|
||||
#define REPLACE_OMG_JITTER 0.02
|
||||
|
||||
#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.01
|
||||
|
||||
#define MAX_SHAPES 256U
|
||||
#define MAX_ENTITIES 128U
|
||||
#define MAX_SHAPES_PER_ENTITY 2
|
||||
#define MAX_COLLISIONS 128U
|
||||
#define SHOT_VEL 0.03
|
||||
#define SHOT_COLLIDE_R 0.03
|
||||
#define SHOT_MASS 0.001
|
||||
|
||||
#define ARROW_SCALE 0.025
|
||||
#define ARROW_WIDTH 4
|
||||
#define ARROW_HEIGHT 4
|
||||
|
||||
#define COUNTER_MASK (1 << 6)
|
||||
|
||||
#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 dead;
|
||||
} entity_t;
|
||||
|
||||
typedef struct {
|
||||
bool visible;
|
||||
int entity;
|
||||
unsigned vert_count;
|
||||
vec2_t verts[MAX_VERTS];
|
||||
bool connect;
|
||||
} shape_t;
|
||||
|
||||
typedef struct {
|
||||
unsigned entities[2];
|
||||
float vs[2];
|
||||
vec2_t normal;
|
||||
} collision_t;
|
||||
enum {
|
||||
SHOT_COLLIDE_PRIOR = 1,
|
||||
SHIP_COLLIDE_PRIOR,
|
||||
};
|
||||
|
||||
static const vec2_t ship_verts[] = {
|
||||
{ 0.0, 0.11 },
|
||||
@@ -112,297 +61,37 @@ static const vec2_t arrow_verts[][MAX_VERTS] = {
|
||||
};
|
||||
static const unsigned arrow_counts[NELEMS(arrow_verts)] = { 3, 2 };
|
||||
|
||||
static entity_t entities[MAX_ENTITIES];
|
||||
static mat3_t transforms[MAX_ENTITIES];
|
||||
static shape_t shapes[MAX_SHAPES];
|
||||
static const vec2_t shot_vel = { 0, SHOT_VEL };
|
||||
|
||||
static unsigned entity_count;
|
||||
static unsigned shape_count;
|
||||
static const asteroid_size_dist_entry_t easy_dist[] = {
|
||||
{ 0.5, ASTEROID_MEDIUM },
|
||||
{ 0.5, ASTEROID_LARGE },
|
||||
{ 0, 0 },
|
||||
};
|
||||
static const asteroid_size_dist_entry_t hard_dist[] = {
|
||||
{ 0.4, ASTEROID_MEDIUM },
|
||||
{ 0.5, ASTEROID_LARGE },
|
||||
{ 0.1, ASTEROID_HUGE },
|
||||
{ 0, 0 },
|
||||
};
|
||||
|
||||
static unsigned ship_entity_id;
|
||||
static unsigned fire_scene_id;
|
||||
|
||||
static unsigned level;
|
||||
|
||||
static bool dead;
|
||||
static bool clear;
|
||||
static bool paused;
|
||||
|
||||
static unsigned asteroid_count;
|
||||
static uint8_t counter;
|
||||
static unsigned score;
|
||||
|
||||
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_ID;
|
||||
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;
|
||||
|
||||
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));
|
||||
|
||||
const vec2_t ship_p = vec2_scale(ship->vel, SHIP_MASS);
|
||||
const vec2_t shot_p = vec2_scale(e->vel, SHOT_MASS);
|
||||
const vec2_t new_ship_p = vec2_sub(ship_p, shot_p);
|
||||
ship->vel = vec2_scale(new_ship_p, 1 / SHIP_MASS);
|
||||
}
|
||||
|
||||
static entity_t *gen_asteroid(float r_mean, unsigned *id_out)
|
||||
{
|
||||
unsigned id;
|
||||
entity_t *e = add_entity(&id);
|
||||
e->radius = r_mean;
|
||||
e->tag = COLLISION_ASTEROID;
|
||||
|
||||
shape_t *s = add_shape(id, nullptr);
|
||||
s->visible = true;
|
||||
s->connect = true;
|
||||
|
||||
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.4)
|
||||
r = ASTEROID_MEDIUM;
|
||||
else if (level > 3 && rnd < 0.45)
|
||||
r = ASTEROID_HUGE;
|
||||
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();
|
||||
|
||||
++asteroid_count;
|
||||
}
|
||||
|
||||
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 cleared()
|
||||
{
|
||||
clear = true;
|
||||
renderer_set_wrap(false);
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
|
||||
{
|
||||
a->dead = true;
|
||||
--asteroid_count;
|
||||
++score;
|
||||
|
||||
float r;
|
||||
if (a->radius == ASTEROID_HUGE) {
|
||||
r = ASTEROID_LARGE;
|
||||
} else if (a->radius == ASTEROID_LARGE) {
|
||||
r = ASTEROID_MEDIUM;
|
||||
} else if (a->radius == ASTEROID_MEDIUM) {
|
||||
r = ASTEROID_SMALL;
|
||||
} else {
|
||||
if (asteroid_count == 0)
|
||||
cleared();
|
||||
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();
|
||||
|
||||
++asteroid_count;
|
||||
}
|
||||
renderer_set_wrap(false);
|
||||
physics_escape(ship_entity_id);
|
||||
}
|
||||
|
||||
static void die()
|
||||
@@ -412,40 +101,39 @@ static void die()
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
static void handle_collisions(const collision_t *collisions, unsigned count)
|
||||
static void shot_collide(unsigned a, unsigned b, physics_sep_t sep)
|
||||
{
|
||||
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) {
|
||||
die();
|
||||
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);
|
||||
}
|
||||
entity_mark(a);
|
||||
if (asteroids_check(b)) {
|
||||
++score;
|
||||
asteroids_hit(a, b, sep);
|
||||
} else {
|
||||
entity_mark(b);
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < entity_count; ++i) {
|
||||
if (entities[i].dead)
|
||||
remove_entity(i--);
|
||||
}
|
||||
static void ship_collide(unsigned, unsigned, physics_sep_t)
|
||||
{
|
||||
die();
|
||||
}
|
||||
|
||||
static void shoot()
|
||||
{
|
||||
physics_t *ship = physics_get(ship_entity_id);
|
||||
|
||||
const vec2_t pos
|
||||
= vec2_add(ship->pos, mat2_mul_vec2(ship->dir, ship_verts[0]));
|
||||
const vec2_t vel
|
||||
= vec2_add(ship->vel, mat2_mul_vec2(ship->dir, shot_vel));
|
||||
|
||||
const unsigned id = entity_add();
|
||||
scene_add(id, shot_verts, NELEMS(shot_verts), false);
|
||||
physics_add(id, pos, ship->dir, vel, 0, SHOT_MASS);
|
||||
collisions_add(id, SHOT_COLLIDE_R, SHOT_COLLIDE_PRIOR, shot_collide);
|
||||
|
||||
const vec2_t p_ship = vec2_scale(ship->vel, ship->mass);
|
||||
const vec2_t p_shot = vec2_scale(vel, SHOT_MASS);
|
||||
ship->vel = vec2_scale(vec2_sub(p_ship, p_shot), 1 / SHIP_MASS);
|
||||
}
|
||||
|
||||
static void draw_arrow()
|
||||
@@ -467,32 +155,38 @@ static void draw_arrow()
|
||||
renderer_draw(arrow_verts[i], arrow_counts[i], m, false);
|
||||
}
|
||||
|
||||
static void anim_fire(vec2_t *verts)
|
||||
{
|
||||
verts[0].y = FIRE_MEAN + FIRE_JITTER * rng_plusminus();
|
||||
}
|
||||
|
||||
static void create_field()
|
||||
{
|
||||
dead = false;
|
||||
clear = false;
|
||||
paused = false;
|
||||
entity_count = shape_count = 0;
|
||||
asteroid_count = 0;
|
||||
|
||||
entity_t *ship = add_entity(&ship_entity_id);
|
||||
ship->radius = SHIP_COLLIDE_R;
|
||||
ship->tag = COLLISION_SHIP;
|
||||
renderer_set_wrap(true);
|
||||
|
||||
shape_t *ship_shape = add_shape(ship_entity_id, &ship_shape_id);
|
||||
ship_shape->visible = true;
|
||||
ship_shape->connect = true;
|
||||
ship_shape->vert_count = NELEMS(ship_verts);
|
||||
memcpy(ship_shape->verts, ship_verts, sizeof(ship_verts));
|
||||
entities_clear();
|
||||
scene_clear();
|
||||
physics_init();
|
||||
collisions_init();
|
||||
|
||||
shape_t *fire = add_shape(ship_entity_id, &fire_shape_id);
|
||||
fire->visible = true;
|
||||
fire->connect = true;
|
||||
fire->vert_count = NELEMS(fire_verts);
|
||||
memcpy(fire->verts, fire_verts, sizeof(fire_verts));
|
||||
ship_entity_id = entity_add();
|
||||
scene_add(ship_entity_id, ship_verts, NELEMS(ship_verts), true);
|
||||
physics_add(
|
||||
ship_entity_id, (vec2_t) {}, MAT2_ID, (vec2_t) {}, 0, SHIP_MASS);
|
||||
collisions_add(
|
||||
ship_entity_id, SHIP_COLLIDE_R, SHIP_COLLIDE_PRIOR, ship_collide);
|
||||
|
||||
fire_scene_id
|
||||
= scene_add(ship_entity_id, fire_verts, NELEMS(fire_verts), true);
|
||||
scene_animate(fire_scene_id, anim_fire);
|
||||
|
||||
for (unsigned i = 0; i < level; ++i)
|
||||
spawn_asteroid();
|
||||
asteroids_add(level < 4 ? easy_dist : hard_dist);
|
||||
}
|
||||
|
||||
static void win()
|
||||
@@ -506,6 +200,7 @@ static void reset()
|
||||
{
|
||||
level = INIT_LEVEL;
|
||||
score = 0;
|
||||
asteroids_clear();
|
||||
create_field();
|
||||
}
|
||||
|
||||
@@ -515,11 +210,31 @@ static void pause()
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
static void ship_update()
|
||||
{
|
||||
physics_t *ship = physics_get(ship_entity_id);
|
||||
|
||||
ship->rot += ROT_PWR * (float)input.spin;
|
||||
|
||||
if (input.thrust != 0) {
|
||||
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);
|
||||
scene_show(fire_scene_id);
|
||||
} else {
|
||||
scene_hide(fire_scene_id);
|
||||
}
|
||||
|
||||
if (clear && ship->pos.x > aspect)
|
||||
win();
|
||||
}
|
||||
|
||||
void game_init()
|
||||
{
|
||||
input_on_shoot(shoot);
|
||||
input_on_restart(reset);
|
||||
input_on_pause(pause);
|
||||
asteroids_on_clear(cleared);
|
||||
|
||||
reset();
|
||||
}
|
||||
@@ -532,49 +247,18 @@ void game_update()
|
||||
return;
|
||||
|
||||
ship_update();
|
||||
physics_update();
|
||||
collisions_update();
|
||||
scene_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 (clear && i == ship_entity_id) {
|
||||
if (e->pos.x > aspect)
|
||||
win();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
entities_purge();
|
||||
}
|
||||
|
||||
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].connect);
|
||||
}
|
||||
scene_draw();
|
||||
|
||||
text_draw_score(score);
|
||||
|
||||
@@ -582,10 +266,8 @@ void game_draw()
|
||||
text_draw_centre("PAUSED");
|
||||
return;
|
||||
}
|
||||
|
||||
if (dead && !(counter & COUNTER_MASK))
|
||||
text_draw_centre("GAME OVER");
|
||||
|
||||
if (clear) {
|
||||
draw_arrow();
|
||||
if (!(counter & COUNTER_MASK))
|
||||
|
||||
129
physics.c
Normal file
129
physics.c
Normal file
@@ -0,0 +1,129 @@
|
||||
#include "physics.h"
|
||||
|
||||
#include "entity.h"
|
||||
#include "renderer.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
bool valid;
|
||||
unsigned component_id;
|
||||
physics_t state;
|
||||
} entry_t;
|
||||
|
||||
static unsigned max;
|
||||
static unsigned escape_entity;
|
||||
static entry_t entries[MAX_ENTITIES];
|
||||
|
||||
static void update(unsigned new_entity_id, void *ref)
|
||||
{
|
||||
entry_t *old_e = (entry_t *)ref;
|
||||
entry_t *new_e = entries + new_entity_id;
|
||||
memcpy(new_e, old_e, sizeof(entry_t));
|
||||
old_e->valid = false;
|
||||
entity_update_component(new_entity_id, new_e->component_id, new_e);
|
||||
}
|
||||
|
||||
static void remove(void *ref)
|
||||
{
|
||||
entry_t *e = (entry_t *)ref;
|
||||
e->valid = false;
|
||||
}
|
||||
|
||||
void physics_init()
|
||||
{
|
||||
max = 0;
|
||||
escape_entity = MAX_ENTITIES;
|
||||
}
|
||||
|
||||
void physics_update()
|
||||
{
|
||||
for (unsigned i = 0; i < max; ++i) {
|
||||
if (!entries[i].valid)
|
||||
continue;
|
||||
physics_t *st = &entries[i].state;
|
||||
|
||||
st->dir = mat2_mul_mat2(mat2_rotation(st->rot), st->dir);
|
||||
st->pos = vec2_add(st->pos, st->vel);
|
||||
|
||||
if (st->pos.y > 1)
|
||||
st->pos.y -= 2;
|
||||
else if (st->pos.y <= -1)
|
||||
st->pos.y += 2;
|
||||
|
||||
if (st->pos.x >= aspect && i != escape_entity)
|
||||
st->pos.x -= 2 * aspect;
|
||||
else if (st->pos.x < -aspect)
|
||||
st->pos.x += 2 * aspect;
|
||||
}
|
||||
}
|
||||
|
||||
void physics_add(
|
||||
unsigned entity, vec2_t pos, mat2_t dir, vec2_t vel, float rot,
|
||||
float mass)
|
||||
{
|
||||
assert(entity < MAX_ENTITIES);
|
||||
if (entity >= max)
|
||||
max = entity + 1;
|
||||
else
|
||||
assert(!entries[entity].valid);
|
||||
|
||||
entry_t *entry = entries + entity;
|
||||
entry->valid = true;
|
||||
entry->component_id
|
||||
= entity_add_component(entity, update, remove, entry);
|
||||
|
||||
entry->state = (physics_t) {
|
||||
.pos = pos,
|
||||
.dir = dir,
|
||||
.vel = vel,
|
||||
.rot = rot,
|
||||
.mass = mass,
|
||||
};
|
||||
}
|
||||
|
||||
physics_t *physics_get(unsigned entity)
|
||||
{
|
||||
assert(entity < max);
|
||||
assert(entries[entity].valid);
|
||||
return &entries[entity].state;
|
||||
}
|
||||
|
||||
void physics_escape(unsigned entity)
|
||||
{
|
||||
assert(entity < max);
|
||||
assert(entries[entity].valid);
|
||||
escape_entity = entity;
|
||||
}
|
||||
|
||||
physics_sep_t physics_separation(unsigned a, unsigned b)
|
||||
{
|
||||
const physics_t *pa = physics_get(a);
|
||||
const physics_t *pb = physics_get(b);
|
||||
|
||||
const vec2_t disp = vec2_sub(pb->pos, pa->pos);
|
||||
const vec2_t norm = vec2_norm(disp);
|
||||
|
||||
return (physics_sep_t) {
|
||||
.dist = vec2_len(disp),
|
||||
.norm = norm,
|
||||
.va = vec2_dot(pa->vel, norm),
|
||||
.vb = vec2_dot(pb->vel, norm),
|
||||
};
|
||||
}
|
||||
|
||||
void physics_bounce(unsigned a, unsigned b, physics_sep_t sep)
|
||||
{
|
||||
physics_t *pa = physics_get(a);
|
||||
physics_t *pb = physics_get(b);
|
||||
|
||||
const float ma = pa->mass;
|
||||
const float mb = pb->mass;
|
||||
const float ms = ma + mb;
|
||||
const float va = (sep.va * (ma - mb) + 2 * mb * sep.vb) / ms;
|
||||
const float vb = (sep.vb * (mb - ma) + 2 * ma * sep.va) / ms;
|
||||
|
||||
pa->vel = vec2_add(pa->vel, vec2_scale(sep.norm, va - sep.va));
|
||||
pb->vel = vec2_add(pb->vel, vec2_scale(sep.norm, vb - sep.vb));
|
||||
}
|
||||
31
physics.h
Normal file
31
physics.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef PHYSICS_H
|
||||
#define PHYSICS_H
|
||||
|
||||
#include "maths.h"
|
||||
|
||||
typedef struct {
|
||||
vec2_t pos;
|
||||
mat2_t dir;
|
||||
vec2_t vel;
|
||||
float rot;
|
||||
float mass;
|
||||
} physics_t;
|
||||
|
||||
typedef struct {
|
||||
float dist, va, vb;
|
||||
vec2_t norm;
|
||||
} physics_sep_t;
|
||||
|
||||
void physics_init();
|
||||
void physics_update();
|
||||
|
||||
void physics_add(
|
||||
unsigned entity, vec2_t pos, mat2_t dir, vec2_t vel, float rot,
|
||||
float mass);
|
||||
physics_t *physics_get(unsigned entity);
|
||||
|
||||
void physics_escape(unsigned entity);
|
||||
physics_sep_t physics_separation(unsigned a, unsigned b);
|
||||
void physics_bounce(unsigned a, unsigned b, physics_sep_t sep);
|
||||
|
||||
#endif
|
||||
105
scene.c
Normal file
105
scene.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "scene.h"
|
||||
|
||||
#include "entity.h"
|
||||
#include "physics.h"
|
||||
#include "renderer.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_SHAPES 256U
|
||||
|
||||
typedef struct {
|
||||
unsigned entity_id;
|
||||
unsigned component_id;
|
||||
unsigned vert_count;
|
||||
vec2_t verts[MAX_VERTS];
|
||||
scene_anim_cb_t anim_cb;
|
||||
bool hidden;
|
||||
bool connect;
|
||||
} shape_t;
|
||||
|
||||
static unsigned count;
|
||||
static shape_t shapes[MAX_SHAPES];
|
||||
|
||||
static void update(unsigned new_entity_id, void *ref)
|
||||
{
|
||||
shape_t *s = (shape_t *)ref;
|
||||
s->entity_id = new_entity_id;
|
||||
}
|
||||
|
||||
static void remove(void *ref)
|
||||
{
|
||||
shape_t *s = (shape_t *)ref;
|
||||
const shape_t *last = shapes + (count - 1);
|
||||
if (s < last) {
|
||||
memcpy(s, last, sizeof(shape_t));
|
||||
entity_update_component(s->entity_id, s->component_id, s);
|
||||
}
|
||||
--count;
|
||||
}
|
||||
|
||||
static mat3_t get_transform(unsigned entity_id)
|
||||
{
|
||||
const physics_t *phys = physics_get(entity_id);
|
||||
return mat3_mul_mat3(
|
||||
mat3_translation(phys->pos), mat2_extend(phys->dir));
|
||||
}
|
||||
|
||||
void scene_clear()
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
void scene_update()
|
||||
{
|
||||
for (unsigned i = 0; i < count; ++i) {
|
||||
if (shapes[i].anim_cb != nullptr)
|
||||
shapes[i].anim_cb(shapes[i].verts);
|
||||
}
|
||||
}
|
||||
|
||||
void scene_draw()
|
||||
{
|
||||
for (unsigned i = 0; i < count; ++i) {
|
||||
const shape_t *s = shapes + i;
|
||||
if (!s->hidden) {
|
||||
const mat3_t m = get_transform(s->entity_id);
|
||||
renderer_draw(s->verts, s->vert_count, m, s->connect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned scene_add(
|
||||
unsigned entity, const vec2_t *verts, unsigned vert_count, bool connect)
|
||||
{
|
||||
assert(count < MAX_SHAPES);
|
||||
const unsigned id = count++;
|
||||
shape_t *s = shapes + id;
|
||||
*s = (shape_t) {
|
||||
.entity_id = entity,
|
||||
.vert_count = vert_count,
|
||||
.connect = connect,
|
||||
};
|
||||
memcpy(s->verts, verts, vert_count * sizeof(vec2_t));
|
||||
s->component_id = entity_add_component(entity, update, remove, s);
|
||||
return id;
|
||||
}
|
||||
|
||||
void scene_hide(unsigned id)
|
||||
{
|
||||
assert(id < count);
|
||||
shapes[id].hidden = true;
|
||||
}
|
||||
|
||||
void scene_show(unsigned id)
|
||||
{
|
||||
assert(id < count);
|
||||
shapes[id].hidden = false;
|
||||
}
|
||||
|
||||
void scene_animate(unsigned id, scene_anim_cb_t cb)
|
||||
{
|
||||
assert(id < count);
|
||||
shapes[id].anim_cb = cb;
|
||||
}
|
||||
18
scene.h
Normal file
18
scene.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef SCENE_H
|
||||
#define SCENE_H
|
||||
|
||||
#include "maths.h"
|
||||
|
||||
typedef void (*scene_anim_cb_t)(vec2_t *verts);
|
||||
|
||||
void scene_clear();
|
||||
void scene_update();
|
||||
void scene_draw();
|
||||
|
||||
unsigned scene_add(
|
||||
unsigned entity, const vec2_t *verts, unsigned vert_count, bool connect);
|
||||
void scene_hide(unsigned id);
|
||||
void scene_show(unsigned id);
|
||||
void scene_animate(unsigned id, scene_anim_cb_t cb);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user