Player.cpp indents fix

This commit is contained in:
MihailRis 2024-03-07 11:11:40 +03:00
parent 8658a170bc
commit 547c80b3f0

View File

@ -19,14 +19,14 @@ const float CHEAT_SPEED_MUL = 5.0f;
const float JUMP_FORCE = 8.0f; const float JUMP_FORCE = 8.0f;
Player::Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv) : Player::Player(glm::vec3 position, float speed, std::shared_ptr<Inventory> inv) :
speed(speed), speed(speed),
chosenSlot(0), chosenSlot(0),
inventory(inv), inventory(inv),
camera(new Camera(position, glm::radians(90.0f))), camera(new Camera(position, glm::radians(90.0f))),
spCamera(new Camera(position, glm::radians(90.0f))), spCamera(new Camera(position, glm::radians(90.0f))),
tpCamera(new Camera(position, glm::radians(90.0f))), tpCamera(new Camera(position, glm::radians(90.0f))),
currentCamera(camera), currentCamera(camera),
hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f))) hitbox(new Hitbox(position, glm::vec3(0.3f,0.9f,0.3f)))
{ {
} }
@ -34,116 +34,116 @@ Player::~Player() {
} }
void Player::updateInput( void Player::updateInput(
Level* level, Level* level,
PlayerInput& input, PlayerInput& input,
float delta) { float delta) {
bool crouch = input.shift && hitbox->grounded && !input.sprint; bool crouch = input.shift && hitbox->grounded && !input.sprint;
float speed = this->speed; float speed = this->speed;
if (flight){ if (flight){
speed *= FLIGHT_SPEED_MUL; speed *= FLIGHT_SPEED_MUL;
} }
if (input.cheat){ if (input.cheat){
speed *= CHEAT_SPEED_MUL; speed *= CHEAT_SPEED_MUL;
} }
if (crouch) { if (crouch) {
speed *= CROUCH_SPEED_MUL; speed *= CROUCH_SPEED_MUL;
} else if (input.sprint) { } else if (input.sprint) {
speed *= RUN_SPEED_MUL; speed *= RUN_SPEED_MUL;
} }
glm::vec3 dir(0,0,0); glm::vec3 dir(0,0,0);
if (input.moveForward){ if (input.moveForward){
dir.x += camera->dir.x; dir.x += camera->dir.x;
dir.z += camera->dir.z; dir.z += camera->dir.z;
} }
if (input.moveBack){ if (input.moveBack){
dir.x -= camera->dir.x; dir.x -= camera->dir.x;
dir.z -= camera->dir.z; dir.z -= camera->dir.z;
} }
if (input.moveRight){ if (input.moveRight){
dir.x += camera->right.x; dir.x += camera->right.x;
dir.z += camera->right.z; dir.z += camera->right.z;
} }
if (input.moveLeft){ if (input.moveLeft){
dir.x -= camera->right.x; dir.x -= camera->right.x;
dir.z -= camera->right.z; dir.z -= camera->right.z;
} }
if (glm::length(dir) > 0.0f){ if (glm::length(dir) > 0.0f){
dir = glm::normalize(dir); dir = glm::normalize(dir);
hitbox->velocity.x += dir.x * speed * delta * 9; hitbox->velocity.x += dir.x * speed * delta * 9;
hitbox->velocity.z += dir.z * speed * delta * 9; hitbox->velocity.z += dir.z * speed * delta * 9;
} }
float vel = std::max(glm::length(hitbox->velocity * 0.25f), 1.0f); float vel = std::max(glm::length(hitbox->velocity * 0.25f), 1.0f);
int substeps = int(delta * vel * 1000); int substeps = int(delta * vel * 1000);
substeps = std::min(100, std::max(1, substeps)); substeps = std::min(100, std::max(1, substeps));
level->physics->step(level->chunks.get(), hitbox.get(), level->physics->step(level->chunks.get(), hitbox.get(),
delta, substeps, delta, substeps,
crouch, flight ? 0.0f : 1.0f, crouch, flight ? 0.0f : 1.0f,
!noclip); !noclip);
if (flight && hitbox->grounded) { if (flight && hitbox->grounded) {
flight = false; flight = false;
} }
if (input.jump && hitbox->grounded){ if (input.jump && hitbox->grounded){
hitbox->velocity.y = JUMP_FORCE; hitbox->velocity.y = JUMP_FORCE;
} }
if ((input.flight && !noclip) || if ((input.flight && !noclip) ||
(input.noclip && flight == noclip)){ (input.noclip && flight == noclip)){
flight = !flight; flight = !flight;
if (flight){ if (flight){
hitbox->grounded = false; hitbox->grounded = false;
} }
} }
if (input.noclip) { if (input.noclip) {
noclip = !noclip; noclip = !noclip;
} }
hitbox->linear_damping = PLAYER_GROUND_DAMPING; hitbox->linear_damping = PLAYER_GROUND_DAMPING;
if (flight){ if (flight){
hitbox->linear_damping = PLAYER_AIR_DAMPING; hitbox->linear_damping = PLAYER_AIR_DAMPING;
hitbox->velocity.y *= 1.0f - delta * 9; hitbox->velocity.y *= 1.0f - delta * 9;
if (input.jump){ if (input.jump){
hitbox->velocity.y += speed * delta * 9; hitbox->velocity.y += speed * delta * 9;
} }
if (input.shift){ if (input.shift){
hitbox->velocity.y -= speed * delta * 9; hitbox->velocity.y -= speed * delta * 9;
} }
} }
if (!hitbox->grounded) { if (!hitbox->grounded) {
hitbox->linear_damping = PLAYER_AIR_DAMPING; hitbox->linear_damping = PLAYER_AIR_DAMPING;
} }
input.noclip = false; input.noclip = false;
input.flight = false; input.flight = false;
if (spawnpoint.y <= 0.1) { if (spawnpoint.y <= 0.1) {
attemptToFindSpawnpoint(level); attemptToFindSpawnpoint(level);
} }
} }
void Player::teleport(glm::vec3 position) { void Player::teleport(glm::vec3 position) {
hitbox->position = position; hitbox->position = position;
} }
void Player::attemptToFindSpawnpoint(Level* level) { void Player::attemptToFindSpawnpoint(Level* level) {
glm::vec3 ppos = hitbox->position; glm::vec3 ppos = hitbox->position;
glm::vec3 newpos {ppos.x + (rand() % 200 - 100), glm::vec3 newpos {ppos.x + (rand() % 200 - 100),
rand() % 80 + 100, rand() % 80 + 100,
ppos.z + (rand() % 200 - 100)}; ppos.z + (rand() % 200 - 100)};
while (newpos.y > 0 && !level->chunks->isObstacleBlock(newpos.x, newpos.y-2, newpos.z)) { while (newpos.y > 0 && !level->chunks->isObstacleBlock(newpos.x, newpos.y-2, newpos.z)) {
newpos.y--; newpos.y--;
} }
voxel* headvox = level->chunks->get(newpos.x, newpos.y+1, newpos.z); voxel* headvox = level->chunks->get(newpos.x, newpos.y+1, newpos.z);
if (level->chunks->isObstacleBlock(newpos.x, newpos.y, newpos.z) || if (level->chunks->isObstacleBlock(newpos.x, newpos.y, newpos.z) ||
headvox == nullptr || headvox->id != 0) headvox == nullptr || headvox->id != 0)
return; return;
spawnpoint = newpos + glm::vec3(0.5f, 0.0f, 0.5f); spawnpoint = newpos + glm::vec3(0.5f, 0.0f, 0.5f);
teleport(spawnpoint); teleport(spawnpoint);
} }
void Player::setChosenSlot(int index) { void Player::setChosenSlot(int index) {
@ -155,7 +155,7 @@ int Player::getChosenSlot() const {
} }
float Player::getSpeed() const { float Player::getSpeed() const {
return speed; return speed;
} }
std::shared_ptr<Inventory> Player::getInventory() const { std::shared_ptr<Inventory> Player::getInventory() const {
@ -163,62 +163,62 @@ std::shared_ptr<Inventory> Player::getInventory() const {
} }
void Player::setSpawnPoint(glm::vec3 spawnpoint) { void Player::setSpawnPoint(glm::vec3 spawnpoint) {
this->spawnpoint = spawnpoint; this->spawnpoint = spawnpoint;
} }
glm::vec3 Player::getSpawnPoint() const { glm::vec3 Player::getSpawnPoint() const {
return spawnpoint; return spawnpoint;
} }
std::unique_ptr<dynamic::Map> Player::serialize() const { std::unique_ptr<dynamic::Map> Player::serialize() const {
glm::vec3 position = hitbox->position; glm::vec3 position = hitbox->position;
auto root = std::make_unique<dynamic::Map>(); auto root = std::make_unique<dynamic::Map>();
auto& posarr = root->putList("position"); auto& posarr = root->putList("position");
posarr.put(position.x); posarr.put(position.x);
posarr.put(position.y); posarr.put(position.y);
posarr.put(position.z); posarr.put(position.z);
auto& rotarr = root->putList("rotation"); auto& rotarr = root->putList("rotation");
rotarr.put(cam.x); rotarr.put(cam.x);
rotarr.put(cam.y); rotarr.put(cam.y);
auto& sparr = root->putList("spawnpoint"); auto& sparr = root->putList("spawnpoint");
sparr.put(spawnpoint.x); sparr.put(spawnpoint.x);
sparr.put(spawnpoint.y); sparr.put(spawnpoint.y);
sparr.put(spawnpoint.z); sparr.put(spawnpoint.z);
root->put("flight", flight); root->put("flight", flight);
root->put("noclip", noclip); root->put("noclip", noclip);
root->put("chosen-slot", chosenSlot); root->put("chosen-slot", chosenSlot);
root->put("inventory", inventory->serialize().release()); root->put("inventory", inventory->serialize().release());
return root; return root;
} }
void Player::deserialize(dynamic::Map *src) { void Player::deserialize(dynamic::Map *src) {
auto posarr = src->list("position"); auto posarr = src->list("position");
glm::vec3& position = hitbox->position; glm::vec3& position = hitbox->position;
position.x = posarr->num(0); position.x = posarr->num(0);
position.y = posarr->num(1); position.y = posarr->num(1);
position.z = posarr->num(2); position.z = posarr->num(2);
camera->position = position; camera->position = position;
auto rotarr = src->list("rotation"); auto rotarr = src->list("rotation");
cam.x = rotarr->num(0); cam.x = rotarr->num(0);
cam.y = rotarr->num(1); cam.y = rotarr->num(1);
if (src->has("spawnpoint")) { if (src->has("spawnpoint")) {
auto sparr = src->list("spawnpoint"); auto sparr = src->list("spawnpoint");
setSpawnPoint(glm::vec3( setSpawnPoint(glm::vec3(
sparr->num(0), sparr->num(0),
sparr->num(1), sparr->num(1),
sparr->num(2) sparr->num(2)
)); ));
} else { } else {
setSpawnPoint(position); setSpawnPoint(position);
} }
src->flag("flight", flight); src->flag("flight", flight);
src->flag("noclip", noclip); src->flag("noclip", noclip);
setChosenSlot(src->getInt("chosen-slot", getChosenSlot())); setChosenSlot(src->getInt("chosen-slot", getChosenSlot()));
auto invmap = src->map("inventory"); auto invmap = src->map("inventory");