Skip to content

Commit

Permalink
Physics can now be modified per-dimension.
Browse files Browse the repository at this point in the history
  • Loading branch information
1SDANi committed Jul 11, 2020
1 parent b4f84df commit 76d278c
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 48 deletions.
7 changes: 4 additions & 3 deletions mods/default/dimensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ mod:dimension {
biomes = {"default:netherland"},

sky = "default:sky_nether",

gravity = 1.4,

player_physics = {
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);
}

99 changes: 68 additions & 31 deletions source/client/world/ClientPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,54 +72,89 @@ 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().physics().moveSpeed * cosf(direction * RADIANS_PER_DEGREES);
m_velocity.y = chunk->dimension().physics().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().physics().jumpSpeed;
}

if(gk::GamePad::isKeyPressed(GameKey::Sneak)) {
m_velocity.z = -0.1;
if (m_isJumping && m_velocity.z > 0.0f) {
m_velocity.z += chunk->dimension().physics().jumpAntigravity * 0.001f;
}

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(90.0f);
else if(gk::GamePad::isKeyPressed(GameKey::Right)) move(-90.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(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::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 (gk::GamePad::isKeyPressed(GameKey::Sprint)) {
m_velocity.x *= 1.5f;
m_velocity.y *= 1.5f;
if (chunk && chunk->isInitialized()) {
if (gk::GamePad::isKeyPressed(GameKey::Sprint)) {
m_velocity.x *= chunk->dimension().physics().horizontalSprintMod;
m_velocity.y *= chunk->dimension().physics().verticalSprintMod;
}

if(gk::GamePad::isKeyPressed(GameKey::Fly)) {
m_velocity.z = chunk->dimension().physics().flySpeed;
}

if(gk::GamePad::isKeyPressed(GameKey::Sneak)) {
if (m_velocity.z == 0 || chunk->dimension().physics().isSneakAlwaysMod) {
m_velocity.x *= chunk->dimension().physics().sneakHorizontalMod;
m_velocity.y *= chunk->dimension().physics().sneakHorizontalMod;
}

m_velocity.z = -chunk->dimension().physics().sneakVerticalSpeed;
}
}
}

void ClientPlayer::updatePosition(const ClientWorld &world) {
ClientChunk *chunk = (ClientChunk *)world.getChunkAtBlockPos(m_x, m_y, m_z);
if (chunk && chunk->isInitialized()) {
if (!Config::isFlyModeEnabled) {
m_velocity.z -= chunk->dimension().gravity() * 0.001f;
m_velocity.z -= chunk->dimension().physics().gravity * 0.001f;

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().physics().glideGravity) // Limit max vertical speed to jump speed
m_velocity.z = -chunk->dimension().physics().glideGravity;
}
else {
if (m_velocity.z < -chunk->dimension().physics().jumpSpeed) // Limit max vertical speed to jump speed
m_velocity.z = -chunk->dimension().physics().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().physics().airSpeedMod;
m_velocity.y *= chunk->dimension().physics().airSpeedMod;
}
}

setPosition(m_x + m_velocity.x, m_y + m_velocity.y, m_z + m_velocity.z);
Expand Down Expand Up @@ -194,7 +231,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_
5 changes: 2 additions & 3 deletions source/common/world/Dimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#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_physics;
}

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_physics;
}

// Please update 'docs/lua-api-cpp.md' if you change this
Expand All @@ -41,4 +41,3 @@ void Dimension::initUsertype(sol::state &lua) {
"id", &Dimension::id
);
}

8 changes: 5 additions & 3 deletions source/common/world/Dimension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

#include <sol/sol.hpp>

#include <DimensionPhysics.hpp>

