idk2
This commit is contained in:
@@ -67,34 +67,36 @@ void entityWalk(entity_t *entity, const entitydir_t direction) {
|
|||||||
entity->direction = direction;
|
entity->direction = direction;
|
||||||
|
|
||||||
// Where are we moving?
|
// Where are we moving?
|
||||||
worldunit_t newX, newY;
|
worldpos_t newPos = entity->position;
|
||||||
newX = entity->position.x;
|
|
||||||
newY = entity->position.y;
|
|
||||||
{
|
{
|
||||||
worldunits_t relX, relY;
|
worldunits_t relX, relY;
|
||||||
entityDirGetRelative(direction, &relX, &relY);
|
entityDirGetRelative(direction, &relX, &relY);
|
||||||
newX += relX;
|
newPos.x += relX;
|
||||||
newY += relY;
|
newPos.y += relY;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Tile in way?
|
// TODO: Tile in way?
|
||||||
// TODO: Map bounds in way?
|
chunkpos_t chunkPos;
|
||||||
|
worldPosToChunkPos(&newPos, &chunkPos);
|
||||||
|
chunkindex_t chunkIndex = mapGetChunkIndexAt(chunkPos);
|
||||||
|
if(chunkIndex != -1) {
|
||||||
|
chunk_t *chunk = mapGetChunkByIndex((uint8_t)chunkIndex);
|
||||||
|
assertNotbNull(chunk, "Chunk pointer cannot be NULL");
|
||||||
|
chunkGetTile
|
||||||
|
} else {
|
||||||
|
return;// Out of bounds
|
||||||
|
}
|
||||||
|
|
||||||
// Entity in way?
|
// Entity in way?
|
||||||
entity_t *other = ENTITIES;
|
entity_t *other = ENTITIES;
|
||||||
do {
|
do {
|
||||||
if(other == entity) continue;
|
if(other == entity) continue;
|
||||||
if(other->type == ENTITY_TYPE_NULL) continue;
|
if(other->type == ENTITY_TYPE_NULL) continue;
|
||||||
if(other->position.x != newX) continue;
|
if(!worldPosIsEqual(other->position, newPos)) continue;
|
||||||
if(other->position.y != newY) continue;
|
|
||||||
if(other->position.z != entity->position.z) continue;
|
|
||||||
return;// Blocked
|
return;// Blocked
|
||||||
} while(++other, other < &ENTITIES[ENTITY_COUNT]);
|
} while(++other, other < &ENTITIES[ENTITY_COUNT]);
|
||||||
|
|
||||||
// Move.
|
entity->position = newPos;
|
||||||
entity->position.x = newX;
|
|
||||||
entity->position.y = newY;
|
|
||||||
|
|
||||||
entity->animation = ENTITY_ANIM_WALK;
|
entity->animation = ENTITY_ANIM_WALK;
|
||||||
entity->animTime = ENTITY_ANIM_WALK_DURATION;// TODO: Running vs walking
|
entity->animTime = ENTITY_ANIM_WALK_DURATION;// TODO: Running vs walking
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include "rpg/entity/entity.h"
|
#include "rpg/entity/entity.h"
|
||||||
#include "rpg/world/map.h"
|
#include "rpg/world/map.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
rpgcamera_t RPG_CAMERA;
|
rpgcamera_t RPG_CAMERA;
|
||||||
|
|
||||||
@@ -17,15 +18,11 @@ void rpgCameraInit(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void rpgCameraUpdate(void) {
|
void rpgCameraUpdate(void) {
|
||||||
|
chunkpos_t chunkPos;
|
||||||
|
|
||||||
switch(RPG_CAMERA.mode) {
|
switch(RPG_CAMERA.mode) {
|
||||||
case RPG_CAMERA_MODE_FREE:
|
case RPG_CAMERA_MODE_FREE:
|
||||||
chunkpos_t chunkPos;
|
|
||||||
worldPosToChunkPos(&RPG_CAMERA.free, &chunkPos);
|
worldPosToChunkPos(&RPG_CAMERA.free, &chunkPos);
|
||||||
mapPositionSet((chunkpos_t){
|
|
||||||
.x = chunkPos.x - (MAP_CHUNK_WIDTH / 2),
|
|
||||||
.y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2),
|
|
||||||
.z = chunkPos.z - (MAP_CHUNK_DEPTH / 2)
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case RPG_CAMERA_MODE_FOLLOW_ENTITY: {
|
case RPG_CAMERA_MODE_FOLLOW_ENTITY: {
|
||||||
@@ -34,18 +31,17 @@ void rpgCameraUpdate(void) {
|
|||||||
|
|
||||||
// Update map position to match camera. By default map wants to know the
|
// Update map position to match camera. By default map wants to know the
|
||||||
// top left but we want to set the center, so we need to sub half map size
|
// top left but we want to set the center, so we need to sub half map size
|
||||||
chunkpos_t chunkPos;
|
|
||||||
worldPosToChunkPos(&entity->position, &chunkPos);
|
worldPosToChunkPos(&entity->position, &chunkPos);
|
||||||
mapPositionSet((chunkpos_t){
|
|
||||||
.x = chunkPos.x - (MAP_CHUNK_WIDTH / 2),
|
|
||||||
.y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2),
|
|
||||||
.z = chunkPos.z - (MAP_CHUNK_DEPTH / 2)
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Unknown camera mode; do nothing.
|
assertUnreachable("Invalid RPG camera mode");
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mapPositionSet((chunkpos_t){
|
||||||
|
.x = chunkPos.x - (MAP_CHUNK_WIDTH / 2),
|
||||||
|
.y = chunkPos.y - (MAP_CHUNK_HEIGHT / 2),
|
||||||
|
.z = chunkPos.z - (MAP_CHUNK_DEPTH / 2)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,10 @@
|
|||||||
|
|
||||||
#include "worldpos.h"
|
#include "worldpos.h"
|
||||||
|
|
||||||
|
bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b) {
|
||||||
|
return a.x == b.x && a.y == b.y && a.z == b.z;
|
||||||
|
}
|
||||||
|
|
||||||
void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out) {
|
void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out) {
|
||||||
out->x = (worldunit_t)(chunkPos->x * CHUNK_WIDTH);
|
out->x = (worldunit_t)(chunkPos->x * CHUNK_WIDTH);
|
||||||
out->y = (worldunit_t)(chunkPos->y * CHUNK_HEIGHT);
|
out->y = (worldunit_t)(chunkPos->y * CHUNK_HEIGHT);
|
||||||
|
|||||||
@@ -33,6 +33,15 @@ typedef struct chunkpos_t {
|
|||||||
chunkunit_t x, y, z;
|
chunkunit_t x, y, z;
|
||||||
} chunkpos_t;
|
} chunkpos_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares two world positions for equality.
|
||||||
|
*
|
||||||
|
* @param a The first world position.
|
||||||
|
* @param b The second world position.
|
||||||
|
* @return true if equal, false otherwise.
|
||||||
|
*/
|
||||||
|
bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a world position to a chunk position.
|
* Converts a world position to a chunk position.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user