Update universe.rkt for latest os2.rkt

This commit is contained in:
Tony Garnock-Jones 2012-05-09 13:44:23 -04:00
parent 7395f9b1fa
commit 92d8a13450
1 changed files with 19 additions and 18 deletions

View File

@ -4,7 +4,7 @@
(require racket/match)
(require racket/class)
(require racket/async-channel)
(require racket/gui/base)
(require (except-in racket/gui/base yield))
(require 2htdp/image)
(require "os2.rkt")
@ -25,7 +25,7 @@
(wrap-evt (alarm-evt msecs) (lambda (_) (current-inexact-milliseconds))))
(define wild-sub (topic-subscriber (wild)))
(define (broadcast message) (send-message (topic-publisher 'universe) message))
(define (broadcast message) (send-message message))
(define (replace-world w1 w2)
(cond
@ -44,43 +44,41 @@
(on-tick tick-expr rate-expr 0))
((_ tick-expr rate-expr limit-expr)
(list
(role wild-sub
(role 'on-tick wild-sub
#:state w
#:id id
['tick (replace-world w (tick-expr w))])
(spawn (transition (ticker-state 0 rate-expr limit-expr)
(role wild-sub
(role 'tick-stopper wild-sub
#:state ts
['stop (transition ts (kill))])
(let loop ((next-alarm-time 0))
(role (topic-subscriber (time-evt next-alarm-time))
(role 'tick-sequence (topic-subscriber (cons (time-evt next-alarm-time) (wild)))
#:state (and ts (ticker-state counter interval limit))
#:id id
[now
[(cons _ now)
(if (and (positive? limit) (>= counter limit))
(transition ts (kill))
(transition (ticker-state (+ counter 1) interval limit)
(delete-role id)
(delete-role 'tick-sequence)
(loop (+ now (* 1000 interval)))
(broadcast 'tick)))]))))))))
(define-syntax-rule (on-key key-expr)
(role wild-sub
(role 'on-key wild-sub
#:state w
[`(key-down ,kev) (replace-world w (key-expr w kev))]))
(define-syntax-rule (on-release release-expr)
(role wild-sub
(role 'on-release wild-sub
#:state w
[`(key-up ,kev) (replace-world w (release-expr w kev))]))
(define-syntax-rule (on-mouse mouse-expr)
(role wild-sub
(role 'on-mouse wild-sub
#:state w
[`(mouse ,x ,y ,mev) (replace-world w (mouse-expr w x y mev))]))
(define-syntax-rule (stop-when last-world?)
(role wild-sub
(role 'stop-when wild-sub
#:state w
[`(new-state ,_) (if (last-world? w)
(replace-world w (stop-with w))
@ -89,19 +87,22 @@
(define-syntax-rule (on-draw render-expr) (to-draw render-expr))
(define-syntax-rule (to-draw render-expr)
(role wild-sub
(role 'to-draw wild-sub
#:state w
[`(new-state ,_) (transition w (broadcast `(render ,(render-expr w))))]))
(define (ui-actions c:ui->world c:world->ui)
(list
(role (topic-subscriber c:ui->world) #:state w)
(role wild-sub
(role 'ui-actions (topic-subscriber (cons c:ui->world (wild)))
#:state w
[(cons _ e)
(transition w (send-message e))])
(role 'ui-stopper wild-sub
#:state w
['stop
(async-channel-put c:world->ui 'stop)
(transition w (kill))])
(role wild-sub
(role 'ui-renderer wild-sub
#:state w
[`(render ,scene)
(async-channel-put c:world->ui `(render ,scene))
@ -173,7 +174,7 @@
(loop))))
(define (error-listener topic)
(role topic
(role 'error-listener topic
#:state w
#:topic flow
#:reason reason