summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/constants.h2
-rw-r--r--src/game.c36
-rw-r--r--src/game.h2
-rw-r--r--src/main.c3
-rw-r--r--src/player.c4
-rw-r--r--src/wall.c17
-rw-r--r--src/wall.h2
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
diff --git a/src/game.c b/src/game.c
index ee4dee9..7872e34 100644
--- a/src/game.c
+++ b/src/game.c
@@ -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(&copy, "\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);
+}
diff --git a/src/game.h b/src/game.h
index 4aa377b..f028569 100644
--- a/src/game.h
+++ b/src/game.h
@@ -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
diff --git a/src/main.c b/src/main.c
index 5fa3ca1..c3b85af 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
}
}
diff --git a/src/wall.c b/src/wall.c
index 5a17f21..b4a3653 100644
--- a/src/wall.c
+++ b/src/wall.c
@@ -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
},
diff --git a/src/wall.h b/src/wall.h
index 17d8a2e..5ca4adf 100644
--- a/src/wall.h
+++ b/src/wall.h
@@ -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_