diff options
| author | BoredGuy <osome3717@gmail.com> | 2026-03-19 09:43:11 +0300 |
|---|---|---|
| committer | BoredGuy <osome3717@gmail.com> | 2026-03-19 09:43:11 +0300 |
| commit | 6dbd16cd920b51bc24b60d0561bd707ff8862cc5 (patch) | |
| tree | 34add659f1ddb15be4a817b66dbf7c9c194c1576 | |
| parent | 9b63c2572ee6d8b73fd0d2f298dc339335875607 (diff) | |
Added Walls
- And more player mechanics
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/constants.h | 2 | ||||
| -rw-r--r-- | src/entity.c | 24 | ||||
| -rw-r--r-- | src/entity.h | 3 | ||||
| -rw-r--r-- | src/main.c | 3 | ||||
| -rw-r--r-- | src/physics.c | 1 | ||||
| -rw-r--r-- | src/player.c | 52 | ||||
| -rw-r--r-- | src/wall.c | 29 | ||||
| -rw-r--r-- | src/wall.h | 9 |
9 files changed, 109 insertions, 16 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e76e65a..1c31b68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(game "src/physics.h" "src/game.h" "src/player.h" + "src/wall.h" #===========SOURCES=============== "src/settings.c" "src/main.c" @@ -18,6 +19,7 @@ add_executable(game "src/physics.c" "src/game.c" "src/player.c" + "src/wall.c" ) target_link_libraries(game PRIVATE raylib) diff --git a/src/constants.h b/src/constants.h index e71c217..3fc36ff 100644 --- a/src/constants.h +++ b/src/constants.h @@ -9,6 +9,6 @@ #define ENTITY_MAX_ANIMATIONS 10 #define ANIMATION_MAX_FRAMES 4 -#define GRAVITY 9.8 +#define GRAVITY 14 #endif diff --git a/src/entity.c b/src/entity.c index 4a7d98f..d737ea9 100644 --- a/src/entity.c +++ b/src/entity.c @@ -4,6 +4,7 @@ #include "game.h" #include "entity.h" //Entities +#include "wall.h" #include "player.h" extern struct game game; @@ -65,7 +66,7 @@ Rectangle animation_get_source_rect(const struct animation* animation) { return animation->source_rects[animation->current_frame]; } -void draw_entity(const struct entity* entity) { +void draw_entity_default(const struct entity* entity) { if (entity_animated(entity)) { const struct animation* current_animation = &entity->animations[entity->current_animation]; @@ -86,6 +87,18 @@ void draw_entity(const struct entity* entity) { } } +void draw_entity(const struct entity* entity) { + switch (entity->type) { + case Wall_Entity: + draw_wall(entity); + break; + + default: + draw_entity_default(entity); + break; + } +} + void add_entity(const struct entity* e) { static int entity_id = 0; @@ -101,5 +114,12 @@ void add_entity(const struct entity* e) { } void update_entity(struct entity* entity, float dt) { - update_player(entity, dt); + switch (entity->type) { + case Player_Entity: + update_player(entity, dt); + break; + + default: + break; + } } diff --git a/src/entity.h b/src/entity.h index ac179c1..4807a2b 100644 --- a/src/entity.h +++ b/src/entity.h @@ -12,7 +12,8 @@ #define ENTITY_ANIMATED (1 << 3) enum entity_type { - Player_Entity + Player_Entity, + Wall_Entity }; enum draw_layer { @@ -3,6 +3,7 @@ #include "settings.h" #include "physics.h" #include "game.h" +#include "wall.h" #include "player.h" struct settings settings; @@ -15,6 +16,8 @@ int main() { SetTargetFPS(settings.target_fps); add_player(20.0, 20.0); + add_wall(50, 100, 200, 200); + add_wall(100, 200, 200, 50); while (!WindowShouldClose()) { update_game(GetFrameTime()); diff --git a/src/physics.c b/src/physics.c index 33065a9..79fddb0 100644 --- a/src/physics.c +++ b/src/physics.c @@ -1,4 +1,5 @@ #include <stdbool.h> +#include <stdio.h> #include "game.h" #include "entity.h" #include "physics.h" diff --git a/src/player.c b/src/player.c index dd758a0..0ab53cd 100644 --- a/src/player.c +++ b/src/player.c @@ -1,7 +1,9 @@ #include <raylib.h> #include <raymath.h> +#include <math.h> #include "physics.h" #include "player.h" +#include <stdio.h> #define TOP_SPEEDX 100 #define TOP_SPEEDY 100 @@ -9,17 +11,23 @@ void add_player(float xpos, float ypos) { struct entity player = { - .flags = (ENTITY_ACTIVE | ENTITY_VISIBLE), + .flags = (ENTITY_ACTIVE | ENTITY_COLLISION_ACTIVE | ENTITY_VISIBLE), .type = Player_Entity, .position = (Vector2) {xpos, ypos}, .texture = LoadTexture("assets/Graphics/spritesheet-characters-double.png"), .sprite_source_rect = (Rectangle) { - .x = 0, - .y = 0, - .width = 257, - .height = 257 + .x = 50, + .y = 57, + .width = 160, + .height = 200 + }, + .collider = (Rectangle) { + .x = -50, + .y = -50, + .width = 100, + .height = 100 }, .sprite_dest_rect = (Rectangle) { .x = -50, @@ -32,7 +40,7 @@ void add_player(float xpos, float ypos) { add_entity(&player); } -float get_x_acceleration_direction() { +float get_x_acceleration_direction(const struct entity* player) { if (IsKeyDown(KEY_A)) { return -1.0; } else if (IsKeyDown(KEY_D)) { @@ -44,19 +52,39 @@ float get_x_acceleration_direction() { void handle_movement(struct entity* player, float dt) { Vector2 accel = { - .x = get_x_acceleration_direction() * ACCEL_X, + .x = get_x_acceleration_direction(player) * ACCEL_X, .y = GRAVITY }; - player->velocity = - Vector2Add(player->velocity, Vector2Scale(accel, dt * 0.5)); + if (accel.x != 0) { + player->velocity.x += dt * accel.x * 0.5; + } else if (player->velocity.x < 0) { + player->velocity.x = + fminf(player->velocity.x + ACCEL_X * 0.5 * dt, 0); + } else if (player->velocity.x > 0) { + player->velocity.x = + fmaxf(player->velocity.x - ACCEL_X * 0.5 * dt, 0); + } + player->velocity.y += GRAVITY * dt * 0.5; + move_and_collide(player, dt); - player->velocity = - Vector2Add(player->velocity, Vector2Scale(accel, dt * 0.5)); + + if (accel.x != 0) { + player->velocity.x += dt * accel.x * 0.5; + } else if (player->velocity.x < 0) { + player->velocity.x = + fminf(player->velocity.x + ACCEL_X * 0.5 * dt, 0); + } else if (player->velocity.x > 0) { + player->velocity.x = + fmaxf(player->velocity.x - ACCEL_X * 0.5 * dt, 0); + } + player->velocity.y += GRAVITY * dt * 0.5; } void handle_input(struct entity* player) { - + if (IsKeyPressed(KEY_SPACE)) { + player->velocity.y = -50; + } } void update_player(struct entity* player, float dt) { diff --git a/src/wall.c b/src/wall.c new file mode 100644 index 0000000..6ee3a2a --- /dev/null +++ b/src/wall.c @@ -0,0 +1,29 @@ +#include <raylib.h> +#include "wall.h" + +void add_wall(float xpos, float ypos, float width, float height) { + struct entity wall = { + .type = Wall_Entity, + .flags = (ENTITY_ACTIVE | ENTITY_COLLISION_ACTIVE | ENTITY_VISIBLE), + + .position = { + .x = xpos, + .y = ypos + }, + .velocity = (Vector2) {0}, + + .collider = (Rectangle) { + .x = xpos - width / 2, + .y = ypos - height / 2, + .width = width, + .height = height + }, + }; + + + add_entity(&wall); +} + +void draw_wall(const struct entity* wall) { + DrawRectangleRec(get_entity_collider_world(wall), RED); +} diff --git a/src/wall.h b/src/wall.h new file mode 100644 index 0000000..2a481f4 --- /dev/null +++ b/src/wall.h @@ -0,0 +1,9 @@ +#ifndef WALL_H_ +#define WALL_H_ + +#include "entity.h" + +void add_wall(float xpos, float ypos, float width, float height); +void draw_wall(const struct entity* wall); + +#endif // WALL_H_ |
