From de833c9d524d2e47b3812612995290795d2e7b84 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 16 Oct 2021 16:22:08 -0400 Subject: Allow player to shoot and add flowers to harvest pollen from. --- bonnie-bee/actor.scm | 60 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'bonnie-bee/actor.scm') diff --git a/bonnie-bee/actor.scm b/bonnie-bee/actor.scm index db49372..a734d6a 100644 --- a/bonnie-bee/actor.scm +++ b/bonnie-bee/actor.scm @@ -10,7 +10,11 @@ hitbox world-hitbox quadtree - on-collide)) + on-collide + + dead? + damage + on-death)) (define-class () (velocity #:getter velocity #:init-form (vec2 0.0 0.0)) @@ -41,27 +45,37 @@ (refresh-world-hitbox actor) (add-to-quadtree actor)) -(define-method (update (actor ) dt) +(define-method (collision-check (actor )) (let ((p (position actor)) - (v (velocity actor)) (r (world-hitbox actor))) - (unless (and (= (vec2-x v) 0.0) (= (vec2-y v) 0.0)) - (remove-from-quadtree actor) - (vec2-add! p v) - (refresh-world-hitbox actor) - (quadtree-find - (quadtree actor) r - (lambda (other) - ;; Calculate overlap. - (let* ((ro (world-hitbox other)) - (xo (max (- (min (rect-right r) (rect-right ro)) - (max (rect-left r) (rect-left ro))) - 0.0)) - (yo (max (- (min (rect-top r) (rect-top ro)) - (max (rect-bottom r) (rect-bottom ro))) - 0.0))) - (if (or (= xo 0.0) (= yo 0.0)) - #f ; no collision - (on-collide actor other))))) - (add-to-quadtree actor) - (dirty! actor)))) + (quadtree-find + (quadtree actor) r + (lambda (other) + (and (not (eq? other actor)) + (rect-intersects? r (world-hitbox other)) + (on-collide actor other)))))) + +(define-method (update (actor ) dt) + (let ((p (position actor)) + (v (velocity actor))) + (if (and (= (vec2-x v) 0.0) (= (vec2-y v) 0.0)) + (collision-check actor) + (begin + (remove-from-quadtree actor) + (vec2-add! p v) + (refresh-world-hitbox actor) + (collision-check actor) + (add-to-quadtree actor) + (dirty! actor))))) + +(define-class () + (health #:accessor health #:init-keyword #:health)) + +(define-method (dead? (d )) + (= (health d) 0)) + +(define-method (damage (d ) x) + (set! (health d) (max (- (health d) x) 0))) + +(define-method (on-death (d ) bullets) + #t) -- cgit v1.2.3