Tile under foot

This commit is contained in:
2025-11-10 11:01:41 -06:00
parent 3eb24da475
commit 562da971e9
6 changed files with 36 additions and 9 deletions

View File

@@ -46,6 +46,8 @@ void inputInit(void) {
#if INPUT_GAMEPAD == 1
#if PSP
INPUT.deadzone = 0.2890625f;// Taken from the PSP firmware
inputBind(inputButtonGetByName("up"), INPUT_ACTION_UP);
inputBind(inputButtonGetByName("down"), INPUT_ACTION_DOWN);
inputBind(inputButtonGetByName("left"), INPUT_ACTION_LEFT);

View File

@@ -75,16 +75,16 @@ void entityWalk(entity_t *entity, const entitydir_t direction) {
newPos.y += relY;
}
// TODO: Tile in way?
// Get tile under foot
chunkpos_t chunkPos;
worldPosToChunkPos(&newPos, &chunkPos);
chunkindex_t chunkIndex = mapGetChunkIndexAt(chunkPos);
tile_t tile = TILE_NULL;
if(chunkIndex != -1) {
chunk_t *chunk = mapGetChunkByIndex((uint8_t)chunkIndex);
assertNotbNull(chunk, "Chunk pointer cannot be NULL");
chunkGetTile
} else {
return;// Out of bounds
chunk_t *chunk = mapGetChunk(chunkIndex);
assertNotNull(chunk, "Chunk pointer cannot be NULL");
chunktileindex_t tileIndex = woprldPosToChunkTileIndex(&newPos);
tile = chunk->tiles[tileIndex];
}
// Entity in way?

View File

@@ -157,7 +157,7 @@ chunkindex_t mapGetChunkIndexAt(const chunkpos_t position) {
return chunkPosToIndex(&relPos);
}
chunk_t* mapGetChunkByIndex(const uint8_t index) {
chunk_t* mapGetChunk(const uint8_t index) {
if(index >= MAP_CHUNK_COUNT) return NULL;
return &MAP.chunks[index];
}

View File

@@ -8,6 +8,9 @@
#pragma once
#include "dusk.h"
typedef struct tile_s {
uint8_t id;
} tile_t;
} tile_t;
#define TILE_NULL ((tile_t){ .id = 0 })

View File

@@ -23,6 +23,18 @@ void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out) {
out->z = (chunkunit_t)(worldPos->z / CHUNK_DEPTH);
}
chunktileindex_t woprldPosToChunkTileIndex(const worldpos_t* worldPos) {
uint8_t localX = (uint8_t)(worldPos->x % CHUNK_WIDTH);
uint8_t localY = (uint8_t)(worldPos->y % CHUNK_HEIGHT);
uint8_t localZ = (uint8_t)(worldPos->z % CHUNK_DEPTH);
return (chunktileindex_t)(
(localZ * CHUNK_WIDTH * CHUNK_HEIGHT) +
(localY * CHUNK_WIDTH) +
localX
);
}
chunkindex_t chunkPosToIndex(const chunkpos_t* pos) {
return (chunkindex_t)(
(pos->z * MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT) +

View File

@@ -20,7 +20,8 @@
typedef int16_t worldunit_t;
typedef int16_t chunkunit_t;
typedef int8_t chunkindex_t;
typedef int16_t chunkindex_t;
typedef uint8_t chunktileindex_t;
typedef int32_t worldunits_t;
typedef int32_t chunkunits_t;
@@ -58,6 +59,15 @@ void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out);
*/
void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out);
/**
* Converts a position in world-space to an index inside a chunk that the tile
* resides in.
*
* @param worldPos The world position.
* @return The tile index within the chunk.
*/
chunktileindex_t woprldPosToChunkTileIndex(const worldpos_t* worldPos);
/**
* Converts a chunk position to a world position.
*