PartikelManager.php
<?php
namespace de\themarcraft\partikel;
class PartikelManager
{
private string $title;
private int $counter;
/**
* @var Partikel[]
*/
private array $partikel;
public function __construct(string $title)
{
$this->partikel = array();
$this->counter = $_COOKIE['counter'] ?? 0;
$this->loadParticles();
}
private function loadParticles(): void
{
for($i = 1; $i < $this->counter; $i++){
$this->partikel[$i] = new Rot($i, unserialize($_COOKIE['pos-'.$i]));
}
}
public function getTitle(): string
{
return $this->title;
}
public function addPartikel(string $type = 'Rot'): void
{
$pos = new Position(rand(0, 500), rand(0, 500));
$partikel = match ($type) {
'Rot' => new Rot($this->counter, $pos),
default => throw new InvalidArgumentException('Invalid particle type')
};
$this->partikel[] = $partikel;
$this->counter++;
$this->saveParticle($partikel);
}
private function saveParticle(Partikel $partikel): void
{
setcookie("pos-".$this->counter, serialize($partikel->getPosition()), time() + (86400 * 30));
setcookie("type-".$this->counter, serialize($partikel->getType()), time() + (86400 * 30));
setcookie("counter", $this->counter, time() + (86400 * 30));
}
public function removeParticle(int $index): void
{
unset($this->partikel[$index]);
// Update particle indices and save changes to persistent storage
// ...
}
public function getPartikel(): array
{
return $this->partikel;
}
}