diff --git a/README.md b/README.md index 147b001..fd043ad 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # 2D Map -Простейший пример генерации карты для игр. \ No newline at end of file +Простейший пример генерации карты для игр. Добавил пародию на ООП и пофиксил большинство ошибок. \ No newline at end of file diff --git a/map.php b/map.php deleted file mode 100644 index 1cd3872..0000000 --- a/map.php +++ /dev/null @@ -1,104 +0,0 @@ -"; -while ($y <= $y_end){ - print ""; - $x = $q; - while ($x <= $x_end){ - if (@$map[$y][$x] == ""){ // ползут ошибки! - $map[$y][$x] = "end"; - } - if ($y == $_SESSION["y"] AND $x == $_SESSION["x"]){ - print ""; - print ""; - print ""; - } - else{ - print ""; - print ""; - print ""; - } - $x++; - } - print ""; - $y++; -} -print ""; -//квадрат построен -?> -
-        
- -
-         -
-    
- -
-    

- -В этой клетке c координатами 2,2 находится магазин"; -} -else{ - print "В этой клетке нет активных элементов"; -} -*/ - -//Описываем свойство целого класса клеток -if ($map[$_SESSION["y"]][$_SESSION["x"]] == "m"){ print "В этой клетке класса m находится магазин"; } - -//Бежим на вторую карту с первой. -if ($_SESSION["y"] == 2 AND $_SESSION["x"] == 0){ - print "Сменить локацию."; -} - -?> \ No newline at end of file diff --git a/mapimg/m-e.png b/mapimg/m-e.png deleted file mode 100644 index 28372c7..0000000 Binary files a/mapimg/m-e.png and /dev/null differ diff --git a/mapimg/m-n.png b/mapimg/m-n.png deleted file mode 100644 index 33fa9d4..0000000 Binary files a/mapimg/m-n.png and /dev/null differ diff --git a/mapimg/m-s.png b/mapimg/m-s.png deleted file mode 100644 index 2bdb44e..0000000 Binary files a/mapimg/m-s.png and /dev/null differ diff --git a/mapimg/m-w.png b/mapimg/m-w.png deleted file mode 100644 index 508cbfc..0000000 Binary files a/mapimg/m-w.png and /dev/null differ diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..3acabb5 --- /dev/null +++ b/public/index.php @@ -0,0 +1,74 @@ +goUp(); +} +if ($_POST['action'] === 'down') { + $map->goDown(); +} +if ($_POST['action'] === 'left') { + $map->goLeft(); +} +if ($_POST['action'] === 'right') { + $map->goRight(); +} +if ($_POST['action'] === 'clear') { + Player::clear(); +} + +/** Для отладки. */ +?> +

+ P:[,] MAP:[]
+ Смена локаций на MAP:[0,2] и MAP2:[4,2]. Да, там надо повторно клацнуть чтобы всё сменилось, но опять же, это + принцип на коленке, а не готовое решение. Если хранить всё хоть в какой-то базе, а не в сессиях от половины этих + костылей можно отказаться. +
+drawVisible(); +require_once '../resources/views/map-controls.html'; + +echo 'Карта целиком:

'; +MapData::drawFullMap(); diff --git a/mapimg/end.png b/resources/img/end.png similarity index 100% rename from mapimg/end.png rename to resources/img/end.png diff --git a/mapimg/g.png b/resources/img/g.png similarity index 100% rename from mapimg/g.png rename to resources/img/g.png diff --git a/mapimg/m.png b/resources/img/m.png similarity index 100% rename from mapimg/m.png rename to resources/img/m.png diff --git a/mapimg/o.png b/resources/img/o.png similarity index 100% rename from mapimg/o.png rename to resources/img/o.png diff --git a/mapimg/point.gif b/resources/img/point.gif similarity index 100% rename from mapimg/point.gif rename to resources/img/point.gif diff --git a/mapimg/w.png b/resources/img/w.png similarity index 100% rename from mapimg/w.png rename to resources/img/w.png diff --git a/mapimg/x.png b/resources/img/x.png similarity index 100% rename from mapimg/x.png rename to resources/img/x.png diff --git a/map.csv b/resources/maps/map.csv similarity index 100% rename from map.csv rename to resources/maps/map.csv diff --git a/map2.csv b/resources/maps/map2.csv similarity index 100% rename from map2.csv rename to resources/maps/map2.csv diff --git a/resources/views/map-controls.html b/resources/views/map-controls.html new file mode 100644 index 0000000..b779688 --- /dev/null +++ b/resources/views/map-controls.html @@ -0,0 +1,43 @@ + +
+
+
+
+ +
+
+
+ +
+
X
+
+ +
+
+
+ +
+
+
+ + diff --git a/showmap.php b/showmap.php deleted file mode 100644 index 879d51b..0000000 --- a/showmap.php +++ /dev/null @@ -1,103 +0,0 @@ -"; -while ($y <= $y_end){ - print ""; - $x = $q; - while ($x <= $x_end){ - if (@$map[$y][$x] == ""){ // ползут ошибки! - $map[$y][$x] = "end"; - } - if ($y == $_SESSION["y"] AND $x == $_SESSION["x"]){ - print ""; - print ""; - print ""; - } - else{ - print ""; - print ""; - print ""; - } - $x++; - } - print ""; - $y++; -} -print ""; -//квадрат построен -/* -?> - -    
- -
-    

- -В этой клетке c координатами 2,2 находится магазин"; -} -else{ - print "В этой клетке нет активных элементов"; -} -*/ - -//Описываем свойство целого класса клеток -/* -if ($map[$_SESSION["y"]][$_SESSION["x"]] == "m"){ - print "В этой клетке класса m находится магазин"; -} -*/ -?> \ No newline at end of file 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 diff --git a/src/Insallah/Player/Player.php b/src/Insallah/Player/Player.php new file mode 100644 index 0000000..1687f9d --- /dev/null +++ b/src/Insallah/Player/Player.php @@ -0,0 +1,45 @@ += 0) { + $_SESSION['x'] += $x_pos; + } + } + + /** + * @param int $y_pos + */ + public static function modYPos(int $y_pos): void + { + if ($_SESSION['y'] + $y_pos >= 0) { + $_SESSION['y'] += $y_pos; + } + } + + public static function clear() + { + session_destroy(); + header('Location: ' . $_SERVER['REQUEST_URI']); + } +} \ No newline at end of file