PlayerController.cpp indents fix
This commit is contained in:
+172
-172
@@ -32,103 +32,103 @@ const float CROUCH_SHIFT_Y = -0.2f;
|
|||||||
|
|
||||||
|
|
||||||
CameraControl::CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings)
|
CameraControl::CameraControl(std::shared_ptr<Player> player, const CameraSettings& settings)
|
||||||
: player(player),
|
: player(player),
|
||||||
camera(player->camera),
|
camera(player->camera),
|
||||||
currentViewCamera(player->currentCamera),
|
currentViewCamera(player->currentCamera),
|
||||||
settings(settings),
|
settings(settings),
|
||||||
offset(0.0f, 0.7f, 0.0f) {
|
offset(0.0f, 0.7f, 0.0f) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CameraControl::refresh() {
|
void CameraControl::refresh() {
|
||||||
camera->position = player->hitbox->position + offset;
|
camera->position = player->hitbox->position + offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CameraControl::updateMouse(PlayerInput& input) {
|
void CameraControl::updateMouse(PlayerInput& input) {
|
||||||
glm::vec2& cam = player->cam;
|
glm::vec2& cam = player->cam;
|
||||||
|
|
||||||
float sensitivity = (input.zoom ? settings.sensitivity / 4.f : settings.sensitivity);
|
float sensitivity = (input.zoom ? settings.sensitivity / 4.f : settings.sensitivity);
|
||||||
cam -= glm::degrees(Events::delta / (float)Window::height * sensitivity);
|
cam -= glm::degrees(Events::delta / (float)Window::height * sensitivity);
|
||||||
|
|
||||||
if (cam.y < -89.9f) {
|
if (cam.y < -89.9f) {
|
||||||
cam.y = -89.9f;
|
cam.y = -89.9f;
|
||||||
}
|
}
|
||||||
else if (cam.y > 89.9f) {
|
else if (cam.y > 89.9f) {
|
||||||
cam.y = 89.9f;
|
cam.y = 89.9f;
|
||||||
}
|
}
|
||||||
if (cam.x > 180.f) {
|
if (cam.x > 180.f) {
|
||||||
cam.x -= 360.f;
|
cam.x -= 360.f;
|
||||||
}
|
}
|
||||||
else if (cam.x < -180.f) {
|
else if (cam.x < -180.f) {
|
||||||
cam.x += 360.f;
|
cam.x += 360.f;
|
||||||
}
|
}
|
||||||
|
|
||||||
camera->rotation = glm::mat4(1.0f);
|
camera->rotation = glm::mat4(1.0f);
|
||||||
camera->rotate(glm::radians(cam.y), glm::radians(cam.x), 0);
|
camera->rotate(glm::radians(cam.y), glm::radians(cam.x), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) {
|
void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) {
|
||||||
Hitbox* hitbox = player->hitbox.get();
|
Hitbox* hitbox = player->hitbox.get();
|
||||||
|
|
||||||
offset = glm::vec3(0.0f, 0.7f, 0.0f);
|
offset = glm::vec3(0.0f, 0.7f, 0.0f);
|
||||||
|
|
||||||
if (settings.shaking && !input.cheat) {
|
if (settings.shaking && !input.cheat) {
|
||||||
const float k = CAM_SHAKE_DELTA_K;
|
const float k = CAM_SHAKE_DELTA_K;
|
||||||
const float oh = CAM_SHAKE_OFFSET;
|
const float oh = CAM_SHAKE_OFFSET;
|
||||||
const float ov = CAM_SHAKE_OFFSET_Y;
|
const float ov = CAM_SHAKE_OFFSET_Y;
|
||||||
const glm::vec3& vel = hitbox->velocity;
|
const glm::vec3& vel = hitbox->velocity;
|
||||||
|
|
||||||
interpVel = interpVel * (1.0f - delta * 5) + vel * delta * 0.1f;
|
interpVel = interpVel * (1.0f - delta * 5) + vel * delta * 0.1f;
|
||||||
if (hitbox->grounded && interpVel.y < 0.0f){
|
if (hitbox->grounded && interpVel.y < 0.0f){
|
||||||
interpVel.y *= -30.0f;
|
interpVel.y *= -30.0f;
|
||||||
}
|
}
|
||||||
shake = shake * (1.0f - delta * k);
|
shake = shake * (1.0f - delta * k);
|
||||||
if (hitbox->grounded) {
|
if (hitbox->grounded) {
|
||||||
float f = glm::length(glm::vec2(vel.x, vel.z));
|
float f = glm::length(glm::vec2(vel.x, vel.z));
|
||||||
shakeTimer += delta * f * CAM_SHAKE_SPEED;
|
shakeTimer += delta * f * CAM_SHAKE_SPEED;
|
||||||
shake += f * delta * k;
|
shake += f * delta * k;
|
||||||
}
|
}
|
||||||
offset += camera->right * glm::sin(shakeTimer) * oh * shake;
|
offset += camera->right * glm::sin(shakeTimer) * oh * shake;
|
||||||
offset += camera->up * glm::abs(glm::cos(shakeTimer)) * ov * shake;
|
offset += camera->up * glm::abs(glm::cos(shakeTimer)) * ov * shake;
|
||||||
offset -= glm::min(interpVel * 0.05f, 1.0f);
|
offset -= glm::min(interpVel * 0.05f, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings.fovEvents){
|
if (settings.fovEvents){
|
||||||
bool crouch = input.shift && hitbox->grounded && !input.sprint;
|
bool crouch = input.shift && hitbox->grounded && !input.sprint;
|
||||||
|
|
||||||
float dt = fmin(1.0f, delta * ZOOM_SPEED);
|
float dt = fmin(1.0f, delta * ZOOM_SPEED);
|
||||||
float zoomValue = 1.0f;
|
float zoomValue = 1.0f;
|
||||||
if (crouch){
|
if (crouch){
|
||||||
offset += glm::vec3(0.f, CROUCH_SHIFT_Y, 0.f);
|
offset += glm::vec3(0.f, CROUCH_SHIFT_Y, 0.f);
|
||||||
zoomValue = CROUCH_ZOOM;
|
zoomValue = CROUCH_ZOOM;
|
||||||
} else if (input.sprint){
|
} else if (input.sprint){
|
||||||
zoomValue = RUN_ZOOM;
|
zoomValue = RUN_ZOOM;
|
||||||
}
|
}
|
||||||
if (input.zoom)
|
if (input.zoom)
|
||||||
zoomValue *= C_ZOOM;
|
zoomValue *= C_ZOOM;
|
||||||
camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt);
|
camera->zoom = zoomValue * dt + camera->zoom * (1.0f - dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto spCamera = player->spCamera;
|
auto spCamera = player->spCamera;
|
||||||
auto tpCamera = player->tpCamera;
|
auto tpCamera = player->tpCamera;
|
||||||
|
|
||||||
if (input.cameraMode) { //ugly but effective
|
if (input.cameraMode) { //ugly but effective
|
||||||
if (player->currentCamera == camera)
|
if (player->currentCamera == camera)
|
||||||
player->currentCamera = tpCamera;
|
player->currentCamera = tpCamera;
|
||||||
else if (player->currentCamera == spCamera)
|
else if (player->currentCamera == spCamera)
|
||||||
player->currentCamera = camera;
|
player->currentCamera = camera;
|
||||||
else if (player->currentCamera == tpCamera)
|
else if (player->currentCamera == tpCamera)
|
||||||
player->currentCamera = spCamera;
|
player->currentCamera = spCamera;
|
||||||
}
|
}
|
||||||
if (player->currentCamera == spCamera) {
|
if (player->currentCamera == spCamera) {
|
||||||
spCamera->position = chunks->rayCastToObstacle(camera->position, camera->front, 3.0f) - 0.2f*(camera->front);
|
spCamera->position = chunks->rayCastToObstacle(camera->position, camera->front, 3.0f) - 0.2f*(camera->front);
|
||||||
spCamera->dir = -camera->dir;
|
spCamera->dir = -camera->dir;
|
||||||
spCamera->front = -camera->front;
|
spCamera->front = -camera->front;
|
||||||
}
|
}
|
||||||
else if (player->currentCamera == tpCamera) {
|
else if (player->currentCamera == tpCamera) {
|
||||||
tpCamera->position = chunks->rayCastToObstacle(camera->position, -camera->front, 3.0f) + 0.2f * (camera->front);
|
tpCamera->position = chunks->rayCastToObstacle(camera->position, -camera->front, 3.0f) + 0.2f * (camera->front);
|
||||||
tpCamera->dir = camera->dir;
|
tpCamera->dir = camera->dir;
|
||||||
tpCamera->front = camera->front;
|
tpCamera->front = camera->front;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 PlayerController::selectedBlockPosition;
|
glm::vec3 PlayerController::selectedBlockPosition;
|
||||||
@@ -141,68 +141,68 @@ PlayerController::PlayerController(
|
|||||||
Level* level,
|
Level* level,
|
||||||
const EngineSettings& settings,
|
const EngineSettings& settings,
|
||||||
BlocksController* blocksController)
|
BlocksController* blocksController)
|
||||||
: level(level),
|
: level(level),
|
||||||
player(level->getObject<Player>(0)),
|
player(level->getObject<Player>(0)),
|
||||||
camControl(player, settings.camera),
|
camControl(player, settings.camera),
|
||||||
blocksController(blocksController) {
|
blocksController(blocksController) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::update(float delta, bool input, bool pause) {
|
void PlayerController::update(float delta, bool input, bool pause) {
|
||||||
if (!pause) {
|
if (!pause) {
|
||||||
if (input) {
|
if (input) {
|
||||||
updateKeyboard();
|
updateKeyboard();
|
||||||
} else {
|
} else {
|
||||||
resetKeyboard();
|
resetKeyboard();
|
||||||
}
|
}
|
||||||
updateCamera(delta, input);
|
updateCamera(delta, input);
|
||||||
updateControls(delta);
|
updateControls(delta);
|
||||||
|
|
||||||
}
|
}
|
||||||
camControl.refresh();
|
camControl.refresh();
|
||||||
if (input) {
|
if (input) {
|
||||||
updateInteraction();
|
updateInteraction();
|
||||||
} else {
|
} else {
|
||||||
selectedBlockId = -1;
|
selectedBlockId = -1;
|
||||||
selectedBlockStates = 0;
|
selectedBlockStates = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::updateKeyboard() {
|
void PlayerController::updateKeyboard() {
|
||||||
input.moveForward = Events::active(BIND_MOVE_FORWARD);
|
input.moveForward = Events::active(BIND_MOVE_FORWARD);
|
||||||
input.moveBack = Events::active(BIND_MOVE_BACK);
|
input.moveBack = Events::active(BIND_MOVE_BACK);
|
||||||
input.moveLeft = Events::active(BIND_MOVE_LEFT);
|
input.moveLeft = Events::active(BIND_MOVE_LEFT);
|
||||||
input.moveRight = Events::active(BIND_MOVE_RIGHT);
|
input.moveRight = Events::active(BIND_MOVE_RIGHT);
|
||||||
input.sprint = Events::active(BIND_MOVE_SPRINT);
|
input.sprint = Events::active(BIND_MOVE_SPRINT);
|
||||||
input.shift = Events::active(BIND_MOVE_CROUCH);
|
input.shift = Events::active(BIND_MOVE_CROUCH);
|
||||||
input.cheat = Events::active(BIND_MOVE_CHEAT);
|
input.cheat = Events::active(BIND_MOVE_CHEAT);
|
||||||
input.jump = Events::active(BIND_MOVE_JUMP);
|
input.jump = Events::active(BIND_MOVE_JUMP);
|
||||||
input.zoom = Events::active(BIND_CAM_ZOOM);
|
input.zoom = Events::active(BIND_CAM_ZOOM);
|
||||||
input.cameraMode = Events::jactive(BIND_CAM_MODE);
|
input.cameraMode = Events::jactive(BIND_CAM_MODE);
|
||||||
input.noclip = Events::jactive(BIND_PLAYER_NOCLIP);
|
input.noclip = Events::jactive(BIND_PLAYER_NOCLIP);
|
||||||
input.flight = Events::jactive(BIND_PLAYER_FLIGHT);
|
input.flight = Events::jactive(BIND_PLAYER_FLIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::updateCamera(float delta, bool movement) {
|
void PlayerController::updateCamera(float delta, bool movement) {
|
||||||
if (movement) {
|
if (movement) {
|
||||||
camControl.updateMouse(input);
|
camControl.updateMouse(input);
|
||||||
}
|
}
|
||||||
camControl.update(input, delta, level->chunks.get());
|
camControl.update(input, delta, level->chunks.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::resetKeyboard() {
|
void PlayerController::resetKeyboard() {
|
||||||
input.zoom = false;
|
input.zoom = false;
|
||||||
input.moveForward = false;
|
input.moveForward = false;
|
||||||
input.moveBack = false;
|
input.moveBack = false;
|
||||||
input.moveLeft = false;
|
input.moveLeft = false;
|
||||||
input.moveRight = false;
|
input.moveRight = false;
|
||||||
input.sprint = false;
|
input.sprint = false;
|
||||||
input.shift = false;
|
input.shift = false;
|
||||||
input.cheat = false;
|
input.cheat = false;
|
||||||
input.jump = false;
|
input.jump = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::updateControls(float delta){
|
void PlayerController::updateControls(float delta){
|
||||||
player->updateInput(level, input, delta);
|
player->updateInput(level, input, delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int determine_rotation(Block* def, glm::ivec3& norm, glm::vec3& camDir) {
|
static int determine_rotation(Block* def, glm::ivec3& norm, glm::vec3& camDir) {
|
||||||
@@ -247,56 +247,56 @@ static void pick_block(ContentIndices* indices, Chunks* chunks, Player* player,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PlayerController::updateInteraction(){
|
void PlayerController::updateInteraction(){
|
||||||
auto indices = level->content->getIndices();
|
auto indices = level->content->getIndices();
|
||||||
Chunks* chunks = level->chunks.get();
|
Chunks* chunks = level->chunks.get();
|
||||||
Lighting* lighting = level->lighting.get();
|
Lighting* lighting = level->lighting.get();
|
||||||
Camera* camera = player->camera.get();
|
Camera* camera = player->camera.get();
|
||||||
|
|
||||||
bool xkey = Events::pressed(keycode::X);
|
bool xkey = Events::pressed(keycode::X);
|
||||||
bool lclick = Events::jactive(BIND_PLAYER_ATTACK) ||
|
bool lclick = Events::jactive(BIND_PLAYER_ATTACK) ||
|
||||||
(xkey && Events::active(BIND_PLAYER_ATTACK));
|
(xkey && Events::active(BIND_PLAYER_ATTACK));
|
||||||
bool rclick = Events::jactive(BIND_PLAYER_BUILD) ||
|
bool rclick = Events::jactive(BIND_PLAYER_BUILD) ||
|
||||||
(xkey && Events::active(BIND_PLAYER_BUILD));
|
(xkey && Events::active(BIND_PLAYER_BUILD));
|
||||||
float maxDistance = 10.0f;
|
float maxDistance = 10.0f;
|
||||||
if (xkey) {
|
if (xkey) {
|
||||||
maxDistance *= 20.0f;
|
maxDistance *= 20.0f;
|
||||||
}
|
}
|
||||||
auto inventory = player->getInventory();
|
auto inventory = player->getInventory();
|
||||||
ItemStack& stack = inventory->getSlot(player->getChosenSlot());
|
ItemStack& stack = inventory->getSlot(player->getChosenSlot());
|
||||||
ItemDef* item = indices->getItemDef(stack.getItemId());
|
ItemDef* item = indices->getItemDef(stack.getItemId());
|
||||||
|
|
||||||
glm::vec3 end;
|
glm::vec3 end;
|
||||||
glm::ivec3 iend;
|
glm::ivec3 iend;
|
||||||
glm::ivec3 norm;
|
glm::ivec3 norm;
|
||||||
voxel* vox = chunks->rayCast(
|
voxel* vox = chunks->rayCast(
|
||||||
camera->position,
|
camera->position,
|
||||||
camera->front,
|
camera->front,
|
||||||
maxDistance,
|
maxDistance,
|
||||||
end, norm, iend
|
end, norm, iend
|
||||||
);
|
);
|
||||||
if (vox != nullptr){
|
if (vox != nullptr){
|
||||||
player->selectedVoxel = *vox;
|
player->selectedVoxel = *vox;
|
||||||
selectedBlockId = vox->id;
|
selectedBlockId = vox->id;
|
||||||
selectedBlockStates = vox->states;
|
selectedBlockStates = vox->states;
|
||||||
selectedBlockPosition = iend;
|
selectedBlockPosition = iend;
|
||||||
selectedPointPosition = end;
|
selectedPointPosition = end;
|
||||||
selectedBlockNormal = norm;
|
selectedBlockNormal = norm;
|
||||||
int x = iend.x;
|
int x = iend.x;
|
||||||
int y = iend.y;
|
int y = iend.y;
|
||||||
int z = iend.z;
|
int z = iend.z;
|
||||||
|
|
||||||
Block* def = indices->getBlockDef(item->rt.placingBlock);
|
Block* def = indices->getBlockDef(item->rt.placingBlock);
|
||||||
uint8_t states = determine_rotation(def, norm, camera->dir);
|
uint8_t states = determine_rotation(def, norm, camera->dir);
|
||||||
|
|
||||||
if (lclick && !input.shift && item->rt.funcsset.on_block_break_by) {
|
if (lclick && !input.shift && item->rt.funcsset.on_block_break_by) {
|
||||||
if (scripting::on_item_break_block(player.get(), item, x, y, z))
|
if (scripting::on_item_break_block(player.get(), item, x, y, z))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Block* target = indices->getBlockDef(vox->id);
|
Block* target = indices->getBlockDef(vox->id);
|
||||||
if (lclick && target->breakable){
|
if (lclick && target->breakable){
|
||||||
blocksController->breakBlock(player.get(), target, x, y, z);
|
blocksController->breakBlock(player.get(), target, x, y, z);
|
||||||
}
|
}
|
||||||
if (rclick && !input.shift) {
|
if (rclick && !input.shift) {
|
||||||
bool preventDefault = false;
|
bool preventDefault = false;
|
||||||
|
|
||||||
@@ -310,25 +310,25 @@ void PlayerController::updateInteraction(){
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (def && rclick){
|
if (def && rclick){
|
||||||
if (!input.shift && target->rt.funcsset.oninteract) {
|
if (!input.shift && target->rt.funcsset.oninteract) {
|
||||||
if (scripting::on_block_interact(player.get(), target, x, y, z))
|
if (scripting::on_block_interact(player.get(), target, x, y, z))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!target->replaceable){
|
if (!target->replaceable){
|
||||||
x = (iend.x)+(norm.x);
|
x = (iend.x)+(norm.x);
|
||||||
y = (iend.y)+(norm.y);
|
y = (iend.y)+(norm.y);
|
||||||
z = (iend.z)+(norm.z);
|
z = (iend.z)+(norm.z);
|
||||||
} else {
|
} else {
|
||||||
if (def->rotations.name == "pipe") {
|
if (def->rotations.name == "pipe") {
|
||||||
states = BLOCK_DIR_UP;
|
states = BLOCK_DIR_UP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vox = chunks->get(x, y, z);
|
vox = chunks->get(x, y, z);
|
||||||
blockid_t chosenBlock = def->rt.id;
|
blockid_t chosenBlock = def->rt.id;
|
||||||
if (vox && (target = indices->getBlockDef(vox->id))->replaceable) {
|
if (vox && (target = indices->getBlockDef(vox->id))->replaceable) {
|
||||||
if (!level->physics->isBlockInside(x,y,z, player->hitbox.get())
|
if (!level->physics->isBlockInside(x,y,z, player->hitbox.get())
|
||||||
|| !def->obstacle){
|
|| !def->obstacle){
|
||||||
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
if (def->grounded && !chunks->isSolidBlock(x, y-1, z)) {
|
||||||
chosenBlock = 0;
|
chosenBlock = 0;
|
||||||
}
|
}
|
||||||
@@ -340,16 +340,16 @@ void PlayerController::updateInteraction(){
|
|||||||
}
|
}
|
||||||
blocksController->updateSides(x, y, z);
|
blocksController->updateSides(x, y, z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Events::jactive(BIND_PLAYER_PICK)) {
|
if (Events::jactive(BIND_PLAYER_PICK)) {
|
||||||
pick_block(indices, chunks, player.get(), x, y, z);
|
pick_block(indices, chunks, player.get(), x, y, z);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
selectedBlockId = -1;
|
selectedBlockId = -1;
|
||||||
selectedBlockStates = 0;
|
selectedBlockStates = 0;
|
||||||
}
|
}
|
||||||
if (rclick) {
|
if (rclick) {
|
||||||
if (item->rt.funcsset.on_use) {
|
if (item->rt.funcsset.on_use) {
|
||||||
scripting::on_item_use(player.get(), item);
|
scripting::on_item_use(player.get(), item);
|
||||||
|
|||||||
Reference in New Issue
Block a user