summaryrefslogtreecommitdiff
path: root/bonnie-bee/player.scm
blob: 6f6cd91784cc6ad25f2dfc26e7ba8ef62264a399 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(define-module (bonnie-bee player)
  #:use-module (bonnie-bee actor)
  #:use-module (bonnie-bee assets)
  #:use-module (bonnie-bee bullet)
  #:use-module (bonnie-bee common)
  #:use-module (bonnie-bee flower)
  #:use-module (chickadee audio)
  #:use-module (chickadee game-loop)
  #:use-module (chickadee math)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (oop goops)
  #:use-module (starling asset)
  #:use-module (starling node)
  #:use-module (starling node-2d)
  #:export (%max-pollen
            <player>
            move-left?
            move-right?
            move-down?
            move-up?
            shoot?
            speed
            lives
            pollen
            score
            shoot-maybe
            bomb
            add-to-score))

(define %max-pollen 50)

(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 (steps 2))
  (speed #:accessor speed #:init-value 2.5)
  (lives #:accessor lives #:init-value 3)
  (invincible? #:accessor invincible? #:init-value #f)
  (pollen #:accessor pollen #:init-value 0)
  (score #:accessor score #:init-value 0)
  (bombing? #:accessor bombing? #:init-value #f))

(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 (after-move (player <player>))
  (let ((p (position player)))
    (cond
     ((< (vec2-x p) 8.0)
      (set-vec2-x! p 8.0))
     ((> (vec2-x p) 312.0)
      (set-vec2-x! p 312.0)))
    (cond
     ((< (vec2-y p) 8.0)
      (set-vec2-y! p 8.0))
     ((> (vec2-y p) 232.0)
      (set-vec2-y! p 232.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 (lose-life (player <player>))
  (unless (invincible? player)
    (set! (lives player) (max (- (lives player) 1) 0))
    (set! (invincible? player) #t)
    (audio-play (asset-ref player-death-sound))
    (run-script player
      (blink player 20 (steps 5))
      (set! (invincible? player) #f))))

(define-method (on-collide (player <player>) (bullet <bullet>))
  (cond
   ((eq? (type bullet) pollen-pickup)
    (audio-play (asset-ref pickup-sound) #:volume 0.3)
    (kill-bullet bullet)
    (add-to-score player 1)
    (set! (pollen player) (min (+ (pollen player) 1) %max-pollen))
    #t)
   ((or (eq? (type bullet) small-enemy-bullet)
        (eq? (type bullet) medium-enemy-bullet)
        (eq? (type bullet) large-enemy-bullet))
    (kill-bullet bullet)
    (lose-life player)
    #t)
   (else #f)))

(define-method (on-collide (player <player>) (thing <damageable>))
  (lose-life player))

(define-method (on-collide (thing <damageable>) (player <player>))
  (lose-life player))

(define-method (on-collide (player <player>) (flower <flower>))
  #f)

(define-method (on-collide (flower <flower>) (player <player>))
  #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))
        (audio-play (asset-ref player-shoot-sound) #:volume 0.2)
        (add-bullet bullets
                    player-primary-bullet
                    (vec2 (vec2-x p) (+ (vec2-y p) 14.0))
                    (vec2 0.0 6.0))))))

(define-method (bomb (player <player>))
  (unless (or (bombing? player) (< (pollen player) 10))
    (run-script player
      (let* ((times (floor (/ (pollen player) 10)))
             (num-bullets 64)
             (theta-step (/ tau num-bullets))
             (radius 16.0)
             (speed 5.0)
             (p (position player))
             (bullets (bullets (parent player))))
        (set! (bombing? player) #t)
        (set! (pollen player) 0)
        (set! (invincible? player) #t)
        (let loop ((i 0))
          (when (< i times)
            (audio-play (asset-ref player-bomb-sound))
            (let shot-loop ((j 0))
              (when (< j num-bullets)
                (let ((theta (* j theta-step)))
                  (add-bullet bullets
                              player-bomb-bullet
                              (vec2/polar p radius theta)
                              (vec2 (* (cos theta) speed)
                                    (* (sin theta) speed))))
                (shot-loop (+ j 1))))
            (sleep (* (current-timestep) 3))
            (loop (+ i 1))))
        (sleep 1.0)
        (set! (invincible? player) #f)
        (set! (bombing? player) #f)))))

(define-method (add-to-score (player <player>) points)
  (set! (score player) (+ (score player) points)))