summaryrefslogtreecommitdiff
path: root/bonnie-bee/actor.scm
diff options
context:
space:
mode:
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)