summaryrefslogtreecommitdiff
path: root/src/game.c
blob: 0bf46779f1f95df9d84d1ab32b31462e55624607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#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) {
  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_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
    if (!entity_active(current))
      continue;

    draw_entity(current);
  }
}

void init_game() {
  memset(&game, 0, sizeof(struct game));
}

static 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;
}

static void load_image_layer_object(const cJSON* layer);
static void load_tile_layer_object(const cJSON* layer);

void load_level_from_tiled_tmj(const char* filename) {
  cJSON *level_data_json = cJSON_Parse(filename);

  {
    int length;
    unsigned char* level_data_raw = LoadFileData(filename, &length);
    level_data_json = cJSON_ParseWithLength((char*)level_data_raw, length);
    UnloadFileData(level_data_raw);
  }

  const cJSON* layers =
    cJSON_GetObjectItemCaseSensitive(level_data_json, "layers");

  if (cJSON_IsArray(layers)) {
    const cJSON* layer = NULL;

    cJSON_ArrayForEach(layer, layers) {
      const cJSON* type = cJSON_GetObjectItemCaseSensitive(layer, "type");

      if (!cJSON_IsString(type))
        continue;

      if (strcmp(type->valuestring, "imagelayer") == 0) {
        load_image_layer_object(layer);
      } else if (strcmp(type->valuestring, "tilelayer") == 0) {
        load_tile_layer_object(layer);
      }
    }
  }

  cJSON_Delete(level_data_json);
}


static void load_image_layer_object(const cJSON* layer) {
  const cJSON* properties = NULL;
  properties = cJSON_GetObjectItemCaseSensitive(layer, "properties");

  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);
    }

  }
}

static void load_tile_layer_object(const cJSON* layer) {
  const cJSON* width = cJSON_GetObjectItemCaseSensitive(layer, "width");
  const cJSON* data = cJSON_GetObjectItemCaseSensitive(layer, "data");
  int c = 0;

  if (!cJSON_IsArray(data) || !cJSON_IsNumber(width))
    return;

  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++;
  }
}