Skip to content

Commit

Permalink
Migrated Move Speed, Fly Speed, Sneak Fall Vertical Speed, and Horizo…
Browse files Browse the repository at this point in the history
…ntal and Vertical Sprint Mods declaration to Lua moddabl Dimension attributes

Added Jump Antigravity and Glide Gravity as Lua moddable Dimension attributes:
Holding Jump while moving upwards after a jump will reduce the effects of Gravity on the player, granting them greater control over the height of their Jump. Values of this and Jump height were modifier to preserve the inability to jump 2 full blocks high
Holding Jump while falling will reduce the player's downwards speed cap from being equal to their Move Speed to the Glide Gravity as defined in the current dimension
Move and Process Inputs now require an instance of ClientWorld to be passed to them
  • Loading branch information
1SDANi committed Jul 9, 2020
1 parent 02b9f8b commit 1832f1b
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 41 deletions.
1 change: 0 additions & 1 deletion mods/default/dimensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ mod:dimension {

gravity = 1.4,
}

3 changes: 1 addition & 2 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -228,4 +228,3 @@ void GameState::draw(gk::RenderTarget &target, gk::RenderStates states) const {

target.draw(m_hud, states);
}

97 changes: 67 additions & 30 deletions source/client/world/ClientPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand All @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}

7 changes: 3 additions & 4 deletions source/client/world/ClientPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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_
9 changes: 6 additions & 3 deletions source/common/world/Dimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,4 +45,3 @@ void Dimension::initUsertype(sol::state &lua) {
"id", &Dimension::id
);
}

50 changes: 50 additions & 0 deletions source/common/world/Dimension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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_
19 changes: 18 additions & 1 deletion source/server/lua/loader/LuaDimensionLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>());

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<sol::table>();
for (auto &it : biomesTable) {
Expand All @@ -51,4 +69,3 @@ void LuaDimensionLoader::loadDimension(const sol::table &table) const {
else
gkError() << "For dimension" << id << ": Invalid biome table";
}

0 comments on commit 1832f1b

Please sign in to comment.