summaryrefslogtreecommitdiff
path: root/bonnie-bee/player.scm
blob: 9a5da1cfd9739746f3421070c81d75d90cb9b683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(define-module (bonnie-bee player)
  #:use-module (bonnie-bee actor)
  #:use-module (bonnie-bee assets)
  #:use-module (bonnie-bee bullet)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (oop goops)
  #:use-module (starling node)
  #:use-module (starling node-2d)
  #:export (<player>
            move-left?
            move-right?
            move-down?
            move-up?
            shoot?
            speed
            lives
            pollen
            shoot-maybe))

(define-class <player> (<actor>)
  (move-left? #:accessor move-left? #:init-value #f)
  (move-right? #:accessor move-right? #:init-value #f)
  (move-down? #:accessor move-down? #:init-value #f)
  (move-up? #:accessor move-up? #:init-value #f)
  (shoot? #:accessor shoot? #:init-value #f #:watch? #t)
  (last-shot #:accessor last-shot #:init-value 0)
  (shot-interval #:getter shot-interval #:init-value 2)
  (speed #:accessor speed #:init-value 2.0)
  (lives #:accessor lives #:init-value 3)
  (pollen #:accessor pollen #:init-value 0))

(define-method (on-boot (player <player>))
  (attach-to player
             (make <atlas-sprite>
               #:atlas bee-atlas
               #:index 12
               #:origin (vec2 16.0 16.0))))

(define-method (on-change (player <player>) slot-name old new)
  (case slot-name
    ((shoot?)
     (when (and new (not (and old new)))
       (set! (last-shot player) 0)))))

(define-method (update (player <player>) dt)
  (let ((v (velocity player)))
    (set-vec2! v
               (+ (if (move-left? player) -1.0 0.0)
                  (if (move-right? player) 1.0 0.0))
               (+ (if (move-down? player) -1.0 0.0)
                  (if (move-up? player) 1.0 0.0)))
    (vec2-normalize! v)
    (vec2-mult! v (speed player)))
  (next-method))

(define-method (on-collide (player <player>) (bullet <bullet>))
  (if (eq? (type bullet) pollen-pickup)
      (begin
        (kill-bullet bullet)
        (set! (pollen player) (+ (pollen player) 1))
        #t)
      #f))

(define-method (shoot-maybe (player <player>) bullets)
  (with-agenda (agenda player)
    (when (and (shoot? player)
               (>= (- (agenda-time) (last-shot player))
                   (shot-interval player)))
      (let ((p (position player)))
        (set! (last-shot player) (agenda-time))
        (add-bullet bullets
                    player-primary-bullet
                    (vec2 (vec2-x p) (+ (vec2-y p) 14.0))
                    (vec2 0.0 6.0))))))