summaryrefslogtreecommitdiff
path: root/src/World
diff options
context:
space:
mode:
Diffstat (limited to 'src/World')
-rw-r--r--src/World/Boid.php76
-rw-r--r--src/World/World.php57
2 files changed, 133 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');
+ }
+}
diff --git a/src/World/World.php b/src/World/World.php
new file mode 100644
index 0000000..06499e8
--- /dev/null
+++ b/src/World/World.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace App\World;
+
+class World
+{
+ protected $width;
+ protected $height;
+ protected $name;
+
+ public function __construct(int $width, int $height, ?string $name = null)
+ {
+ $this->width($width);
+ $this->height($height);
+
+ if (!$name) {
+ $name = $this->generateName();
+ }
+
+ $this->name($name);
+ }
+
+ public function width(int $width = null): int|World
+ {
+ if ($width) {
+ $this->width = $width;
+ return $this;
+ }
+
+ return $this->width;
+ }
+
+ public function height(int $height = null): int|World
+ {
+ if ($height) {
+ $this->height = $height;
+ return $this;
+ }
+
+ return $this->height;
+ }
+
+ public function name(string $name = null): string|World
+ {
+ if ($name) {
+ $this->name = $name;
+ return $this;
+ }
+
+ return $this->name;
+ }
+
+ protected function generateName()
+ {
+ return uniqid('world');
+ }
+}