diff options
| author | BoredGuy <osome3717@gmail.com> | 2026-03-23 19:28:36 +0300 |
|---|---|---|
| committer | BoredGuy <osome3717@gmail.com> | 2026-03-23 19:28:36 +0300 |
| commit | c9a8845b53ab13c00ed535a2075cc45765dbbfea (patch) | |
| tree | 3f28626d6be0945efed8d6d18ba4bc09409aaeb1 | |
| parent | e446e70fea6ef68c0807c9d6793402f1cd2d3929 (diff) | |
Level Loading!!!
| -rw-r--r-- | src/constants.h | 2 | ||||
| -rw-r--r-- | src/game.c | 36 | ||||
| -rw-r--r-- | src/game.h | 2 | ||||
| -rw-r--r-- | src/main.c | 3 | ||||
| -rw-r--r-- | src/player.c | 4 | ||||
| -rw-r--r-- | src/wall.c | 17 | ||||
| -rw-r--r-- | src/wall.h | 2 |
7 files changed, 53 insertions, 13 deletions
diff --git a/src/constants.h b/src/constants.h index 4eca1ea..0790229 100644 --- a/src/constants.h +++ b/src/constants.h @@ -3,7 +3,7 @@ #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 -#define TARGET_FPS 60 +#define TARGET_FPS 144 #define MAX_ENTITY_COUNT 500 @@ -1,6 +1,10 @@ #include <stdio.h> #include <string.h> +#include <stdlib.h> +#include <raylib.h> #include "game.h" +#include "player.h" +#include "wall.h" struct game game; @@ -28,3 +32,35 @@ void draw_game() { void init_game() { memset(&game, 0, sizeof(struct game)); } + +void load_level_from_tiled_csv(const char* filename) { + int length; + unsigned char* level_data_raw = LoadFileData(filename, &length); + + char* buffer = malloc(length + 1); + memcpy(buffer, level_data_raw, length); + buffer[length] = '\0'; + + UnloadFileData(level_data_raw); + + int y_pos = 0, x_pos; + char *copy = buffer, *line = NULL; + + while (((line = strsep(©, "\n")) != NULL) && + strcmp(line, "") != 0) { + x_pos = 0; + + char* tile_string; + while ((tile_string = strsep(&line, ",")) != NULL) { + int tile = atoi(tile_string); + + if (tile != -1) + add_wall(x_pos * 128 + 128 / 2, y_pos * 128 + 128 / 2, tile); + x_pos++; + } + + y_pos++; + } + + free(buffer); +} @@ -16,4 +16,6 @@ void update_game(float dt); void draw_game(); void init_game(); +void load_level_from_tiled_csv(const char* filename); + #endif @@ -19,8 +19,7 @@ int main() { load_assets(); add_player(300.0, 200.0); - add_wall(50, 100); - add_wall(100, 200); + load_level_from_tiled_csv("level1.csv"); while (!WindowShouldClose()) { update_game(GetFrameTime()); diff --git a/src/player.c b/src/player.c index 76e6349..36788aa 100644 --- a/src/player.c +++ b/src/player.c @@ -112,9 +112,9 @@ void handle_fall(struct entity* player) { } void handle_flip(struct entity* player) { - if (player->velocity.x < 0) { + if (IsKeyDown(KEY_A)) { player->flip = true; - } else if (player->velocity.x > 0){ + } else if (IsKeyDown(KEY_D)){ player->flip = false; } } @@ -6,21 +6,24 @@ extern struct Texture wall_texture; -void add_wall(float xpos, float ypos) { +void add_wall(float xpos, float ypos, int tile) { + int tile_x = tile % 18; + int tile_y = tile / 18; + struct entity wall = { .type = Wall_Entity, .flags = (ENTITY_ACTIVE | ENTITY_COLLISION_ACTIVE | ENTITY_VISIBLE), .texture = wall_texture, .sprite_source_rect = (Rectangle) { - .x = 0, - .y = 0, + .x = tile_x * 129, + .y = tile_y * 129, .width = 128, .height = 128 }, .sprite_dest_rect = (Rectangle) { - .x = xpos - BLOCK_WIDTH / 2, - .y = ypos - BLOCK_HEIGHT / 2, + .x = -BLOCK_WIDTH / 2, + .y = -BLOCK_HEIGHT / 2, .width = BLOCK_WIDTH, .height = BLOCK_HEIGHT }, @@ -32,8 +35,8 @@ void add_wall(float xpos, float ypos) { .velocity = (Vector2) {0}, .collider = (Rectangle) { - .x = xpos - BLOCK_WIDTH / 2, - .y = ypos - BLOCK_HEIGHT / 2, + .x = -BLOCK_WIDTH / 2, + .y = -BLOCK_HEIGHT / 2, .width = BLOCK_WIDTH, .height = BLOCK_HEIGHT }, @@ -3,6 +3,6 @@ #include "entity.h" -void add_wall(float xpos, float ypos); +void add_wall(float xpos, float ypos, int tile); #endif // WALL_H_ |
