Skip to content

Instantly share code, notes, and snippets.

@smwhr
Created November 26, 2020 22:39
Show Gist options
  • Save smwhr/545b7ae26f6faf5d2211f97ca5f4edca to your computer and use it in GitHub Desktop.
Save smwhr/545b7ae26f6faf5d2211f97ca5f4edca to your computer and use it in GitHub Desktop.
Pirate map
<?php
const TY_PATH = "https://troisyaourts.com/~smwhr/supinternet/tileset";
class Tile{
protected $char;
protected $img_url;
protected $css_class="";
public function __construct($char){
$this->char = $char;
$this->img_url = TY_PATH."/tiles/tile_".[23,24,39,40][rand(0,3)].".png";
}
public function __toString(){
$water_url = TY_PATH."/tiles/tile_73.png";
return <<<EOD
<img src="{$water_url}" />
<img class="{$this->css_class}" src="{$this->img_url}" />
EOD;
}
}
class Boat extends Tile{
public function __construct($char){
$this->char = $char;
switch($char){
case 'X':
$this->img_url = TY_PATH."/ships/ship_1.png";
$this->css_class = "boat ".["n", "s", "e", "w", "sw", "se", "ne", "nw"][rand(0, 7)];
break;
case 'E':
$this->img_url = TY_PATH."/ships/ship_2.png";
$this->css_class = "boat s";
break;
case 'T':
$this->img_url = TY_PATH."/ships/ship_3.png";
$this->css_class = "boat n";
break;
}
}
}
class Sea extends Tile{
public function __construct($char, $n = "C"){
$this->char = $char;
if($n == "C"){
$n = 73;
}
$this->img_url = TY_PATH."/tiles/tile_{$n}.png";
}
}
class Land extends Tile{
public function __construct($char, $n = "C"){
if($n == "C"){
$n = [23,24,39,40][rand(0,3)];
}
$this->img_url = TY_PATH."/tiles/tile_{$n}.png";
}
}
class TileFactory{
public static function decide($map, $x, $y){
$char = $map->charAt($x, $y);
$neighs = array_map(function($c){
return $c == "#" ? "#" : "~";
},[
$map->charAt($x - 1, $y - 1),
$map->charAt($x , $y - 1),
$map->charAt($x + 1, $y - 1),
$map->charAt($x - 1, $y ),
$map->charAt($x , $y ),
$map->charAt($x + 1, $y ),
$map->charAt($x - 1, $y + 1),
$map->charAt($x , $y + 1),
$map->charAt($x + 1, $y + 1),
]);
if( in_array($char, ['X', 'E', 'T']) ){
return new Boat($char);
}else{
switch($neighs){
// Land tiles
case ["#","#","#","#","#","#","#","#","#"] :
return new Land($char, "C");
case ["#","#","~","#","#","~","~","~","~"] :
case ["#","#","#","#","#","~","~","~","~"] :
case ["#","#","~","#","#","~","#","~","~"] :
case ["#","#","#","#","#","~","#","~","~"] :
return new Land($char, "57");
case ["#","#","#","#","#","#","~","~","~"] :
case ["#","#","#","#","#","#","~","~","#"] :
case ["#","#","#","#","#","#","#","~","~"] :
return new Land($char, ["55", "56"][rand(0,1)]);
case ["~","#","#","~","#","#","~","~","~"] :
case ["#","#","#","~","#","#","~","~","~"] :
case ["#","#","#","~","#","#","~","~","#"] :
case ["~","#","#","~","#","#","~","~","#"] :
return new Land($char, "54");
case ["~","#","#","#","#","#","#","#","#"] :
return new Land($char, "53");
case ["#","#","~","#","#","#","#","#","#"] :
return new Land($char, "52");
case ["#","#","~","#","#","~","#","#","~"] :
case ["#","#","#","#","#","~","#","#","~"] :
case ["#","#","~","#","#","~","#","#","#"] :
return new Land($char, ["25", "41"][rand(0,1)]);
case ["~","#","#","~","#","#","~","#","#"] :
case ["#","#","#","~","#","#","~","#","#"] :
case ["~","#","#","~","#","#","#","#","#"] :
return new Land($char, ["22", "38"][rand(0,1)]);
case ["#","#","#","#","#","#","~","#","#"] :
return new Land($char, "37");
case ["#","#","#","#","#","#","#","#","~"] :
return new Land($char, "36");
case ["~","~","~","#","#","~","#","#","~"] :
case ["#","~","~","#","#","~","#","#","~"] :
case ["~","~","~","#","#","~","#","#","#"] :
case ["#","~","~","#","#","~","#","#","#"] :
return new Land($char, "09");
case ["~","~","~","#","#","#","#","#","#"] :
case ["~","~","#","#","#","#","#","#","#"] :
case ["#","~","~","#","#","#","#","#","#"] :
return new Land($char, ["07", "08"][rand(0,1)]);
case ["~","~","~","~","#","#","~","#","#"] :
case ["~","~","#","~","#","#","~","#","#"] :
case ["~","~","~","~","#","#","#","#","#"] :
case ["~","~","#","~","#","#","#","#","#"] :
return new Land($char, "06");
case ["~","~","~",
"~","#","~",
"~","~","~"] :
return new Land($char, ["49", "50", "51"][rand(0,2)]);
// // Seashore tiles
// case ["~","~","~","~","~","~","#","#","#"] :
// return new Sea($char, "11");
case ["~","~","~","~","~","~","~","~","~"] :
return new Sea($char, "C");
}
if($char == "#"){
return new Land($char);
}else{
return new Sea($char);
}
}
return new Tile($char);
}
}
class GridMap{
private $charmap;
private $width;
private $height;
public static function fromFile($rawfilename){
$lines = file_get_contents($rawfilename);
$charmap =
array_filter(
array_map(function($l){
return array_filter(
array_map(function($char){return $char;}, str_split($l)),
fn($char) => !empty($char)
);
},
explode("\n", $lines)),
fn($char) => !empty($char)
);
return new static($charmap);
}
public function __construct($charmap){
$this->charmap = $charmap;
$this->height = count($this->charmap);
$this->width = count($this->charmap[0]);
}
public function yIterator(){
return range(0, $this->height - 1);
}
public function xIterator(){
return range(0, $this->width - 1);
}
public function charAt($x, $y){
return $this->charmap[$y][$x] ?? "~";
}
public function at($x, $y): Tile{
return TileFactory::decide($this, $x, $y);
}
public function focusOn($x, $y, $range){
//bug on border tiles
$focusLines = array_slice($this->charmap, $y - $range - 1, 2 * $range + 1);
$focusCharmap =
array_map(function($line) use ($x, $range){
return array_slice($line, $x - $range - 1, 2 * $range + 1);
}, $focusLines);
return new static($focusCharmap);
}
}
// $map = new GridMap(TY_PATH."/map.txt");
$map = GridMap::fromFile(TY_PATH."/map.txt");
// $map = GridMap::fromFile("map.txt");
// $map = $globalMap->focusOn(10, 10, 5);
// include("template.html.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Map</title>
<style>
.map{display: grid;grid-auto-flow: row; width: 300px;gap: 0;}
.line{display: grid; grid-auto-flow: column;gap: 0;}
.cell{display: grid;gap: 0; position: relative; height: 28px; width: 28px;}
.cell img{height: 28px; position: absolute;}
.boat{z-index: 20;}
.boat.n {left: 50%; transform: translate(-50%, 0) rotateZ(180deg);}
.boat.s {left: 50%; transform: translate(-50%, 0) rotateZ(0deg);}
.boat.e {left: 50%; transform: translate(-50%, 0) rotateZ(-90deg);}
.boat.w {left: 50%; transform: translate(-50%, 0) rotateZ(90deg);}
.boat.ne{left: 50%; transform: translate(-50%, 0) rotateZ(-135deg);}
.boat.nw{left: 50%; transform: translate(-50%, 0) rotateZ(135deg);}
.boat.se{left: 50%; transform: translate(-50%, 0) rotateZ(45deg);}
.boat.sw{left: 50%; transform: translate(-50%, 0) rotateZ(-45deg);}
</style>
</head>
<body>
<div class="map">
<?php foreach($map->yIterator() as $y):?>
<div class="line">
<?php foreach($map->xIterator() as $x):?>
<div class="cell"><?php echo $map->at($x, $y); ?></div>
<?php endforeach;?>
</div>
<?php endforeach;?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment