Fix movement in random_input mode (#11592)

This commit is contained in:
NeroBurner 2021-09-05 19:58:50 +02:00 committed by GitHub
parent a3e32d81c5
commit 7f3401412e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 7 deletions

@ -138,11 +138,8 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
#endif #endif
} else if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) { } else if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
/* TODO add a check like: // joystick may be nullptr if game is launched with '--random-input' parameter
if (event.JoystickEvent != joystick_we_listen_for) return joystick && joystick->handleEvent(event.JoystickEvent);
return false;
*/
return joystick->handleEvent(event.JoystickEvent);
} else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) { } else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
// Handle mouse events // Handle mouse events
KeyPress key; KeyPress key;
@ -243,4 +240,39 @@ void RandomInputHandler::step(float dtime)
} }
} }
mousepos += mousespeed; mousepos += mousespeed;
static bool useJoystick = false;
{
static float counterUseJoystick = 0;
counterUseJoystick -= dtime;
if (counterUseJoystick < 0.0) {
counterUseJoystick = 5.0; // switch between joystick and keyboard direction input
useJoystick = !useJoystick;
}
}
if (useJoystick) {
static float counterMovement = 0;
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;
}
} 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;
}
}
} }

@ -393,8 +393,8 @@ public:
virtual bool wasKeyPressed(GameKeyType k) { return false; } virtual bool wasKeyPressed(GameKeyType k) { return false; }
virtual bool wasKeyReleased(GameKeyType k) { return false; } virtual bool wasKeyReleased(GameKeyType k) { return false; }
virtual bool cancelPressed() { return false; } virtual bool cancelPressed() { return false; }
virtual float getMovementSpeed() {return 0.0f;} virtual float getMovementSpeed() { return movementSpeed; }
virtual float getMovementDirection() {return 0.0f;} virtual float getMovementDirection() { return movementDirection; }
virtual v2s32 getMousePos() { return mousepos; } virtual v2s32 getMousePos() { return mousepos; }
virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); } virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
@ -408,4 +408,6 @@ private:
KeyList keydown; KeyList keydown;
v2s32 mousepos; v2s32 mousepos;
v2s32 mousespeed; v2s32 mousespeed;
float movementSpeed;
float movementDirection;
}; };