From a50f6a6f91ec746d4634b1fd3a722a7db7336500 Mon Sep 17 00:00:00 2001 From: Yorick van Pelt Date: Wed, 29 Nov 2017 01:54:56 +0100 Subject: [PATCH] display functionality --- LemmingSim.html | 2 ++ lemming_sim_student.js | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/LemmingSim.html b/LemmingSim.html index 95c31ae..f9aaa4d 100644 --- a/LemmingSim.html +++ b/LemmingSim.html @@ -46,6 +46,8 @@ along with this program. If not, see . Sensor values + Display + diff --git a/lemming_sim_student.js b/lemming_sim_student.js index c9af1b0..eef21d4 100644 --- a/lemming_sim_student.js +++ b/lemming_sim_student.js @@ -108,21 +108,29 @@ class Lemming extends Robot { block = 'blue' } const {distL, distR, wallL, wallR} = vals + this.block = block if (distL < wallL - 5 || distR < wallR - 5) { + this.state = 'block' // if it senses a block if (!block) return this.drive(2e-4) // no block: drive towards block? if (block == 'blue') void 0; // if blue: ignore if (block == 'red') return this.turnDeg(-90) // if red: leave block } else if (wallL < Infinity || wallR < Infinity) { + this.state = 'wall' // no block: turn left or right if (!block) return this.turnDeg(90 * (Math.random() < 0.5 ? -1 : 1)) if (block == 'blue') return this.turnDeg(-90) // blue: leave if (block == 'red') return this.turnDeg(90) // red: keep + } else { + this.state = 'nothing' } // by default: wander - this.rotate(+0.002); + this.rotate(+0.001); this.drive(0.0002); } + getDisplay() { + return "carrying " + this.block + "\nseeing " + this.state + } } var sim = null class Simulation { @@ -319,6 +327,7 @@ class Bay { }).join('') document.getElementById('SensorLabel').innerHTML = sensorString; + document.getElementById('bayDisplay').innerHTML = robot.getDisplay() } } transformMouse({x,y}) { @@ -374,6 +383,9 @@ Object.assign(Robot.prototype, { */ this.body.torque = torque; }, + getDisplay() { + return "" + }, drive(force=0) { /* Apply a force to the robot to move it.