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