General refactor
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
#include "ChunksController.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <limits.h>
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
|
||||
#include "../voxels/Block.h"
|
||||
#include "../voxels/Chunk.h"
|
||||
#include "../voxels/Chunks.h"
|
||||
#include "../voxels/ChunksStorage.h"
|
||||
#include "../voxels/WorldGenerator.h"
|
||||
#include "../content/Content.h"
|
||||
#include "../graphics/Mesh.h"
|
||||
#include "../lighting/Lighting.h"
|
||||
#include "../files/WorldFiles.h"
|
||||
#include "../world/Level.h"
|
||||
#include "../world/World.h"
|
||||
#include "../maths/voxmaths.h"
|
||||
|
||||
#define MAX_WORK_PER_FRAME 16
|
||||
#define MIN_SURROUNDING 9
|
||||
|
||||
using std::unique_ptr;
|
||||
using std::shared_ptr;
|
||||
using std::chrono::high_resolution_clock;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::microseconds;
|
||||
|
||||
|
||||
ChunksController::ChunksController(
|
||||
Level* level,
|
||||
Chunks* chunks,
|
||||
Lighting* lighting,
|
||||
uint padding)
|
||||
: level(level),
|
||||
chunks(chunks),
|
||||
lighting(lighting),
|
||||
padding(padding),
|
||||
generator(new WorldGenerator(level->content)) {
|
||||
}
|
||||
|
||||
ChunksController::~ChunksController(){
|
||||
delete generator;
|
||||
}
|
||||
|
||||
void ChunksController::update(int64_t maxDuration) {
|
||||
int64_t mcstotal = 0;
|
||||
for (uint i = 0; i < MAX_WORK_PER_FRAME; i++) {
|
||||
auto start = high_resolution_clock::now();
|
||||
if (loadVisible()) {
|
||||
auto elapsed = high_resolution_clock::now() - start;
|
||||
int64_t mcs = duration_cast<microseconds>(elapsed).count();
|
||||
avgDurationMcs = mcs * 0.2 + avgDurationMcs * 0.8;
|
||||
if (mcstotal + max(avgDurationMcs, mcs) * 2 < maxDuration * 1000) {
|
||||
mcstotal += mcs;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool ChunksController::loadVisible(){
|
||||
const Content* content = level->content;
|
||||
const int w = chunks->w;
|
||||
const int d = chunks->d;
|
||||
const int ox = chunks->ox;
|
||||
const int oz = chunks->oz;
|
||||
int nearX = 0;
|
||||
int nearZ = 0;
|
||||
int minDistance = ((w-padding*2)/2)*((w-padding*2)/2);
|
||||
for (uint z = padding; z < d-padding; z++){
|
||||
for (uint x = padding; x < w-padding; x++){
|
||||
int index = z * w + x;
|
||||
shared_ptr<Chunk> chunk = chunks->chunks[index];
|
||||
if (chunk != nullptr){
|
||||
int surrounding = 0;
|
||||
for (int oz = -1; oz <= 1; oz++){
|
||||
for (int ox = -1; ox <= 1; ox++){
|
||||
Chunk* other = chunks->getChunk(chunk->x+ox, chunk->z+oz);
|
||||
if (other != nullptr) surrounding++;
|
||||
}
|
||||
}
|
||||
chunk->surrounding = surrounding;
|
||||
if (surrounding == MIN_SURROUNDING && !chunk->isLighted()) {
|
||||
lighting->buildSkyLight(chunk->x, chunk->z);
|
||||
lighting->onChunkLoaded(chunk->x, chunk->z);
|
||||
chunk->setLighted(true);
|
||||
return true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
int lx = x - w / 2;
|
||||
int lz = z - d / 2;
|
||||
int distance = (lx * lx + lz * lz);
|
||||
if (distance < minDistance){
|
||||
minDistance = distance;
|
||||
nearX = x;
|
||||
nearZ = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int index = nearZ * w + nearX;
|
||||
shared_ptr<Chunk> chunk = chunks->chunks[index];
|
||||
if (chunk != nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
chunk = level->chunksStorage->create(nearX+ox, nearZ+oz);
|
||||
chunks->putChunk(chunk);
|
||||
|
||||
if (!chunk->isLoaded()) {
|
||||
generator->generate(chunk->voxels, chunk->x, chunk->z, level->world->seed);
|
||||
chunk->setUnsaved(true);
|
||||
}
|
||||
|
||||
chunk->updateHeights();
|
||||
|
||||
ContentIndices* indices = content->indices;
|
||||
for (size_t i = 0; i < CHUNK_VOL; i++) {
|
||||
blockid_t id = chunk->voxels[i].id;
|
||||
if (indices->getBlockDef(id) == nullptr) {
|
||||
std::cout << "corruped block detected at " << i << " of chunk ";
|
||||
std::cout << chunk->x << "x" << chunk->z;
|
||||
std::cout << " -> " << (int)id << std::endl;
|
||||
chunk->voxels[i].id = 11;
|
||||
}
|
||||
}
|
||||
lighting->prebuildSkyLight(chunk->x, chunk->z);
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef VOXELS_CHUNKSCONTROLLER_H_
|
||||
#define VOXELS_CHUNKSCONTROLLER_H_
|
||||
|
||||
#include "../typedefs.h"
|
||||
|
||||
class Level;
|
||||
class Chunks;
|
||||
class Lighting;
|
||||
class WorldFiles;
|
||||
class VoxelRenderer;
|
||||
class ChunksLoader;
|
||||
class WorldGenerator;
|
||||
|
||||
class ChunksController {
|
||||
private:
|
||||
Level* level;
|
||||
Chunks* chunks;
|
||||
Lighting* lighting;
|
||||
int64_t avgDurationMcs = 1000;
|
||||
uint padding;
|
||||
WorldGenerator* generator;
|
||||
public:
|
||||
ChunksController(Level* level, Chunks* chunks, Lighting* lighting, uint padding);
|
||||
~ChunksController();
|
||||
|
||||
void update(int64_t maxDuration);
|
||||
bool loadVisible();
|
||||
};
|
||||
|
||||
#endif /* VOXELS_CHUNKSCONTROLLER_H_ */
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "LevelController.h"
|
||||
#include "../world/Level.h"
|
||||
|
||||
#include "PlayerController.h"
|
||||
#include "ChunksController.h"
|
||||
|
||||
LevelController::LevelController(EngineSettings& settings, Level* level)
|
||||
: settings(settings), level(level) {
|
||||
chunks = new ChunksController(
|
||||
level,
|
||||
level->chunks,
|
||||
level->lighting,
|
||||
settings.chunks.padding);
|
||||
player = new PlayerController(level, settings);
|
||||
}
|
||||
|
||||
LevelController::~LevelController() {
|
||||
delete player;
|
||||
delete chunks;
|
||||
}
|
||||
|
||||
void LevelController::update(
|
||||
float delta,
|
||||
bool input,
|
||||
bool pause,
|
||||
bool interactions) {
|
||||
if (!pause) {
|
||||
if (input) {
|
||||
player->updateKeyboard();
|
||||
} else {
|
||||
player->resetKeyboard();
|
||||
}
|
||||
player->updateCamera(delta, input);
|
||||
player->updateControls(delta);
|
||||
|
||||
}
|
||||
player->refreshCamera();
|
||||
if (interactions) {
|
||||
player->updateInteraction();
|
||||
} else {
|
||||
player->selectedBlockId = -1;
|
||||
}
|
||||
level->update();
|
||||
chunks->update(settings.chunks.loadSpeed);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef LOGIC_LEVEL_CONTROLLER_H_
|
||||
#define LOGIC_LEVEL_CONTROLLER_H_
|
||||
|
||||
#include "../settings.h"
|
||||
|
||||
class Level;
|
||||
class ChunksController;
|
||||
class PlayerController;
|
||||
|
||||
class LevelController {
|
||||
EngineSettings& settings;
|
||||
|
||||
Level* level;
|
||||
|
||||
ChunksController* chunks;
|
||||
PlayerController* player;
|
||||
public:
|
||||
LevelController(EngineSettings& settings, Level* level);
|
||||
~LevelController();
|
||||
|
||||
void update(float delta,
|
||||
bool input,
|
||||
bool pause,
|
||||
bool interactions);
|
||||
};
|
||||
|
||||
#endif // LOGIC_LEVEL_CONTROLLER_H_
|
||||
@@ -0,0 +1,232 @@
|
||||
#include "PlayerController.h"
|
||||
|
||||
#include "../objects/Player.h"
|
||||
#include "../physics/PhysicsSolver.h"
|
||||
#include "../physics/Hitbox.h"
|
||||
#include "../lighting/Lighting.h"
|
||||
#include "../world/Level.h"
|
||||
#include "../content/Content.h"
|
||||
#include "../voxels/Block.h"
|
||||
#include "../voxels/voxel.h"
|
||||
#include "../voxels/Chunks.h"
|
||||
#include "../window/Camera.h"
|
||||
#include "../window/Events.h"
|
||||
#include "../window/input.h"
|
||||
|
||||
#include "../core_defs.h"
|
||||
|
||||
#define CAM_SHAKE_OFFSET 0.025f
|
||||
#define CAM_SHAKE_OFFSET_Y 0.031f
|
||||
#define CAM_SHAKE_SPEED 1.6f
|
||||
#define CAM_SHAKE_DELTA_K 10.0f
|
||||
#define ZOOM_SPEED 16.0f
|
||||
#define CROUCH_ZOOM 0.9f
|
||||
#define RUN_ZOOM 1.1f
|
||||
#define C_ZOOM 0.1f
|
||||
#define CROUCH_SHIFT_Y -0.2f
|
||||
|
||||
using glm::vec2;
|
||||
using glm::vec3;
|
||||
|
||||
CameraControl::CameraControl(Player* player, const CameraSettings& settings)
|
||||
: player(player),
|
||||
camera(player->camera),
|
||||
settings(settings),
|
||||
offset(0.0f, 0.7f, 0.0f) {
|
||||
}
|
||||
|
||||
void CameraControl::refresh() {
|
||||
camera->position = player->hitbox->position + offset;
|
||||
}
|
||||
|
||||
void CameraControl::updateMouse(PlayerInput& input) {
|
||||
float rotX = -Events::deltaX / Window::height * 2;
|
||||
float rotY = -Events::deltaY / Window::height * 2;
|
||||
|
||||
if (input.zoom){
|
||||
rotX /= 4;
|
||||
rotY /= 4;
|
||||
}
|
||||
|
||||
float& camX = player->camX;
|
||||
float& camY = player->camY;
|
||||
camX += rotX;
|
||||
camY += rotY;
|
||||
if (camY < -radians(89.9f)){
|
||||
camY = -radians(89.9f);
|
||||
}
|
||||
if (camY > radians(89.9f)){
|
||||
camY = radians(89.9f);
|
||||
}
|
||||
|
||||
camera->rotation = mat4(1.0f);
|
||||
camera->rotate(camY, camX, 0);
|
||||
}
|
||||
|
||||
void CameraControl::update(PlayerInput& input, float delta) {
|
||||
Hitbox* hitbox = player->hitbox;
|
||||
|
||||
offset = vec3(0.0f, 0.7f, 0.0f);
|
||||
|
||||
if (settings.shaking && !input.cheat) {
|
||||
const float k = CAM_SHAKE_DELTA_K;
|
||||
const float oh = CAM_SHAKE_OFFSET;
|
||||
const float ov = CAM_SHAKE_OFFSET_Y;
|
||||
const vec3& vel = hitbox->velocity;
|
||||
|
||||
interpVel = interpVel * (1.0f - delta * 5) + vel * delta * 0.1f;
|
||||
if (hitbox->grounded && interpVel.y < 0.0f){
|
||||
interpVel.y *= -30.0f;
|
||||
}
|
||||
shake = shake * (1.0f - delta * k);
|
||||
if (hitbox->grounded) {
|
||||
float f = length(vec2(vel.x, vel.z));
|
||||
shakeTimer += delta * f * CAM_SHAKE_SPEED;
|
||||
shake += f * delta * k;
|
||||
}
|
||||
offset += camera->right * sin(shakeTimer) * oh * shake;
|
||||
offset += camera->up * abs(cos(shakeTimer)) * ov * shake;
|
||||
offset -= glm::min(interpVel * 0.05f, 1.0f);
|
||||
}
|
||||
|
||||
if (settings.fovEvents){
|
||||
bool crouch = input.shift && hitbox->grounded && !input.sprint;
|
||||
|
||||
float dt = fmin(1.0f, delta * ZOOM_SPEED);
|
||||
float zoomValue = 1.0f;
|
||||
if (crouch){
|
||||
offset += vec3(0.f, CROUCH_SHIFT_Y, 0.f);
|
||||
zoomValue = CROUCH_ZOOM;
|
||||
} else if (input.sprint){
|
||||
zoomValue = RUN_ZOOM;
|
||||
}
|
||||
if (input.zoom)
|
||||
zoomValue *= C_ZOOM;
|
||||
camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt);
|
||||
}
|
||||
}
|
||||
|
||||
vec3 PlayerController::selectedBlockPosition;
|
||||
int PlayerController::selectedBlockId = -1;
|
||||
|
||||
PlayerController::PlayerController(Level* level, const EngineSettings& settings)
|
||||
: level(level),
|
||||
player(level->player),
|
||||
camControl(level->player, settings.camera) {
|
||||
}
|
||||
|
||||
void PlayerController::updateKeyboard() {
|
||||
input.moveForward = Events::active(BIND_MOVE_FORWARD);
|
||||
input.moveBack = Events::active(BIND_MOVE_BACK);
|
||||
input.moveLeft = Events::active(BIND_MOVE_LEFT);
|
||||
input.moveRight = Events::active(BIND_MOVE_RIGHT);
|
||||
input.sprint = Events::active(BIND_MOVE_SPRINT);
|
||||
input.shift = Events::active(BIND_MOVE_CROUCH);
|
||||
input.cheat = Events::active(BIND_MOVE_CHEAT);
|
||||
input.jump = Events::active(BIND_MOVE_JUMP);
|
||||
input.zoom = Events::active(BIND_CAM_ZOOM);
|
||||
|
||||
input.noclip = Events::jactive(BIND_PLAYER_NOCLIP);
|
||||
input.flight = Events::jactive(BIND_PLAYER_FLIGHT);
|
||||
|
||||
// block choice
|
||||
for (int i = 1; i < 10; i++){
|
||||
if (Events::jpressed(keycode::NUM_0+i)){
|
||||
player->choosenBlock = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerController::updateCamera(float delta, bool movement) {
|
||||
if (movement) {
|
||||
camControl.updateMouse(input);
|
||||
}
|
||||
camControl.update(input, delta);
|
||||
}
|
||||
|
||||
void PlayerController::resetKeyboard() {
|
||||
input.zoom = false;
|
||||
input.moveForward = false;
|
||||
input.moveBack = false;
|
||||
input.moveLeft = false;
|
||||
input.moveRight = false;
|
||||
input.sprint = false;
|
||||
input.shift = false;
|
||||
input.cheat = false;
|
||||
input.jump = false;
|
||||
}
|
||||
|
||||
void PlayerController::updateControls(float delta){
|
||||
player->update(level, input, delta);
|
||||
}
|
||||
|
||||
void PlayerController::refreshCamera() {
|
||||
camControl.refresh();
|
||||
}
|
||||
|
||||
void PlayerController::updateInteraction(){
|
||||
const ContentIndices* contentIds = level->contentIds;
|
||||
Chunks* chunks = level->chunks;
|
||||
Player* player = level->player;
|
||||
Lighting* lighting = level->lighting;
|
||||
Camera* camera = player->camera;
|
||||
vec3 end;
|
||||
vec3 norm;
|
||||
|
||||
bool xkey = Events::pressed(keycode::X);
|
||||
bool lclick = Events::jactive(BIND_PLAYER_ATTACK) ||
|
||||
(xkey && Events::active(BIND_PLAYER_ATTACK));
|
||||
bool rclick = Events::jactive(BIND_PLAYER_BUILD) ||
|
||||
(xkey && Events::active(BIND_PLAYER_BUILD));
|
||||
float maxDistance = 10.0f;
|
||||
if (xkey) {
|
||||
maxDistance *= 20.0f;
|
||||
}
|
||||
vec3 iend;
|
||||
voxel* vox = chunks->rayCast(camera->position,
|
||||
camera->front,
|
||||
maxDistance,
|
||||
end, norm, iend);
|
||||
if (vox != nullptr){
|
||||
player->selectedVoxel = *vox;
|
||||
selectedBlockId = vox->id;
|
||||
selectedBlockPosition = iend;
|
||||
int x = (int)iend.x;
|
||||
int y = (int)iend.y;
|
||||
int z = (int)iend.z;
|
||||
uint8_t states = 0;
|
||||
|
||||
if (contentIds->getBlockDef(player->choosenBlock)->rotatable){
|
||||
if (abs(norm.x) > abs(norm.z)){
|
||||
if (abs(norm.x) > abs(norm.y)) states = BLOCK_DIR_X;
|
||||
if (abs(norm.x) < abs(norm.y)) states = BLOCK_DIR_Y;
|
||||
}
|
||||
if (abs(norm.x) < abs(norm.z)){
|
||||
if (abs(norm.z) > abs(norm.y)) states = BLOCK_DIR_Z;
|
||||
if (abs(norm.z) < abs(norm.y)) states = BLOCK_DIR_Y;
|
||||
}
|
||||
}
|
||||
|
||||
Block* block = contentIds->getBlockDef(vox->id);
|
||||
if (lclick && block->breakable){
|
||||
chunks->set(x,y,z, 0, 0);
|
||||
lighting->onBlockSet(x,y,z, 0);
|
||||
}
|
||||
if (rclick){
|
||||
if (block->model != BlockModel::xsprite){
|
||||
x = (int)(iend.x)+(int)(norm.x);
|
||||
y = (int)(iend.y)+(int)(norm.y);
|
||||
z = (int)(iend.z)+(int)(norm.z);
|
||||
}
|
||||
if (!level->physics->isBlockInside(x,y,z, player->hitbox)){
|
||||
chunks->set(x, y, z, player->choosenBlock, states);
|
||||
lighting->onBlockSet(x,y,z, player->choosenBlock);
|
||||
}
|
||||
}
|
||||
if (Events::jactive(BIND_PLAYER_PICK)){
|
||||
player->choosenBlock = chunks->get(x,y,z)->id;
|
||||
}
|
||||
} else {
|
||||
selectedBlockId = -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef PLAYER_CONTROL_H_
|
||||
#define PLAYER_CONTROL_H_
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "../settings.h"
|
||||
#include "../objects/Player.h"
|
||||
|
||||
class Camera;
|
||||
class Player;
|
||||
class Level;
|
||||
|
||||
class CameraControl {
|
||||
Player* player;
|
||||
Camera* camera;
|
||||
const CameraSettings& settings;
|
||||
glm::vec3 offset;
|
||||
float shake = 0.0f;
|
||||
float shakeTimer = 0.0f;
|
||||
glm::vec3 interpVel {0.0f};
|
||||
public:
|
||||
CameraControl(Player* player, const CameraSettings& settings);
|
||||
void updateMouse(PlayerInput& input);
|
||||
void update(PlayerInput& input, float delta);
|
||||
void refresh();
|
||||
};
|
||||
|
||||
class PlayerController {
|
||||
Level* level;
|
||||
Player* player;
|
||||
PlayerInput input;
|
||||
CameraControl camControl;
|
||||
public:
|
||||
static glm::vec3 selectedBlockPosition;
|
||||
static int selectedBlockId;
|
||||
|
||||
PlayerController(Level* level, const EngineSettings& settings);
|
||||
void updateKeyboard();
|
||||
void updateCamera(float delta, bool movement);
|
||||
void refreshCamera();
|
||||
void resetKeyboard();
|
||||
void updateControls(float delta);
|
||||
void updateInteraction();
|
||||
};
|
||||
|
||||
#endif /* PLAYER_CONTROL_H_ */
|
||||
Reference in New Issue
Block a user