Files
zombie-game/app/Game/Entities/Bullet.php
T
2026-09-07 20:37:40 +02:00

83 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Game\Entities;
use App\Game\EntityList;
use App\Game\GameObject;
use App\Game\Player;
use App\Game\Room;
use App\Game\Vector2;
final class Bullet extends GameObject
{
private EntityList $container;
private Room $room;
public float $size;
public int $damage;
/** @var array<float> */
public array $color;
public Player $owner;
public function __construct(
float $x,
float $y,
Vector2 $direction,
EntityList $container,
Room $room,
Player $owner,
float $speed = 800.0,
float $size = 8.0,
int $damage = 1,
) {
parent::__construct($x, $y);
$this->container = $container;
$this->room = $room;
$this->owner = $owner;
$this->velocity = $direction->mulS($speed);
$this->size = $size;
$this->damage = $damage;
$this->color = [1.0, 0.2, 0.2, 1.0];
$this->width = $size;
$this->height = $size;
}
public function kind(): string
{
return 'bullet';
}
public function update(float $dt): void
{
parent::update($dt);
if (
$this->pos->x < -$this->size
|| $this->pos->x > $this->room->width + $this->size
|| $this->pos->y < -$this->size
|| $this->pos->y > $this->room->height + $this->size
) {
$this->container->remove($this);
}
}
public function toSnapshot(): array
{
return [
'eid' => $this->eid,
'd' => $this->kind(),
'x' => round($this->pos->x, 1),
'y' => round($this->pos->y, 1),
'vx' => round($this->velocity->x, 1),
'vy' => round($this->velocity->y, 1),
's' => $this->size,
];
}
}