Last active
January 2, 2016 23:39
-
-
Save uadev/8377885 to your computer and use it in GitHub Desktop.
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
/* | |
* This other bot is helping-- somewhat. | |
*/ | |
this.on('start', function() { | |
var directions = { | |
top: function() { | |
this.thrusters.bottom(true); | |
}, | |
bottom: function() { | |
this.thrusters.top(true); | |
}, | |
right: function() { | |
this.thrusters.left(true); | |
}, | |
left: function() { | |
this.thrusters.right(true); | |
}, | |
stop: function() { }, | |
topRight: function() { | |
stopGo.call(this); | |
this.thrusters.left(true); | |
this.thrusters.bottom(true); | |
} | |
} | |
var conditions = { | |
gt: function(b, a) { | |
console.log(a + ' > ' + b) | |
return a > b; | |
}, | |
lt: function(b, a) { | |
console.log(a + ' < ' + b) | |
return a < b; | |
}, | |
miss: function() {console.log('hit'); return false;} | |
} | |
var directionToAngles = function(direction) { | |
var angles = { | |
right: 0, | |
bottom: 90, | |
left: 180, | |
top: 270 | |
} | |
return angles[direction]; | |
} | |
var go = function(direction) { | |
console.log(direction) | |
stopGo.call(this); | |
directions[direction].call(this); | |
mapNext.call(this); | |
} | |
var makeCheckFunction = function(check) { | |
var pair = check.split(' '); | |
console.log(pair) | |
return conditions[pair[0]].bind(void(0), pair[1]); | |
} | |
var navigate = function(mapItem) { | |
var hasPreCondition = (mapItem.sensor || mapItem.aim); | |
if (!hasPreCondition) { | |
go.call(this, mapItem.go); | |
return; | |
} | |
if (mapItem.go) { | |
var goBound = go.bind(this, mapItem.go); | |
} | |
if (mapItem.sensor) { | |
this.on('sensor:' + mapItem.sensor, goBound); | |
} | |
if (mapItem.check) { | |
var check = makeCheckFunction(mapItem.check); | |
if (mapItem.check === 'miss') { | |
this.on('radar:miss', function(angle) { | |
console.log('missed ping') | |
goBound(); | |
this.radar.ping(); | |
}) | |
} | |
} | |
if (mapItem.aim) { | |
console.log('aim ' + mapItem.aim) | |
this.on('radar:hit', function(angle, distance) { | |
if ( check(distance) ) { | |
goBound(); | |
return; | |
} | |
this.radar.ping(); | |
}); | |
this.radar.angle(directionToAngles(mapItem.aim)); | |
this.radar.ping(); | |
} | |
} | |
var stopGo = function() { | |
this.thrusters.left(false); | |
this.thrusters.right(false); | |
this.thrusters.bottom(false); | |
this.thrusters.top(false); | |
} | |
var off = function() { | |
this.off('sensor:right'); | |
this.off('sensor:left'); | |
this.off('sensor:top'); | |
this.off('sensor:bottom'); | |
this.off('radar:hit'); | |
this.off('radar:miss'); | |
this.radar.ping(); | |
}; | |
var parseInstruction = function(instruction) { | |
var actions = instruction.split(','); | |
var mapItem = {} | |
actions.forEach(function(action) { | |
var pair = action.split(':'); | |
mapItem[pair[0]] = pair[1]; | |
}); | |
return mapItem; | |
} | |
var mapNext = function() { | |
off.call(this); | |
var instruction = this.map.shift(); | |
if (!instruction) { | |
console.log('All instructions are mapped'); | |
return; | |
} | |
var mapItem = parseInstruction(instruction); | |
navigate.call(this, mapItem); | |
}; | |
var run = function() { | |
mapNext.call(this); | |
}; | |
this.map = [ | |
'go:right', | |
// 'sensor:top,go:right', | |
'aim:right,check:lt 75,go:topRight', | |
'aim:right,check:lt 60,go:top', | |
'aim:bottom,check:gt 270,go:stop', | |
'aim:right,check:miss,go:stop', | |
'aim:right,check:lt 30,go:stop', | |
'aim:right,check:miss,go:right', | |
]; | |
run.call(this); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment