You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.8 KiB
PHTML

8 years ago
<?php
8 years ago
class InvalidDirectionException extends UnexpectedValueException{
}
8 years ago
class Direction
{
8 years ago
private static $top = 0;
private static $bottom = 1;
private static $left = 2;
private static $right = 3;
8 years ago
private $value;
8 years ago
public $deltaX;
public $deltaY;
8 years ago
public function __construct(){
$this->value = 0;
}
8 years ago
private function setValue($value){
$this->value = $value;
switch ($value){
case Direction::$bottom:
$this->deltaY = -1;
$this->deltaX = 0;
break;
case Direction::$top:
$this->deltaY = 1;
$this->deltaX = 0;
break;
case Direction::$left:
$this->deltaY = 0;
$this->deltaX = -1;
break;
case Direction::$right:
$this->deltaY = 0;
$this->deltaX = 1;
break;
}
}
8 years ago
public function __toString(){
switch ($this->value){
case Direction::$top:
return "y+";
break;
case Direction::$bottom:
return "y-";
break;
case Direction::$left:
return "x-";
break;
case Direction::$right:
return "x+";
break;
}
}
8 years ago
8 years ago
public static function make($str){
$dir = new Direction();
switch((string)$str){
case "x+":
8 years ago
$dir->setValue(Direction::$right);
8 years ago
break;
case "x-":
8 years ago
$dir->setValue(Direction::$left);
8 years ago
break;
case "y+":
8 years ago
$dir->setValue(Direction::$top);
8 years ago
break;
case "y-":
8 years ago
$dir->setValue(Direction::$bottom);
8 years ago
break;
default:
throw new InvalidDirectionException("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received.");
}
return $dir;
}
8 years ago
public function opposite(){
$opposites = array(
Direction::$top => Direction::$bottom,
Direction::$bottom => Direction::$top,
Direction::$left => Direction::$right,
Direction::$right => Direction::$left
);
$opposite = new Direction();
8 years ago
$opposite->setValue($opposites[$this->value]);
8 years ago
return $opposite;
8 years ago
}
8 years ago
}