game-village-politics-backend/app/Game/Map/MapBounds.php

40 lines
899 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Game\Map;
use App\Game\WorldConfig;
/**
* The map's coordinates: the world starts at 0|0 and runs from −size/2 to size/2 − 1 on
* both axes, so a 1000-field map spans −500 to 499.
*/
final class MapBounds
{
public function __construct(public readonly int $size) {}
public static function of(WorldConfig $config): self
{
return new self((int) $config->get('world.map.size', 1000));
}
public function min(): int
{
return -intdiv($this->size, 2);
}
public function max(): int
{
return $this->min() + $this->size - 1;
}
public function contains(int $x, int $y): bool
{
return $x >= $this->min() && $x <= $this->max() && $y >= $this->min() && $y <= $this->max();
}
public function clamp(int $value): int
{
return max($this->min(), min($this->max(), $value));
}
}