Our Scripts Vault contains many game scripts that you can use to create your own game!
class MapReference{ // ...}define('EASTING_INCREMENT', 5000);define('NORTHING_INCREMENT', 5);$Map = array( 530610 => new MapReference(530610, '1A') , 535610 => new MapReference(535610, '1B') // ... , 530605 => new MapReference(530605, '2A') , 535605 => new MapReference(535605, '2B') // ...);function referenceExists($reference){ global $Map; if (!is_int($reference)) { throw new Exception(); } // ... return isset($Map[$reference]);}// ...class Character{ // ... protected $_reference; // ... public function getReference() { return $this->_reference; } public function setReference($value) { // ... $this->_reference = $value; }}class PlayerCharacter extends Character{ // ... public function goNorth() { if (referenceExists($this->getReference() + NORTHING_INCREMENT)) { $this->setReference($this->getReference() + NORTHING_INCREMENT); } } // ...}
class MapReference{ // ...}final class Map{ const EASTING_INCREMENT = 5000 , NORTHING_INCREMENT = 5; protected static $_instance; protected static $_map; private function __construct() { self::$_map = array( 530610 => new MapReference(530610, '1A') , 535610 => new MapReference(535610, '1B') // ... , 530605 => new MapReference(530605, '2A') , 535605 => new MapReference(535605, '2B') // ... ); } private function __clone() { } public static function getInstance() { if (self::$_instance === NULL) { self::$_instance = new self(); } return self::$_instance; } public function referenceExists($reference) { if (!is_int($reference)) { throw new Exception(); } // ... return isset(self::$_map[$reference]); } // ...}class Character{ // ... protected $_reference; // ... public function getReference() { return $this->_reference; } public function setReference($value) { // ... $this->_reference = $value; }}class PlayerCharacter extends Character{ // ... public function goNorth() { $map = Map::getInstance(); if ($map->referenceExists($this->getReference() + Map::NORTHING_INCREMENT)) { $this->setReference($this->getReference() + Map::NORTHING_INCREMENT); } } // ...}
$playerCharacter = new PlayerCharacter();$playerCharacter->setReference(535605);echo $playerCharacter->getReference() . PHP_EOL;$playerCharacter->goNorth();echo $playerCharacter->getReference() . PHP_EOL;
Chris, please ignore.
I haven't made the PlayerCharacter class a Singleton. The only Singleton class in the code I've posted is Map.
I'm still using 5.2.x at the moment but I'll bear the base Singleton object shizzle in mind if I find myself in a position to upgrade to 5.3.
@Harkins: I don't think Globalton would catch on. But Global Class might!
public function referenceExists($reference) { if (!is_int($reference)) { throw new Exception(); } // ... $link = Mysql_connect(); $fmt = <<<EOFSELECT *FROM map_referenceWHERE reference = %dEOF; $query = sprintf($fmt, $reference); if ( ($result = mysql_query($query, $link)) === FALSE) { throw new Exception(); } $exists = (mysql_result($result, 0) == 1); Mysql_close($link); return $exists; }
My justification for switching from a global variable to a Singleton for my world Map data structure is twofold: