From c38a7c4bca5b1efcdc2fb83551c34f28a8069026 Mon Sep 17 00:00:00 2001
From: nathansmithsmith <nathansmith7@mailfence.com>
Date: Sun, 5 Nov 2023 17:58:22 -0700
Subject: Better star system

---
 src/gameScreen.c | 35 ++++-------------------------------
 src/gameScreen.h |  3 +++
 src/settings.c   |  2 +-
 src/stars.c      | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/stars.h      | 19 +++++++++++++++++++
 5 files changed, 78 insertions(+), 32 deletions(-)
 create mode 100644 src/stars.c
 create mode 100644 src/stars.h

(limited to 'src')

diff --git a/src/gameScreen.c b/src/gameScreen.c
index 036107d..ce9bcb5 100644
--- a/src/gameScreen.c
+++ b/src/gameScreen.c
@@ -42,6 +42,8 @@ void initGameScreen(Game * game, GameScreen * gameScreen) {
 
 	// Zoom view.
 	gameScreen->zoomViewTexture = LoadRenderTexture(GAME_SCREEN_ZOOM_VIEW_SIZE, GAME_SCREEN_ZOOM_VIEW_SIZE);
+
+	initStars(&gameScreen->stars);
 }
 
 void freeGameScreen(GameScreen * gameScreen) {
@@ -243,40 +245,11 @@ void gameScreenHandleLevels(Game * game, GameScreen * gameScreen) {
 	}
 }
 
-void renderStars(Game * game) {
-	Entity * player = getEntityFromWorld(game->world, 0);
-
-	float starSpacing = 30.0;
-
-	Vector3 startPosition = player->position;
-	startPosition = Vector3Scale(startPosition, 1.0 / starSpacing);
-	startPosition = (Vector3){(int)startPosition.x, (int)startPosition.y, (int)startPosition.z};
-	startPosition = Vector3Scale(startPosition, starSpacing);
-
-	Vector3 endAt = Vector3Subtract(player->position, startPosition);
-	endAt = Vector3Zero();
-	endAt = Vector3AddValue(endAt, 200.0);
-
-	for (float z = -endAt.z; z < endAt.z; z += starSpacing) {
-		for (float y = -endAt.y; y < endAt.y; y += starSpacing) {
-			for (float x = -endAt.x; x < endAt.x; x += starSpacing) {
-				Vector3 starPosition = Vector3Add((Vector3){x, y, z}, startPosition);
-				float starDistance = Vector3Distance(player->position, starPosition);
-
-				if (starDistance < 100.0)
-					continue;
-
-				DrawPoint3D(starPosition, (Color){0xff, 0xff, 0xff, 0xff - (starDistance * 10.0)});
-			}
-		}
-	}
-}
-
 void renderWorldGameScreen(Game * game, GameScreen * gameScreen) {
 	BeginMode3D(game->cameras[gameScreen->mainCamera]);
 
-	//DrawModel(game->assets.models[SKY_ASSET], Vector3Zero(), 500.0, WHITE);
-	renderStars(game);
+	//DrawModel(game->assets.models[SKY_ASSET], getEntityFromWorld(game->world, 0)->position, 500.0, WHITE);
+	drawStars(game, &gameScreen->stars);
 
 	// Draw world.
 	drawWorld(&game->world, game);
diff --git a/src/gameScreen.h b/src/gameScreen.h
index 112897c..bedee78 100644
--- a/src/gameScreen.h
+++ b/src/gameScreen.h
@@ -2,6 +2,7 @@
 #include "gyroscope.h"
 #include "radar.h"
 #include "cameras.h"
+#include "stars.h"
 
 #ifndef GAME_SCREEN_H
 #define GAME_SCREEN_H
@@ -31,6 +32,8 @@ typedef struct GameScreen {
 
     RenderTexture worldRender;
     bool usingWorldRenderTexture;
+
+    Stars stars;
 } GameScreen;
 
 void initGameScreen(Game * game, GameScreen * gameScreen);
diff --git a/src/settings.c b/src/settings.c
index 8f35206..889867c 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -12,7 +12,7 @@ void initSettings(Settings * settings) {
 		.rollStick = 2,
 		.speedStick = 3,
 		.joystickSensitivity = 0.5,
-		.fps = 60.0,
+		.fps = 0.0,
 		.drawFps = true,
 		.renderWidth = 640,
 		.renderHeight = 360,
diff --git a/src/stars.c b/src/stars.c
new file mode 100644
index 0000000..2b036e9
--- /dev/null
+++ b/src/stars.c
@@ -0,0 +1,51 @@
+#include "stars.h"
+#include "world.h"
+#include "game.h"
+#include "entity.h"
+
+void initStars(Stars * stars) {
+    int i;
+
+    for (i = 0; i < STAR_COUNT; ++i)
+        stars->starsUsed[i] = false;
+}
+
+Vector3 generateStar() {
+    SetRandomSeed(clock());
+
+    // Get direction.
+    Vector3 star = (Vector3){
+        GetRandomValue(1000, -1000),
+        GetRandomValue(1000, -1000),
+        GetRandomValue(1000, -1000)
+    };
+
+    star = Vector3Normalize(star);
+
+    // Move far away.
+    float distance = STAR_MAX_DISTANCE;
+    star = Vector3Scale(star, distance);
+
+    return star;
+}
+
+void drawStars(Game * game, Stars * stars) {
+    int i;
+
+    Entity * player = getEntityFromWorld(game->world, 0);
+
+    for (i = 0; i < STAR_COUNT; ++i) {
+        if (stars->starsUsed[i]) {
+            DrawSphereEx(stars->stars[i], 0.5 - (Vector3Distance(stars->stars[i], player->position) * 0.005), 4, 4, WHITE);
+
+            // Flag for reset if to close or far.
+            float distance = Vector3Distance(stars->stars[i], player->position);
+
+            if (distance < STAR_MIN_DISTANCE || distance > STAR_MAX_DISTANCE)
+                stars->starsUsed[i] = false;
+        } else { // Reset star.
+            stars->starsUsed[i] = true;
+            stars->stars[i] = Vector3Add(player->position, generateStar());
+        }
+    }
+}
diff --git a/src/stars.h b/src/stars.h
new file mode 100644
index 0000000..aa30fa4
--- /dev/null
+++ b/src/stars.h
@@ -0,0 +1,19 @@
+#include "gameCommon.h"
+
+#ifndef STARS_H
+#define STARS_H
+
+#define STAR_COUNT 64
+
+#define STAR_MIN_DISTANCE 400.0
+#define STAR_MAX_DISTANCE 1000.0
+
+typedef struct Stars {
+    Vector3 stars[STAR_COUNT];
+    bool starsUsed[STAR_COUNT];
+} Stars;
+
+void initStars(Stars * stars);
+void drawStars(Game * game, Stars * stars);
+
+#endif
-- 
cgit v1.2.3