TR
games/dmmotor / src / Dm / Play / PlayerMove.php · commit 9d248a4
<?php/* * dmmotor — the player's moves, view height, death, weapon change and powers (p_user.c of linuxdoom-1.10). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Dm\Play;use App\Dm\Info;trait PlayerMove{    private const MAXBOB = 16.0;    private const INVERSECOLORMAP = 32;    private bool $onGround = false;    private function thrust(Player $player, float $angle, float $move): void    {        $player->mo->momX += $move * cos($angle);        $player->mo->momY += $move * sin($angle);    }    /** P_CalcHeight: the eye height, with the bobbing of the walk. */    private function calcHeight(Player $player): void    {        $mo = $player->mo;        $player->bob = ($mo->momX * $mo->momX + $mo->momY * $mo->momY) / 4;        if ($player->bob > self::MAXBOB) {            $player->bob = self::MAXBOB;        }        if (!$this->onGround) {            $player->viewZ = min($mo->z + self::VIEWHEIGHT, $mo->ceilingZ - 4);            return;        }        $angle = (intdiv(8192, 20) * $this->levelTime) % 8192 * (2 * M_PI / 8192);        $bob = $player->bob / 2 * sin($angle);        if ($player->playerState === Player::LIVE) {            $player->viewHeight += $player->deltaViewHeight;            if ($player->viewHeight > self::VIEWHEIGHT) {                $player->viewHeight = self::VIEWHEIGHT;                $player->deltaViewHeight = 0;            }            if ($player->viewHeight < self::VIEWHEIGHT / 2) {                $player->viewHeight = self::VIEWHEIGHT / 2;                if ($player->deltaViewHeight <= 0) {                    $player->deltaViewHeight = 1 / 65536;                }            }            if ($player->deltaViewHeight != 0) {                $player->deltaViewHeight += 0.25;                if ($player->deltaViewHeight == 0) {                    $player->deltaViewHeight = 1 / 65536;                }            }        }        $player->viewZ = $mo->z + $player->viewHeight + $bob;        if ($player->viewZ > $mo->ceilingZ - 4) {            $player->viewZ = $mo->ceilingZ - 4;        }    }    private function movePlayer(Player $player): void    {        $cmd = $player->cmd;        $mo = $player->mo;        $mo->angle = self::normAngle($mo->angle + $cmd->angleTurn);        $this->onGround = $mo->z <= $mo->floorZ;        if ($cmd->forwardMove && $this->onGround) {            $this->thrust($player, $mo->angle, $cmd->forwardMove / 32);        }        if ($cmd->sideMove && $this->onGround) {            $this->thrust($player, $mo->angle - M_PI / 2, $cmd->sideMove / 32);        }        if (($cmd->forwardMove || $cmd->sideMove) && $mo->state === Info::S_PLAY) {            $this->setMobjState($mo, Info::S_PLAY_RUN1);        }    }    /** P_DeathThink: the view falls, turns to the killer; "use" restarts. */    private function deathThink(Player $player): void    {        $this->movePsprites($player);        $mo = $player->mo;        if ($player->viewHeight > 6) {            $player->viewHeight -= 1;        }        if ($player->viewHeight < 6) {            $player->viewHeight = 6;        }        $player->deltaViewHeight = 0;        $this->onGround = $mo->z <= $mo->floorZ;        $this->calcHeight($player);        $a = $player->attacker;        if ($a !== null && $a !== $mo) {            $angle = self::pointToAngle($mo->x, $mo->y, $a->x, $a->y);            $delta = self::normAngle($angle - $mo->angle);            $ang5 = M_PI / 36;            if ($delta < $ang5 || $delta > 2 * M_PI - $ang5) {                // looking at the killer: fade the damage                $mo->angle = $angle;                if ($player->damageCount) {                    $player->damageCount--;                }            } elseif ($delta < M_PI) {                $mo->angle += $ang5;            } else {                $mo->angle -= $ang5;            }        } elseif ($player->damageCount) {            $player->damageCount--;        }        if ($player->cmd->use) {            $player->playerState = Player::REBORN;        }    }    /** P_PlayerThink: one tic of the player. */    public function playerThink(Player $player): void    {        $mo = $player->mo;        if ($mo === null) {            return;        }        $cmd = $player->cmd;        if ($mo->flags & Info::MF_JUSTATTACKED) {            // the chainsaw lunges forward            $cmd->angleTurn = 0;            $cmd->forwardMove = 100;            $cmd->sideMove = 0;            $mo->flags &= ~Info::MF_JUSTATTACKED;        }        if ($player->playerState === Player::DEAD) {            $this->deathThink($player);            return;        }        if ($mo->reactionTime) {            $mo->reactionTime--;   // frozen after a teleport        } else {            $this->movePlayer($player);        }        $this->calcHeight($player);        if ($mo->subsector->sector->special) {            $this->playerInSpecialSector($player);        }        // weapon change        if ($cmd->changeWeapon >= 0) {            $new = $cmd->changeWeapon;            if ($new === Player::WP_FIST && $player->weaponOwned[Player::WP_CHAINSAW]                && !($player->readyWeapon === Player::WP_CHAINSAW && $player->powers[Player::PW_STRENGTH])) {                $new = Player::WP_CHAINSAW;            }            if ($this->commercial && $new === Player::WP_SHOTGUN && $player->weaponOwned[Player::WP_SUPERSHOTGUN]                && $player->readyWeapon !== Player::WP_SUPERSHOTGUN) {                $new = Player::WP_SUPERSHOTGUN;            }            if (($player->weaponOwned[$new] ?? false) && $new !== $player->readyWeapon) {                $player->pendingWeapon = $new;            }        }        // use        if ($cmd->use) {            if (!$player->useDown) {                $this->useLines($player);                $player->useDown = true;            }        } else {            $player->useDown = false;        }        $this->movePsprites($player);        // powers        if ($player->powers[Player::PW_STRENGTH]) {            $player->powers[Player::PW_STRENGTH]++;   // counts up: the red tint fades        }        if ($player->powers[Player::PW_INVULNERABILITY]) {            $player->powers[Player::PW_INVULNERABILITY]--;        }        if ($player->powers[Player::PW_INVISIBILITY]) {            if (!--$player->powers[Player::PW_INVISIBILITY]) {                $mo->flags &= ~Info::MF_SHADOW;            }        }        if ($player->powers[Player::PW_INFRARED]) {            $player->powers[Player::PW_INFRARED]--;        }        if ($player->powers[Player::PW_IRONFEET]) {            $player->powers[Player::PW_IRONFEET]--;        }        if ($player->damageCount) {            $player->damageCount--;        }        if ($player->bonusCount) {            $player->bonusCount--;        }        // the light maps of the powers (blinking before they end)        $inv = $player->powers[Player::PW_INVULNERABILITY];        $ir = $player->powers[Player::PW_INFRARED];        if ($inv) {            $player->fixedColormap = ($inv > 4 * 32 || ($inv & 8)) ? self::INVERSECOLORMAP : 0;        } elseif ($ir) {            $player->fixedColormap = ($ir > 4 * 32 || ($ir & 8)) ? 1 : 0;        } else {            $player->fixedColormap = 0;        }    }}