summaryrefslogtreecommitdiff
path: root/src/game.c
blob: 7872e346f201989968c37086ee57808f3e13728d (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <raylib.h>
#include "game.h"
#include "player.h"
#include "wall.h"

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

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