PT
games/dmmotor / src / Dm / Play / Door.php · commit 9d248a4
<?php/* * dmmotor — a door: its ceiling goes up, waits, comes down (vldoor_t, T_VerticalDoor of p_doors.c). * Copyright (C) 1993-1996 by id Software, Inc.; PHP port for dmmotor. GPL v2 (see LICENSE). */namespace App\Dm\Play;use App\Dm\Sector;final class Door implements Thinker{    public const NORMAL = 0;    public const CLOSE30_THEN_OPEN = 1;    public const CLOSE = 2;    public const OPEN = 3;    public const RAISE_IN_5_MINS = 4;    public const BLAZE_RAISE = 5;    public const BLAZE_OPEN = 6;    public const BLAZE_CLOSE = 7;    public const VDOORSPEED = 2.0;    public const VDOORWAIT = 150;    public int $type = self::NORMAL;    public float $topHeight = 0;    public float $speed = self::VDOORSPEED;    /** 1 up, 0 waiting, -1 down, 2 initial wait */    public int $direction = 1;    public int $topWait = self::VDOORWAIT;    public int $topCountdown = 0;    public bool $removed = false;    public function __construct(public Sector $sector)    {        $sector->specialData = $this;    }    /** EV_DoDoor for one sector. */    public static function start(World $w, Sector $sec, int $type): self    {        $d = new self($sec);        $d->type = $type;        $lowest = World::lowestCeilingSurrounding($sec) - 4;        switch ($type) {            case self::BLAZE_CLOSE:                $d->topHeight = $lowest;                $d->direction = -1;                $d->speed = self::VDOORSPEED * 4;                $w->sectorSound($sec, 'bdcls');                break;            case self::CLOSE:                $d->topHeight = $lowest;                $d->direction = -1;                $w->sectorSound($sec, 'dorcls');                break;            case self::CLOSE30_THEN_OPEN:                $d->topHeight = $sec->ceilingHeight;                $d->direction = -1;                $w->sectorSound($sec, 'dorcls');                break;            case self::BLAZE_RAISE:            case self::BLAZE_OPEN:                $d->direction = 1;                $d->topHeight = $lowest;                $d->speed = self::VDOORSPEED * 4;                if ($d->topHeight != $sec->ceilingHeight) {                    $w->sectorSound($sec, 'bdopn');                }                break;            default:                $d->direction = 1;                $d->topHeight = $lowest;                if ($d->topHeight != $sec->ceilingHeight) {                    $w->sectorSound($sec, 'doropn');                }        }        return $d;    }    /** P_SpawnDoorCloseIn30 (sector special 10). */    public static function closeIn30(Sector $sec): self    {        $d = new self($sec);        $sec->special = 0;        $d->direction = 0;        $d->type = self::NORMAL;        $d->topCountdown = 30 * 35;        return $d;    }    /** P_SpawnDoorRaiseIn5Mins (sector special 14). */    public static function raiseIn5Mins(World $w, Sector $sec): self    {        $d = new self($sec);        $sec->special = 0;        $d->direction = 2;        $d->type = self::RAISE_IN_5_MINS;        $d->topHeight = World::lowestCeilingSurrounding($sec) - 4;        $d->topCountdown = 5 * 60 * 35;        return $d;    }    private function finish(): void    {        $this->sector->specialData = null;        $this->removed = true;    }    public function think(World $w): void    {        $sec = $this->sector;        switch ($this->direction) {            case 0:                if (--$this->topCountdown <= 0) {                    if ($this->type === self::BLAZE_RAISE) {                        $this->direction = -1;                        $w->sectorSound($sec, 'bdcls');                    } elseif ($this->type === self::NORMAL) {                        $this->direction = -1;                        $w->sectorSound($sec, 'dorcls');                    } elseif ($this->type === self::CLOSE30_THEN_OPEN) {                        $this->direction = 1;                        $w->sectorSound($sec, 'doropn');                    }                }                break;            case 2:                if (--$this->topCountdown <= 0 && $this->type === self::RAISE_IN_5_MINS) {                    $this->direction = 1;                    $this->type = self::NORMAL;                    $w->sectorSound($sec, 'doropn');                }                break;            case -1:                $res = $w->movePlane($sec, $this->speed, $sec->floorHeight, false, true, -1);                if ($res === World::MOVE_PASTDEST) {                    if ($this->type === self::BLAZE_RAISE || $this->type === self::BLAZE_CLOSE) {                        $this->finish();                        $w->sectorSound($sec, 'bdcls');                    } elseif ($this->type === self::NORMAL || $this->type === self::CLOSE) {                        $this->finish();                    } elseif ($this->type === self::CLOSE30_THEN_OPEN) {                        $this->direction = 0;                        $this->topCountdown = 35 * 30;                    }                } elseif ($res === World::MOVE_CRUSHED) {                    if ($this->type !== self::BLAZE_CLOSE && $this->type !== self::CLOSE) {                        $this->direction = 1;   // something is in the way: go back up                        $w->sectorSound($sec, 'doropn');                    }                }                break;            case 1:                $res = $w->movePlane($sec, $this->speed, $this->topHeight, false, true, 1);                if ($res === World::MOVE_PASTDEST) {                    if ($this->type === self::BLAZE_RAISE || $this->type === self::NORMAL) {                        $this->direction = 0;   // wait at the top                        $this->topCountdown = $this->topWait;                    } else {                        $this->finish();                    }                }                break;        }    }    public function isRemoved(): bool    {        return $this->removed;    }}