summaryrefslogtreecommitdiff
path: root/bonnie-bee/game.scm
blob: 5290ab8a4b2f693b789dc247c0654e31cd298da6 (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
(define-module (bonnie-bee game)
  #:use-module (bonnie-bee actor)
  #:use-module (bonnie-bee assets)
  #:use-module (bonnie-bee bullet)
  #:use-module (bonnie-bee common)
  #:use-module (bonnie-bee player)
  #:use-module (chickadee data quadtree)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee math rect)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (oop goops)
  #:use-module (starling kernel)
  #:use-module (starling node)
  #:use-module (starling node-2d)
  #:use-module (starling scene)
  #:export (<game>))

(define %game-bounds (make-rect 0.0 0.0 %game-width %game-height))

(define-class <game> (<scene-2d>)
  (quadtree #:getter quadtree #:init-form (make-quadtree %game-bounds)))

(define-method (on-boot (game <game>))
  (set-cameras! game)
  (attach-to game
             (make <bullets>
               #:name 'bullets
               #:quadtree (quadtree game)))
  (let loop ((i 0))
    (when (< i 100)
      (add-bullet (& game bullets) pollen-pickup
                  (vec2 (* (random:uniform) 320.0)
                        (* (random:uniform) 240.0))
                  (vec2 (random:uniform)
                        (random:uniform)))
      (loop (+ i 1)))))

(define-method (spawn (game <game>) (actor <actor>))
  (set! (quadtree actor) (quadtree game))
  (attach-to game actor))

(define-method (on-enter (game <game>))
  (spawn game
         (make <player>
           #:name 'player
           #:position (vec2 (/ %game-width 2.0) 20.0)
           #:hitbox (make-rect -2.0 -2.0 4.0 4.0))))

(define-method (player (game <game>))
  (& game player))

(define-method (on-key-press (game <game>) key modifiers repeat?)
  (case key
    ((q)
     (pop-scene (current-kernel)))
    ((left)
     (set! (move-left? (player game)) #t))
    ((right)
     (set! (move-right? (player game)) #t))
    ((down)
     (set! (move-down? (player game)) #t))
    ((up)
     (set! (move-up? (player game)) #t))
    ((z)
     (set! (shoot? (player game)) #t))))

(define-method (on-key-release (game <game>) key modifiers)
  (case key
    ((left)
     (set! (move-left? (player game)) #f))
    ((right)
     (set! (move-right? (player game)) #f))
    ((down)
     (set! (move-down? (player game)) #f))
    ((up)
     (set! (move-up? (player game)) #f))
    ((z)
     (set! (shoot? (player game)) #f))))