PHP Classes

File: source/Map/Actions.php

Recommend this page to a friend!
  Classes of Vitalij Mik   PHP Map Tiler   source/Map/Actions.php   Download  
File: source/Map/Actions.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: PHP Map Tiler
Display maps using tile images on Web pages
Author: By
Last change: Update of source/Map/Actions.php
Date: 1 year ago
Size: 1,402 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

function
getMapData($map,$possibleTiles){
    if(isset(
$_SESSION['mapData'])){
        return
$_SESSION['mapData'];
    }
   
$mapData = '';
   
mt_srand(1337);
    for(
$y = 0;$y < $map['height'];$y++){
        for(
$x = 0;$x<$map['width'];$x++){
           
$mapData .= mt_rand(0,count($possibleTiles)-1);
        }
    }
   
$_SESSION['mapData'] = $mapData;
    return
$mapData;
}
function
viewMap() {
   

   
$tile = [
       
'width'=>64,
       
'height'=>64
   
];

   
$viewPort = [
       
'width'=>17,
       
'height'=>9
   
];
   
$map = [
       
'width'=>500,
       
'height'=>500
   
];
   
$currentPosition = [
       
'y'=>0,
       
'x'=>0
   
];

   
$possibleTiles = [
       
'gras',
       
'water',
       
'dirt'
   
];
   
$mapData = getMapData($map,$possibleTiles);
   
$halfViewPortWidth = floor($viewPort['width'] / 2);
   
$halfViewPortHeight = floor($viewPort['height'] / 2);
   
$viewPort['left'] = $currentPosition['x'] - $halfViewPortWidth;
   
$viewPort['top'] = $currentPosition['y'] - $halfViewPortHeight;
   
$viewPort['right'] = $viewPort['left'] + $viewPort['width'];
   
$viewPort['bottom'] = $viewPort['top'] + $viewPort['height'];

   
$data = [
      
'viewPort'=>$viewPort,
      
'tile'=>$tile,
      
'possibleTiles'=>$possibleTiles,
      
'mapData'=>$mapData,
      
'map'=>$map
   
];
    return
render('map', $data);
}