summaryrefslogtreecommitdiff
path: root/src/wall.c
blob: 2aa20ddd9f132208bdc630fc2abea20dc345fa2c (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
#include <raylib.h>
#include <stdbool.h>
#include <stdlib.h>
#include "wall.h"

extern struct Texture wall_texture;

static inline bool tile_is_collidable(int tile) {
  int non_collidable[] = {65, 122};

  for (size_t i = 0; i < sizeof(non_collidable) / sizeof(non_collidable[0]); i++)
    if (non_collidable[i] == tile)
      return false;

  return true;
}

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_VISIBLE),

    .texture = wall_texture,
    .sprite_source_rect = (Rectangle) {
      .x = tile_x * 129,
      .y = tile_y * 129,
      .width = 128,
      .height = 128
    },
    .sprite_dest_rect = (Rectangle) {
      .x = -BLOCK_WIDTH / 2,
      .y = -BLOCK_HEIGHT / 2,
      .width = BLOCK_WIDTH,
      .height = BLOCK_HEIGHT
    },

    .position = {
      .x = xpos,
      .y = ypos
    },
    .velocity = (Vector2) {0},

    .collider = (Rectangle) {
      .x = -BLOCK_WIDTH / 2,
      .y = -BLOCK_HEIGHT / 2,
      .width = BLOCK_WIDTH,
      .height = BLOCK_HEIGHT
    },
  };

  if (tile_is_collidable(tile))
    wall.flags |= ENTITY_COLLISION_ACTIVE;

  add_entity(&wall);
}