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 13, 2020
1 parent bf793e9 commit 3fe772d
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 41 deletions.
133 changes: 133 additions & 0 deletions docs/lua-api-dimension.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ Example:
biomes = {"default:grassland", "default:desert"}
```

### `player_physics`

Table containing physics the properties of the dimension

Example:
```lua
player_physics = {
gravity = 1.0,
jump_antigravity = 0.3,
}
```

### `gravity`

Gravity of the dimension.
Expand All @@ -35,6 +47,127 @@ gravity = 0.5

Default value is `1`.

### `jump_speed`

Jump speed of the dimension.

Example:
```lua
jump_speed = 0.3
```

Default value is `0.3`.

### `jump_antigravity`

The gravity of the dimension is decreased by this amount while holding space prior to the peak of your jump.

Example:
```lua
jump_antigravity = 0.04
```

Default value is `0.04`.

### `glide_gravity`

Fall Speed while holding Space in the dimension.

Example:
```lua
glide_gravity = 0.05
```

Default value is `0.05`.

### `horizontal_sprint_mod`

Amount to multiply a player's horizontal velocity while sprinting in this dimension.

Example:
```lua
horizontal_sprint_mod = 1.5
```

Default value is `1.5`.

### `vertical_sprint_mod`

Amount to multiply a player's vertical velocity while sprinting in this dimension.

Example:
```lua
vertical_sprint_mod = 1.5
```

Default value is `1.5`.

### `move_speed`

Movement speed in this dimension.

Example:
```lua
move_speed = 0.04
```

Default value is `0.04`.

### `air_speed_mod`

The movement speed of this dimension is multiplied by this amount while in the air.

Example:
```lua
air_speed_mod = 0.75
```

Default value is `0.75`.

### `fly_speed`

Fly speed in this dimension.

Example:
```lua
fly_speed = 0.75
```

Default value is `0.75`.

### `sneak_vertical_speed`

The vertical movement speed of this dimension is multiplied by this amount while sneaking.

Example:
```lua
sneak_vertical_speed = 0.1
```

Default value is `0.1`.

### `sneak_horizontal_mod`

The horizontal movement speed of this dimension is multiplied by this amount while sneaking.

Example:
```lua
sneak_horizontal_mod = 0.1
```

Default value is `0.1`.

### `is_sneak_always_mod`

Whether the horizontal movement speed of this dimension is only multiplied while sneaking on the ground

Example:
```lua
sneak_horizontal_mod = false
```

Default value is `false`.

### `id`

ID of the dimension. **Mandatory field.**
Expand Down
6 changes: 4 additions & 2 deletions mods/default/dimensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ mod:dimension {
biomes = {"default:netherland"},

sky = "default:sky_nether",

gravity = 1.4,

player_physics = {
gravity = 1.4,
}
}

2 changes: 1 addition & 1 deletion 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
85 changes: 57 additions & 28 deletions source/client/world/ClientPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,54 +72,82 @@ 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) {
void ClientPlayer::move(const ClientWorld &world, float direction) {
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 = world->dimension().physics().moveSpeed * cosf(direction * RADIANS_PER_DEGREES);
m_velocity.y = world->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(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 += world->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;
m_velocity.x *= world->dimension().physics().horizontalSprintMod;
m_velocity.y *= world->dimension().physics().verticalSprintMod;
}

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

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

m_velocity.z = -world->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 glide gravity
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 +159,8 @@ 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;
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,6 +222,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;
}
}
Expand Down
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_
4 changes: 2 additions & 2 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 Down
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; }
const DimensionPhysics physics() const { return m_physics; }
void setPhysics(const 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_
Loading

0 comments on commit 3fe772d

Please sign in to comment.