Skip to content

Instantly share code, notes, and snippets.

@raphaelbastide
Last active December 22, 2015 04:18
Show Gist options
  • Save raphaelbastide/6415687 to your computer and use it in GitHub Desktop.
Save raphaelbastide/6415687 to your computer and use it in GitHub Desktop.
Your modul will follow the nearest modul
// follow
var activeFollow = false;
function follow(){
var siblingModuls = modul.sonar();
var c = modul.coordinates();
if(siblingModuls.length === 0){
ui.log("You seem alone…");
}else{
var modul1 = siblingModuls[0];
var mc = modul1.coordinates();
var mx = mc[0];
var my = mc[1];
if (c[0]-1 > mx) {
modul.move("left");
} else if (c[0]+1 < mx) {
modul.move("right");
}
if (c[1]-1 > my) {
modul.move("top");
} else if (c[1]+1 < my) {
modul.move("bottom");
}
}
}
function initFollow() {
activeFollow = true;
}
function stopFollow() {
activeFollow = false;
}
// Don’t forget to call the function each world’s time
world.on('interval', function(){
if (activeFollow) follow();
});
// …and buttons
var actionsPanel = ui.buttonsPanel("Actions", [
ui.button("follow", initFollow),
ui.button("stopFollow", stopFollow)
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment