57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Game;
|
|
|
|
abstract class GameObject
|
|
{
|
|
private static int $nextId = 0;
|
|
|
|
public readonly int $eid;
|
|
|
|
public Vector2 $pos;
|
|
|
|
public Vector2 $velocity;
|
|
|
|
public float $width;
|
|
|
|
public float $height;
|
|
|
|
public function __construct(float $x = 0.0, float $y = 0.0)
|
|
{
|
|
$this->eid = ++self::$nextId;
|
|
$this->pos = new Vector2($x, $y);
|
|
$this->velocity = Vector2::zero();
|
|
$this->width = 0.0;
|
|
$this->height = 0.0;
|
|
}
|
|
|
|
public function update(float $dt): void
|
|
{
|
|
$this->move($dt);
|
|
}
|
|
|
|
/** Integrate position from velocity; used directly by subclasses that override update(). */
|
|
public function move(float $dt): void
|
|
{
|
|
$this->pos = new Vector2(
|
|
$this->pos->x + $this->velocity->x * $dt,
|
|
$this->pos->y + $this->velocity->y * $dt,
|
|
);
|
|
}
|
|
|
|
public function intersects(GameObject $other): bool
|
|
{
|
|
return abs($this->pos->x - $other->pos->x) * 2.0 < $this->width + $other->width
|
|
&& abs($this->pos->y - $other->pos->y) * 2.0 < $this->height + $other->height;
|
|
}
|
|
|
|
public function removeFrom(EntityList $container): void
|
|
{
|
|
$container->remove($this);
|
|
}
|
|
|
|
abstract public function kind(): string;
|
|
}
|