MovePlayer
Jump to navigation
Jump to search
virtual void MovePlayer()
Usage
A virtual function defined in the PlayerPawn class. Called from HandleMovement every tic to handle primary movement: forward, backward, sideways. Also handles camera bobbing.
The function reads movement input from the UserCmd struct stored in the PlayerPawn's player.cmd field. From there:
- GetFriction is called to obtain friction for the current floor. This obtains the
frictionandmovefactorvalues of the player's current Sector. - ApplyAirControl is called to apply air friction (if the player is in the air and not affected by the NOGRAVITY flag).
- TweakSpeeds is called to modify target movement speed based on whether the input is a run or a walk
- ForwardThrust is called to handle forward/backward movement (if there's forward/backward input)
- Thrust is called to handle sideways movement (if there's sideways input) — this is the Actor function in this sequence not specific to PlayerPawn
- Bob is called to bob the camera
Additionally, this function calls PlayRunning to play the movement animation on the PlayerPawn (by default it's the See state sequence).
ZScript defintion
| Note: The ZScript definition below is for reference and may be different in the current version of UZDoom. The most up-to-date version of this code can be found on UZDoom GitHub. |
virtual void MovePlayer ()
{
let player = self.player;
UserCmd cmd = player.cmd;
// [RH] 180-degree turn overrides all other yaws
if (player.turnticks)
{
player.turnticks--;
A_SetAngle(Angle + (180. / TURN180_TICKS), SPF_INTERPOLATE);
}
else
{
Angle += cmd.yaw * (360./65536.);
}
player.onground = (pos.z <= floorz) || bOnMobj || bMBFBouncer || (player.cheats & CF_NOCLIP2);
// killough 10/98:
//
// We must apply thrust to the player and bobbing separately, to avoid
// anomalies. The thrust applied to bobbing is always the same strength on
// ice, because the player still "works just as hard" to move, while the
// thrust applied to the movement varies with 'movefactor'.
if (cmd.forwardmove | cmd.sidemove)
{
double forwardmove, sidemove;
double bobfactor;
double friction, movefactor;
double fm, sm;
[friction, movefactor] = GetFriction();
bobfactor = friction < ORIG_FRICTION ? movefactor : ORIG_FRICTION_FACTOR;
if (!player.onground && !bNoGravity && !waterlevel)
{
// [RH] allow very limited movement if not on ground.
// [AA] but also allow authors to override it.
ApplyAirControl(movefactor, bobfactor);
}
fm = cmd.forwardmove;
sm = cmd.sidemove;
[fm, sm] = TweakSpeeds (fm, sm);
fm *= Speed / 256;
sm *= Speed / 256;
// When crouching, speed and bobbing have to be reduced
if (CanCrouch() && player.crouchfactor != 1)
{
fm *= player.crouchfactor;
sm *= player.crouchfactor;
bobfactor *= player.crouchfactor;
}
forwardmove = fm * movefactor * (35 / TICRATE);
sidemove = sm * movefactor * (35 / TICRATE);
if (forwardmove)
{
Bob(Angle, cmd.forwardmove * bobfactor / 256., true);
ForwardThrust(forwardmove, Angle);
}
if (sidemove)
{
let a = Angle - 90;
Bob(a, cmd.sidemove * bobfactor / 256., false);
Thrust(sidemove, a);
}
if (!(player.cheats & CF_PREDICTING) && (forwardmove != 0 || sidemove != 0))
{
PlayRunning ();
}
if (player.cheats & CF_REVERTPLEASE)
{
player.cheats &= ~CF_REVERTPLEASE;
player.camera = player.mo;
}
}
}