nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
walking.cc
Go to the documentation of this file.
1#include "server/walking.h"
2
3#include <algorithm>
4#include <cmath>
5
6namespace nanommo {
7
8void Walk(const Terrain& terrain, float& x, float& z, float to_x, float to_z) {
9 if (terrain.Walkable(to_x, to_z)) {
10 x = to_x;
11 z = to_z;
12 } else if (terrain.Walkable(to_x, z)) {
13 x = to_x;
14 } else if (terrain.Walkable(x, to_z)) {
15 z = to_z;
16 }
17}
18
19void WalkAlong(const Terrain& terrain, float& x, float& z, float direction_x,
20 float direction_z, bool run, float seconds) {
21 // A diagonal goes no faster than a straight line. The server's input is
22 // that long at most already, but the client walks you with the keys as they
23 // are, and W and D together would take it ahead of the server.
24 const float length = std::hypot(direction_x, direction_z);
25 if (length > 1.0f) {
26 direction_x /= length;
27 direction_z /= length;
28 }
29 const float speed = run ? kRunSpeed : kWalkSpeed;
30 const float edge = terrain.edge();
31 Walk(terrain, x, z,
32 std::clamp(x + direction_x * speed * seconds, -edge, edge),
33 std::clamp(z + direction_z * speed * seconds, -edge, edge));
34}
35
36} // namespace nanommo
float edge() const
Half a metre short of each side, so that players stay on the grid.
Definition terrain.h:91
bool Walkable(float x, float z) const
Definition terrain.cc:123
Definition chat.cc:9
constexpr float kWalkSpeed
In tiles a second. A tile is a metre, and walking is the walk clip's speed.
Definition walking.h:9
constexpr float kRunSpeed
Definition walking.h:10
void Walk(const Terrain &terrain, float &x, float &z, float to_x, float to_z)
Definition walking.cc:8
void WalkAlong(const Terrain &terrain, float &x, float &z, float direction_x, float direction_z, bool run, float seconds)
Definition walking.cc:19