From 0d0d38496e05f880055c744845631e31582520c2 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Mon, 12 Sep 2011 17:02:50 -0400 Subject: [PATCH] translating entity references --- web-world/examples/boid/boid.rkt | 53 ++++++++++++++++++++---------- web-world/examples/boid/index.html | 19 +++++++++++ web-world/js-impl.js | 6 ++++ 3 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 web-world/examples/boid/index.html diff --git a/web-world/examples/boid/boid.rkt b/web-world/examples/boid/boid.rkt index 3ec7d10..8d9c885 100644 --- a/web-world/examples/boid/boid.rkt +++ b/web-world/examples/boid/boid.rkt @@ -1,17 +1,21 @@ #lang planet dyoo/whalesong (require (planet dyoo/whalesong/js) - (planet dyoo/whalesong/web-world)) + (planet dyoo/whalesong/image) + (planet dyoo/whalesong/web-world) + (planet dyoo/whalesong/resource)) +(define-resource index.html) + ;; Boid flocking behavior. ;; ;; http://www.vergenet.net/~conrad/boids/pseudocode.html ;; ;; -;; A Boid has a velocity and position vector, as well as a color -(define-struct boid (velocity position color)) +;; A Boid has an identity, a velocity and position vector, as well as a color +(define-struct boid (id velocity position color)) (define width (viewport-width)) @@ -209,7 +213,8 @@ [new-position (vec+ (boid-position b) (boid-velocity b))]) - (make-boid new-velocity + (make-boid (boid-id b) + new-velocity new-position (boid-color b))))) @@ -236,11 +241,12 @@ ;; (: tick ((Listof boid) dom -> (Listof boid))) (define (tick boids dom) - (for/list ([b boids]) - (let ([mass-data (collect-mass-data (boid-neighborhood b boids 40))]) - (cap-boid-velocity - (move-boid b boids mass-data) - 15)))) + (map (lambda (b) + (let ([mass-data (collect-mass-data (boid-neighborhood b boids 40))]) + (cap-boid-velocity + (move-boid b boids mass-data) + 15))) + boids)) @@ -253,8 +259,9 @@ (define (cap-boid-velocity b mag) - (make-boid (vec-cap (boid-vel b) mag) - (boid-pos b) + (make-boid (boid-id b) + (vec-cap (boid-velocity b) mag) + (boid-position b) (boid-color b))) @@ -268,15 +275,17 @@ (define (slow-down-boids boids) (map (lambda (b) - (make-boid (vec-scale (boid-vel b) 0.9) - (boid-pos b) + (make-boid (boid-id b) + (vec-scale (boid-velocity b) 0.9) + (boid-position b) (boid-color b))) boids)) (define (speed-up-boids boids) (map (lambda (b) - (make-boid (vec-scale (boid-vel b) 1.1) - (boid-pos b) + (make-boid (boid-id b) + (vec-scale (boid-velocity b) 1.1) + (boid-position b) (boid-color b))) boids)) @@ -298,7 +307,8 @@ ;; Makes a random boid that starts near the upper left corner, ;; drifting downward. (define (make-random-boid) - (make-boid (make-vec (random 10) + (make-boid (fresh-id) + (make-vec (random 10) (random 10)) (make-vec (+ 20 (random 600)) (+ 20 (random 300))) @@ -314,7 +324,16 @@ ;; visualize: -> void ;; Animates a scene of the boids flying around. (define (visualize) - (big-bang (new-population) + (define population (new-population)) + (big-bang population + (initial-view + (view-append-child + (view-focus (->view index.html) "playground") + (xexp->dom `(div ,@(map (lambda (b) + `(div (@ (id ,(boid-id b)) + (class "boid")) + nbsp)) + population))))) (on-tick tick) #;(to-draw draw) )) diff --git a/web-world/examples/boid/index.html b/web-world/examples/boid/index.html new file mode 100644 index 0000000..ee7c9b2 --- /dev/null +++ b/web-world/examples/boid/index.html @@ -0,0 +1,19 @@ + +Boid + + + +
+
+ + diff --git a/web-world/js-impl.js b/web-world/js-impl.js index 050b205..b580941 100644 --- a/web-world/js-impl.js +++ b/web-world/js-impl.js @@ -1333,6 +1333,9 @@ if (isString(x)) { return true; } + if (isSymbol(x)) { + return true; + } if (isList(x) && !(isEmpty(x))) { if (isSymbol(x.first)) { children = x.rest; @@ -1396,6 +1399,9 @@ if (isString(x)) { return document.createTextNode(x); } + if (isSymbol(x)) { + return $("
&" + x.val + ";
").get(0).firstChild; + } if (isList(x) && !(isEmpty(x))) { if (isSymbol(x.first)) { name = x.first.val;