More progress.
This commit is contained in:
@@ -6,6 +6,19 @@
|
||||
#define GRID_MIN_WIDTH 8
|
||||
#define GRID_MIN_HEIGHT 8
|
||||
|
||||
static bool from_grid_index(Grid *grid, uint16_t grid_index, uint16_t *out_x, uint16_t *out_y) {
|
||||
assert(grid != NULL);
|
||||
assert(out_x != NULL);
|
||||
assert(out_y != NULL);
|
||||
|
||||
if (!grid || !out_x || !out_y) return false;
|
||||
if (grid_index >= grid->width * grid->height) return false;
|
||||
|
||||
*out_x = grid_index % grid->width;
|
||||
*out_y = grid_index / grid->width;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool grid_initialize(Grid *grid, uint16_t width, uint16_t height) {
|
||||
assert(grid != NULL);
|
||||
|
||||
@@ -16,6 +29,12 @@ bool grid_initialize(Grid *grid, uint16_t width, uint16_t height) {
|
||||
grid->width = width;
|
||||
grid->height = height;
|
||||
grid->cells = calloc(width * height, sizeof(Grid_Cell));
|
||||
|
||||
for(uint16_t i = 0; i < (width * height); i++) {
|
||||
Grid_Cell *cell = &grid->cells[i];
|
||||
assert(from_grid_index(grid, i, &cell->x, &cell->y));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -37,19 +56,6 @@ static bool to_grid_index(Grid *grid, uint16_t x, uint16_t y, uint16_t *out_grid
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool from_grid_index(Grid *grid, uint16_t grid_index, uint16_t *out_x, uint16_t *out_y) {
|
||||
assert(grid != NULL);
|
||||
assert(out_x != NULL);
|
||||
assert(out_y != NULL);
|
||||
|
||||
if (!grid || !out_x || !out_y) return false;
|
||||
if (grid_index >= grid->width * grid->height) return false;
|
||||
|
||||
*out_x = grid_index % grid->width;
|
||||
*out_y = grid_index / grid->width;
|
||||
return true;
|
||||
}
|
||||
|
||||
Grid_Cell *grid_get_cell(Grid *grid, uint16_t x, uint16_t y) {
|
||||
assert(grid != NULL);
|
||||
if(x >= grid->width || y >= grid->height) return NULL;
|
||||
@@ -60,4 +66,36 @@ Grid_Cell *grid_get_cell(Grid *grid, uint16_t x, uint16_t y) {
|
||||
}
|
||||
|
||||
return &grid->cells[grid_index];
|
||||
}
|
||||
|
||||
bool grid_try_add_entity_to_cell(Grid_Cell *cell, Entity *entity) {
|
||||
assert(cell != NULL);
|
||||
assert(entity != NULL);
|
||||
|
||||
// TODO: SS - Could check what type of cell it is to determine if
|
||||
// we're allowed to place an entity here. Is it a wall? Lava?
|
||||
|
||||
if(cell->entity != NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cell->entity = entity;
|
||||
entity->x = cell->x;
|
||||
entity->y = cell->y;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool grid_try_remove_entity_from_cell(Grid_Cell *cell) {
|
||||
assert(cell != NULL);
|
||||
|
||||
if(cell->entity == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cell->entity->x = 0;
|
||||
cell->entity->y = 0;
|
||||
cell->entity = NULL;
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user