maps2 #2

Merged
lopar merged 23 commits from maps2 into master 2022-02-20 23:15:31 +00:00
2 changed files with 172 additions and 0 deletions
Showing only changes of commit b714e32dcc - Show all commits

100
src/Insallah/Map/Map.php Normal file
View File

@ -0,0 +1,100 @@
<?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;
}
}

View File

@ -0,0 +1,72 @@
<?php
# Date: 20.02.2022 (16:21)
namespace Insallah\Map;
class MapData
{
private array $map;
private int $startx;
private int $starty;
private const DEF_XY = [
'map' => ['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 '<table>';
while (($data = fgetcsv($file, null, ';')) !== false) {
echo '<tr>';
foreach ($data as $i) {
if (empty($i)) {
$i = 'end';
}
echo "<td><img src='../resources/img/$i.png' width='25px' height='25px' alt='map'></td>";
}
echo "</tr>\n";
}
echo '</table>';
fclose($file);
}
}