summaryrefslogtreecommitdiff
path: root/lisparuga.scm
blob: 125fc5555665f28005b8672ae72ce6b9860cab87 (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
;;; Lisparuga
;;; Copyright © 2020 David Thompson <dthompson2@worcester.edu>
;;;
;;; Lisparuga is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Lisparuga is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Lisparuga.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Main scene.
;;
;;; Code:

(define-module (lisparuga)
  #:use-module ((chickadee) #:select (key-pressed?))
  #:use-module (chickadee math rect)
  #:use-module (chickadee math vector)
  #:use-module (chickadee render color)
  #:use-module (chickadee render texture)
  #:use-module (ice-9 match)
  #:use-module (lisparuga asset)
  #:use-module (lisparuga config)
  #:use-module (lisparuga game)
  #:use-module (lisparuga kernel)
  #:use-module (lisparuga node)
  #:use-module (lisparuga node-2d)
  #:use-module (lisparuga player)
  #:use-module (lisparuga scene)
  #:use-module (oop goops)
  #:export (launch-lisparuga))

(define %framebuffer-width 320)
(define %framebuffer-height 240)

(define-asset background (load-image (scope-asset "images/background.png")))

(define-class <lisparuga> (<scene-2d>)
  (state #:accessor state #:init-value 'init))

(define-method (game-over? (lisparuga <lisparuga>))
  (game-over? (& lisparuga game)))

(define-method (complete? (lisparuga <lisparuga>))
  (complete? (& lisparuga game)))

(define-method (on-boot (lisparuga <lisparuga>))
  ;; Scale a small framebuffer up to the window size.
  (set! (views lisparuga)
    (list (make <view-2d>
            #:camera (make <camera-2d>
                       #:width %framebuffer-width
                       #:height %framebuffer-height)
            #:area (let ((wc (window-config (current-kernel))))
                     (make-rect 0 0 (window-width wc) (window-height wc))))))
  (attach-to lisparuga
             (make <sprite>
               #:name 'background
               #:rank 0
               #:texture background)
             (make <game>
               #:name 'game
               #:rank 1
               #:position (vec2 80.0 0.0))))

(define-method (on-enter (lisparuga <lisparuga>))
  (new-game-transition lisparuga))

(define-method (new-game-transition lisparuga)
  (set! (state lisparuga) 'play)
  (let ((game-over (& lisparuga game-over)))
    (and game-over (detach game-over)))
  (reset (& lisparuga game))
  (set! (state lisparuga) 'play)
  (start-stage (& lisparuga game)))

(define-method (game-over-transition (lisparuga <lisparuga>))
  (stop-stage (& lisparuga game))
  (set! (state lisparuga) 'game-over)
  (let ((game-over (make <node-2d>
                     #:name 'game-over
                     #:rank 999)))
    (attach-to game-over
               (make <label>
                 #:name 'game-over
                 #:text "GAME OVER"
                 #:position (vec2 160.0 120.0)
                 #:align 'center
                 #:vertical-align 'center)
               (make <label>
                 #:name 'instructions
                 #:text "press ENTER to play again"
                 #:position (vec2 160.0 90.0)
                 #:align 'center))
    (attach-to lisparuga game-over)))

(define-method (win-transition (lisparuga <lisparuga>))
  (set! (state lisparuga) 'win))

(define-method (pause-transition (lisparuga <lisparuga>))
  (set! (state lisparuga) 'pause)
  (pause (& lisparuga game))
  (let ((container (make <node-2d>
                     #:name 'pause
                     #:rank 999)))
    (attach-to container
               (make <filled-rect>
                 #:region (make-rect 80.0 0.0 160.0 240.0)
                 #:color (make-color 0.0 0.0 0.0 0.4))
               (make <label>
                 #:name 'pause
                 #:rank 1
                 #:text "PAUSE"
                 #:position (vec2 160.0 120.0)
                 #:align 'center
                 #:vertical-align 'center))
    (attach-to lisparuga container)))

(define-method (resume-transition (lisparuga <lisparuga>))
  (set! (state lisparuga) 'play)
  (detach (& lisparuga pause))
  (resume (& lisparuga game))
  (if (key-pressed? 'z)
      (start-player-shooting (& lisparuga game))
      (stop-player-shooting (& lisparuga game))))

(define-method (update (lisparuga <lisparuga>) dt)
  (match (state lisparuga)
    ('play
     (cond
      ((game-over? lisparuga)
       (game-over-transition lisparuga))
      ((complete? lisparuga)
       (win-transition lisparuga))
      (else
       (steer-player (& lisparuga game)
                     (key-pressed? 'up)
                     (key-pressed? 'down)
                     (key-pressed? 'left)
                     (key-pressed? 'right)))))
    (_ #f)))

(define-method (on-key-press (lisparuga <lisparuga>) key scancode modifiers repeat?)
  (match (state lisparuga)
    ('play
     (match key
       ('z (start-player-shooting (& lisparuga game)))
       ('x (unless repeat?
             (toggle-player-polarity (& lisparuga game))))
       ('c (unless repeat?
             (fire-player-homing-missiles (& lisparuga game))))
       ('return (pause-transition lisparuga))
       (_ #t)))
    ('pause
     (match key
       ('return (resume-transition lisparuga))
       (_ #t)))
    ((or 'win 'game-over)
     (match key
       ('return (new-game-transition lisparuga))
       (_ #f)))
    (_ #f)))

(define-method (on-key-release (lisparuga <lisparuga>) key scancode modifiers)
  (match (state lisparuga)
    ('play
     (match key
       ('z (stop-player-shooting (& lisparuga game)))
       (_ #t)))
    (_ #f)))

(define* (launch-lisparuga #:key (window-width 640) (window-height 480))
  (boot-kernel (make <kernel>
                 #:window-config (make <window-config>
                                   #:title "lisparuga"
                                   #:width window-width
                                   #:height window-height))
               (lambda () (make <lisparuga>))))