Last active
December 22, 2015 04:18
-
-
Save raphaelbastide/6415687 to your computer and use it in GitHub Desktop.
Your modul will follow the nearest modul
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
// 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