diff --git a/mods/default/dimensions.lua b/mods/default/dimensions.lua index 7567ecadd..a12a93848 100644 --- a/mods/default/dimensions.lua +++ b/mods/default/dimensions.lua @@ -44,4 +44,3 @@ mod:dimension { gravity = 1.4, } - diff --git a/source/client/states/GameState.cpp b/source/client/states/GameState.cpp index c3de47466..c98ed19f7 100644 --- a/source/client/states/GameState.cpp +++ b/source/client/states/GameState.cpp @@ -159,7 +159,7 @@ void GameState::update() { m_camera.setFieldOfView(Config::cameraFOV); if (!m_stateStack->empty() && &m_stateStack->top() == this) { - m_player.processInputs(); + m_player.processInputs(m_world); } if (!m_areModKeysLoaded) { @@ -228,4 +228,3 @@ void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const { target.draw(m_hud, states); } - diff --git a/source/client/world/ClientPlayer.cpp b/source/client/world/ClientPlayer.cpp index 453762fdc..166d16861 100644 --- a/source/client/world/ClientPlayer.cpp +++ b/source/client/world/ClientPlayer.cpp @@ -72,41 +72,70 @@ void ClientPlayer::updateCamera() { m_camera.setUpVector(gk::Vector3f{sh * sr - ch * sv * cr, -ch * sr - sh * sv * cr, cv * cr}); } -void ClientPlayer::move(float direction) { - direction += m_viewAngleH; +void ClientPlayer::move(const ClientWorld &world, float direction) { + ClientChunk *chunk = (ClientChunk *)world.getChunkAtBlockPos(m_x, m_y, m_z); + if (chunk && chunk->isInitialized()) { + direction += m_viewAngleH; - m_velocity.x = 0.04f * cosf(direction * RADIANS_PER_DEGREES); - m_velocity.y = 0.04f * sinf(direction * RADIANS_PER_DEGREES); + m_velocity.x = chunk->dimension().moveSpeed() * cosf(direction * RADIANS_PER_DEGREES); + m_velocity.y = chunk->dimension().moveSpeed() * sinf(direction * RADIANS_PER_DEGREES); + } } -void ClientPlayer::processInputs() { - if(gk::GamePad::isKeyPressed(GameKey::Jump) && !m_isJumping) { - m_isJumping = true; - m_velocity.z = m_jumpSpeed; - } +void ClientPlayer::processInputs(const ClientWorld &world) { + ClientChunk *chunk = (ClientChunk *)world.getChunkAtBlockPos(m_x, m_y, m_z); - if(gk::GamePad::isKeyPressed(GameKey::Fly)) { - m_velocity.z = 0.1; - } + if (chunk && chunk->isInitialized()) { + if(gk::GamePad::isKeyPressed(GameKey::Jump)) { + if (!m_isJumping) { + m_isJumping = true; + m_velocity.z = chunk->dimension().jumpSpeed(); + } + + if (m_isJumping && m_velocity.z > 0.0f) { + m_velocity.z += chunk->dimension().jumpAntigravity() * 0.001f; + } - if(gk::GamePad::isKeyPressed(GameKey::Sneak)) { - m_velocity.z = -0.1; + if (m_velocity.z < 0.0f) { + if (!m_isGliding) m_isGliding = true; + } + } + else { + if (m_velocity.z < 0.0f) { + if (m_isGliding) m_isGliding = false; + } + } } - if(gk::GamePad::isKeyPressed(GameKey::Forward)) move(0.0f); - else if(gk::GamePad::isKeyPressed(GameKey::Back)) move(180.0f); + if(gk::GamePad::isKeyPressed(GameKey::Forward)) move(world, 0.0f); + else if(gk::GamePad::isKeyPressed(GameKey::Back)) move(world, 180.0f); + + if(gk::GamePad::isKeyPressed(GameKey::Left)) move(world, 90.0f); + else if(gk::GamePad::isKeyPressed(GameKey::Right)) move(world, -90.0f); + + if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Forward)) move(world, 45.0f); + if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Forward)) move(world, -45.0f); + if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Back)) move(world, 135.0f); + if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Back)) move(world, -135.0f); + + if (chunk && chunk->isInitialized()) { + if (gk::GamePad::isKeyPressed(GameKey::Sprint)) { + m_velocity.x *= chunk->dimension().horizontalSprintMod(); + m_velocity.y *= chunk->dimension().verticalSprintMod(); + } - if(gk::GamePad::isKeyPressed(GameKey::Left)) move(90.0f); - else if(gk::GamePad::isKeyPressed(GameKey::Right)) move(-90.0f); + if(gk::GamePad::isKeyPressed(GameKey::Fly)) { + m_velocity.z = chunk->dimension().flySpeed(); + } - if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Forward)) move(45.0f); - if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Forward)) move(-45.0f); - if (gk::GamePad::isKeyPressed(GameKey::Left) && gk::GamePad::isKeyPressed(GameKey::Back)) move(135.0f); - if (gk::GamePad::isKeyPressed(GameKey::Right) && gk::GamePad::isKeyPressed(GameKey::Back)) move(-135.0f); + if(gk::GamePad::isKeyPressed(GameKey::Sneak)) { + if (m_velocity.z == 0 || chunk->dimension().isSneakAlwaysMod()){ + m_velocity.x *= chunk->dimension().sneakHorizontalMod(); + m_velocity.y *= chunk->dimension().sneakHorizontalMod(); + } - if (gk::GamePad::isKeyPressed(GameKey::Sprint)) { - m_velocity.x *= 1.5f; - m_velocity.y *= 1.5f; + m_velocity.z = -chunk->dimension().sneakVerticalSpeed(); + } } } @@ -118,8 +147,14 @@ void ClientPlayer::updatePosition(const ClientWorld &world) { m_isJumping = true; - if (m_velocity.z < -m_jumpSpeed) // Limit max vertical speed to jump speed - m_velocity.z = -m_jumpSpeed; + if (m_isGliding) { + if (m_velocity.z < -chunk->dimension().glideGravity()) // Limit max vertical speed to jump speed + m_velocity.z = -chunk->dimension().glideGravity(); + } + else { + if (m_velocity.z < -chunk->dimension().jumpSpeed()) // Limit max vertical speed to jump speed + m_velocity.z = -chunk->dimension().jumpSpeed(); + } } } else { @@ -131,8 +166,10 @@ void ClientPlayer::updatePosition(const ClientWorld &world) { checkCollisions(world); if (!Config::isFlyModeEnabled && m_velocity.z != 0.f) { - m_velocity.x *= 0.75f; - m_velocity.y *= 0.75f; + if (chunk && chunk->isInitialized()) { + m_velocity.x *= chunk->dimension().airSpeedMod(); + m_velocity.y *= chunk->dimension().airSpeedMod(); + } } setPosition(m_x + m_velocity.x, m_y + m_velocity.y, m_z + m_velocity.z); @@ -196,7 +233,7 @@ void ClientPlayer::testPoint(const ClientWorld &world, double x, double y, doubl if(!passable(world, x, y + vel.y, z)) vel.y = 0.f; if(!passable(world, x, y, z + vel.z)) { if(vel.z < 0.f && m_isJumping) m_isJumping = false; + if(vel.z < 0.f && m_isGliding) m_isGliding = false; vel.z = 0.f; } } - diff --git a/source/client/world/ClientPlayer.hpp b/source/client/world/ClientPlayer.hpp index 9789881fe..c07ffad52 100644 --- a/source/client/world/ClientPlayer.hpp +++ b/source/client/world/ClientPlayer.hpp @@ -54,9 +54,9 @@ class ClientPlayer : public Player { void updateCamera(); - void move(float direction); + void move(const ClientWorld &world, float direction); - void processInputs(); + void processInputs(const ClientWorld &world); void updatePosition(const ClientWorld &world); void checkCollisions(const ClientWorld &world); @@ -86,8 +86,7 @@ class ClientPlayer : public Player { glm::vec3 m_velocity{0.f}; bool m_isJumping = false; - - const float m_jumpSpeed = 0.06f; + bool m_isGliding = false; }; #endif // CLIENTPLAYER_HPP_ diff --git a/source/common/world/Dimension.cpp b/source/common/world/Dimension.cpp index a4826b973..a42b9ac4a 100644 --- a/source/common/world/Dimension.cpp +++ b/source/common/world/Dimension.cpp @@ -28,11 +28,15 @@ #include "NetworkUtils.hpp" void Dimension::serialize(sf::Packet &packet) const { - packet << m_id << m_stringID << m_name << m_biomes << m_sky << m_gravity; + packet << m_id << m_stringID << m_name << m_biomes << m_sky << m_gravity << m_moveSpeed << m_glideGravity + << m_jumpAntigravity << m_jumpSpeed << m_horizontalSprintMod << m_verticalSprintMod << m_airSpeedMod << + m_sneakVerticalSpeed << m_sneakHorizontalMod << m_isSneakAlwaysMod << m_flySpeed; } void Dimension::deserialize(sf::Packet &packet) { - packet >> m_id >> m_stringID >> m_name >> m_biomes >> m_sky >> m_gravity; + packet >> m_id >> m_stringID >> m_name >> m_biomes >> m_sky >> m_gravity >> m_moveSpeed >> m_glideGravity + >> m_jumpAntigravity >> m_jumpSpeed >> m_horizontalSprintMod >> m_verticalSprintMod >> m_airSpeedMod >> + m_sneakVerticalSpeed >> m_sneakHorizontalMod >> m_isSneakAlwaysMod >> m_flySpeed; } // Please update 'docs/lua-api-cpp.md' if you change this @@ -41,4 +45,3 @@ void Dimension::initUsertype(sol::state &lua) { "id", &Dimension::id ); } - diff --git a/source/common/world/Dimension.hpp b/source/common/world/Dimension.hpp index 4d604c014..b65fb6a49 100644 --- a/source/common/world/Dimension.hpp +++ b/source/common/world/Dimension.hpp @@ -58,6 +58,39 @@ class Dimension : public gk::ISerializable { float gravity() const { return m_gravity; } void setGravity(float gravity) { m_gravity = gravity; } + float glideGravity() const { return m_glideGravity; } + void setGlideGravity(float glideGravity) { m_glideGravity = glideGravity; } + + float jumpAntigravity() const { return m_jumpAntigravity; } + void setJumpAntigravity(float jumpAntigravity) { m_jumpAntigravity = jumpAntigravity; } + + float jumpSpeed() const { return m_jumpSpeed; } + void setJumpSpeed(float jumpSpeed) { m_jumpSpeed = jumpSpeed; } + + float moveSpeed() const { return m_moveSpeed; } + void setMoveSpeed(float moveSpeed) { m_moveSpeed = moveSpeed; } + + float horizontalSprintMod() const { return m_horizontalSprintMod; } + void setHorizontalSprintMod(float horizontalSprintMod) { m_horizontalSprintMod = horizontalSprintMod; } + + float verticalSprintMod() const { return m_verticalSprintMod; } + void setVerticalSprintMod(float verticalSprintMod) { m_verticalSprintMod = verticalSprintMod; } + + float airSpeedMod() const { return m_airSpeedMod; } + void setAirSpeedMod(float airSpeedMod) { m_airSpeedMod = airSpeedMod; } + + float flySpeed() const { return m_flySpeed; } + void setFlySpeed(float flySpeed) { m_flySpeed = flySpeed; } + + float sneakVerticalSpeed() const { return m_sneakVerticalSpeed; } + void setSneakVerticalSpeed(float sneakVerticalSpeed) { m_sneakVerticalSpeed = sneakVerticalSpeed; } + + float sneakHorizontalMod() const { return m_sneakHorizontalMod; } + void setSneakHorizontalMod(float sneakHorizontalMod) { m_sneakHorizontalMod = sneakHorizontalMod; } + + bool isSneakAlwaysMod() const { return m_isSneakAlwaysMod; } + void setIsSneakAlwaysMod(bool isSneakAlwaysMod) { m_isSneakAlwaysMod = isSneakAlwaysMod; } + static void initUsertype(sol::state &lua); private: @@ -71,6 +104,23 @@ class Dimension : public gk::ISerializable { std::string m_sky; float m_gravity = 1.f; + float m_jumpAntigravity = 0.3f; + + float m_glideGravity = 0.04f; + float m_jumpSpeed = 0.05f; + + float m_horizontalSprintMod = 1.5f; + float m_verticalSprintMod = 1.5f; + + float m_moveSpeed = 0.04f; + float m_airSpeedMod = 0.75f; + + float m_flySpeed = 0.1f; + + float m_sneakVerticalSpeed = 0.1f; + float m_sneakHorizontalMod = 0.75f; + + bool m_isSneakAlwaysMod = false; }; #endif // DIMENSION_HPP_ diff --git a/source/server/lua/loader/LuaDimensionLoader.cpp b/source/server/lua/loader/LuaDimensionLoader.cpp index 9412f6755..fa6ca8cdd 100644 --- a/source/server/lua/loader/LuaDimensionLoader.cpp +++ b/source/server/lua/loader/LuaDimensionLoader.cpp @@ -38,7 +38,25 @@ void LuaDimensionLoader::loadDimension(const sol::table &table) const { if (biomesObject.valid() && biomesObject.get_type() == sol::type::table) { Dimension &dimension = Registry::getInstance().registerDimension(id, name); dimension.setSky(table["sky"].get()); + dimension.setGravity(table["gravity"].get_or(1.f)); + dimension.setJumpAntigravity(table["jumpAntigravity"].get_or(0.3f)); + + dimension.setGlideGravity(table["glideGravity"].get_or(0.04f)); + dimension.setJumpSpeed(table["jumpSpeed"].get_or(0.05f)); + + dimension.setHorizontalSprintMod(table["horizontalSprintMod"].get_or(1.5f)); + dimension.setVerticalSprintMod(table["verticalSprintMod"].get_or(1.5f)); + + dimension.setMoveSpeed(table["moveSpeed"].get_or(0.04f)); + dimension.setAirSpeedMod(table["airSpeedMod"].get_or(0.75f)); + + dimension.setFlySpeed(table["flySpeed"].get_or(0.1f)); + + dimension.setSneakVerticalSpeed(table["sneakVerticalSpeed"].get_or(0.1f)); + dimension.setSneakHorizontalMod(table["sneakHorizontalMod"].get_or(0.1f)); + + dimension.setIsSneakAlwaysMod(table["isSneakAlwaysMod"].get_or(false)); sol::table biomesTable = biomesObject.as(); for (auto &it : biomesTable) { @@ -51,4 +69,3 @@ void LuaDimensionLoader::loadDimension(const sol::table &table) const { else gkError() << "For dimension" << id << ": Invalid biome table"; } -