Last active
December 22, 2015 10:18
-
-
Save raphaelbastide/6457699 to your computer and use it in GitHub Desktop.
A checkpoint static modul. Contains skin and behaviour.
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
var ctx = modul.context; | |
// Skin | |
function checkpt(ledColor){ | |
function circle(x, y, r, col){ | |
ctx.beginPath(); | |
ctx.fillStyle = col; | |
ctx.arc(x, y, r, 0, Math.PI * 2, false); | |
ctx.closePath(); | |
ctx.fill(); | |
} | |
function drawRotated(originx, originy, distance, size, color, steps) { | |
ctx.save(); | |
ctx.translate( originx, originy ); | |
for (var i=0;i<steps;i++){ | |
ctx.rotate(360/steps*Math.PI/180); | |
circle(distance,distance,size,color); | |
} | |
ctx.restore(); | |
} | |
drawRotated(25, 25, 16, 3, '#999', 12); | |
circle(25,25,23,'rgba(200, 200, 200, 1)'); | |
circle(25,25,22,'rgba(230, 230, 230, 1)'); | |
circle(25,25,8,'rgba(70, 70, 70, .7)'); | |
circle(25,25,7,ledColor); | |
circle(25,23,3,'rgba(255, 255, 255, .6)'); | |
drawRotated(25, 25, 12, 2, '#ccc', 4); | |
} | |
// You can fill your own panel | |
// Here is the default "Actions" panel | |
var actionsPanel = ui.buttonsPanel("Actions", [ | |
ui.button("ON", activate), | |
ui.button("OFF", desactivate), | |
ui.button("goto", initGoto), | |
ui.button("stopGoto", stopGoto) | |
]); | |
var activeGoto = false; | |
var posGoto = [0,0]; | |
function goto() { | |
var c = modul.coordinates(); | |
if (c[0] > posGoto[0]) { | |
modul.move("left"); | |
} else if (c[0] < posGoto[0]) { | |
modul.move("right"); | |
} | |
if (c[1] > posGoto[1]) { | |
modul.move("top"); | |
} else if (c[1] < posGoto[1]) { | |
modul.move("bottom"); | |
} | |
if (c[0] === posGoto[0] && c[1] === posGoto[1]) { | |
ui.log("End of the trip!"); // never shown? | |
activeGoto = false; | |
} | |
} | |
function initGoto(x, y) { | |
posGoto = [x, y]; | |
activeGoto = true; | |
} | |
function stopGoto() { | |
ui.log("Trip stopped"); // never shown? | |
activeGoto = false; | |
} | |
world.on('interval', function(){ | |
if (activeGoto) goto(); | |
if (activated) com('<3'); | |
}); | |
// emission | |
var activated = true; | |
function com(msg){ | |
var siblingModuls = modul.sonar(); | |
if(siblingModuls.length === 0){ | |
checkpt('grey'); | |
}else{ | |
checkpt('#00FF00'); | |
var modul1 = siblingModuls[0]; | |
var modulimg = modul1.image(); | |
modul1.send(msg); | |
ui.log("Sent: “"+msg+"”"); | |
} | |
} | |
function desactivate() { | |
activated = false; | |
checkpt('grey'); | |
} | |
function activate() { | |
activated = true; | |
} | |
// Apply skin | |
checkpt('grey'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment