Skip to content

Commit 7f34014

Browse files
authoredSep 5, 2021
Fix movement in random_input mode (#11592)
1 parent a3e32d8 commit 7f34014

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed
 

‎src/client/inputhandler.cpp

+37-5
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,8 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
138138
#endif
139139

140140
} else if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
141-
/* TODO add a check like:
142-
if (event.JoystickEvent != joystick_we_listen_for)
143-
return false;
144-
*/
145-
return joystick->handleEvent(event.JoystickEvent);
141+
// joystick may be nullptr if game is launched with '--random-input' parameter
142+
return joystick && joystick->handleEvent(event.JoystickEvent);
146143
} else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
147144
// Handle mouse events
148145
KeyPress key;
@@ -243,4 +240,39 @@ void RandomInputHandler::step(float dtime)
243240
}
244241
}
245242
mousepos += mousespeed;
243+
static bool useJoystick = false;
244+
{
245+
static float counterUseJoystick = 0;
246+
counterUseJoystick -= dtime;
247+
if (counterUseJoystick < 0.0) {
248+
counterUseJoystick = 5.0; // switch between joystick and keyboard direction input
249+
useJoystick = !useJoystick;
250+
}
251+
}
252+
if (useJoystick) {
253+
static float counterMovement = 0;
254+
counterMovement -= dtime;
255+
if (counterMovement < 0.0) {
256+
counterMovement = 0.1 * Rand(1, 40);
257+
movementSpeed = Rand(0,100)*0.01;
258+
movementDirection = Rand(-100, 100)*0.01 * M_PI;
259+
}
260+
} else {
261+
bool f = keydown[keycache.key[KeyType::FORWARD]],
262+
l = keydown[keycache.key[KeyType::LEFT]];
263+
if (f || l) {
264+
movementSpeed = 1.0f;
265+
if (f && !l)
266+
movementDirection = 0.0;
267+
else if (!f && l)
268+
movementDirection = -M_PI_2;
269+
else if (f && l)
270+
movementDirection = -M_PI_4;
271+
else
272+
movementDirection = 0.0;
273+
} else {
274+
movementSpeed = 0.0;
275+
movementDirection = 0.0;
276+
}
277+
}
246278
}

‎src/client/inputhandler.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ class RandomInputHandler : public InputHandler
393393
virtual bool wasKeyPressed(GameKeyType k) { return false; }
394394
virtual bool wasKeyReleased(GameKeyType k) { return false; }
395395
virtual bool cancelPressed() { return false; }
396-
virtual float getMovementSpeed() {return 0.0f;}
397-
virtual float getMovementDirection() {return 0.0f;}
396+
virtual float getMovementSpeed() { return movementSpeed; }
397+
virtual float getMovementDirection() { return movementDirection; }
398398
virtual v2s32 getMousePos() { return mousepos; }
399399
virtual void setMousePos(s32 x, s32 y) { mousepos = v2s32(x, y); }
400400

@@ -408,4 +408,6 @@ class RandomInputHandler : public InputHandler
408408
KeyList keydown;
409409
v2s32 mousepos;
410410
v2s32 mousespeed;
411+
float movementSpeed;
412+
float movementDirection;
411413
};

0 commit comments

Comments
 (0)
Please sign in to comment.