summaryrefslogtreecommitdiffstats
path: root/site
diff options
context:
space:
mode:
Diffstat (limited to 'site')
-rw-r--r--site/main.mjs30
-rw-r--r--site/robo.mjs3
-rw-r--r--site/samples/rob.fs2
3 files changed, 32 insertions, 3 deletions
diff --git a/site/main.mjs b/site/main.mjs
index 31f1375..703062d 100644
--- a/site/main.mjs
+++ b/site/main.mjs
@@ -112,8 +112,7 @@ function renderArena(robos, delta=0.0) {
robo.speedx, robo.speedy
].map(x => timeScale * x);
- const x = robo.x + velx;
- const y = robo.y + vely;
+ const [_, x, y] = bounce(25, robo.heading, robo.x + velx, robo.y + vely, canvas.width, canvas.height);
//console.debug('bjc render robo', robo, velx, vely, x, y)
renderRobo(ctx, x, y);
});
@@ -154,6 +153,26 @@ function loadForth(taintedPath) {
});
}
+function bounce(radius, heading, x, y, width, height) {
+ // todo: don't just clamp the x/y, instead calculate based on
+ // where the intersection with the wall is.
+ if (y < radius) {
+ // top wall
+ return [-1 * heading, x, radius];
+ } else if (y >= height-radius) {
+ // bottom wall
+ return [-1 * heading, x, height-radius];
+ } else if (x < radius) {
+ // left wall
+ return [heading - 180, radius, y];
+ } else if (x >= width-radius) {
+ // right wall
+ return [180 - heading, width-radius, y];
+ } else {
+ return [heading, x, y];
+ }
+}
+
async function loaded() {
const roboWorker = new Worker('robo.mjs', { type: 'module' });
const robo = {
@@ -206,6 +225,13 @@ async function loaded() {
robo.speedy = speedy;
robo.x += speedx;
robo.y += speedy;
+ const canvas = document.querySelector(CANVAS_SELECTOR);
+ const [newHeading, newx, newy] = bounce(25, trans.vars.heading, robo.x, robo.y, canvas.width, canvas.height);
+ if (newHeading != trans.vars.heading) {
+ roboWorker.postMessage({ kind: 'setHeading', heading: newHeading });
+ robo.x = newx;
+ robo.y = newy;
+ }
renderArena([robo]);
break;
default:
diff --git a/site/robo.mjs b/site/robo.mjs
index 1b7c3d8..d80baad 100644
--- a/site/robo.mjs
+++ b/site/robo.mjs
@@ -26,6 +26,9 @@ async function messageHandler(e) {
case 'tick':
tick();
break;
+ case 'setHeading':
+ vm.setheading(e.data.heading);
+ break;
default:
console.error('invalid message to robo worker', e.data);
postMessage({ kind: 'error', error: 'badmsg' });
diff --git a/site/samples/rob.fs b/site/samples/rob.fs
index 32112d4..59dc946 100644
--- a/site/samples/rob.fs
+++ b/site/samples/rob.fs
@@ -3,5 +3,5 @@
loop
;
-4 setspeed
+7 setspeed
loop