(function() { var lightID; var colors = [{red: 255, green: 255, blue: 255 }, {red: 0, green: 255, blue: 0 }, { red: 0, green: 0, blue: 255 }]; var loop; var isAttack = false; var alienListener = function(channel, message, sender){ message = JSON.parse(message); if (channel == "InvasionChannel" && message.type == 'before_it_all_happens') { Script.clearInterval(loop); Entities.editEntity(lightID, {'color' : {red: 255, green: 0, blue: 0}}); isAttack = true; } else if (channel == "InvasionChannel" && message.type == 'gameover') { isAttack = false; Script.setInterval(lightEffect, 10000); } } var lightEffect = function(){ var color = Math.round(Math.random()*3); // one of three random colors if (!isAttack){ Entities.editEntity(lightID, {'color' : colors[color]}); } } var HatEffect = function(){ }; HatEffect.prototype = { preload: function(entityID){ lightID = Entities.getChildrenIDs(entityID)[0]; //we only have one child, the light Messages.subscribe("InvasionChannel"); Messages.messageReceived.connect(alienListener); loop = Script.setInterval(lightEffect, 10000); }, unload: function(){ Messages.messageReceived.disconnect(alienListener); Script.clearInterval(loop); } }; return new HatEffect(); });