From 02455892dac1d52914e5702fd8e895d5cee1f083 Mon Sep 17 00:00:00 2001 From: FBeans Date: Sat, 21 May 2022 19:07:32 +0100 Subject: Add world, boid and all collision detection --- src/World/Boid.php | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/World/Boid.php (limited to 'src/World/Boid.php') 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 @@ +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'); + } +} -- cgit v1.2.3