summaryrefslogtreecommitdiff
path: root/lisparuga.scm
blob: b552a7a202046643074e6a7a3fb32e7d6c80fcdf (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
;;; 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 'play))

(define (game-over? lisparuga)
  (zero? (lives (& lisparuga actor-canvas game player))))

(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))))))
  ;; This 160x240 canvas is where the actual game actors will get
  ;; rendered.
  (let ((actor-canvas (make <canvas>
                        #:name 'actor-canvas
                        #:rank 1
                        #:views (list (make <view-2d>
                                        #:camera (make <camera-2d>
                                                   #:width 160
                                                   #:height 240)
                                        #:area (make-rect 80 0 160 240)
                                        #:clear-color (make-color 0.0 0.0 0.0 1.0))))))
    (attach-to lisparuga
               (make <sprite>
                 #:name 'background
                 #:rank 0
                 #:texture background)
               actor-canvas)
    (new-game-transition lisparuga)))

(define (new-game-transition lisparuga)
  (set! (state lisparuga) 'play)
  (let ((game-over (& lisparuga game-over)))
    (and game-over (detach game-over)))
  (let ((old-game (& lisparuga actor-canvas game)))
    (and old-game (detach old-game)))
  (attach-to (& lisparuga actor-canvas) (make <game> #:name 'game)))

(define (game-over-transition lisparuga)
  (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 (/ (* 9.0 8.0) 2.0))
                                  116.0)
                 #:rank 999))
    (attach-to lisparuga game-over)))

(define-method (update (lisparuga <lisparuga>) dt)
  (match (state lisparuga)
    ('play
     (if (game-over? lisparuga)
         (game-over-transition lisparuga)
         (steer-player (& lisparuga actor-canvas 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
     (unless repeat?
       (match key
         ('z (start-player-shooting (& lisparuga actor-canvas game)))
         ('x (toggle-player-polarity (& lisparuga actor-canvas game)))
         ('c (fire-player-homing-missiles (& lisparuga actor-canvas game)))
         (_ #t))))
    ('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 actor-canvas 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>))))