diff --git a/js/main.js b/js/main.js index e91c30e..c5c5f7b 100644 --- a/js/main.js +++ b/js/main.js @@ -22,10 +22,9 @@ class WindSimulation { constructor(width, height, mills) { this.width = width; this.height = height; - this.mills = mills; this.wms = []; - this.init_windmills(); + this.init_windmills(mills); this.wm = null; this.simulation = null; @@ -63,19 +62,16 @@ class WindSimulation { scene.add(this.flag); } - init_windmills() { + init_windmills(mills) { if(!WindMill.isReady()) { - setTimeout(() => { this.init_windmills() }, 100); + setTimeout(() => { this.init_windmills(mills) }, 100); return; } - for(var i = 0; i < this.mills; i++) { - var wm = new WindMill(); - wm.position.set(4 + 0.5, i + 5 - Math.ceil(this.mills / 2) + 0.5, 0); - wm.rotation.z = this.targetRotation; - scene.add(wm); - this.wms.push(wm); - } - this.wm = this.wms[0]; + + for(var i = 0; i < mills; i++) + this.addWindMill(); + + this.wm = null; } move(pos, ended=false, count=2) { @@ -180,7 +176,7 @@ class WindSimulation { this.simulation = null; this.simulationPots = null; this.simulationPot = null; - for(var i = 0; i < this.mills; i++) + for(var i = 0; i < this.wms.length; i++) this.wms[i].lowPower(false); document.getElementById("follower").style.display = "none"; document.getElementById("results").style.display = "none"; @@ -202,28 +198,35 @@ class WindSimulation { this.targetRotation = rotations[dir]; } - addWindMill() { - if(this.mills >= 10) return; - - this.mills++; - this.reset(); + positionValid(pos) { + for(let i = 0; i < this.wms.length; i++) + if(pos.clone().sub(this.wms[i].position).length() < 1) + return false; + return true; } - removeWindMill() { - if(this.mills <= 1) return; - - this.mills--; - this.reset(); + addWindMill() { + if(this.wms.length >= 10) return; + + for(let x = 0; x < 5; x++) + for(let y = 0; y < 10; y++) { + var pos = new THREE.Vector3(4.5 + Math.ceil(x / 2) * (x % 2 ? 1 : -1), 4.5 + Math.ceil(y / 2) * (y % 2 ? 1 : -1), 0); + if(this.positionValid(pos)) { + var wm = new WindMill(); + wm.position.set(pos.x, pos.y, 0); + wm.rotation.z = this.targetRotation; + scene.add(wm); + this.wms.push(wm); + return; + } + } } - reset() { - for(var i = 0; i < this.wms.length; i++) - scene.remove(this.wms[i]); - - this.wms = []; - this.rotation = this.targetRotation; + removeWindMill() { + if(this.wms.length <= 1) return; - this.init_windmills(); + if(this.wms[this.wms.length - 1] == this.wm) this.wm = null; + scene.remove(this.wms.pop()); } simulate() { @@ -310,15 +313,15 @@ class WindSimulation { var results = document.getElementById("results") var innerHTML = '
🥳🥳🥳 ¡Tu simulación está bien! ¿Probaste otros vientos? Si sí agregá más aerogeneradores.
' : - '⚠️⚠️⚠️ ¡Tu simulación no funciona! Probá otras configuraciones.
') + 'Nº' + (i + 1) + ': | ' + Math.round(pot[i] * 100) / 100 + ' GWh | ' + this.divPercentBar(perc, 50) + ' ' + perc + '% ' + (perc < 50 ? '⚠️' : '') + ' |
Nº' + (i + 1) + ': | ' + Math.round(pot[i] * 100) / 100 + ' GWh | ' + this.divPercentBar(perc, 50) + ' ' + perc + '% ' + (perc < 50 ? EXCLAMATION : '') + ' |
TOTAL: | ' + Math.round(sum * 100) / 100 + ' GWh | ' + this.divPercentBar(perc, 80) + ' ' + perc + '% ' + (perc < 80 ? '⚠️' : '') + ' |