Created
July 12, 2013 19:19
-
-
Save blairanderson/5987005 to your computer and use it in GitHub Desktop.
this is a 3d robot simulator
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
Plane2D = function() { | |
var directions = ["north", "east", "south", "west"]; | |
this.rightOf = function(direction){ | |
var currentIndex = directions.indexOf(direction); | |
return directions[(currentIndex+1 + directions.length) % directions.length] | |
} | |
this.leftOf = function(direction){ | |
var currentIndex = directions.indexOf(direction); | |
return directions[(currentIndex-1 + directions.length) % directions.length] | |
} | |
this.advance = function(location, direction) { | |
if (direction == 'north') { | |
return Plane2D.make( | |
location.x, | |
location.y + 1 | |
) | |
} else { | |
throw "unknown direction: " + direction | |
} | |
} | |
} | |
// Plane3D | |
Plane2D.origin = function() { | |
return {x: 0, y: 0} | |
} | |
Plane2D.make = function(x, y) { | |
return {x: x, y: y} | |
} | |
Plane3D = function() { | |
var directions = ["north", "east", "south", "west", "up"]; | |
this.rightOf = function(direction){ | |
var currentIndex = directions.indexOf(direction); | |
return directions[(currentIndex+1 + directions.length) % directions.length] | |
} | |
this.leftOf = function(direction){ | |
var currentIndex = directions.indexOf(direction); | |
return directions[(currentIndex-1 + directions.length) % directions.length] | |
} | |
this.advance = function(location, direction) { | |
if (direction == 'up') { | |
return Plane3D.make( | |
location.x, | |
location.y, | |
location.z + 1 | |
); | |
} else { | |
throw "unknown direction: " + direction; | |
} | |
}; | |
}; | |
// Plane3D | |
Plane3D.origin = function() { | |
return {x: 0, y: 0, z: 0}; | |
}; | |
Plane3D.make = function(x, y, z) { | |
return {x: x, y: y, z: z} | |
}; | |
// Location | |
Location = function(plane, coordinates, direction){ | |
this._plane = plane; | |
this._coordinates = coordinates; | |
this._direction = direction; | |
// .forward() // -> return a new location | |
this.forward = function() { | |
return new Location( | |
this._plane, | |
this._plane.advance(this._coordinates, this._direction), | |
this._direction | |
); | |
} | |
this.rotateRight = function() { | |
return new Location( | |
this._plane, | |
this._coordinates, | |
this._plane.rightOf(this._direction) | |
) | |
} | |
this.rotateLeft = function() { | |
return new Location( | |
this._plane, | |
this._coordinates, | |
this._plane.leftOf(this._direction) | |
) | |
} | |
} | |
Robot = function() { | |
this.location = new Location( | |
new Plane2D(), | |
Plane2D.origin(), | |
'north' | |
) | |
this.orient = function (direction){ | |
this.location._direction = direction | |
}; | |
this.at = function (l){ | |
this.location = l | |
}; | |
this.growWings = function () { | |
this.location = new Location( | |
new Plane3D, | |
Plane3D.origin(), | |
"north") | |
} | |
this.coordinates = function(){ | |
return this.location._coordinates | |
} | |
this.advance = function () { | |
this.at(this.location.forward()) | |
} | |
this.bearing = function (){ | |
return this.location._direction | |
} | |
this.turnRight = function() { | |
this.location = this.location.rotateRight() | |
} | |
this.turnLeft = function() { | |
this.location = this.location.rotateLeft() | |
} | |
} | |
Simulator = function() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment