small refactoring
This commit is contained in:
parent
73595cd558
commit
d671188680
@ -70,7 +70,7 @@ void BlocksController::updateBlock(int x, int y, int z) {
|
||||
if (vox == nullptr)
|
||||
return;
|
||||
const Block* def = level->content->indices->getBlockDef(vox->id);
|
||||
if (def->grounded && !chunks->isSolid(x, y-1, z)) {
|
||||
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
||||
breakBlock(nullptr, def, x, y, z);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -281,7 +281,7 @@ void PlayerController::updateInteraction(){
|
||||
if (!level->physics->isBlockInside(x,y,z, player->hitbox)
|
||||
|| !def->obstacle){
|
||||
Block* def = contentIds->getBlockDef(chosenBlock);
|
||||
if (def->grounded && !chunks->isSolid(x, y-1, z)) {
|
||||
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
||||
chosenBlock = 0;
|
||||
}
|
||||
if (chosenBlock != vox->id) {
|
||||
|
||||
@ -23,7 +23,7 @@ int l_is_solid_at(lua_State* L) {
|
||||
int y = lua_tointeger(L, 2);
|
||||
int z = lua_tointeger(L, 3);
|
||||
|
||||
lua_pushboolean(L, scripting::level->chunks->isSolid(x, y, z));
|
||||
lua_pushboolean(L, scripting::level->chunks->isSolidBlock(x, y, z));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -200,7 +200,7 @@ int l_is_replaceable_at(lua_State* L) {
|
||||
int y = lua_tointeger(L, 2);
|
||||
int z = lua_tointeger(L, 3);
|
||||
|
||||
lua_pushboolean(L, scripting::level->chunks->isReplaceable(x, y, z));
|
||||
lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@ -48,7 +48,7 @@ struct AABB {
|
||||
}
|
||||
|
||||
/* Check if given point is inside */
|
||||
inline bool inside(const glm::vec3 pos) const {
|
||||
inline bool contains(const glm::vec3 pos) const {
|
||||
const glm::vec3 p = min();
|
||||
const glm::vec3 s = size();
|
||||
return !(pos.x < p.x || pos.y < p.y || pos.z < p.z ||
|
||||
|
||||
@ -46,7 +46,7 @@ void PhysicsSolver::step(
|
||||
hitbox->grounded = false;
|
||||
for (float x = (px-half.x+E); x <= (px+half.x-E); x+=s){
|
||||
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
|
||||
if (chunks->isObstacle(x,y,z)){
|
||||
if (chunks->isObstacleAt(x,y,z)){
|
||||
hitbox->grounded = true;
|
||||
break;
|
||||
}
|
||||
@ -58,7 +58,7 @@ void PhysicsSolver::step(
|
||||
hitbox->grounded = false;
|
||||
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
|
||||
for (float z = (pz-half.z+E); z <= (pz+half.z-E); z+=s){
|
||||
if (chunks->isObstacle(x,y,z)){
|
||||
if (chunks->isObstacleAt(x,y,z)){
|
||||
hitbox->grounded = true;
|
||||
break;
|
||||
}
|
||||
@ -88,7 +88,7 @@ void PhysicsSolver::colisionCalc(
|
||||
for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){
|
||||
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
|
||||
float x = (pos.x-half.x-E);
|
||||
if ((aabb = chunks->isObstacle(x,y,z))){
|
||||
if ((aabb = chunks->isObstacleAt(x,y,z))){
|
||||
vel.x *= 0.0f;
|
||||
pos.x = floor(x) + aabb->max().x + half.x + E;
|
||||
break;
|
||||
@ -100,7 +100,7 @@ void PhysicsSolver::colisionCalc(
|
||||
for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){
|
||||
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
|
||||
float x = (pos.x+half.x+E);
|
||||
if ((aabb = chunks->isObstacle(x,y,z))){
|
||||
if ((aabb = chunks->isObstacleAt(x,y,z))){
|
||||
vel.x *= 0.0f;
|
||||
pos.x = floor(x) - half.x + aabb->min().x - E;
|
||||
break;
|
||||
@ -113,7 +113,7 @@ void PhysicsSolver::colisionCalc(
|
||||
for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){
|
||||
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
|
||||
float z = (pos.z-half.z-E);
|
||||
if ((aabb = chunks->isObstacle(x,y,z))){
|
||||
if ((aabb = chunks->isObstacleAt(x,y,z))){
|
||||
vel.z *= 0.0f;
|
||||
pos.z = floor(z) + aabb->max().z + half.z + E;
|
||||
break;
|
||||
@ -126,7 +126,7 @@ void PhysicsSolver::colisionCalc(
|
||||
for (float y = (pos.y-half.y+E); y <= (pos.y+half.y-E); y+=s){
|
||||
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
|
||||
float z = (pos.z+half.z+E);
|
||||
if ((aabb = chunks->isObstacle(x,y,z))){
|
||||
if ((aabb = chunks->isObstacleAt(x,y,z))){
|
||||
vel.z *= 0.0f;
|
||||
pos.z = floor(z) - half.z + aabb->min().z - E;
|
||||
break;
|
||||
@ -139,7 +139,7 @@ void PhysicsSolver::colisionCalc(
|
||||
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
|
||||
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
|
||||
float y = (pos.y-half.y-E);
|
||||
if ((aabb = chunks->isObstacle(x,y,z))){
|
||||
if ((aabb = chunks->isObstacleAt(x,y,z))){
|
||||
vel.y *= 0.0f;
|
||||
pos.y = floor(y) + aabb->max().y + half.y;
|
||||
hitbox->grounded = true;
|
||||
@ -152,7 +152,7 @@ void PhysicsSolver::colisionCalc(
|
||||
for (float x = (pos.x-half.x+E); x <= (pos.x+half.x-E); x+=s){
|
||||
for (float z = (pos.z-half.z+E); z <= (pos.z+half.z-E); z+=s){
|
||||
float y = (pos.y+half.y+E);
|
||||
if ((aabb = chunks->isObstacle(x,y,z))){
|
||||
if ((aabb = chunks->isObstacleAt(x,y,z))){
|
||||
vel.y *= 0.0f;
|
||||
pos.y = floor(y) - half.y + aabb->min().y - E;
|
||||
break;
|
||||
|
||||
@ -50,12 +50,9 @@ Chunks::~Chunks(){
|
||||
voxel* Chunks::get(int x, int y, int z){
|
||||
x -= ox * CHUNK_W;
|
||||
z -= oz * CHUNK_D;
|
||||
int cx = x / CHUNK_W;
|
||||
int cy = y / CHUNK_H;
|
||||
int cz = z / CHUNK_D;
|
||||
if (x < 0) cx--;
|
||||
if (y < 0) cy--;
|
||||
if (z < 0) cz--;
|
||||
int cx = floordiv(x, CHUNK_W);
|
||||
int cy = floordiv(y, CHUNK_H);
|
||||
int cz = floordiv(z, CHUNK_D);
|
||||
if (cx < 0 || cy < 0 || cz < 0 || cx >= w || cy >= 1 || cz >= d)
|
||||
return nullptr;
|
||||
shared_ptr<Chunk> chunk = chunks[cz * w + cx]; // chunks is 2D-array
|
||||
@ -67,7 +64,7 @@ voxel* Chunks::get(int x, int y, int z){
|
||||
return &chunk->voxels[(ly * CHUNK_D + lz) * CHUNK_W + lx];
|
||||
}
|
||||
|
||||
const AABB* Chunks::isObstacle(float x, float y, float z){
|
||||
const AABB* Chunks::isObstacleAt(float x, float y, float z){
|
||||
int ix = floor(x);
|
||||
int iy = floor(y);
|
||||
int iz = floor(z);
|
||||
@ -82,7 +79,7 @@ const AABB* Chunks::isObstacle(float x, float y, float z){
|
||||
if (def->rt.solid) {
|
||||
return &hitbox;
|
||||
} else {
|
||||
if (hitbox.inside({x - ix, y - iy, z - iz}))
|
||||
if (hitbox.contains({x - ix, y - iy, z - iz}))
|
||||
return &hitbox;
|
||||
return nullptr;
|
||||
}
|
||||
@ -90,20 +87,27 @@ const AABB* Chunks::isObstacle(float x, float y, float z){
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool Chunks::isSolid(int x, int y, int z) {
|
||||
bool Chunks::isSolidBlock(int x, int y, int z) {
|
||||
voxel* v = get(x, y, z);
|
||||
if (v == nullptr)
|
||||
return false;
|
||||
return contentIds->getBlockDef(v->id)->rt.solid;
|
||||
}
|
||||
|
||||
bool Chunks::isReplaceable(int x, int y, int z) {
|
||||
bool Chunks::isReplaceableBlock(int x, int y, int z) {
|
||||
voxel* v = get(x, y, z);
|
||||
if (v == nullptr)
|
||||
return false;
|
||||
return contentIds->getBlockDef(v->id)->replaceable;
|
||||
}
|
||||
|
||||
bool Chunks::isObstacleBlock(int x, int y, int z) {
|
||||
voxel* v = get(x, y, z);
|
||||
if (v == nullptr)
|
||||
return false;
|
||||
return contentIds->getBlockDef(v->id)->obstacle;
|
||||
}
|
||||
|
||||
ubyte Chunks::getLight(int x, int y, int z, int channel){
|
||||
x -= ox * CHUNK_W;
|
||||
z -= oz * CHUNK_D;
|
||||
|
||||
@ -53,9 +53,10 @@ public:
|
||||
|
||||
glm::vec3 rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist);
|
||||
|
||||
const AABB* isObstacle(float x, float y, float z);
|
||||
bool isSolid(int x, int y, int z);
|
||||
bool isReplaceable(int x, int y, int z);
|
||||
const AABB* isObstacleAt(float x, float y, float z);
|
||||
bool isSolidBlock(int x, int y, int z);
|
||||
bool isReplaceableBlock(int x, int y, int z);
|
||||
bool isObstacleBlock(int x, int y, int z);
|
||||
|
||||
// does not move chunks inside
|
||||
void _setOffset(int x, int z);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user