summaryrefslogtreecommitdiff
path: root/bonnie-bee/player.scm
blob: 09770a013e31a1e5fa7adb342741bed7106d2c8b (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(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 graphics particles)
  #:use-module (chickadee math)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (oop goops)
  #:use-module (catbird asset)
  #:use-module (catbird node)
  #:use-module (catbird node-2d)
  #:export (%max-pollen
            <player>
            move-left?
            move-right?
            move-down?
            move-up?
            moving?
            shoot?
            invincible?
            bombing?
            scoring-enabled?
            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 #:watch? #t)
  (move-right? #:accessor move-right? #:init-value #f #:watch? #t)
  (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-form (steps 2))
  (speed #:accessor speed #:init-value 1.8)
  (lives #:accessor lives #:init-value 3)
  (invincible? #:accessor invincible? #:init-value #f)
  (pollen #:accessor pollen #:init-value 0)
  (score #:accessor score #:init-value 0)
  (scoring-enabled? #:accessor scoring-enabled? #:init-value #f)
  (bombing? #:accessor bombing? #:init-value #f))

(define-method (moving? (player <player>))
  (or (move-left? player)
      (move-right? player)
      (move-down? player)
      (move-up? player)))

(define-method (on-boot (player <player>))
  (attach-to player
             (make <animated-sprite>
               #:name 'sprite
               #:atlas bee-atlas
               #:origin (vec2 16.0 16.0)
               #:animations `((default . ,(make <animation>
                                            #:frames #(0 1)
                                            #:frame-duration 0.05))
                              (move-left . ,(make <animation>
                                              #:frames #(2 3)
                                              #:frame-duration 0.05))
                              (move-right . ,(make <animation>
                                               #:frames #(4 5)
                                               #:frame-duration 0.05))
                              (idle . ,(make <animation>
                                         #:frames #(0 1)
                                         #:frame-duration 0.75))))))

(define-method (update-animation (player <player>))
  (change-animation (& player sprite)
                    (cond
                     ((and (move-left? player)
                           (not (move-right? player)))
                      'move-left)
                     ((and (not (move-left? player))
                           (move-right? player))
                      'move-right)
                     (else
                      'default))))

(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)))
    ((move-left? move-right?)
     (unless (eq? old new)
       (update-animation player)))))

(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! (pollen player) 0)
    (set! (invincible? player) #t)
    (audio-play (artifact player-death-sound))
    (add-particle-emitter (particles (& (parent player) particles))
                          (make-particle-emitter (world-hitbox player)
                                                 10 5))
    (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 (artifact pickup-sound) #:volume 0.3)
    (kill-bullet bullet)
    (add-to-score player 5)
    (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 (artifact 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 (& (parent player) bullets)))
        (add-to-score player (expt 5 times))
        (set! (bombing? player) #t)
        (set! (pollen player) 0)
        (set! (invincible? player) #t)
        (let loop ((i 0))
          (when (< i times)
            (audio-play (artifact 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)
  (when (scoring-enabled? player)
    (set! (score player) (+ (score player) points))))