Maps/src/Insallah/Map/Map.php
Ivor Barhansky b714e32dcc Загрузил(а) файлы в 'src/Insallah/Map'
Signed-off-by: Ivor Barhansky <lopar@noreply.lopar.us>
2022-02-20 23:11:42 +00:00

100 lines
3.2 KiB
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
# Date: 20.02.2022 (16:42)
namespace Insallah\Map;
use Insallah\Player\Player;
class Map
{
private array $map;
/** Это должно прилетать вместе с картой, но мне лень. */
private const ALLOWED_PATH_MARKS = [
'g' => ['status' => 1],
'm' => ['status' => 1, 'message' => 'Тут может быть ваша реклама!'],
];
public function __construct(MapData $mapData, $force = null)
{
$this->map = $mapData->getMap();
Player::start([$mapData->getStartx(), $mapData->getStarty()], $force);
}
private function canMoveTo($x, $y): bool
{
return
$x >= 0 && $y >= 0 &&
!empty($this->map[$y][$x]) &&
!empty(self::ALLOWED_PATH_MARKS[$this->map[$y][$x]]) &&
self::ALLOWED_PATH_MARKS[$this->map[$y][$x]]['status'] === 1;
}
private function mapImage(int $x, int $y): string
{
if ($x < 0 || $y < 0 || empty($this->map[$y][$x])) {
return '../resources/img/end.png';
}
return '../resources/img/' . $this->map[$y][$x] . '.png';
}
public function goUp()
{
if ($this->canMoveTo(Player::getPos()[0], Player::getPos()[1] - 1)) {
Player::modYPos(-1);
}
}
public function goDown()
{
if ($this->canMoveTo(Player::getPos()[0], Player::getPos()[1] + 1)) {
Player::modYPos(+1);
}
}
public function goLeft()
{
if ($this->canMoveTo(Player::getPos()[0] - 1, Player::getPos()[1])) {
Player::modXPos(-1);
}
}
public function goRight()
{
if ($this->canMoveTo(Player::getPos()[0] + 1, Player::getPos()[1])) {
Player::modXPos(+1);
}
}
public function drawVisible(int $radius = 2): string
{
$x_last = Player::getPos()[0] + $radius;
$y = Player::getPos()[1] - $radius;
$y_last = Player::getPos()[1] + $radius;
$message = !empty(self::ALLOWED_PATH_MARKS[$this->map[Player::getPos()[1]][Player::getPos()[0]]]['message']) ?
self::ALLOWED_PATH_MARKS[$this->map[Player::getPos()[1]][Player::getPos()[0]]]['message'] : '';
$str = '<table>';
$str .= '<caption>Некий остров</caption>';
$str .= "<tr><th colspan='" . ($radius * 2 + 1) . "'>$message</th></tr>";
while ($y <= $y_last) {
$str .= '<tr>';
$x = Player::getPos()[0] - $radius;
while ($x <= $x_last) {
$mapImage = $this->mapImage($x, $y);
if ($x === Player::getPos()[0] && $y === Player::getPos()[1]) {
$str .= "<td style='background-image: url($mapImage)'>";
$str .= '<img src="../resources/img/point.gif" alt="player">';
} else {
$str .= '<td>';
$str .= "<img src='$mapImage' alt='mapsqare' title='$mapImage'>";
}
$str .= '</td>';
$x++;
}
$str .= '</td>';
$y++;
}
$str .= '</table>';
return $str;
}
}