diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-30 08:28:47 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-30 08:28:47 +0200 |
| commit | 1950fe7b0679c6b6486cc7b25bef813db2b1bb4e (patch) | |
| tree | c2098c9ec65f9a9e5fada68c0b51b0217f14b00c /src/terrain | |
| parent | 06056a9b5e900f67702c509ce1ba2e2351357fb7 (diff) | |
| download | jancity-1950fe7b0679c6b6486cc7b25bef813db2b1bb4e.tar.gz | |
Implement sub-tile collboxes
These will be later used by the pathfinding algorithm.
Diffstat (limited to 'src/terrain')
| -rw-r--r-- | src/terrain/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/terrain/inc/terrain.h | 9 | ||||
| -rw-r--r-- | src/terrain/src/terrain.c | 55 |
3 files changed, 63 insertions, 3 deletions
diff --git a/src/terrain/CMakeLists.txt b/src/terrain/CMakeLists.txt index c74de94..7a0e5bd 100644 --- a/src/terrain/CMakeLists.txt +++ b/src/terrain/CMakeLists.txt @@ -1,3 +1,3 @@ add_library(terrain "src/terrain.c") target_include_directories(terrain PUBLIC "inc") -target_link_libraries(terrain PUBLIC container camera gfx) +target_link_libraries(terrain PUBLIC container camera gfx util) diff --git a/src/terrain/inc/terrain.h b/src/terrain/inc/terrain.h index 508fe25..fbe8af8 100644 --- a/src/terrain/inc/terrain.h +++ b/src/terrain/inc/terrain.h @@ -3,6 +3,7 @@ #include <camera.h> #include <gfx.h> +#include <util.h> #include <stddef.h> #ifdef __cplusplus @@ -25,13 +26,19 @@ enum terrain_type struct terrain_map { - enum terrain_type m[MAP_TILES][MAP_TILES]; + struct terrain_tile + { + enum terrain_type t; + unsigned char bl; + } m[MAP_TILES][MAP_TILES]; + int nx, ny, last_w, last_h; }; void terrain_init(struct terrain_map *map); void terrain_update(struct terrain_map *map); int terrain_render(const struct terrain_map *map, const struct camera *cam); +void terrain_block_update(const struct util_rect *dim, bool alive, void *p); extern struct sprite grass_sprite; diff --git a/src/terrain/src/terrain.c b/src/terrain/src/terrain.c index 23c4015..8f1924c 100644 --- a/src/terrain/src/terrain.c +++ b/src/terrain/src/terrain.c @@ -1,6 +1,7 @@ #include <terrain.h> #include <camera.h> #include <gfx.h> +#include <util.h> #include <stddef.h> #include <stdlib.h> #include <string.h> @@ -26,6 +27,56 @@ void terrain_update(struct terrain_map *const map) } } +struct block +{ + size_t x0, x1, y0, y1; +}; + +void terrain_block_update(const struct util_rect *const dim, const bool alive, + void *const p) +{ + const struct block b = + { + .x0 = dim->x / TERRAIN_SZ, + .x1 = (dim->x + dim->w) / TERRAIN_SZ, + .y0 = dim->y / TERRAIN_SZ, + .y1 = (dim->y + dim->h) / TERRAIN_SZ + }; + + for (size_t x = b.x0; x <= b.x1; x++) + for (size_t y = b.y0; y <= b.y1; y++) + for (size_t i = 0; i < 2; i++) + for (size_t j = 0; j < 2; j++) + { + const struct util_rect sub = + { + .x = x * TERRAIN_SZ + i * (TERRAIN_SZ / 2), + .y = y * TERRAIN_SZ + j * (TERRAIN_SZ / 2), + .w = TERRAIN_SZ / 2, + .h = TERRAIN_SZ / 2 + }; + + if (util_collision(dim, &sub)) + { + const enum + { + NO_SUB = 0, + UPPER_LEFT = 1 << 0, + UPPER_RIGHT = 1 << 1, + LOWER_LEFT = 1 << 2, + LOWER_RIGHT = 1 << 3, + ALL_BLOCKS = UPPER_LEFT | UPPER_RIGHT + | LOWER_LEFT | LOWER_RIGHT + } sub = 1 << (i + j * 2); + + struct terrain_map *const map = p; + unsigned char *const bl = &map->m[y][x].bl; + + *bl = alive ? *bl | sub : *bl & ~sub; + } + } +} + int terrain_render(const struct terrain_map *const map, const struct camera *const cam) { @@ -54,9 +105,11 @@ int terrain_render(const struct terrain_map *const map, for (struct m y = {.i = start_y, .p = remy}; y.i < ny + start_y; y.i++, y.p += TERRAIN_SZ) { + const struct terrain_tile *const t = &map->m[y.i][x.i]; + sprite_get_or_ret(s, -1); - switch (map->m[y.i][x.i]) + switch (t->t) { case TERRAIN_TYPE_GRASS: if (sprite_clone(&grass_sprite, s)) |
