mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-05 19:31:04 +00:00
Expose analog joystick input to the Lua API (#14348)
This commit is contained in:
parent
6569fdd4d1
commit
22ef4c8be1
13 changed files with 127 additions and 83 deletions
|
@ -1034,7 +1034,7 @@ void Client::Send(NetworkPacket* pkt)
|
|||
m_con->Send(PEER_ID_SERVER, scf.channel, pkt, scf.reliable);
|
||||
}
|
||||
|
||||
// Will fill up 12 + 12 + 4 + 4 + 4 + 1 + 1 + 1 bytes
|
||||
// Will fill up 12 + 12 + 4 + 4 + 4 + 1 + 1 + 1 + 4 + 4 bytes
|
||||
void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *pkt, bool camera_inverted)
|
||||
{
|
||||
v3f pf = myplayer->getPosition() * 100;
|
||||
|
@ -1046,6 +1046,8 @@ void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *
|
|||
u8 fov = std::fmin(255.0f, clientMap->getCameraFov() * 80.0f);
|
||||
u8 wanted_range = std::fmin(255.0f,
|
||||
std::ceil(clientMap->getWantedRange() * (1.0f / MAP_BLOCKSIZE)));
|
||||
f32 movement_speed = myplayer->control.movement_speed;
|
||||
f32 movement_dir = myplayer->control.movement_direction;
|
||||
|
||||
v3s32 position(pf.X, pf.Y, pf.Z);
|
||||
v3s32 speed(sf.X, sf.Y, sf.Z);
|
||||
|
@ -1060,10 +1062,13 @@ void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *
|
|||
[12+12+4+4+4] u8 fov*80
|
||||
[12+12+4+4+4+1] u8 ceil(wanted_range / MAP_BLOCKSIZE)
|
||||
[12+12+4+4+4+1+1] u8 camera_inverted (bool)
|
||||
[12+12+4+4+4+1+1+1] f32 movement_speed
|
||||
[12+12+4+4+4+1+1+1+4] f32 movement_direction
|
||||
*/
|
||||
*pkt << position << speed << pitch << yaw << keyPressed;
|
||||
*pkt << fov << wanted_range;
|
||||
*pkt << camera_inverted;
|
||||
*pkt << movement_speed << movement_dir;
|
||||
}
|
||||
|
||||
void Client::interact(InteractAction action, const PointedThing& pointed)
|
||||
|
@ -1397,6 +1402,8 @@ void Client::sendPlayerPos()
|
|||
|
||||
u32 keyPressed = player->control.getKeysPressed();
|
||||
bool camera_inverted = m_camera->getCameraMode() == CAMERA_MODE_THIRD_FRONT;
|
||||
f32 movement_speed = player->control.movement_speed;
|
||||
f32 movement_dir = player->control.movement_direction;
|
||||
|
||||
if (
|
||||
player->last_position == player->getPosition() &&
|
||||
|
@ -1406,7 +1413,9 @@ void Client::sendPlayerPos()
|
|||
player->last_keyPressed == keyPressed &&
|
||||
player->last_camera_fov == camera_fov &&
|
||||
player->last_camera_inverted == camera_inverted &&
|
||||
player->last_wanted_range == wanted_range)
|
||||
player->last_wanted_range == wanted_range &&
|
||||
player->last_movement_speed == movement_speed &&
|
||||
player->last_movement_dir == movement_dir)
|
||||
return;
|
||||
|
||||
player->last_position = player->getPosition();
|
||||
|
@ -1417,8 +1426,10 @@ void Client::sendPlayerPos()
|
|||
player->last_camera_fov = camera_fov;
|
||||
player->last_camera_inverted = camera_inverted;
|
||||
player->last_wanted_range = wanted_range;
|
||||
player->last_movement_speed = movement_speed;
|
||||
player->last_movement_dir = movement_dir;
|
||||
|
||||
NetworkPacket pkt(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4 + 1 + 1 + 1);
|
||||
NetworkPacket pkt(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4 + 1 + 1 + 1 + 4 + 4);
|
||||
|
||||
writePlayerPos(player, &map, &pkt, camera_inverted);
|
||||
|
||||
|
|
|
@ -2752,9 +2752,10 @@ void Game::updatePlayerControl(const CameraOrientation &cam)
|
|||
isKeyDown(KeyType::PLACE),
|
||||
cam.camera_pitch,
|
||||
cam.camera_yaw,
|
||||
input->getMovementSpeed(),
|
||||
input->getMovementDirection()
|
||||
input->getJoystickSpeed(),
|
||||
input->getJoystickDirection()
|
||||
);
|
||||
control.setMovementFromKeys();
|
||||
|
||||
// autoforward if set: move at maximum speed
|
||||
if (player->getPlayerSettings().continuous_forward &&
|
||||
|
|
|
@ -220,48 +220,19 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
|
|||
/*
|
||||
* RealInputHandler
|
||||
*/
|
||||
float RealInputHandler::getMovementSpeed()
|
||||
float RealInputHandler::getJoystickSpeed()
|
||||
{
|
||||
bool f = m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]),
|
||||
b = m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]),
|
||||
l = m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]),
|
||||
r = m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]);
|
||||
if (f || b || l || r)
|
||||
{
|
||||
// if contradictory keys pressed, stay still
|
||||
if (f && b && l && r)
|
||||
return 0.0f;
|
||||
else if (f && b && !l && !r)
|
||||
return 0.0f;
|
||||
else if (!f && !b && l && r)
|
||||
return 0.0f;
|
||||
return 1.0f; // If there is a keyboard event, assume maximum speed
|
||||
}
|
||||
if (g_touchcontrols && g_touchcontrols->getMovementSpeed())
|
||||
return g_touchcontrols->getMovementSpeed();
|
||||
if (g_touchcontrols && g_touchcontrols->getJoystickSpeed())
|
||||
return g_touchcontrols->getJoystickSpeed();
|
||||
return joystick.getMovementSpeed();
|
||||
}
|
||||
|
||||
float RealInputHandler::getMovementDirection()
|
||||
float RealInputHandler::getJoystickDirection()
|
||||
{
|
||||
float x = 0, z = 0;
|
||||
|
||||
/* Check keyboard for input */
|
||||
if (m_receiver->IsKeyDown(keycache.key[KeyType::FORWARD]))
|
||||
z += 1;
|
||||
if (m_receiver->IsKeyDown(keycache.key[KeyType::BACKWARD]))
|
||||
z -= 1;
|
||||
if (m_receiver->IsKeyDown(keycache.key[KeyType::RIGHT]))
|
||||
x += 1;
|
||||
if (m_receiver->IsKeyDown(keycache.key[KeyType::LEFT]))
|
||||
x -= 1;
|
||||
|
||||
if (x != 0 || z != 0) /* If there is a keyboard event, it takes priority */
|
||||
return std::atan2(x, z);
|
||||
// `getMovementDirection() == 0` means forward, so we cannot use
|
||||
// `getMovementDirection()` as a condition.
|
||||
else if (g_touchcontrols && g_touchcontrols->getMovementSpeed())
|
||||
return g_touchcontrols->getMovementDirection();
|
||||
// `getJoystickDirection() == 0` means forward, so we cannot use
|
||||
// `getJoystickDirection()` as a condition.
|
||||
if (g_touchcontrols && g_touchcontrols->getJoystickSpeed())
|
||||
return g_touchcontrols->getJoystickDirection();
|
||||
return joystick.getMovementDirection();
|
||||
}
|
||||
|
||||
|
@ -320,25 +291,11 @@ void RandomInputHandler::step(float dtime)
|
|||
counterMovement -= dtime;
|
||||
if (counterMovement < 0.0) {
|
||||
counterMovement = 0.1 * Rand(1, 40);
|
||||
movementSpeed = Rand(0,100)*0.01;
|
||||
movementDirection = Rand(-100, 100)*0.01 * M_PI;
|
||||
joystickSpeed = Rand(0,100)*0.01;
|
||||
joystickDirection = Rand(-100, 100)*0.01 * M_PI;
|
||||
}
|
||||
} else {
|
||||
bool f = keydown[keycache.key[KeyType::FORWARD]],
|
||||
l = keydown[keycache.key[KeyType::LEFT]];
|
||||
if (f || l) {
|
||||
movementSpeed = 1.0f;
|
||||
if (f && !l)
|
||||
movementDirection = 0.0;
|
||||
else if (!f && l)
|
||||
movementDirection = -M_PI_2;
|
||||
else if (f && l)
|
||||
movementDirection = -M_PI_4;
|
||||
else
|
||||
movementDirection = 0.0;
|
||||
} else {
|
||||
movementSpeed = 0.0;
|
||||
movementDirection = 0.0;
|
||||
}
|
||||
joystickSpeed = 0.0f;
|
||||
joystickDirection = 0.0f;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -247,8 +247,8 @@ public:
|
|||
virtual bool wasKeyReleased(GameKeyType k) = 0;
|
||||
virtual bool cancelPressed() = 0;
|
||||
|
||||
virtual float getMovementSpeed() = 0;
|
||||
virtual float getMovementDirection() = 0;
|
||||
virtual float getJoystickSpeed() = 0;
|
||||
virtual float getJoystickDirection() = 0;
|
||||
|
||||
virtual void clearWasKeyPressed() {}
|
||||
virtual void clearWasKeyReleased() {}
|
||||
|
@ -304,9 +304,9 @@ public:
|
|||
return m_receiver->WasKeyReleased(keycache.key[k]) || joystick.wasKeyReleased(k);
|
||||
}
|
||||
|
||||
virtual float getMovementSpeed();
|
||||
virtual float getJoystickSpeed();
|
||||
|
||||
virtual float getMovementDirection();
|
||||
virtual float getJoystickDirection();
|
||||
|
||||
virtual bool cancelPressed()
|
||||
{
|
||||
|
@ -388,8 +388,8 @@ public:
|
|||
virtual bool wasKeyPressed(GameKeyType k) { return false; }
|
||||
virtual bool wasKeyReleased(GameKeyType k) { return false; }
|
||||
virtual bool cancelPressed() { return false; }
|
||||
virtual float getMovementSpeed() { return movementSpeed; }
|
||||
virtual float getMovementDirection() { return movementDirection; }
|
||||
virtual float getJoystickSpeed() { return joystickSpeed; }
|
||||
virtual float getJoystickDirection() { return joystickDirection; }
|
||||
virtual v2s32 getMousePos() { return mousepos; }
|
||||
virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
|
||||
|
||||
|
@ -403,6 +403,6 @@ private:
|
|||
KeyList keydown;
|
||||
v2s32 mousepos;
|
||||
v2s32 mousespeed;
|
||||
float movementSpeed;
|
||||
float movementDirection;
|
||||
float joystickSpeed;
|
||||
float joystickDirection;
|
||||
};
|
||||
|
|
|
@ -105,6 +105,8 @@ public:
|
|||
u8 last_camera_fov = 0;
|
||||
u8 last_wanted_range = 0;
|
||||
bool last_camera_inverted = false;
|
||||
f32 last_movement_speed = 0.0f;
|
||||
f32 last_movement_dir = 0.0f;
|
||||
|
||||
float camera_impact = 0.0f;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue