-
Notifications
You must be signed in to change notification settings - Fork 2
/
7.js
74 lines (65 loc) · 1.67 KB
/
7.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* 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.
*/
/**
* Tactic: Find the middle of the maze (distance to both sides ~equal), then
* go down until door is found on the right.
*/
this.on("start", function(){
this.thrusters.left(true);
fireBoth(this);
});
var distanceRight = null
, distanceLeft = null;
function fireBoth(bit){
bit.radar.angle(0);
bit.radar.ping();
bit.radar.angle(180);
bit.radar.ping();
}
this.on("radar:hit", function(angle, distance){
if (angle === 0){
distanceRight = distance;
} else {
distanceLeft = distance;
}
// If approx middle
if (Math.abs(distanceLeft - distanceRight) < 15){
this.thrusters.left(false);
this.thrusters.top(true);
}
if (this.thrusters.left()){
fireBoth(this);
} else {
this.radar.angle(0);
this.radar.ping();
}
});
// Door spotted
this.on("radar:miss", function(){
this.thrusters.left(true);
this.thrusters.top(false);
});