Sourcecode

Liveparticle/de/themarcraft/partikel/Partikel.php


Partikel.php

<?php

namespace de\themarcraft\partikel;

class Partikel
{
    private Types $type;
    private int $id;
    private String $color;
    private Position $position;

    /**
     * @param Types $type
     * @param Position $position
     * @param string $color
     */
    function __construct(int $id, Types $type, Position $position, string $color = ""){
        $this->type = $type;
        $this->color = $color;
        $this->position = $position;
    }

    /**
     * @return Types
     */
    public function getType() : Types
    {
        return $this->type;
    }

    /**
     * @return string
     */
    public function getColor() : String
    {
        return $this->color;
    }

    public function getNextElementInRadius(float $radius, PartikelManager $instance) : ?Partikel
    {
        $minDistance = INF;
        $nearestPartikel = null;

        foreach ($instance->getPartikel() as $partikel) {
            $distance = $this->calculateDistance($partikel);
            if ($distance < $minDistance && $distance <= $radius) {
                $minDistance = $distance;
                $nearestPartikel = $partikel;
            }
        }

        return $nearestPartikel;
    }

    public function getPosition(): Position
    {
        return $this->position;
    }

    private function calculateDistance(Partikel $otherPartikel) : float
    {
        $dx = $this->position->getX() - $otherPartikel->getPosition()->getX();
        $dy = $this->position->getY() - $otherPartikel->getPosition()->getY();
        return sqrt($dx * $dx + $dy * $dy);
    }
}