summaryrefslogtreecommitdiff
path: root/sly/render/particles.scm
diff options
context:
space:
mode:
Diffstat (limited to 'sly/render/particles.scm')
-rw-r--r--sly/render/particles.scm62
1 files changed, 35 insertions, 27 deletions
diff --git a/sly/render/particles.scm b/sly/render/particles.scm
index 88196c5..ca79a04 100644
--- a/sly/render/particles.scm
+++ b/sly/render/particles.scm
@@ -34,7 +34,8 @@
particle-system?
particle-system-texture
particle-system-duration
- particle-system-emit-rate
+ particle-system-emit-interval
+ particle-system-emit-count
particle-system-life-span
particle-system-renderer
render-particles))
@@ -53,53 +54,60 @@ time, and life span. PROC must return a 2D vector."
(polar2 time n))))
(define-record-type <particle-system>
- (%make-particle-system texture sprite-rect duration emit-rate
- life-span renderer)
+ (%make-particle-system texture sprite-rect duration emit-interval
+ emit-count life-span renderer)
particle-system?
(texture particle-system-texture)
(sprite-rect particle-system-sprite-rect)
(duration particle-system-duration)
- (emit-rate particle-system-emit-rate)
+ (emit-interval particle-system-emit-interval)
+ (emit-count particle-system-emit-count)
(life-span particle-system-life-span)
(renderer particle-system-renderer))
(define* (make-particle-system #:key (texture null-texture) duration
- (emit-rate 1) (life-span 60)
+ (emit-interval 1) (emit-count 1)
+ (life-span 60)
(renderer default-particle-renderer))
"Create a new particle system where each particle is rendered using
-TEXTURE. Particles are emitted at a rate of EMIT-RATE per tick of the
-game clock for DURATION ticks and each particle exists for LIFE-SPAN
-ticks. The procedure RENDERER is responsible for placing and
-rendering each particle, and takes the following arguments: graphics
-context, sprite batch, texture, sprite rectangle, particle ID, time,
-and life span."
+TEXTURE. EMIT-COUNT particles are emitted every EMIT-INTERVAL ticks
+of the game clock for DURATION ticks and each particle exists for
+LIFE-SPAN ticks. The procedure RENDERER is responsible for placing
+and rendering each particle, and takes the following arguments:
+graphics context, sprite batch, texture, sprite rectangle, particle
+ID, time, and life span."
(let* ((w (texture-width texture))
(h (texture-height texture))
(sprite-rect (make-rect (- (/ w 2)) (- (/ h 2)) w h)))
- (%make-particle-system texture sprite-rect duration emit-rate
- life-span renderer)))
+ (%make-particle-system texture sprite-rect duration emit-interval
+ emit-count life-span renderer)))
(define (render-particles particle-system batch time)
"Create a renderer for PARTICLE-SYSTEM at TIME. The particles will
be rendered using BATCH, a sprite batcher."
(let* ((life-span (particle-system-life-span particle-system))
- (emit-rate (particle-system-emit-rate particle-system))
+ (emit-count (particle-system-emit-count particle-system))
+ (interval (particle-system-emit-interval particle-system))
(duration (particle-system-duration particle-system))
- (count (* emit-rate life-span))
- (first (* (- time life-span) emit-rate))
- (last (if duration
- (min (+ first count)
- (floor (* duration emit-rate)))
- (+ first count)))
+ (first-wave (max (floor (/ (- time life-span) interval)) 0))
+ (last-wave (floor
+ (/ (if duration
+ (min time duration)
+ time)
+ interval)))
(render (particle-system-renderer particle-system))
(texture (particle-system-texture particle-system))
(sprite-rect (particle-system-sprite-rect particle-system)))
(lambda (gfx)
(with-sprite-batch batch gfx
- (let loop ((particle (max first 0)))
- (when (< particle last)
- (let* ((start-time (/ particle emit-rate))
- (current-time (- time start-time)))
- (render gfx batch texture sprite-rect particle
- current-time life-span))
- (loop (1+ particle))))))))
+ (let loop ((w first-wave))
+ (when (<= w last-wave)
+ (let ((life-time (- time (* w interval)))
+ (first-particle (* w emit-count)))
+ (let loop ((p 0))
+ (when (< p emit-count)
+ (render gfx batch texture sprite-rect
+ (+ first-particle p)
+ life-time life-span)
+ (loop (1+ p)))))
+ (loop (1+ w))))))))