summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c117
1 files changed, 96 insertions, 21 deletions
diff --git a/src/game.c b/src/game.c
index 7872e34..21b80fd 100644
--- a/src/game.c
+++ b/src/game.c
@@ -2,10 +2,14 @@
#include <string.h>
#include <stdlib.h>
#include <raylib.h>
+#include "cJSON.h"
#include "game.h"
#include "player.h"
#include "wall.h"
+extern Texture color_hills;
+extern Texture color_desert;
+
struct game game;
void update_game(float dt) {
@@ -18,7 +22,34 @@ void update_game(float dt) {
}
}
+void draw_background() {
+ float image_scale =
+ (float)WINDOW_HEIGHT / game.background_texture.height;
+
+ float xpos = 0;
+ while (xpos < WINDOW_WIDTH) {
+ DrawTexturePro(game.background_texture, (Rectangle){
+ .x = 0,
+ .y = 0,
+ .width = game.background_texture.width,
+ .height = game.background_texture.height
+ }, (Rectangle) {
+ .x = xpos,
+ .y = 0,
+ .width = game.background_texture.width * image_scale,
+ .height = game.background_texture.height * image_scale
+ },
+ (Vector2) {0, 0},
+ 0,
+ WHITE);
+
+ xpos += game.background_texture.width * image_scale;
+ }
+}
+
void draw_game() {
+ draw_background();
+
for (int i = 0; i < MAX_ENTITY_COUNT; i++) {
const struct entity* current = &game.entities[i];
//Skip inactive entities
@@ -33,34 +64,78 @@ 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);
+void set_background(const char* background_name) {
+ if (strcmp(background_name, "color_hills") == 0)
+ game.background_texture = color_hills;
+ else if (strcmp(background_name, "color_desert") == 0)
+ game.background_texture = color_desert;
+}
- char* buffer = malloc(length + 1);
- memcpy(buffer, level_data_raw, length);
- buffer[length] = '\0';
+void load_level_from_tiled_tmj(const char* filename) {
+ cJSON *level_data_json = cJSON_Parse(filename);
- UnloadFileData(level_data_raw);
+ {
+ int length;
+ unsigned char* level_data_raw = LoadFileData(filename, &length);
+ level_data_json = cJSON_ParseWithLength((char*)level_data_raw, length);
+ UnloadFileData(level_data_raw);
+ }
- int y_pos = 0, x_pos;
- char *copy = buffer, *line = NULL;
+ const cJSON* layers =
+ cJSON_GetObjectItemCaseSensitive(level_data_json, "layers");
- while (((line = strsep(&copy, "\n")) != NULL) &&
- strcmp(line, "") != 0) {
- x_pos = 0;
+ if (cJSON_IsArray(layers)) {
+ const cJSON* layer = NULL;
- char* tile_string;
- while ((tile_string = strsep(&line, ",")) != NULL) {
- int tile = atoi(tile_string);
+ cJSON_ArrayForEach(layer, layers) {
+ const cJSON* type = cJSON_GetObjectItemCaseSensitive(layer, "type");
- if (tile != -1)
- add_wall(x_pos * 128 + 128 / 2, y_pos * 128 + 128 / 2, tile);
- x_pos++;
- }
+ if (!cJSON_IsString(type))
+ continue;
+
+ if (strcmp(type->valuestring, "imagelayer") == 0) {
+ const cJSON* properties = NULL;
+ properties = cJSON_GetObjectItemCaseSensitive(layer, "properties");
- y_pos++;
+ if (cJSON_IsArray(properties)) {
+ const cJSON* property = NULL;
+
+ cJSON_ArrayForEach(property, properties) {
+ const cJSON* name = cJSON_GetObjectItemCaseSensitive(property, "name");
+ const cJSON* bg_name = cJSON_GetObjectItemCaseSensitive(property, "value");
+
+ if (!cJSON_IsString(name)
+ || !cJSON_IsString(bg_name)
+ || strcmp(name->valuestring, "bg_name") != 0)
+ continue;
+
+ set_background(bg_name->valuestring);
+ }
+
+ }
+ } else if (strcmp(type->valuestring, "tilelayer") == 0) {
+ const cJSON* width = cJSON_GetObjectItemCaseSensitive(layer, "width");
+ const cJSON* data = cJSON_GetObjectItemCaseSensitive(layer, "data");
+ int c = 0;
+
+ if (!cJSON_IsArray(data) || !cJSON_IsNumber(width))
+ continue;
+
+ const cJSON* tile = NULL;
+ cJSON_ArrayForEach(tile, data) {
+
+ if (cJSON_IsNumber(tile) && tile->valueint != 0) {
+ int x_pos = (c % width->valueint) * BLOCK_WIDTH + BLOCK_WIDTH / 2;
+ int y_pos = (c / width->valueint) * BLOCK_HEIGHT + BLOCK_HEIGHT / 2;
+
+ add_wall(x_pos, y_pos, tile->valueint - 1);
+ }
+
+ c++;
+ }
+ }
+ }
}
- free(buffer);
+ cJSON_Delete(level_data_json);
}