diff options
author | FBeans <phil@pgburton.com> | 2022-05-21 19:07:32 +0100 |
---|---|---|
committer | FBeans <phil@pgburton.com> | 2022-05-21 19:07:32 +0100 |
commit | 02455892dac1d52914e5702fd8e895d5cee1f083 (patch) | |
tree | 5cbafc717c099ee42c32ee21270b569a55089261 /src/World/Boid.php | |
parent | 5832634b35b475f607f4c695ccad310ee2467e57 (diff) |
Diffstat (limited to 'src/World/Boid.php')
-rw-r--r-- | src/World/Boid.php | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/World/Boid.php b/src/World/Boid.php new file mode 100644 index 0000000..3787516 --- /dev/null +++ b/src/World/Boid.php @@ -0,0 +1,76 @@ +<?php + +namespace App\World; + +use App\Collision\Collision; +use Exception; + +class Boid +{ + protected $radius; + private $initial_x; + private $initial_y; + + protected $x; + protected $y; + + public function __construct(int $radius, int $initial_x, int $initial_y, ?string $name = null) + { + $this->radius($radius); + $this->initial_x = $initial_x; + $this->initial_y = $initial_y; + + $this->position($initial_x, $initial_y); + + if (!$name) { + $name = $this->generateName(); + } + + $this->name($name); + } + + public function radius(int $radius = null): int|Boid + { + if ($radius) { + $this->radius = $radius; + return $this; + } + + return $this->radius; + } + + public function position(int $x = null, int $y = null): array|Boid + { + if ($x !== null xor $y !== null) { + throw new Exception('Either 0 or 2 arguments are requried for Boid::position()'); + } + + if ($x !== null) { + $this->x = $x; + $this->y = $y; + return $this; + } + + return [$this->x, $this->y]; + } + + public function name(string $name = null): string|Boid + { + if ($name) { + $this->name = $name; + return $this; + } + + return $this->name; + } + + public function isCollisionWithWorld(int $w_w, int $w_h, int $w_x, int $w_y): bool + { + return Collision::circleWorld($this->radius, $this->x, $this->y, $w_w, $w_h, $w_x, $w_y); + } + + protected function generateName() + { + return uniqid('boid'); + } +} |