Last active
April 11, 2016 09:17
-
-
Save garveen/6f9c3dbaba6f7e3ff52e0c300880e254 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Map | |
{ | |
const TREE = 0x1; | |
const GRESS = 0x2; | |
const BRIDGE = 0x4; | |
const BLOCKED = 0x8; | |
protected $size_x; | |
protected $size_y; | |
protected $char_length; | |
public function __construct($size_x = 2048, $size_y = 2048, $bit_length = 16) | |
{ | |
$this->size_x = $size_x; | |
$this->size_y = $size_y; | |
$this->char_length = (int) ($bit_length / 8); | |
$this->map = str_repeat("\0", $size_x * $size_y * $this->char_length); | |
} | |
public function getOffset($x, $y) | |
{ | |
return $x * $this->size_y + $y * $this->char_length; | |
} | |
public function getCell($x, $y) | |
{ | |
$str = substr($this->map, $this->getOffset($x, $y), $this->char_length); | |
$cell = 0; | |
for ($i = 0; $i < $this->char_length; $i++) { | |
$cell |= (ord($str[$i]) << ($i * 8)); | |
} | |
return $cell; | |
} | |
public function setCell($x, $y, $cell) | |
{ | |
$offset = $this->getOffset($x, $y); | |
for ($i = 0; $i < $this->char_length; $i++) { | |
$this->map[$offset + $i] = chr(($cell >> ($i * 8)) & 0xFF); | |
} | |
} | |
public function setCellProperty($x, $y, $flag) | |
{ | |
$cell = $this->getCell($x, $y); | |
$cell |= $flag; | |
$this->setCell($x, $y, $cell); | |
} | |
} | |
$map = new Map; | |
$map->setCellProperty(5, 10, Map::BLOCKED); | |
$map->setCellProperty(5, 10, Map::GRESS); | |
var_dump((bool) ($map->getCell(5, 10) & Map::BLOCKED)); | |
var_dump((bool) ($map->getCell(5, 10) & Map::GRESS)); | |
var_dump((bool) ($map->getCell(5, 10) & Map::BRIDGE)); | |
var_dump($map->getCell(5, 9)); | |
var_dump($map->getCell(5, 11)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment