#lang syndicate

(module+ main
  (require syndicate/distributed/tcp)
  (require syndicate/drivers/timer)
  (require syndicate/gensym)
  (require racket/math)

  (require "schemas/scene.rkt")
  (require "schemas/shapes.rkt")

  (assertion-struct SceneHandle (ds))

  (standard-actor-system (ds)
    (define (on-connected remote-ds)
      (at remote-ds
        (during (SceneHandle $scene-e)
          (define scene (embedded-value scene-e))

          (define (bouncy-box)
            (define me (symbol->string (strong-gensym 'user)))

            (define x (* (- (random) 0.5) 100))
            (define y0 (+ 1.0 (* (random) 10)))
            (define z (* (- (random) 0.5) 100))
            (define r (random))
            (define g (random))
            (define b (random))

            (define-field y y0)
            (define start-time (current-inexact-milliseconds))
            (define-field deadline start-time)
            (at ds
              (on (asserted (LaterThan (deadline)))
                (deadline (+ (deadline) (/ 1000 30)))
                (y (+ y0 (cos (/ (- (deadline) start-time) 1000.0 (/ 1 2 pi)))))))

            (at scene
                (assert (Variable me 'y (y)))
                (assert (Sprite me '(y) (Move (ImmediateVector3 x 'y z) (Color-opaque r g b (Box)))))))

          (for [(i 100)] (bouncy-box)))))

    (run-tcp-client-relay ds #:hostname "localhost" #;"vr.demo.leastfixedpoint.com" #:port 9001
                          #:import (lambda (v) (on-connected (embedded-value v))))))
