Advertisement
DArcher

RPG Map Class (Old)

Feb 5th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.93 KB | None | 0 0
  1. <?php
  2. class map{
  3.     private $sql;
  4.     private $uniqueId;
  5.     private $easyName;
  6.     private $cellData;
  7.     private $cellAttributes;
  8.  
  9.     public function saveMap(){
  10.         $check = "SELECT entry_id FROM maps WHERE map_id = {$this->uniqueId}";
  11.         $exe = $this->sql->query($check);
  12.         $save = array();
  13.         if($exe->num_rows > 1){
  14.             foreach($this->cellData AS $z => $cell){
  15.                 foreach ($cell AS $xy => $style) {
  16.                     $save[] = "UPDATE maps SET style = '{$style}' WHERE map_id = {$this->uniqueId} AND xy = '{$xy}';" . "\n";
  17.                 }
  18.             }
  19.             $save[] = "UPDATE map_names SET name = '{$this->easyName}' WHERE map_id = {$this->uniqueId}";
  20.  
  21.         }else{
  22.             foreach($this->cellData AS $z => $cell){
  23.                 foreach ($cell AS $xy => $style) {
  24.                     $save[] = "INSERT INTO maps (map_id, style, xy) VALUES ({$this->uniqueId},'{$style}','{$xy}');" . "\n";
  25.                 }
  26.             }
  27.  
  28.             $save[] = "INSERT INTO map_names (name, map_id) VALUES ('{$this->easyName}',{$this->uniqueId})";
  29.         }
  30.  
  31.         foreach ($this->cellAttributes as $cellid => $data) {
  32.             $check = "SELECT entry_id FROM map_cell_data WHERE cellxy = '{$cellid}' AND map_id = {$this->uniqueId}";
  33.             $exe = $this->sql->query($check);
  34.             if($exe->num_rows > 1){
  35.                 $save[] = "UPDATE map_cell_data SET data = '{$data}' WHERE map_id = {$this->uniqueId} AND cellxy = '{$cellid}';" . "\n";
  36.             }else{
  37.                 $save[] = "INSERT INTO map_cell_data (map_id, data, cellxy) VALUES ({$this->uniqueId},'{$data}','{$cellid}');" . "\n";
  38.             }
  39.         }
  40.  
  41.  
  42.         foreach($save as $z => $q){
  43.             $exe = $this->sql->query($q);
  44.             if($this->sql->error){
  45.                 echo $this->sql->error;
  46.                 echo $q;
  47.             }
  48.         }
  49.  
  50.  
  51.     }
  52.  
  53.     public function loadMap($p = false){
  54.         $getData = "SELECT maps.map_id AS mid, maps.style AS style, maps.xy AS xy, map_cell_data.cellxy AS cxy, map_cell_data.data AS data FROM maps LEFT JOIN map_cell_data ON map_cell_data.map_id = maps.map_id WHERE maps.map_id = {$this->uniqueId}";
  55.         $exe = $this->sql->query($getData);
  56.         if($this->sql->error){
  57.             echo $this->sql->error;
  58.         }
  59.         $cellData = array();
  60.         $cellAttr = array();
  61.         while($r = $exe->fetch_array()){
  62.             $cellData[$r['xy']] = $r['style'];
  63.             $cellAttr[$r['cxy']] = $r['data'];
  64.             if(!isset($mid)){
  65.                 $mid = $r['mid'];
  66.             }
  67.         }
  68.  
  69.         $cols = 25;
  70.         $rows = 20;
  71.  
  72.         $grid = "";
  73.  
  74.         if($p == true){
  75.             $grid .= '<div id="playArea">';
  76.             $grid .= '<div id="moving"><div id="toonSprite"></div></div>';
  77.         }
  78.  
  79.         $grid .= "<div id='gridWrap' data-mapid='{$mid}'>";
  80.  
  81.         for($r=0;$r<$rows;$r++){
  82.             $r2 = $r *32;
  83.             for($c=0;$c<$cols;$c++){
  84.                 $c2 = $c*32;
  85.                 $cellClass = "";
  86.                 $dataxyz = " data-xyz='0,0,0'";
  87.                 if(isset($cellAttr[$c2."|".$r2])){
  88.                     if($cellAttr[$c2."|".$r2] == "nomove"){
  89.                         $cellClass = " nomove";
  90.                     }elseif($cellAttr[$c2."|".$r2] != ""){
  91.                         $cellClass = " warpto";
  92.                         $dataxyz = " data-xyz='{$cellAttr[$c2."|".$r2]}'";
  93.                     }
  94.                 }
  95.                 $grid .= "<div id='{$c2}-{$r2}cell' class='cell{$cellClass}'{$dataxyz} data-x='{$c2}' data-y='{$r2}'><div style='background: " . $cellData[$c."|".$r."layer1"] . "' class='layer1' id='{$c}|{$r}layer1'></div><div style='background: " . $cellData[$c."|".$r."layer2"] . "' class='layer2' id='{$c}|{$r}layer2'></div><div style='background: " . $cellData[$c."|".$r."mask1"] . "' class='mask1' id='{$c}|{$r}mask1'></div><div data-x='{$c}' data-y='{$r}' style='background: " . $cellData[$c."|".$r."mask2"] . "' class='mask2' id='{$c}|{$r}mask2'></div></div>";
  96.             }
  97.             $grid .= "<div class='c'></div>";
  98.         }
  99.         $grid .= "</div>";
  100.  
  101.         if($p == true){
  102.             $grid .= '</div>';
  103.  
  104.         }
  105.         return $grid;
  106.     }
  107.  
  108.     public function preLoadMap(){
  109.         $getData = "SELECT maps.style AS style, maps.xy AS xy, map_cell_data.cellxy AS cxy, map_cell_data.data AS data FROM maps LEFT JOIN map_cell_data ON map_cell_data.map_id = maps.map_id WHERE maps.map_id = {$this->uniqueId}";
  110.         $exe = $this->sql->query($getData);
  111.         if($this->sql->error){
  112.             echo $this->sql->error;
  113.         }
  114.         $cellData = array();
  115.         $cellAttr = array();
  116.         while($r = $exe->fetch_array()){
  117.             $cellData[$r['xy']] = $r['style'];
  118.             $cellAttr[$r['cxy']] = $r['data'];
  119.         }
  120.  
  121.         $cols = 25;
  122.         $rows = 20;
  123.  
  124.         $grid = "<div id='preLoad{$this->uniqueId}'>";
  125.         for($r=0;$r<$rows;$r++){
  126.             $r2 = $r *32;
  127.             for($c=0;$c<$cols;$c++){
  128.                 $c2 = $c*32;
  129.                 $cellClass = "";
  130.                 $dataxyz = " data-xyz='0,0,0'";
  131.                 if(isset($cellAttr[$c2."|".$r2])){
  132.                     if($cellAttr[$c2."|".$r2] == "nomove"){
  133.                         $cellClass = " nomove";
  134.                     }elseif($cellAttr[$c2."|".$r2] != ""){
  135.                         $cellClass = " warpto";
  136.                         $dataxyz = " data-xyz='{$cellAttr[$c2."|".$r2]}'";
  137.                     }
  138.                 }
  139.                 $grid .= "<div id='{$c2}-{$r2}cell' class='cell{$cellClass}'{$dataxyz} data-x='{$c2}' data-y='{$r2}'><div style='background: " . $cellData[$c."|".$r."layer1"] . "' class='layer1' id='{$c}|{$r}layer1'></div><div style='background: " . $cellData[$c."|".$r."layer2"] . "' class='layer2' id='{$c}|{$r}layer2'></div><div style='background: " . $cellData[$c."|".$r."mask1"] . "' class='mask1' id='{$c}|{$r}mask1'></div><div data-x='{$c}' data-y='{$r}' style='background: " . $cellData[$c."|".$r."mask2"] . "' class='mask2' id='{$c}|{$r}mask2'></div></div>";
  140.             }
  141.             $grid .= "<div class='c'></div>";
  142.         }
  143.         $grid .= "</div>";
  144.  
  145.  
  146.         return $grid;
  147.     }
  148.  
  149.     function __construct($data){
  150.         $this->sql = new mysqli(sqlhost,sqluser,sqlpass,sqldb);
  151.         if(isset($data['uniqueid'])){
  152.             $this->uniqueId = $this->sql->escape_string($data['uniqueid']);
  153.         }
  154.         if(isset($data['easyName'])){
  155.             $this->easyName = $this->sql->escape_string($data['easyName']);
  156.             $this->easyName = str_replace(" ","_",$this->easyName);
  157.         }
  158.  
  159.         if(isset($data['layer1'])){
  160.             $this->cellData['layer1'] = $data['layer1'];
  161.             $this->cellData['layer2'] = $data['layer2'];
  162.             $this->cellData['mask1'] = $data['mask1'];
  163.             $this->cellData['mask2'] = $data['mask2'];
  164.             $this->cellAttributes = $data['cells'];
  165.         }
  166.  
  167.  
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement