Created
January 5, 2014 16:20
-
-
Save dchaplinsky/8270220 to your computer and use it in GitHub Desktop.
bit's quest #7
This file contains hidden or 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
| /* | |
| * Not only does your robot come equipped with sensors and thrusters. It also | |
| * has a radar that can be used to determine distances. | |
| * | |
| * The radar has two methods: | |
| * | |
| * | |
| * - angle() - the current direction the radar is pointing (0-359) | |
| * - angle(number) - set the radar direction (0-359) | |
| * | |
| * - ping() - fires the radar | |
| * | |
| * One of two events will return after firing the radar: | |
| * - 'radar:hit' - an object was found | |
| * - 'radar:miss' - no object was found | |
| * | |
| * When a hit event is received, the handler will receive the angle the | |
| * ping was sent out on and the distance to the object, e.g., | |
| * this.on('radar:hit', function(angle, distance) { | |
| * // do stuff | |
| * }); | |
| * | |
| * Bonus info: | |
| * | |
| * Those red jumpy things will kill your robot. Don't touch them. | |
| */ | |
| var stop = function(self) { | |
| self.thrusters.top(false); | |
| self.thrusters.left(false); | |
| self.thrusters.bottom(false); | |
| self.thrusters.right(false); | |
| } | |
| this.on("start", function(){ | |
| this.thrusters.left(true); | |
| this.radar.angle(0); | |
| this.radar.ping(); | |
| }); | |
| var to_the_left = 0, | |
| to_the_right = 500; | |
| this.on('radar:hit', function(angle, distance) { | |
| if (angle == 0) { | |
| to_the_right = distance; | |
| this.radar.angle(180); | |
| } else { | |
| to_the_left = distance; | |
| this.radar.angle(0); | |
| } | |
| if (Math.abs(to_the_left - to_the_right) < 15) { | |
| stop(this); | |
| this.thrusters.top(true); | |
| } | |
| else { | |
| this.radar.ping(); | |
| } | |
| }); | |
| this.on("sensor:bottom", function(foo){ | |
| if (foo) { | |
| stop(this); | |
| this.thrusters.left(true); | |
| } | |
| }); | |
| this.on("sensor:right", function(foo){ | |
| this.thrusters.bottom(true); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment