summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmin Mesbah <mesbahamin@gmail.com>2017-12-17 21:22:35 -0800
committerAmin Mesbah <mesbahamin@gmail.com>2017-12-17 21:22:35 -0800
commitb372c824cbe993191425a7315350bde81ba3e67b (patch)
treeb2091d6b2e047bb03a35544c3d8aed3826ac0569
parent91661f8784aa01957cd91ec8bdabdec4ee89a7d3 (diff)
downloadohsp-b372c824cbe993191425a7315350bde81ba3e67b.zip
ohsp-b372c824cbe993191425a7315350bde81ba3e67b.tar.gz
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.
-rw-r--r--src/entity.h23
-rw-r--r--src/game.c55
-rw-r--r--src/game.h2
3 files changed, 79 insertions, 1 deletions
diff --git a/src/entity.h b/src/entity.h
index 25ac435..81762b2 100644
--- 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
index ee6c962..c79a614 100644
--- 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
index 52d0654..7a74ac2 100644
--- 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