class Dimension : public gk::ISerializable {
public:
Dimension() = default;
Expand All @@ -55,8 +57,8 @@ class Dimension : public gk::ISerializable {
const std::string &sky() const { return m_sky; }
void setSky(const std::string &sky) { m_sky = sky; }

float gravity() const { return m_gravity; }
void setGravity(float gravity) { m_gravity = gravity; }
DimensionPhysics physics() const { return m_physics; }
void setPhysics(DimensionPhysics physics) { m_physics = physics; }

static void initUsertype(sol::state &lua);

Expand All @@ -70,7 +72,7 @@ class Dimension : public gk::ISerializable {

std::string m_sky;

float m_gravity = 1.f;
DimensionPhysics m_physics;
};

#endif // DIMENSION_HPP_
15 changes: 15 additions & 0 deletions source/common/world/DimensionPhysics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#include "DimensionPhysics.hpp"
#include "NetworkUtils.hpp"

void DimensionPhysics::serialize(sf::Packet &packet) const {
packet << gravity << jumpSpeed << jumpAntigravity << glideGravity << horizontalSprintMod <<
verticalSprintMod << moveSpeed << airSpeedMod << flySpeed << sneakVerticalSpeed <<
sneakHorizontalMod << isSneakAlwaysMod;
}

void DimensionPhysics::deserialize(sf::Packet &packet) {
packet >> gravity >> jumpSpeed >> jumpAntigravity >> glideGravity >> horizontalSprintMod >>
verticalSprintMod >> moveSpeed >> airSpeedMod >> flySpeed >> sneakVerticalSpeed >>
sneakHorizontalMod >> isSneakAlwaysMod;
}
25 changes: 25 additions & 0 deletions source/common/world/DimensionPhysics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#include <gk/core/ISerializable.hpp>

struct DimensionPhysics : public gk::ISerializable {
void serialize(sf::Packet &packet) const override;
void deserialize(sf::Packet &packet) override;

float gravity = 1.f;
float jumpSpeed = 0.05f;

float jumpAntigravity = 0.3f;
float glideGravity = 0.04f;

float horizontalSprintMod = 1.5f;
float verticalSprintMod = 1.5f;

float moveSpeed = 0.04f;
float airSpeedMod = 0.75f;

float flySpeed = 0.1f;
float sneakVerticalSpeed = 0.1f;
float sneakHorizontalMod = 0.75f;

bool isSneakAlwaysMod = false;
};
29 changes: 27 additions & 2 deletions source/server/lua/loader/LuaDimensionLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,34 @@ 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));

sol::object dimensionPhysicsObject = table["dimension_physics"];
if (dimensionPhysicsObject.valid() && dimensionPhysicsObject.get_type() == sol::type::table) {
sol::table dimensionPhysicsTable = dimensionPhysicsObject.as<sol::table>();

DimensionPhysics dimensionPhysics = dimension.physics();

dimensionPhysics.gravity = table["gravity"].get_or(1.f);
dimensionPhysics.jumpSpeed = table["jump_antigravity"].get_or(0.3f);

dimensionPhysics.jumpAntigravity = table["glide_gravity"].get_or(0.04f);
dimensionPhysics.glideGravity = table["jump_speed"].get_or(0.05f);

dimensionPhysics.horizontalSprintMod = table["horizontal_sprint_mod"].get_or(1.5f);
dimensionPhysics.verticalSprintMod = table["vertical_sprint_mod"].get_or(1.5f);

dimensionPhysics.moveSpeed = table["move_speed"].get_or(0.04f);
dimensionPhysics.airSpeedMod = table["air_speed_mod"].get_or(0.75f);

dimensionPhysics.flySpeed = table["fly_speed"].get_or(0.1f);
dimensionPhysics.sneakVerticalSpeed = table["sneak_vertical_speed"].get_or(0.1f);
dimensionPhysics.sneakHorizontalMod = table["sneak_horizontal_mod"].get_or(0.1f);

dimensionPhysics.isSneakAlwaysMod = table["is_sneak_always_mod"].get_or(false);

dimension.setPhysics(dimensionPhysics);
}

sol::table biomesTable = biomesObject.as<sol::table>();
for (auto &it : biomesTable) {
if (it.second.get_type() == sol::type::string)
Expand All @@ -51,4 +77,3 @@ void LuaDimensionLoader::loadDimension(const sol::table &table) const {
else
gkError() << "For dimension" << id << ": Invalid biome table";
}

0 comments on commit 76d278c

Please sign in to comment.