diff --git a/activity_2/img/r0.png b/activity_2/img/r0.png new file mode 100644 index 0000000000000000000000000000000000000000..ede692f25240af718f3511b3d703988e0870699d Binary files /dev/null and b/activity_2/img/r0.png differ diff --git a/activity_2/main-script.js b/activity_2/main-script.js index 911c24de36b71134cbfe1625a5ecc21e7b30ee43..fa3fe0731eaea6804fd24e685a68f23599248e55 100644 --- a/activity_2/main-script.js +++ b/activity_2/main-script.js @@ -2,20 +2,17 @@ avec les fonctions et variables déjà définies dans 'activity_functions.js' du dossier 'res'.*/ -// Gets the modal (intiated as the intro_modal at first) var modal = document.getElementById("intro_modal"); -// Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; -// Displays the modal when DOM is Loaded document.addEventListener('DOMContentLoaded', event => { modal.style.display = "block"; + canvas = SVG().addTo('#animationArea').size(660, 510) + initCanvas(); }); -// When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } -// When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; @@ -25,7 +22,7 @@ window.onclick = function(event) { - +var canvas; var firstTime; var curSpeed; var curPos; @@ -36,87 +33,31 @@ var rocketLaunched; var simu_timeouts = []; var manual_enabled; -var sky_rect -var maxq_rect -var maxq_text -var path -var rocket -var length - -var fuel_rect -var fuel_box -var fuel_text - -var power_rect -var power_box -var power_text - -var rocket_img +var sky_rect; +var maxq_rect; +var maxq_text; +var path; +var rocket; +var length; +var fuel_rect; +var fuel_box; +var fuel_text; +var power_rect; +var power_box; +var power_text; +var rocket_img; -var canvas = SVG().addTo('#animationArea').size(660, 510) -var fuel_gradient = canvas.gradient('linear', function(add) { - add.stop(0, '#ff2e27') - add.stop(1, '#ffde00') -}) -var power_gradient = canvas.gradient('linear', function(add) { - add.stop(0, '#3c8001') - add.stop(1, '#93f45e') -}) -var sky_gradient = canvas.gradient('linear', function(add) { - add.stop(0, '#4cdaff') - add.stop(1, '#001b91') -}).from(0, 1).to(0, 0) -sky_rect = canvas.rect(500, 350).fill(sky_gradient).stroke({ width: 3, color: '#333' }).attr({x: 5, y: 5}) -maxq_rect = canvas.rect(500, 30).stroke({ width: 3, color: '#555' }).attr({x: 5, y: 220, fill:'#FFA343', }) -maxq_text = canvas.text("Zone de max Q").attr({x: 350, y:220}) -path = canvas.path("M40,353 C40,70 300,48 503,41").fill('none').stroke({width:5, color: '#ff0000'}) -rocket = canvas.image('img/r1.png').size(30, 1000).center(40,353) -length = path.length() -fuel_rect = canvas.rect(490, 50).attr({x: 160, y: 450, fill: fuel_gradient}) -fuel_box = canvas.rect(490, 50).fill('none').stroke({ width: 3, color: '#333' }).attr({x: 160, y: 450}) -fuel_text = canvas.text("Carburant de l'étage :").attr({x: 0, y: 460}) - -power_rect = canvas.rect(490, 50).attr({x: 160, y: 395, fill: power_gradient}) -power_box = canvas.rect(490, 50).fill('none').stroke({ width: 3, color: '#333' }).attr({x: 160, y: 395}) -power_text = canvas.text("Puissance moteurs :").attr({x: 0, y: 405}) - -rocket_img = canvas.image('img/r1.png').attr({x: 550, y : 0}) - -function enableManual() { - manual_enabled = true; - document.addEventListener('keydown', function(event) { - if(event.keyCode == 38) { - changePower(power_ratio+0.01) - } - else if(event.keyCode == 40) { - changePower(power_ratio-0.01) - } else if (event.keyCode == 32) { - if (rocketLaunched) changeRocketStage() - else launchRocket() - } - }); -} +/*Fonctions liées à l'animation, la simu et à sa gestion */ -function init_var() { - for (var i=0; i<simu_timeouts.length; i++) { - clearTimeout(simu_timeouts[i]); - } - console.log("Initialisation de la simulation") - firstTime = (new Date()).getTime() - manual_enabled = false; - curPos = 0; - curSpeed = 0; - curStage = 1; - rocketLaunched = false; - canvas.clear() +function initCanvas() { var fuel_gradient = canvas.gradient('linear', function(add) { add.stop(0, '#ff2e27') add.stop(1, '#ffde00') @@ -133,35 +74,108 @@ function init_var() { maxq_rect = canvas.rect(500, 30).stroke({ width: 3, color: '#555' }).attr({x: 5, y: 220, fill:'#FFA343', }) maxq_text = canvas.text("Zone de max Q").attr({x: 350, y:220}) path = canvas.path("M40,353 C40,70 300,48 503,41").fill('none').stroke({width:5, color: '#ff0000'}) - rocket = canvas.image('img/r1.png').size(30, 1000).center(40,353) + rocket = canvas.image('img/r0.png').size(30, 1000).center(40,353) length = path.length() - + fuel_rect = canvas.rect(490, 50).attr({x: 160, y: 450, fill: fuel_gradient}) fuel_box = canvas.rect(490, 50).fill('none').stroke({ width: 3, color: '#333' }).attr({x: 160, y: 450}) fuel_text = canvas.text("Carburant de l'étage :").attr({x: 0, y: 460}) - + power_rect = canvas.rect(490, 50).attr({x: 160, y: 395, fill: power_gradient}) power_box = canvas.rect(490, 50).fill('none').stroke({ width: 3, color: '#333' }).attr({x: 160, y: 395}) power_text = canvas.text("Puissance moteurs :").attr({x: 0, y: 405}) + + rocket_img = canvas.image('img/r0.png').attr({x: 550, y : 0}) +} + +/** + * Fonction appelée à chaque lancement du code. + */ - rocket_img = canvas.image('img/r1.png').attr({x: 550, y : 0}) +function init_var() { + for (var i=0; i<simu_timeouts.length; i++) { + clearTimeout(simu_timeouts[i]); + } + console.log("Initialisation de la simulation") + firstTime = (new Date()).getTime() + manual_enabled = false; + curPos = 0; + curSpeed = 0; + curStage = 1; + rocketLaunched = false; + canvas.clear(); + initCanvas(); changeFuel(1, 0) changePower(0, 0) stepSimulation(getCurTime()) } +function enableManual() { + manual_enabled = true; + document.addEventListener('keydown', function(event) { + console.log("c") + if (manual_enabled) { + if(event.keyCode == 38) { + changePower(power_ratio+0.02) + } + else if(event.keyCode == 40) { + changePower(power_ratio-0.02) + } else if (event.keyCode == 32) { + if (rocketLaunched) changeRocketStage() + else launchRocket() + } + } + + }); +} + function launchRocket() { rocketLaunched = true; + changeRocketImg(1); } -function getCurTime() { - return(new Date()).getTime() - firstTime; + +function changeRocketStage() { + curStage = (curStage < 4)?(curStage+1):4; + changeRocketImg(curStage) + if (curStage < 4) changeFuel(1, 200) +} + +function changeFuel(ratio, duration) { + fuel_ratio = Math.max(ratio, 0); + if (duration <= 0 ) { + fuel_rect.width(fuel_box.width()*fuel_ratio); + } else { + fuel_rect.animate(duration, 0, 'now').width(fuel_box.width()*fuel_ratio); + } +} + +function changePower(ratio, duration) { + if (rocketLaunched || ratio == 0) { + power_ratio = Math.max(ratio, 0); + power_ratio = Math.min(power_ratio, 1); + power_rect.animate(duration, 0, 'now').width(power_box.width()*power_ratio); + } +} + +function isFuelEmpty() { + return fuel_ratio <= 0; } +function isInMaxQ() { + return curPos>0.14 && curPos<0.23 +} + +function isInOrbit() { + return curPos>=1; +} + + function update_speed(deltaT) { if (curPos <= 0 && curSpeed < 0 && power_ratio<=0.5) curSpeed = 0 else if (!rocketLaunched) curSpeed = 0 - else curSpeed = curSpeed + (power_ratio-0.5)*deltaT*0.01 + else if (fuel_ratio > 0) curSpeed = curSpeed + (power_ratio-0.5)*deltaT*0.01 + else curSpeed = curSpeed + (0-0.5)*deltaT*0.01 } @@ -171,19 +185,18 @@ function update_pos(deltaT) { } function update_fuel(deltaT) { - changeFuel(fuel_ratio - power_ratio*deltaT*0.12, 0) - if (fuel_ratio <= 0) { - changePower(0, 200) + if (fuel_ratio >= 0) { + changeFuel(fuel_ratio - power_ratio*deltaT*0.12, 0) + } else { + changeFuel(0, 0); } } -function isFuelEmpty() { - return fuel_ratio <= 0; +function getCurTime() { + return(new Date()).getTime() - firstTime; } -function isInMaxQ() { - return curPos>0.14 && curPos<0.23 -} + function stepSimulation(prevTime) { var t = getCurTime() @@ -194,8 +207,8 @@ function stepSimulation(prevTime) { var p = path.pointAt(curPos*length) rocket.center(p.x, p.y) //console.log('pos' + curPos + 'speed' + curSpeed) - if (curPos>0.17 && curPos<0.20 && power_ratio > 0.6) { - changeRocketImg('img/explosion.png') + if (curPos>0.17 && curPos<0.20 && power_ratio > 0.6 && fuel_ratio > 0) { + changeRocketImgExplosion() alert("Explosion ! Trop de puissance au point max Q !") } else if (curPos >= 1) { @@ -212,33 +225,13 @@ function stepSimulation(prevTime) { } } - - - -function changeFuel(ratio, duration) { - fuel_ratio = Math.max(ratio, 0); - if (duration <= 0 ) { - fuel_rect.width(fuel_box.width()*fuel_ratio); - } else { - fuel_rect.animate(duration, 0, 'now').width(fuel_box.width()*fuel_ratio); - } -} - -function changePower(ratio, duration) { - power_ratio = Math.max(ratio, 0); - power_ratio = Math.min(ratio, 1); - power_rect.animate(duration, 0, 'now').width(power_box.width()*power_ratio); -} - -function changeRocketImg(imgPath) { - rocket_img.load(imgPath) - rocket.load(imgPath) +function changeRocketImg(curStage) { + let p = 'img/r' + curStage +'.png'; + rocket_img.load(p) + rocket.load(p) } +function changeRocketImgExplosion() { + rocket_img.load('img/explosion.png') + rocket.load('img/explosion.png') + } - - -function changeRocketStage() { - curStage = (curStage < 4)?(curStage+1):4; - changeRocketImg('img/r' + curStage +'.png') - if (curStage < 4) changeFuel(1, 200) -} diff --git a/activity_2/toolbox.xml b/activity_2/toolbox.xml index bf2db3afe636c338a0e1cca72f62386adc506971..09ca34982a55a321d648b87ae3581ad65d139d57 100644 --- a/activity_2/toolbox.xml +++ b/activity_2/toolbox.xml @@ -10,6 +10,7 @@ <category name="Conditions" colour="#5b67a5"> <block type="is_fuel_empty"></block> <block type="is_in_max_q"></block> + <block type="is_in_orbit"></block> </category> <category name="Boucles" colour="#5ba55b"> <block type="controls_whileUntil"> diff --git a/res/activity_functions.js b/res/activity_functions.js index 24178e3d7035ec1bfb3147a097746379d039b932..cc2fccb240a035f95ecad085d711417cfcd5b76c 100644 --- a/res/activity_functions.js +++ b/res/activity_functions.js @@ -102,6 +102,12 @@ function initApi(interpreter, scope) { }; interpreter.setProperty(scope, 'isInMaxQ', interpreter.createNativeFunction(wrapper)); + + var wrapper = function(text) { + return isInOrbit(); + }; + interpreter.setProperty(scope, 'isInOrbit', + interpreter.createNativeFunction(wrapper)); var wrapper = function(text) { return isFuelEmpty(); diff --git a/res/blocks/arduino_blocks_def.js b/res/blocks/arduino_blocks_def.js index 092d8e89f8c252de77e95a84cc0b8b28951dcf6e..5ce4de84e6d1a58e067047d7e6ea07f314b63a30 100644 --- a/res/blocks/arduino_blocks_def.js +++ b/res/blocks/arduino_blocks_def.js @@ -120,6 +120,17 @@ Blockly.Blocks['is_in_max_q'] = { } }; +Blockly.Blocks['is_in_orbit'] = { + init: function() { + this.appendDummyInput() + .appendField("on est en orbite"); + this.setOutput(true, "Boolean"); + this.setColour(230); + this.setTooltip("Vérifie si la fusée est en orbite."); + this.setHelpUrl(""); + } +}; + Blockly.Blocks['enable_manual_mode'] = { init: function() { this.appendDummyInput() diff --git a/res/blocks/arduino_blocks_gen.js b/res/blocks/arduino_blocks_gen.js index 291df2d15b51e53876ff92ebd9ccd5823ffb70cd..288295e924b0e269cde5fc62c53cf7e6842fc61f 100644 --- a/res/blocks/arduino_blocks_gen.js +++ b/res/blocks/arduino_blocks_gen.js @@ -66,6 +66,13 @@ Blockly.JavaScript['is_in_max_q'] = function(block) { return [code, Blockly.JavaScript.ORDER_NONE]; }; +Blockly.JavaScript['is_in_orbit'] = function(block) { + // TODO: Assemble JavaScript into code variable. + var code = 'isInOrbit()'; + // TODO: Change ORDER_NONE to the correct strength. + return [code, Blockly.JavaScript.ORDER_NONE]; +}; + Blockly.JavaScript['enable_manual_mode'] = function(block) { // TODO: Assemble JavaScript into code variable.