PHP, Zend Framework and Other Crazy Stuff
Redux: First Official Code!
I guess you can call it the first foundation stone of this project. It’s nothing special, but it is the first piece of code written specifically for the project. ![]()
The code sets the stage for the Redux mapping system. Basically, we’re aiming at a grid-based map with each grid coordinate representing a Sector. Sectors are not Star Systems – a full Star System could occupy up to 100 Sectors leaving plenty of scope for exploration, manouvering, tactics, and my hoped for use of technology such as Satelites, Probes, Sensor Grids, etc. to have a decent space worth using them in. Enough of the banter – say hello to the code…
This represents a skeletal class structure. Redux_Map is a manager class which is the main class the client code would interact with to generate a map. Redux_Map_Grid is a simple grid generator – it creates an array of all sector coordinates. As you’ll see it’s stick simple which may explain why it took so little time to write…
. Unit Tests are appended. I guess it’s important to note the use of Unit Tests – I’ve adopted the Test Driven Development practice with a lot of enthusiasm since last Spring and find it a pretty powerful technique in developing well designed, easy to extend OOP code.
In case any malefactors from the Solar Empire camp decide to comment – it’s not within the project goals at this time to optimise early. I appreciate that OOP has its performance issues, but my personal goals are easy to maintain code within a testable framework. Optimisation at such an early stage hurts those two objectives more often than not unless the developer has the experience to avoid it. So yes, it is my intention to factor unique reuseable logic into small classes like dirt simple Redux_Map_Grid…
The code!
private $grid = array();
private $options = array(
‘height’=>100,
‘width’=>100
);
public function __construct()
{
}
public function map()
{
$grid = new Redux_Map_Grid(
$this->options[‘height’],
$this->options[‘width’]
);
$this->grid = $grid->generate();
}
private function __set($key, $val)
{
if(array_key_exists($key, $this->options))
{
$this->options[$key] = $val;
}
}
public function __get($key)
{
if(isset($this->options[$key]))
{
return$this->options[$key];
}
returnnull;
}
private function __isset($name)
{
returnisset($this->options[$name]);
}
private function __unset($name)
{
unset($this->options[$name]);
}
public function getGrid()
{
return$this->grid;
}
}
And we have our specialised Map_Grid class for generating the coordinate array…
private $height = 0;
private $width = 0;
public function __construct($height, $width)
{
$this->height = (int)$height – 1;
$this->width = (int)$width – 1;
if($this->width<= 0 || $this->height<= 0)
{
throw new Exception(‘Invalid height or width supplied.’);
}
}
public function generate()
{
$grid = array();
for($i=0;$i<=$this->height;$i++)
{
for($j=0;$j<=$this->width;$j++)
{
$grid[] = $i . ‘,’ . $j;
}
}
return$grid;
}
}
Finally, let’s never release code unless accompanied by unit tests! Unit tests are written for Simpletest 1.0.1beta (available from CVS only). We need this version since it avoid future deprecated methods and allows the new expectException() method for checking if an exception is thrown.
public function __construct(){
$this->UnitTestCase(‘Test of Redux_Map’);
}
public function testMapGrid()
{
$map = new Redux_Map;
$map->height = 2;
$map->width = 2;
$map->map();
$this->assertEqual(count($map->getGrid()), 4);
$this->assertIdentical($map->getGrid(), array(
0=>’0,0′,
1=>’0,1′,
2=>’1,0′,
3=>’1,1′,
));
}
public function testMapGridException()
{
$map = new Redux_Map;
$map->height = 0;
$map->width = 0;
$this->expectException();
$map->map();
}
}
Et voilá! Nothing complicated. Next up – adding actual entities (Planets, Facilities, etc.) to those sector coordinates – hopefully approximating star systems with a simple clustering algorithm. Onwards!
Related posts:
| Print article | This entry was posted by Pádraic Brady on October 19, 2006 at 2:27 am, and is filed under PHP Game Development. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
