diff options
| author | BoredGuy <osome3717@gmail.com> | 2026-03-16 17:47:57 +0300 |
|---|---|---|
| committer | BoredGuy <osome3717@gmail.com> | 2026-03-16 17:47:57 +0300 |
| commit | ac02199694ffef81a00a8ea47420e88ef0a35f67 (patch) | |
| tree | ab8be37dd6960954d7baa54bb1273980063c66ba | |
| parent | 2c2be83a370318ca69824538ed3e61784295a772 (diff) | |
More work on systems
- Will be done soon (I think)
- Added first entity
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/entity.c | 66 | ||||
| -rw-r--r-- | src/entity.h | 9 | ||||
| -rw-r--r-- | src/game.c | 8 | ||||
| -rw-r--r-- | src/main.c | 6 | ||||
| -rw-r--r-- | src/player.c | 33 | ||||
| -rw-r--r-- | src/player.h | 9 |
7 files changed, 130 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ddf4277..e76e65a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,12 +10,14 @@ add_executable(game "src/entity.h" "src/physics.h" "src/game.h" + "src/player.h" #===========SOURCES=============== "src/settings.c" "src/main.c" "src/entity.c" "src/physics.c" "src/game.c" + "src/player.c" ) target_link_libraries(game PRIVATE raylib) diff --git a/src/entity.c b/src/entity.c index 7a9db49..4a7d98f 100644 --- a/src/entity.c +++ b/src/entity.c @@ -3,6 +3,8 @@ #include <string.h> #include "game.h" #include "entity.h" +//Entities +#include "player.h" extern struct game game; @@ -15,6 +17,15 @@ Rectangle get_entity_collider_world(const struct entity* entity) { }; } +Rectangle get_entity_dest_rect_world(const struct entity* entity) { + return (Rectangle) { + .x = entity->sprite_dest_rect.x + entity->position.x, + .y = entity->sprite_dest_rect.y + entity->position.y, + .width = entity->sprite_dest_rect.width, + .height = entity->sprite_dest_rect.height + }; +} + void entity_handle_collision ( struct entity*, @@ -23,17 +34,72 @@ void entity_handle_collision ) { } +static inline bool entity_animated(const struct entity* e) { + return (e->flags & ENTITY_ANIMATED) != 0; +} + +void update_animation(struct animation* animation, float dt) { + if (animation->is_completed) + return; + + animation->current_frame_time += dt; + + while (animation->current_frame_time >= animation->frame_times[animation->current_frame]) { + bool animation_completed = animation->current_frame >= animation->frame_count-1; + + if (animation_completed && animation->is_looping) { + animation->current_frame = 0; + animation->current_frame_time = 0; + return; + } else if (animation_completed) { + animation->is_completed = true; + return; + } + + animation->current_frame_time -= animation->frame_times[animation->current_frame]; + animation->current_frame++; + } +} + +Rectangle animation_get_source_rect(const struct animation* animation) { + return animation->source_rects[animation->current_frame]; +} + void draw_entity(const struct entity* entity) { + if (entity_animated(entity)) { + const struct animation* current_animation = + &entity->animations[entity->current_animation]; + DrawTexturePro(entity->texture, + animation_get_source_rect(current_animation), + get_entity_dest_rect_world(entity), + (Vector2) {0.0, 0.0}, + 0, + WHITE); + } else { + DrawTexturePro(entity->texture, + entity->sprite_source_rect, + get_entity_dest_rect_world(entity), + (Vector2) {0.0, 0.0}, + 0, + WHITE); + } } void add_entity(const struct entity* e) { + static int entity_id = 0; + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { struct entity* current = &game.entities[i]; if (entity_active(current)) continue; memcpy(current, e, sizeof(struct entity)); + current->entity_id = entity_id++; return; } } + +void update_entity(struct entity* entity, float dt) { + update_player(entity, dt); +} diff --git a/src/entity.h b/src/entity.h index 94eb00f..ac179c1 100644 --- a/src/entity.h +++ b/src/entity.h @@ -9,6 +9,7 @@ #define ENTITY_ACTIVE 1 #define ENTITY_COLLISION_ACTIVE (1 << 1) #define ENTITY_VISIBLE (1 << 2) +#define ENTITY_ANIMATED (1 << 3) enum entity_type { Player_Entity @@ -38,6 +39,9 @@ struct animation { float current_frame_time; }; +void update_animation(struct animation* animation, float dt); +Rectangle get_current_srcrect(const struct animation* animation); + struct entity { int entity_id; enum entity_type type; @@ -48,6 +52,7 @@ struct entity { Rectangle collider; Texture texture; + Rectangle sprite_source_rect; //Used if entity doesn't have sprite animations Rectangle sprite_dest_rect; //Relative to the position of entity (roughly the center) struct animation animations[ENTITY_MAX_ANIMATIONS]; int current_animation; @@ -69,7 +74,7 @@ void entity_handle_collision void add_entity(const struct entity* e); static inline bool entity_active(const struct entity* e) { - return e->flags & ENTITY_ACTIVE; + return (e->flags & ENTITY_ACTIVE) != 0; } static inline bool same_entity(const struct entity* a, const struct entity* b) { @@ -77,7 +82,7 @@ static inline bool same_entity(const struct entity* a, const struct entity* b) { } static inline bool entity_visible(const struct entity* e) { - return e->flags & ENTITY_VISIBLE; + return (e->flags & ENTITY_VISIBLE) != 0; } #endif @@ -5,7 +5,13 @@ struct game game; void update_game(float dt) { - + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + struct entity* current = &game.entities[i]; + if (!entity_active(current)) + continue; + + update_entity(current, dt); + } } void draw_game() { @@ -3,6 +3,7 @@ #include "settings.h" #include "physics.h" #include "game.h" +#include "player.h" struct settings settings; extern struct game game; @@ -13,9 +14,14 @@ int main() { InitWindow(settings.window_width, settings.window_height, "Platformer"); SetTargetFPS(settings.target_fps); + add_player(20.0, 20.0); + while (!WindowShouldClose()) { + update_game(GetFrameTime()); + BeginDrawing(); ClearBackground(RAYWHITE); + draw_game(); EndDrawing(); } diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..5dd77c1 --- /dev/null +++ b/src/player.c @@ -0,0 +1,33 @@ +#include <raylib.h> +#include "physics.h" +#include "player.h" + +void add_player(float xpos, float ypos) { + struct entity player = { + .flags = (ENTITY_ACTIVE | ENTITY_VISIBLE), + .type = Player_Entity, + + .position = (Vector2) {xpos, ypos}, + .velocity = {100.0, 100.0}, + .texture = LoadTexture("assets/Graphics/spritesheet-characters-double.png"), + + .sprite_source_rect = (Rectangle) { + .x = 0, + .y = 0, + .width = 257, + .height = 257 + }, + .sprite_dest_rect = (Rectangle) { + .x = -50, + .y = -50, + .width = 100, + .height = 100 + } + }; + + add_entity(&player); +} + +void update_player(struct entity* entity, float dt) { + move_and_collide(entity, dt); +} diff --git a/src/player.h b/src/player.h new file mode 100644 index 0000000..36f2d7b --- /dev/null +++ b/src/player.h @@ -0,0 +1,9 @@ +#ifndef PLAYER_H_ +#define PLAYER_H_ + +#include "entity.h" + +void add_player(float xpos, float ypos); +void update_player(struct entity* player, float dt); + +#endif // PLAYER_H_ |
