summaryrefslogtreecommitdiff
path: root/src/floor.c
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-01-31 00:33:53 +0500
committerilotterytea <iltsu@alright.party>2025-01-31 00:33:53 +0500
commita15f24294d5113fec767fb9007d906f534dc0485 (patch)
tree20c869d1444a91256a10d4dfe0376f2f72ab4021 /src/floor.c
parent2ca545f754acc9c8a00b5ce8c0b3ca1175c74a3a (diff)
upd: moved some editor logic to tileset and floor files
Diffstat (limited to 'src/floor.c')
-rw-r--r--src/floor.c179
1 files changed, 179 insertions, 0 deletions
diff --git a/src/floor.c b/src/floor.c
new file mode 100644
index 0000000..22b2718
--- /dev/null
+++ b/src/floor.c
@@ -0,0 +1,179 @@
+#include "floor.h"
+
+#include "logger.h"
+#include "stdlib.h"
+
+Tile *SE_CreateTile(TileLayer *layer, TilesetTile *tilesetTile) {
+ Tile *tile = malloc(sizeof(Tile));
+
+ tile->id = layer->tileCount;
+ tile->tilesetTile = tilesetTile;
+ tile->position = (Vector2){0.0f, 0.0f};
+
+ layer->tiles[layer->tileCount] = tile;
+ layer->tileCount++;
+
+ return tile;
+}
+
+void SE_RemoveTile(TileLayer *layer, Tile *tile) {
+ int index = -1;
+
+ for (int i = 0; i < layer->tileCount; i++) {
+ Tile *t = layer->tiles[i];
+ if (t->id == tile->id) {
+ index = i;
+ break;
+ }
+ }
+
+ if (index == -1) {
+ SE_LOG_WARN("Tile doesn't belong to the layer");
+ return;
+ }
+
+ free(tile);
+ layer->tiles[index] = NULL;
+
+ // clearing layer from null values
+ int i, j = 0;
+ Tile **tiles = layer->tiles;
+ int *count = &layer->tileCount;
+
+ for (i = 0; i < *count; i++) {
+ if (tiles[i] != NULL) {
+ tiles[j++] = tiles[i];
+ }
+ }
+
+ *count = j;
+
+ for (i = j; i < *count; i++) {
+ tiles[i] = NULL;
+ }
+}
+
+Tile *SE_FindTileAtPosition(TileLayer *layer, Vector2 position) {
+ Tile *tile = NULL;
+
+ for (int i = 0; i < layer->tileCount; i++) {
+ Tile *t = layer->tiles[i];
+ if (t->position.x == position.x && t->position.y == position.y) {
+ tile = t;
+ break;
+ }
+ }
+
+ return tile;
+}
+
+TileFloor *SE_CreateTileFloor(int width, int height) {
+ TileFloor *floor = malloc(sizeof(TileFloor));
+
+ floor->width = width;
+ floor->height = height;
+ floor->layerCount = 0;
+
+ return floor;
+}
+
+TileLayer *SE_CreateTileLayer(TileFloor *floor) {
+ TileLayer *layer = malloc(sizeof(TileLayer));
+
+ layer->tileCount = 0;
+ layer->index = floor->layerCount;
+
+ floor->layers[floor->layerCount] = layer;
+ floor->layerCount++;
+
+ return layer;
+}
+
+void SE_UpdateTileFloor(EditorState *state, TileFloor *floor,
+ Camera2D *camera) {
+ // creating a new layer if it does not exists
+ TileLayer *layer;
+ if (floor->layerCount <= state->activeTileLayerId) {
+ layer = SE_CreateTileLayer(floor);
+ } else {
+ layer = floor->layers[state->activeTileLayerId];
+ }
+
+ // tile manipulation
+ Vector2 mousePos = GetScreenToWorld2D(GetMousePosition(), *camera);
+
+ for (int x = 0; x < floor->width; x++) {
+ for (int y = 0; y < floor->height; y++) {
+ Vector2 pos = (Vector2){x, y};
+
+ if (CheckCollisionPointRec(
+ mousePos, (Rectangle){pos.x * camera->zoom, pos.y * camera->zoom,
+ camera->zoom, camera->zoom})) {
+ // placing tiles
+ if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) &&
+ state->activeTilesetTile != NULL) {
+ Tile *tile = SE_FindTileAtPosition(layer, pos);
+
+ if (tile == NULL) {
+ tile = SE_CreateTile(layer, state->activeTilesetTile);
+ tile->position = pos;
+ }
+
+ tile->tilesetTile = state->activeTilesetTile;
+ }
+
+ // removing tiles
+ if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) {
+ Tile *tile = SE_FindTileAtPosition(layer, pos);
+ if (tile != NULL) {
+ SE_RemoveTile(layer, tile);
+ }
+ }
+ }
+ }
+ }
+}
+
+void SE_DrawTileFloor(TileFloor *floor, EditorState *state, Camera2D *camera) {
+ // draw placed tiles
+ for (int layerIndex = 0; layerIndex < floor->layerCount; layerIndex++) {
+ TileLayer *layer = floor->layers[layerIndex];
+
+ for (int tileIndex = 0; tileIndex < layer->tileCount; tileIndex++) {
+ Tile *tile = layer->tiles[tileIndex];
+
+ Rectangle s = {0, 0, TILE_WIDTH, TILE_HEIGHT};
+ Rectangle d = {tile->position.x * camera->zoom,
+ tile->position.y * camera->zoom, camera->zoom,
+ camera->zoom};
+
+ DrawTexturePro(tile->tilesetTile->texture, s, d, (Vector2){0, 0}, 0.0f,
+ WHITE);
+ }
+ }
+
+ // draw active tile
+ if (state->activeTilesetTile != NULL) {
+ Vector2 mousePos = GetScreenToWorld2D(GetMousePosition(), *camera);
+ Rectangle s = {0, 0, TILE_WIDTH, TILE_HEIGHT};
+ Rectangle d = {mousePos.x, mousePos.y, camera->zoom / 2.0f,
+ camera->zoom / 2.0f};
+ DrawTexturePro(state->activeTilesetTile->texture, s, d, (Vector2){0, 0},
+ 0.0f, WHITE);
+ }
+}
+
+void SE_UnloadTileFloor(TileFloor *floor) {
+ for (int layerIndex = 0; layerIndex < floor->layerCount; layerIndex++) {
+ TileLayer *layer = floor->layers[layerIndex];
+
+ for (int tileIndex = 0; tileIndex < layer->tileCount; tileIndex++) {
+ Tile *tile = layer->tiles[tileIndex];
+ free(tile);
+ }
+
+ free(layer);
+ }
+
+ free(floor);
+} \ No newline at end of file