From b714e32dcc3e90bc6fa0076cc24799ed7ebbf6c8 Mon Sep 17 00:00:00 2001 From: Ivor Barhansky Date: Sun, 20 Feb 2022 23:11:42 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20'src?= =?UTF-8?q?/Insallah/Map'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ivor Barhansky --- src/Insallah/Map/Map.php | 100 +++++++++++++++++++++++++++++++++++ src/Insallah/Map/MapData.php | 72 +++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 src/Insallah/Map/Map.php create mode 100644 src/Insallah/Map/MapData.php diff --git a/src/Insallah/Map/Map.php b/src/Insallah/Map/Map.php new file mode 100644 index 0000000..12e1548 --- /dev/null +++ b/src/Insallah/Map/Map.php @@ -0,0 +1,100 @@ + ['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 = ''; + $str .= ''; + $str .= ""; + while ($y <= $y_last) { + $str .= ''; + $x = Player::getPos()[0] - $radius; + while ($x <= $x_last) { + $mapImage = $this->mapImage($x, $y); + if ($x === Player::getPos()[0] && $y === Player::getPos()[1]) { + $str .= "'; + $x++; + } + $str .= ''; + $y++; + } + $str .= '
Некий остров
$message
"; + $str .= 'player'; + } else { + $str .= ''; + $str .= "mapsqare"; + } + $str .= '
'; + return $str; + } +} \ No newline at end of file diff --git a/src/Insallah/Map/MapData.php b/src/Insallah/Map/MapData.php new file mode 100644 index 0000000..4b28fce --- /dev/null +++ b/src/Insallah/Map/MapData.php @@ -0,0 +1,72 @@ + ['x' => 5, 'y' => 5], + 'map2' => ['x' => 2, 'y' => 4], + ]; + private static string $filename; + + public function __construct(?string $mapName) + { + if (empty($mapName)) { + $mapName = 'map'; + } + self::$filename = dirname(getcwd()) . '/resources/maps/' . htmlspecialchars($mapName) . '.csv'; + $handle = fopen(self::$filename, 'r'); + while (($data = fgetcsv($handle, 1000, ';')) !== false) { + $this->map[] = $data; + } + fclose($handle); + $this->startx = self::DEF_XY[$mapName]['x']; + $this->starty = self::DEF_XY[$mapName]['y']; + } + + /** + * @return array + */ + public function getMap(): array + { + return $this->map; + } + + /** + * @return int + */ + public function getStartx(): int + { + return $this->startx; + } + + /** + * @return int + */ + public function getStarty(): int + { + return $this->starty; + } + + public static function drawFullMap() + { + $file = fopen(self::$filename, 'r'); + echo ''; + while (($data = fgetcsv($file, null, ';')) !== false) { + echo ''; + foreach ($data as $i) { + if (empty($i)) { + $i = 'end'; + } + echo ""; + } + echo "\n"; + } + echo '
map
'; + fclose($file); + } +} \ No newline at end of file