summaryrefslogtreecommitdiff
path: root/bonnie-bee/actor.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@vistahigherlearning.com>2021-10-16 16:22:08 -0400
committerDavid Thompson <dthompson@vistahigherlearning.com>2021-10-16 16:22:08 -0400
commitde833c9d524d2e47b3812612995290795d2e7b84 (patch)
tree707246a8c3324b6b9d16507241a1e00ce2843168 /bonnie-bee/actor.scm
parent58266627bf3ea66dfc6f8434c32304758d1d9e98 (diff)
Allow player to shoot and add flowers to harvest pollen from.
Diffstat (limited to 'bonnie-bee/actor.scm')
-rw-r--r--bonnie-bee/actor.scm60
1 files changed, 37 insertions, 23 deletions
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
+ <damageable>
+ dead?
+ damage
+ on-death))
(define-class <actor> (<node-2d>)
(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 <actor>) dt)
+(define-method (collision-check (actor <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 <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 <damageable> ()
+ (health #:accessor health #:init-keyword #:health))
+
+(define-method (dead? (d <damageable>))
+ (= (health d) 0))
+
+(define-method (damage (d <damageable>) x)
+ (set! (health d) (max (- (health d) x) 0)))
+
+(define-method (on-death (d <damageable>) bullets)
+ #t)