From 14de64d80759402eb135998a405f6e29142b8b79 Mon Sep 17 00:00:00 2001 From: Ivor Barhansky Date: Sun, 20 Feb 2022 23:15:30 +0000 Subject: [PATCH] maps2 (#2) 2.0 Reviewed-on: https://src.lopar.space/Gamefunk/Maps/pulls/2 --- README.md | 2 +- map.php | 104 ---------------------------- mapimg/m-e.png | Bin 1512 -> 0 bytes mapimg/m-n.png | Bin 1454 -> 0 bytes mapimg/m-s.png | Bin 1532 -> 0 bytes mapimg/m-w.png | Bin 1512 -> 0 bytes public/index.php | 74 ++++++++++++++++++++ {mapimg => resources/img}/end.png | Bin {mapimg => resources/img}/g.png | Bin {mapimg => resources/img}/m.png | Bin {mapimg => resources/img}/o.png | Bin {mapimg => resources/img}/point.gif | Bin {mapimg => resources/img}/w.png | Bin {mapimg => resources/img}/x.png | Bin map.csv => resources/maps/map.csv | 0 map2.csv => resources/maps/map2.csv | 0 resources/views/map-controls.html | 43 ++++++++++++ showmap.php | 103 --------------------------- src/Insallah/Map/Map.php | 100 ++++++++++++++++++++++++++ src/Insallah/Map/MapData.php | 72 +++++++++++++++++++ src/Insallah/Player/Player.php | 45 ++++++++++++ 21 files changed, 335 insertions(+), 208 deletions(-) delete mode 100644 map.php delete mode 100644 mapimg/m-e.png delete mode 100644 mapimg/m-n.png delete mode 100644 mapimg/m-s.png delete mode 100644 mapimg/m-w.png create mode 100644 public/index.php rename {mapimg => resources/img}/end.png (100%) rename {mapimg => resources/img}/g.png (100%) rename {mapimg => resources/img}/m.png (100%) rename {mapimg => resources/img}/o.png (100%) rename {mapimg => resources/img}/point.gif (100%) rename {mapimg => resources/img}/w.png (100%) rename {mapimg => resources/img}/x.png (100%) rename map.csv => resources/maps/map.csv (100%) rename map2.csv => resources/maps/map2.csv (100%) create mode 100644 resources/views/map-controls.html delete mode 100644 showmap.php create mode 100644 src/Insallah/Map/Map.php create mode 100644 src/Insallah/Map/MapData.php create mode 100644 src/Insallah/Player/Player.php 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 28372c7a2a13ee10f7dcaa3eb2011b478943384b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1512 zcmeAS@N?(olHy`uVBq!ia0vp^QXtI13?%1G+4BcTv7|ftIx;YR|DNig)WpGT%PfAtr%uP&B4N6T+sVqF1YQw<5v^Bsd#P!;M1%Hx4$x_x%3d zXZQSSLJa&(jDyTGR~F{2F4OlmD&JafpXOp3W@#R7Z5eN86l7+b;?#9`ik7RcWwfnX zgq3xIeg39$`xIxB5KCQeLtQUJbw{nbE$vR3?#3bJo%<*0dKfnE?a5wSVijj^5o_DA zf8x?TD>9ZB)b422^)#&8+R?PXJ8@Ea^|~hOID5SyWAj)W(_oAG-JN|0r(49>TE^S1 z-m^AtN=m`zis&gx_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NAahCLG diff --git a/mapimg/m-n.png b/mapimg/m-n.png deleted file mode 100644 index 33fa9d4d80b33cc59637f3ec46478675b8b76886..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1454 zcmdr~`BPH|5Pl+#(Q3uwbQGPc9idZD&?F?}Fyw^1+`gB@gal1O9^obAg4~#dBOCz~ znp%oPP#lmJ5EO_Yc!Bk(b!x|IwOH-c)}xodX@6)t^wIto-I?8;o!R|%zi&1wlN7FA znYI!DfYl(M3t>5J(Q!+$*mL;&Usy^uvBWF@7`;JW*8DS8Z*cKcF4$h`^6D!PfMv8B zkS(B9Z$cnMZ}dAqN9X{M)Fl(id3kC&8!gNc5tt?(eLs^`RzOzKLg$(|7Cr$Y*I#U7 z7&(^VN}f%)3(h5}seHSb2zvbwhP3j+yTm@bfI7G^`MLA;V!b z&=urbKv_T$J{v7?iJSv|u0v2cT;(1LqTLQjP+pU00c{d_V^R4jm$E_2M)@=YlYww{ z8`#!CH#+RBnu^d%SWrae=<^cfWPwZCljtSyr70s?oP?CZQH)@bRYrch3HsBD2;5{{a9VdO} z!3tSOnaSG`y$~a*XrN2#AFV-~%Jm&4>i+!jOmoAr=+Mlm92sGkESK+<)J?ZWW)9^l zDCW46x{o0ZD=Yc~&LKbA=PWx^o^Rq-9SsK(5uQ`vNrZAF1VN>gqGOPhR3^%;7!TZ; z|61Ue;rEaZU+G?0Sa>+BslZ-@IX}B^5|cS&BDp zZC=L&whDh=6K~$WEQQI3@Efn*-^`39;kHENI^&hK%D>~d_p1RmzuP*vVysle;x9=~ z(f)GakMCB`ZEAdP{ainggv(G=A?IFn3k6ws-9bGBEJ^;T`K`Pj#{{|Y$G1zeEbquy zZmB*F33;zOr(RD@Y88m*biDur;+kKbcv5`xp?_?X_{b|?!o%sq;I`-9Uihi>j3C;% z@%*Ycp5*k5$5(V<7q!dztY_ORvl8iJq{b&{Pl*$M((71@>tgte*TUu2vv;W@o|j*w z+NB2S4;NCmVT^WOx99$9oc#Xzz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cHKDJ m--bbW!|C}h9~OOw0dNDhp=|Pb7s6}!u>l4xn%Wj7&HooirXxK7 diff --git a/mapimg/m-w.png b/mapimg/m-w.png deleted file mode 100644 index 508cbfc34e10feef6d441ea2e2278dceb57a7907..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1512 zcmd^->rayh6vkhaGDgH9(`C3UR%hmlv{DMa(7wHw_Jy{TQdZj1mR_N>PzvQ{xm%C| z14R%-E+Qg2MY)VIZu9h&WCf7$06XeeSLQN z007_%ahO84?%Zha&FwH4ggTWr&vIKw0)S0J0#Aqg@N8y`+gq?r42C|X9Sh=7bjBO}rKXn}=; zhv^13Muc-X>yRAup&Ql49&5OQf|r98&ZAw|&n0)K7yMF&gwTaU6{XH9Rb7(pQYleE zK?yN9IVmg_Uo`Bf8EwQTfCMFY;d+mvUYkCUE2~#ibd1b1`SY#?Nu^R$EDNsp?c zvSZZkDYl+NU?MFlOArVlQ34EA%fuy+n5j_l=`yqcOHfkeH5z%9hGXHCwN$V(xY<2L z$^s3jp=F)4^^XqTpPm9WjMS3!!mAYrqA_7{1iXZFaLC@-+l7-5V=E+ac3EtZ1TQ0D zW#I^B5MBz>li370880XEY{InDSx^pN({8l%=8{xYD1*<*?Pv zJGj{~-!lS{oZ$E>h4hFjM1%|EAgKnXw$-F-Nu`-0m?ns-W2JUyqax8%J0+FUE>_!|l5fZop^W9Yoox9!+{}^7P|If?4Z5eiIYl9*f5fd|VPY zz9iec{HOv%JND%2+|=iP>fR-6ee;}{H#zk&07C^zx;~B_`(h@@SmU}ly*NK|-|(#P zovSZt3*g~d7cw)n>=F=@G7?G{hWA%GBVnzzEqmM0?i&Tm%cBn6ok!xd9WxKM_iy(% zBNJTLepsExcohTcTIos8sm_uhmaY4`E~Z zE7~V^n0@68Z+3IsS+WN3h98kTx+3&{tA#%&SVDNXmzb}NuWs%-@xIyvUOa-TezwEL z3b~tnD_rsG(;3!P)U&sI+3JRO>fh$Z+vBprqlT5{eKwS%1*g4cT-$IGJNiU}@kI^e z0f^`ig&(?2rF)*XZ@fU-lG6r`I+@Mewwg|OZ1Udr(6{5_P-*oy0jG*r0;s_539-QJ z|DFFXc(FiD_+!pdrsdLKWZ|<2YT&Zb3rD+!_M}Bj{uKfIBj1iTgTiX N1|Sxnd5NA_`X9m<_}Kse 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