PHP code example of pew-pew / map

1. Go to this page and download the library: Download pew-pew/map library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pew-pew / map example snippets


$size = new \PewPew\Map\Data\Size\IntSize(
    width: 100, // optional, default = 1
    height: 50, // optional, default = 1
);

$size = new \PewPew\Map\Data\Size\IntSize(100, 50);

echo $size->getArea(); // 5000

$size = new \PewPew\Map\Data\Size\IntSize(3, 3);

echo $size->getX(id: 1); // X = 1
echo $size->getY(id: 1); // Y = 0

echo $size->getPosition(id: 1); // object<Position> { x: 1, y: 0 }

$position = new \PewPew\Map\Data\Position\IntPosition(
    x: 1, // optional, default = 0
    y: 0, // optional, default = 0
);

$layer = new \PewPew\Map\Data\Layer(
    // optional, default = { width: 1, height: 1 }
    size: new \PewPew\Map\Data\Size\IntSize(
        width: 3,
        height: 3,
    ),
    // optional, default = { x: 0, y: 0 }
    position: new \PewPew\Map\Data\Position\IntPosition(
        x: 0, 
        y: 0,
    ),
);

$layer = new \PewPew\Map\Data\Layer\TilesLayer(
    tiles: [
        0, 2, 1,
        1, 2, 1,
        0, 0, 1,
    ],
    size: new \PewPew\Map\Data\Size\IntSize(3, 3),
);

$tileSet = new \PewPew\Map\Data\TileSet(
    // tional, default = 1
    tileIdStartsAt: 1,
    // optional, default = { width: 1, height: 1 }
    size: new \PewPew\Map\Data\Size\IntSize(
        width: 3,
        height: 3,
    ),
);

$set = new \PewPew\Map\Data\TileSet(
    pathname: ...,
    tileIdStartsAt: 1,
    size: new \PewPew\Map\Data\Size\IntSize(1, 1),
);

$set->containsId(tileId: 0); // bool(false)
$set->containsId(tileId: 1); // bool(true)
$set->containsId(tileId: 2); // bool(false)

$set = new \PewPew\Map\Data\TileSet( ... );

$set->getX(tileId: 1);          // X = 0
$set->getY(tileId: 1);          // Y = 0

$set->getPosition(tileId: 1);   // object<Position> { x: 0, y: 0 }

$previous = new \PewPew\Map\Data\TileSet(
    pathname: __DIR__ . '/tiles-1.png',
);

$new = $previous->withPathname(
    pathname: __DIR__ . '/tiles-2.png',
);

echo $previous->pathname; // string(".../tiles-1.png")
echo $new->pathname; // string(".../tiles-2.png")

use PewPew\Map\Data\Layer\TilesLayer;
use PewPew\Map\Data\Size\IntSize;
use PewPew\Map\Data\TileSet;
use PewPew\Map\Map;

$map = new Map(
    layers: [
        new TilesLayer(
            tiles: [
                0, 1, 2,
                0, 1, 1,
                1, 2, 1,
            ],
            size: new IntSize(3, 3),
        ),
    ],
    tileSets: [
        new TileSet(
            pathname: __DIR__ . '/tiles.png',
            size: new IntSize(2, 2),
        ),
    ],
    size: new IntSize(3, 3),
);

echo \json_encode($map);