change convention: from allman to K&R. add: gravity component

This commit is contained in:
2026-03-07 22:11:16 +01:00
parent f307470f56
commit 0220af27bc
12 changed files with 40 additions and 54 deletions

12
src/components/gravity.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef GRAVITY_H
#define GRAVITY_H
#include "raylib.h"
#include "entity.h"
typedef struct {
EntityId id;
int a;
} GravityComponent;
#endif

View File

@@ -2,8 +2,7 @@
#define MAX_ENTITIES 100 #define MAX_ENTITIES 100
typedef struct typedef struct {
{
Camera2D camera; Camera2D camera;
bool running; bool running;
int score; int score;
@@ -21,8 +20,7 @@ VelocityComponent *velocities = NULL;
MovementIntent *intents = NULL; MovementIntent *intents = NULL;
SpriteComponent *sprites = NULL; SpriteComponent *sprites = NULL;
Result InitGame(void) Result InitGame(void) {
{
game.camera = (Camera2D){0}; game.camera = (Camera2D){0};
game.running = true; game.running = true;
game.score = 0; game.score = 0;
@@ -65,8 +63,7 @@ int AddEntity(void) {
return id; return id;
} }
void UpdateGame(float dt) void UpdateGame(float dt) {
{
HandleWindowEvents(); HandleWindowEvents();
InputSystemUpdate(entities, inputs, entityCount); InputSystemUpdate(entities, inputs, entityCount);
@@ -75,8 +72,7 @@ void UpdateGame(float dt)
AnimationSystemUpdate(entities, sprites, intents, entityCount, dt); AnimationSystemUpdate(entities, sprites, intents, entityCount, dt);
} }
void DrawGame(void) void DrawGame(void) {
{
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
RenderSystemDraw(entities, positions, sprites, entityCount); RenderSystemDraw(entities, positions, sprites, entityCount);

View File

@@ -4,8 +4,7 @@ const char *TITLE = "Punk n' Spray";
const int WIDTH = 900; const int WIDTH = 900;
const int HEIGHT = 600; const int HEIGHT = 600;
Result InitGameWindow(void) Result InitGameWindow(void) {
{
SetConfigFlags(FLAG_WINDOW_HIGHDPI); SetConfigFlags(FLAG_WINDOW_HIGHDPI);
InitWindow(WIDTH, HEIGHT, TITLE); InitWindow(WIDTH, HEIGHT, TITLE);
@@ -16,8 +15,7 @@ Result InitGameWindow(void)
return RESULT_OK; return RESULT_OK;
} }
void HandleWindowEvents() void HandleWindowEvents() {
{
if (IsKeyPressed(KEY_F11)) if (IsKeyPressed(KEY_F11))
{ {
int display = GetCurrentMonitor(); int display = GetCurrentMonitor();
@@ -35,8 +33,7 @@ void HandleWindowEvents()
} }
} }
void ShowSplashScreen(void) void ShowSplashScreen(void) {
{
Image logoImg = LoadImage("resources/images/PunkNSpray.png"); Image logoImg = LoadImage("resources/images/PunkNSpray.png");
Image miarmaImg = LoadImage("resources/images/Miarma.png"); Image miarmaImg = LoadImage("resources/images/Miarma.png");
ImageResize(&miarmaImg, 64, 64); ImageResize(&miarmaImg, 64, 64);

View File

@@ -2,13 +2,11 @@
#include "raylib.h" #include "raylib.h"
#include "core/game.h" #include "core/game.h"
int main() int main() {
{
InitGame(); InitGame();
ShowSplashScreen(); ShowSplashScreen();
while (!WindowShouldClose()) while (!WindowShouldClose()) {
{
float dt = GetFrameTime(); float dt = GetFrameTime();
UpdateGame(dt); UpdateGame(dt);
DrawGame(); DrawGame();

View File

@@ -1,7 +1,6 @@
#include "vector.h" #include "vector.h"
void NormalizeVector(Vector2 *vector) void NormalizeVector(Vector2 *vector) {
{
float m = sqrtf(vector->x * vector->x + vector->y * vector->y); float m = sqrtf(vector->x * vector->x + vector->y * vector->y);
if (m > 0.0001f) if (m > 0.0001f)
*vector = (Vector2){ vector->x / m, vector->y / m }; *vector = (Vector2){ vector->x / m, vector->y / m };

View File

@@ -4,10 +4,8 @@ void AnimationSystemUpdate(Entity *entities,
SpriteComponent *sprites, SpriteComponent *sprites,
MovementIntent *intents, MovementIntent *intents,
size_t count, size_t count,
float dt) float dt) {
{ for (size_t i = 0; i < count; i++) {
for (size_t i = 0; i < count; i++)
{
if (!(entities[i].components & COMP_RENDERABLE)) if (!(entities[i].components & COMP_RENDERABLE))
continue; continue;
@@ -19,8 +17,7 @@ void AnimationSystemUpdate(Entity *entities,
if (intents[i].direction.x != 0 || intents[i].direction.y != 0) if (intents[i].direction.x != 0 || intents[i].direction.y != 0)
newAnim = intents[i].running ? 2 : 1; newAnim = intents[i].running ? 2 : 1;
if (s->currentAnimation != newAnim) if (s->currentAnimation != newAnim) {
{
s->currentAnimation = newAnim; s->currentAnimation = newAnim;
s->animations[newAnim].currentFrame = 0; s->animations[newAnim].currentFrame = 0;
s->animations[newAnim].elapsedTime = 0; s->animations[newAnim].elapsedTime = 0;
@@ -30,8 +27,7 @@ void AnimationSystemUpdate(Entity *entities,
// Actualizar frame // Actualizar frame
animToDraw->elapsedTime += dt; animToDraw->elapsedTime += dt;
if (animToDraw->elapsedTime >= animToDraw->frameTime) if (animToDraw->elapsedTime >= animToDraw->frameTime) {
{
animToDraw->currentFrame = (animToDraw->currentFrame + 1) % animToDraw->frameCount; animToDraw->currentFrame = (animToDraw->currentFrame + 1) % animToDraw->frameCount;
animToDraw->elapsedTime = 0; animToDraw->elapsedTime = 0;
} }

View File

@@ -2,8 +2,7 @@
void InputSystemUpdate(Entity *entities, void InputSystemUpdate(Entity *entities,
InputComponent *inputs, InputComponent *inputs,
size_t count) size_t count) {
{
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
{ {
if (!(entities[i].components & COMP_INPUT)) if (!(entities[i].components & COMP_INPUT))

View File

@@ -3,10 +3,8 @@
void IntentSystemUpdate(Entity *entities, void IntentSystemUpdate(Entity *entities,
InputComponent *inputs, InputComponent *inputs,
MovementIntent *intents, MovementIntent *intents,
size_t count) size_t count) {
{ for (size_t i = 0; i < count; i++) {
for (size_t i = 0; i < count; i++)
{
if (!(entities[i].components & COMP_INPUT)) if (!(entities[i].components & COMP_INPUT))
continue; continue;

View File

@@ -5,13 +5,11 @@ void MovementSystemUpdate(Entity *entities,
VelocityComponent *velocities, VelocityComponent *velocities,
MovementIntent *intents, MovementIntent *intents,
size_t count, size_t count,
float dt) float dt) {
{
const float baseSpeed = 200.0f; const float baseSpeed = 200.0f;
const float runMultiplier = 1.5f; const float runMultiplier = 1.5f;
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++) {
{
if (!(entities[i].components & COMP_POSITION) || if (!(entities[i].components & COMP_POSITION) ||
!(entities[i].components & COMP_VELOCITY)) !(entities[i].components & COMP_VELOCITY))
continue; continue;

View File

@@ -2,8 +2,8 @@
void PhysicsSystemUpdate(Entity *entities, void PhysicsSystemUpdate(Entity *entities,
size_t count, size_t count,
float dt) float dt) {
{
for(size_t i = 0; i < count; i++) { for(size_t i = 0; i < count; i++) {
} }

View File

@@ -1,16 +1,13 @@
#include "render_system.h" #include "render_system.h"
void RenderSystemDraw(Entity *entities, PositionComponent *positions, SpriteComponent *sprites, size_t count) void RenderSystemDraw(Entity *entities, PositionComponent *positions, SpriteComponent *sprites, size_t count) {
{ for (size_t i = 0; i < count; i++) {
for (size_t i = 0; i < count; i++)
{
if (!(entities[i].components & COMP_POSITION)) if (!(entities[i].components & COMP_POSITION))
continue; continue;
Vector2 pos = positions[i].position; Vector2 pos = positions[i].position;
if (entities[i].components & COMP_RENDERABLE) if (entities[i].components & COMP_RENDERABLE) {
{
SpriteComponent *s = &sprites[i]; SpriteComponent *s = &sprites[i];
DrawTextureRec( DrawTextureRec(
@@ -18,9 +15,7 @@ void RenderSystemDraw(Entity *entities, PositionComponent *positions, SpriteComp
s->currentAnimationPtr->source, s->currentAnimationPtr->source,
pos, pos,
WHITE); WHITE);
} } else {
else
{
DrawRectangle(pos.x, pos.y, 32, 32, RED); DrawRectangle(pos.x, pos.y, 32, 32, RED);
} }
} }

View File

@@ -1,14 +1,12 @@
#include "animation_util.h" #include "animation_util.h"
Animation *MirrorAnimations(Animation *originalAnimations, int count) Animation *MirrorAnimations(Animation *originalAnimations, int count) {
{
if (!originalAnimations || count <= 0) return NULL; if (!originalAnimations || count <= 0) return NULL;
Animation *mirrored = malloc(sizeof(Animation) * count); Animation *mirrored = malloc(sizeof(Animation) * count);
if (!mirrored) return NULL; if (!mirrored) return NULL;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++) {
{
mirrored[i] = originalAnimations[i]; mirrored[i] = originalAnimations[i];
mirrored[i].currentFrame = 0; mirrored[i].currentFrame = 0;
mirrored[i].elapsedTime = 0; mirrored[i].elapsedTime = 0;