commit 9c351ad49f9342d7393aa8dc8e523db6136da2de
parent a4782159caa589e640728e77294a69ff486315bf
Author: amin <dev@aminmesbah.com>
Date: Mon, 18 Dec 2017 05:22:34 +0000
Make a bunch of stars float around
I also made the player smaller.
Avoiding the stars is fun. I should make a win and lose state based on
that, then work on the controls a bit (deadzone, etc.).
I'm trying to get used to taking a data-oriented approach. It's tricky
to shake the OOP drilled into my brain, but I'm finding it to be a
valuable exercise. It certainly makes it easier to not worry about
everything being perfect at the very beginning.
FossilOrigin-Name: c0030736eb15485cf63ec90f14fe89382fecb7d5a31784b4b3cb3889f076eaa2
Diffstat:
3 files changed, 79 insertions(+), 1 deletion(-)
diff --git a/src/entity.h b/src/entity.h
@@ -4,6 +4,8 @@
#include <stdbool.h>
#include <stdint.h>
+#define ENTITIES_MAX 100
+
#ifndef M_PI
#define M_PI 3.141592f
#endif
@@ -27,6 +29,7 @@ struct Vec2d
};
};
+// TODO: get rid of this or rename it
struct Entity
{
float mass;
@@ -36,6 +39,26 @@ struct Entity
uint32_t color;
};
+struct EntityRender
+{
+ float size;
+ uint32_t color;
+};
+
+struct EntityPhysical
+{
+ struct Vec2d velocity;
+ float mass;
+};
+
+struct Entities
+{
+ struct Vec2d positions[ENTITIES_MAX];
+ struct EntityRender render[ENTITIES_MAX];
+ struct EntityPhysical physics[ENTITIES_MAX];
+ uint32_t num_entities;
+};
+
void entity_accelerate(struct Entity *e, struct Vec2d *v);
struct Vec2d vec2d_add(struct Vec2d v0, struct Vec2d v1);
struct Vec2d vec2d_add_c(float x0, float y0, float x1, float y1);
diff --git a/src/game.c b/src/game.c
@@ -1,14 +1,27 @@
#include "game.h"
// TODO: remove this
#include <math.h>
+#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
+// TODO: move this into the gamestate struct
bool PAUSED = false;
+// TODO: move to util
+float rand_float(float min, float max)
+{
+ assert(min < max);
+ float random = ((float) rand()) / (float) RAND_MAX;
+ float diff = max - min;
+ float r = random * diff;
+ return min + r;
+}
+
+
void game_init(struct GameState *game_state, int field_width, int field_height)
{
if (!game_state)
@@ -18,7 +31,7 @@ void game_init(struct GameState *game_state, int field_width, int field_height)
}
game_state->player.mass = 10;
- game_state->player.size = 30;
+ game_state->player.size = 20;
game_state->player.color = 0x888888;
game_state->player.position.x = field_width / 2;
game_state->player.position.y = field_height / 2;
@@ -35,9 +48,30 @@ void game_init(struct GameState *game_state, int field_width, int field_height)
game_state->view.dx = 0;
game_state->view.dy = 0;
game_state->view.zoom = 1;
+
+ // TODO: change to calloc?
+ game_state->entities = malloc(sizeof (struct Entities));
+ assert(game_state->entities);
+ game_state->entities->num_entities = ENTITIES_MAX;
+ assert(game_state->entities->num_entities == ENTITIES_MAX);
+
+ {
+ float max_speed = 0.3f;
+ for (uint32_t i = 0; i < game_state->entities->num_entities; ++i)
+ {
+ game_state->entities->physics[i].mass = 10.0f;
+ game_state->entities->physics[i].velocity.x = rand_float(-max_speed, max_speed);
+ game_state->entities->physics[i].velocity.y = rand_float(-max_speed, max_speed);
+ game_state->entities->positions[i].x = rand() % field_width;
+ game_state->entities->positions[i].y = rand() % field_height;
+ game_state->entities->render[i].color = 0xFFFFFF;
+ game_state->entities->render[i].size = 10.0f;
+ }
+ }
}
+// TODO: move to util
float wrap(float n, float min, float max)
{
if (n > max)
@@ -84,6 +118,16 @@ void game_update(struct GameState *game_state, struct GameControllerInput game_i
player->position.y += player->velocity.y;
player->position.x = wrap(player->position.x, 0, field_width);
player->position.y = wrap(player->position.y, 0, field_height);
+
+ { // other entities
+ for (size_t i = 0; i < game_state->entities->num_entities; ++i)
+ {
+ game_state->entities->positions[i].x += game_state->entities->physics[i].velocity.x;
+ game_state->entities->positions[i].y += game_state->entities->physics[i].velocity.y;
+ game_state->entities->positions[i].x = wrap(game_state->entities->positions[i].x, 0, field_width);
+ game_state->entities->positions[i].y = wrap(game_state->entities->positions[i].y, 0, field_height);
+ }
+ }
}
@@ -106,6 +150,14 @@ void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *gam
player.position.y + player_vec.y,
5,
0x00FF00);
+
+ for (size_t i = 0; i < game_state->entities->num_entities; ++i)
+ {
+ game_set_pixel(buffer,
+ game_state->entities->positions[i].x,
+ game_state->entities->positions[i].y,
+ game_state->entities->render[i].color);
+ }
}
@@ -356,4 +408,5 @@ void game_cleanup(struct GameState *game_state)
// TODO: handle invalid pointer error
return;
}
+ free(game_state->entities);
}
diff --git a/src/game.h b/src/game.h
@@ -40,6 +40,7 @@ struct GameControllerInput
struct GameView
{
+ // TODO: Use a vec2d here
float dx;
float dy;
float zoom;
@@ -52,6 +53,7 @@ struct GameState
struct Vec2d thrust_vector02;
struct Vec2d thrust_vector_sum;
struct GameView view;
+ struct Entities *entities;
};
struct OffscreenBuffer