diff options
author | BoredGuy <osome3717@gmail.com> | 2025-07-28 05:14:54 -0700 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-07-28 05:14:54 -0700 |
commit | 9e1627c229d8d094c7b55751d82db9d3579a16e1 (patch) | |
tree | b8623b5980e090b3faa91e55ed6f84cd1634b570 | |
parent | dc3d98ab47fefc8388455dbbd4d330e81499d95a (diff) |
Completed Basics
-rw-r--r-- | CMakeLists.txt | 8 | ||||
-rw-r--r-- | include/game.h | 17 | ||||
-rw-r--r-- | include/physics.h | 5 | ||||
-rw-r--r-- | include/player.h | 6 | ||||
-rw-r--r-- | src/game.c | 130 | ||||
-rw-r--r-- | src/main.c | 3 | ||||
-rw-r--r-- | src/player.c | 43 |
7 files changed, 210 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c10168..a1560e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,18 +5,26 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) find_package(raylib REQUIRED) +option(BEATEMUP_DEBUG "Enable or disable debug highlihts" OFF) add_executable(game "include/game.h" "include/constants.h" + "include/physics.h" + "include/player.h" "src/game.c" "src/main.c" + "src/player.c" ) target_include_directories(game PRIVATE "include") target_link_libraries(game raylib) +if (BEATEMUP_DEBUG) + target_compile_definitions(game PRIVATE BEATEMUP_DEBUG) +endif() + if (UNIX) target_link_libraries(game m) endif() diff --git a/include/game.h b/include/game.h index 9f44f46..64da8e7 100644 --- a/include/game.h +++ b/include/game.h @@ -10,7 +10,8 @@ #define ENTITY_PHYSICS_ACTIVE (1 << 2) typedef enum EntityType { - Player_Entity + Player_Entity, + Wall_Entity } EntityType; typedef struct Entity { @@ -19,21 +20,33 @@ typedef struct Entity { uint16_t flags; Vector2 position; + Vector2 velocity; //Physics information - Rectangle collider; + Rectangle physicsCollider; int numHitBoxes; Rectangle hitBoxes[MAX_AREA_COUNT]; int numHurtBoxes; Rectangle hurtBoxes[MAX_AREA_COUNT]; + + #ifdef BEATEMUP_DEBUG + //Debug information + Color physicsColliderColor; + #endif } Entity; typedef struct Game { bool paused; Entity entities[MAX_ENTITY_COUNT]; + + Texture2D background; + float backgroundPosition; } Game; +void AddEntity(Entity* e); +void AddWall(float xpos, float ypos, float width, float height); + void InitGame(); void UpdateGame(float deltaTime); void DrawGame();
\ No newline at end of file diff --git a/include/physics.h b/include/physics.h new file mode 100644 index 0000000..b393d08 --- /dev/null +++ b/include/physics.h @@ -0,0 +1,5 @@ +#pragma once + +#include "game.h" + +void MoveAndSlide(Entity* e, float dt);
\ No newline at end of file diff --git a/include/player.h b/include/player.h new file mode 100644 index 0000000..bcd3326 --- /dev/null +++ b/include/player.h @@ -0,0 +1,6 @@ +#include <raylib.h> +#include <raymath.h> +#include "game.h" + +void UpdatePlayer(Entity* player, float deltaTime); +void AddPlayer(float xpos, float ypos);
\ No newline at end of file @@ -2,12 +2,85 @@ #include <string.h> #include "game.h" +#include "player.h" + Game game; static inline bool EntityAllocated(const Entity* e) { return (e->flags & ENTITY_ALLOCATED); } +static inline bool PhysicsEnabled(const Entity* e) { + return EntityAllocated(e) && (e->flags & ENTITY_PHYSICS_ACTIVE); +} + +static inline bool ShouldDrawEntity(const Entity* e) { + return EntityAllocated(e) && (e->flags & ENTITY_VISIBLE); +} + +static inline bool SameEntity(const Entity* a, const Entity* b) { + return a->id == b->id; +} + +static inline bool IsColliding(const Rectangle* a, const Rectangle* b) { + bool collisionX = (a->x <= b->x && a->x + a->width > b->x) + || (b->x <= a->x && b->x + b->width > a->x); + + bool collisionY = (a->y <= b->y && a->y + a->height > b->height) + || (b->y <= a->y && b->y + b->height > a->y); + + return collisionX && collisionY; +} + +static inline Rectangle GetPhysicsColliderGlobal(const Entity* e) { + Rectangle physicsColliderGlobal = e->physicsCollider; + physicsColliderGlobal.x += e->position.x; + physicsColliderGlobal.y += e->position.y; + + return physicsColliderGlobal; +} + +void MoveAndSlide(Entity* e, float deltaTime) { + Vector2 velocity = e->velocity; + Rectangle physicsCollider = GetPhysicsColliderGlobal(e); + + physicsCollider.x += e->velocity.x * deltaTime; + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + Entity* c = &game.entities[i]; + + if(!PhysicsEnabled(c) || SameEntity(c, e)) continue; + Rectangle otherCollider = GetPhysicsColliderGlobal(c); + + if(IsColliding(&physicsCollider, &otherCollider)) { + if (velocity.x > 0) { + physicsCollider.x = otherCollider.x - physicsCollider.width; + } else { + physicsCollider.x = otherCollider.x + otherCollider.width; + } + } + } + + physicsCollider.y += e->velocity.y * deltaTime; + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + Entity* c = &game.entities[i]; + + if(!PhysicsEnabled(c) || SameEntity(c, e)) continue; + Rectangle otherCollider = GetPhysicsColliderGlobal(c); + + if(IsColliding(&physicsCollider, &otherCollider)) { + if (velocity.y > 0) { + physicsCollider.y = otherCollider.y - physicsCollider.height; + } else { + physicsCollider.y = otherCollider.y + otherCollider.height; + } + } + } + + e->position.x = physicsCollider.x; + e->position.y = physicsCollider.y; +} + + void AddEntity(Entity* e) { static int nextId = 0; @@ -17,6 +90,7 @@ void AddEntity(Entity* e) { for(int i = 0; i < MAX_ENTITY_COUNT; i++) { if (!EntityAllocated(&game.entities[i])) { game.entities[i] = *e; + break; } } } @@ -27,12 +101,68 @@ void InitGame() { memset(game.entities, 0, sizeof(game.entities)); } +void UpdateEntity(Entity* e, float deltaTime) { + switch (e->type) { + case Player_Entity: + UpdatePlayer(e, deltaTime); + break; + + default: + break; + } +} + void UpdateGame(float deltaTime) { (void)deltaTime; + + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + Entity* e = &game.entities[i]; + + if (EntityAllocated(e)) + UpdateEntity(e, deltaTime); + } +} + +#ifdef BEATEMUP_DEBUG +void DebugHighlights(const Entity* e) { + Rectangle dstRect = GetPhysicsColliderGlobal(e); + DrawRectangle(dstRect.x, dstRect.y, dstRect.width, dstRect.height, e->physicsColliderColor); +} +#endif + +void DrawEntity(const Entity* e) { } void DrawGame() { BeginDrawing(); ClearBackground(RAYWHITE); + + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + Entity* e = &game.entities[i]; + + #ifdef BEATEMUP_DEBUG + DebugHighlights(e); + #endif + + if (ShouldDrawEntity(e)) + DrawEntity(e); + } + EndDrawing(); +} + +void AddWall(float xpos, float ypos, float width, float height) { + Entity wall; + wall.type = Wall_Entity; + + wall.flags |= ENTITY_PHYSICS_ACTIVE; + + wall.position = (Vector2) {xpos, ypos}; + wall.physicsCollider = (Rectangle) {0, 0, width, height}; + + #ifdef BEATEMUP_DEBUG + wall.physicsColliderColor = BLUE; + #endif + + AddEntity(&wall); }
\ No newline at end of file @@ -2,6 +2,7 @@ #include <raylib.h> #include "constants.h" #include "game.h" +#include "player.h" int main() { SetConfigFlags(FLAG_VSYNC_HINT); //Always better than fixed FPS imo @@ -9,6 +10,8 @@ int main() { SetTargetFPS(0); InitGame(); + AddPlayer(0.0, 0.0); + AddWall(200, 100, 10000, 100); while(!WindowShouldClose()) { UpdateGame(GetFrameTime()); diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..d65b873 --- /dev/null +++ b/src/player.c @@ -0,0 +1,43 @@ +#include "player.h" +#include "physics.h" + +#define PLAYER_SPEED 300.0f + +void UpdatePlayer(Entity* player, float deltaTime) { + player->velocity = (Vector2) {0.0f, 0.0f}; + + if (IsKeyDown(KEY_W)) { + player->velocity.y = -1.0f; + } else if (IsKeyDown(KEY_S)) { + player->velocity.y = 1.0f; + } + + if (IsKeyDown(KEY_A)) { + player->velocity.x = -1.0f; + } else if (IsKeyDown(KEY_D)) { + player->velocity.x = 1.0f; + } + + if (Vector2Length(player->velocity) > 0.0f) { + player->velocity = Vector2Normalize(player->velocity); + } + + player->velocity = Vector2Scale(player->velocity, PLAYER_SPEED); + MoveAndSlide(player, deltaTime); +} + +void AddPlayer(float xpos, float ypos) { + Entity player; + player.type = Player_Entity; + + player.position = (Vector2) {xpos, ypos}; + player.flags |= (ENTITY_PHYSICS_ACTIVE | ENTITY_VISIBLE); + + player.physicsCollider = (Rectangle) {0, 0, 100, 100}; + + #ifdef BEATEMUP_DEBUG + player.physicsColliderColor = RED; + #endif + + AddEntity(&player); +}
\ No newline at end of file |