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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,13 +5,11 @@ void MovementSystemUpdate(Entity *entities,
VelocityComponent *velocities,
MovementIntent *intents,
size_t count,
float dt)
{
float dt) {
const float baseSpeed = 200.0f;
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) ||
!(entities[i].components & COMP_VELOCITY))
continue;

View File

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

View File

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

View File

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