Skip to content

Instantly share code, notes, and snippets.

@1e4
Last active June 28, 2019 23:10
Show Gist options
  • Save 1e4/3c31843e2370417d1823f809576fafbb to your computer and use it in GitHub Desktop.
Save 1e4/3c31843e2370417d1823f809576fafbb to your computer and use it in GitHub Desktop.
Tiled map generator
<?php
class TSX {
/**
* Holds the map indexed by tile GID
*
* @var array
*/
private $map = [];
/**
* Holds the rendered map from \TiledTMX\JSONMapWriter
*
* @var
*/
private $render;
/**
* Contains information on the tiledimage
*
* @var
*/
private $tiledMapData;
/**
* Holds the main attributes from the map
*
* @var
*/
private $tiledMapAttributes;
/**
* Directory where maps and tilesets are
*
* @var string
*/
private $mapDirectory = 'maps';
public function __construct($json, $tiledMapData)
{
$this->render = $json;
$this->tiledMapData = $tiledMapData;
foreach($this->tiledMapData->attributes() as $key => $value)
{
$this->tiledMapAttributes[$key] = $value;
}
foreach($this->tiledMapData->image->attributes() as $key => $value)
{
$this->tiledMapAttributes['image'][$key] = $value;
}
$this->generateMap();
}
/**
* Generates the sprite sheet styles
*/
public function styles() {
echo <<<EOF
.map-container {
position: relative;
}
.map {
top: 0;
left: 0;
display: block;
width: 100%;
}
.map-layer-row {
width: 100%;
display: block;
height: 16px;
}
.map-tile {
background-image: url("{$this->mapDirectory}/{$this->tiledMapAttributes['image']['source']}");
background-repeat: no-repeat;
height: 16px;
width: 16px;
display: inline-block;
}
.map-tile:hover:not(.tile-collision) {
background-color: rgba(0,0,0,0.4);
}
.layer-absolute {
position: absolute;
top: 0;
left: 0;
}
.layer-collision .tile-collision {
z-index: 5;
cursor: not-allowed;
}
EOF;
foreach ($this->map as $index => $position)
{
echo ".map-tile-{$index} {";
echo "background-position: -{$position['offsetRight']}px -{$position['offsetTop']}px";
echo "}";
}
}
/**
* Generates the map offsets for each tile
*/
public function generateMap() {
$tileCount = $this->tiledMapAttributes['tilecount'];
$columns = $this->tiledMapAttributes['columns'];
$tileWidth = $this->tiledMapAttributes['tilewidth'];
$tileHeight = $this->tiledMapAttributes['tileheight'];
$rowSize = $tileCount / $columns; //26 rows
$i = 0;
for($r=0;$r<$rowSize;$r++)
{
$offsetTop = ($r * $tileWidth);
for($c=0;$c<$columns;$c++)
{
$i++;
$offsetRight = ($c * $tileHeight);
$this->map[$i] = [
'offsetRight' => $offsetRight,
'offsetTop' => $offsetTop
];
}
}
}
/**
* Renders the physical map
*/
public function renderMap() {
$layerIndex = 0;
echo "<div class=\"map-container\">";
foreach ($this->render->layer as $layer) {
if($layerIndex !== 0)
$absolute = 'layer-absolute';
else
$absolute = '';
if($layer->name)
$name = $layer->name;
else
$name = 'unnamed';
echo "<div class=\"layer layer-{$layerIndex} {$absolute} layer-{$name}\">";
for ($height = 0; $height < $this->render->height; $height++) {
echo "<div class='map-layer-row'>";
for ($length = 0; $length < $this->render->width; $length++) {
$coords = ($length + $height * 30);
if($name === 'collision' && $layer->data[$coords] !== 0)
$collision = 'tile-collision';
else
$collision = '';
echo "<div class='map-tile map-tile-{$layer->data[$coords]} {$collision}'>";
echo "</div>";
}
echo "</div>";
}
echo "</div>";
$layerIndex++;
}
echo "</div>";
}
public function setMapDirectory($dir)
{
$this->mapDirectory = $dir;
}
}
$map = (new \TiledTmx\Parser())->parseFile('./maps/beach_event.tmx');
$render = \TiledTmx\JsonMapWriter::render($map);
$render = json_decode($render);
$tileMapData = simplexml_load_file('./maps/beach.tsx');
$generatedMap = new TSX($render, $tileMapData);
?>
<style>
<?=$generatedMap->styles();?>
</style>
<?=$generatedMap->renderMap(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment