This commit is contained in:
2024-11-26 07:30:29 +02:00
parent 35718723e9
commit c52ccce29b
+66 -24
View File
@@ -4,14 +4,10 @@
#define STB_DS_IMPLEMENTATION #define STB_DS_IMPLEMENTATION
#include "libs/stb_ds.h" #include "libs/stb_ds.h"
#define WIN_WIDTH 1000.0 #define WIN_WIDTH 800.0
#define WIN_HEIGHT 800.0 #define WIN_HEIGHT 450.0
#define BG CLITERAL(Color){0x18, 0x18, 0x18, 0xFF} #define BG CLITERAL(Color){0x18, 0x18, 0x18, 0xFF}
// Adjust gravity value for smoother fall
#define GRAVITY 1000.0f
#define MAX_SPEED 16.0f
typedef enum typedef enum
{ {
Circle Circle
@@ -19,6 +15,7 @@ typedef enum
typedef struct { typedef struct {
Vector2 position; Vector2 position;
Vector2 velocity;
Color color; Color color;
int size; int size;
entity_type type; entity_type type;
@@ -27,6 +24,7 @@ typedef struct {
typedef struct { typedef struct {
bool is_running; bool is_running;
Entity* entities; Entity* entities;
int entity_count;
} App; } App;
void render_entities(Entity* entities, int count) void render_entities(Entity* entities, int count)
@@ -45,19 +43,68 @@ void update_entities(Entity* entities, int count, float deltaTime)
{ {
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
// Calculate new position based on gravity and deltaTime Entity* ball = &entities[i];
float fallSpeed = GRAVITY * deltaTime;
// Clamp the fall speed to prevent choppiness
if (fallSpeed > MAX_SPEED) fallSpeed = MAX_SPEED;
// Update the position of each entity // Update position based on velocity
if(!(entities[i].position.y >= WIN_HEIGHT - entities[i].size)) ball->position.y += ball->velocity.y;
{ ball->position.x += ball->velocity.x;
entities[i].position.y += fallSpeed;
// Check Y boundaries (top and bottom)
if (ball->position.y >= (GetScreenHeight() - ball->size)) {
if (ball->velocity.y > 0) {
int randVelocity = GetRandomValue(-15, -5);
ball->velocity.y = (float)randVelocity;
}
}
else if (ball->position.y <= ball->size) {
if (ball->velocity.y < 0) {
int randVelocity = GetRandomValue(5, 15);
ball->velocity.y = (float)randVelocity;
}
}
if (ball->position.x >= (GetScreenWidth() - ball->size)) {
if (ball->velocity.x > 0) {
int randVelocity = GetRandomValue(-15, -5);
ball->velocity.x = (float)randVelocity;
}
}
else if (ball->position.x <= ball->size) {
if (ball->velocity.x < 0) {
int randVelocity = GetRandomValue(5, 15);
ball->velocity.x = (float)randVelocity;
}
} }
} }
} }
void generate_entities(App* app, int count)
{
while(app->entity_count < count) {
int rand_grav_x = GetRandomValue(-10, 10);
int rand_grav_y = GetRandomValue(5, 15);
int rand_size = GetRandomValue(15, 30);
Entity c = {
.position = { WIN_WIDTH / 2, WIN_HEIGHT / 2 },
.velocity = {(float)rand_grav_x, (float)rand_grav_y},
.color = (Color){
GetRandomValue(0, 255),
GetRandomValue(0, 255),
GetRandomValue(0, 255),
255
},
.size = rand_size,
.type = Circle
};
arrput(app->entities, c);
app->entity_count++;
}
return;
}
int main(void) { int main(void) {
@@ -65,17 +112,10 @@ int main(void) {
Entity* entity_array = NULL; Entity* entity_array = NULL;
Entity c = {
{ WIN_WIDTH / 2, 0 },
RED,
20,
Circle
};
arrput(entity_array,c);
App my_app = { App my_app = {
true, .is_running = true,
entity_array .entities = entity_array,
.entity_count = 0
}; };
// raylib begin // raylib begin
@@ -89,6 +129,8 @@ int main(void) {
{ {
float dt = GetFrameTime(); float dt = GetFrameTime();
generate_entities(&my_app, 333);
BeginDrawing(); BeginDrawing();
ClearBackground(BG); ClearBackground(BG);