summaryrefslogtreecommitdiff
path: root/src/World/Boid.php
blob: 378751673e701dd89e25e66d24a27bfbbd36a5b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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');
    }
}