summaryrefslogtreecommitdiff
path: root/starling/scene.scm
blob: afe3827c4daa1cf3a181ed118cc58337b3d4a630 (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
;;; Starling Game Engine
;;; Copyright © 2018 David Thompson <davet@gnu.org>
;;;
;;; This program 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.
;;;
;;; This program 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 Starling.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Scenes are the main state machine abstraction.  A scene represents
;; a distinct portion of a game: main menu, overworld map, inventory
;; screen, etc.  The kernel tracks the currently active scene.
;;
;;; Code:

(define-module (starling scene)
  #:use-module (chickadee)
  #:use-module (chickadee audio)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (starling node)
  #:export (<scene>
            background-music
            background-music-volume
            background-music-loop?
            on-quit
            on-key-press
            on-key-release
            on-text-input
            on-mouse-press
            on-mouse-release
            on-mouse-move
            on-controller-add
            on-controller-remove
            on-controller-press
            on-controller-release
            on-controller-move

            <scene-mux>
            current-scene
            previous-scene
            push-scene
            replace-scene
            pop-scene
            on-scenes-empty))

(define-class <scene> (<node>)
  (background-music-source #:getter background-music-source
                           #:init-form (make-source #:loop? #t))
  (background-music #:accessor background-music #:init-form #f
                    #:init-keyword #:music)
  (background-music-volume #:accessor background-music-volume #:init-form 1.0
                           #:init-keyword #:music-volume)
  (background-music-loop? #:accessor background-music-loop? #:init-form #t
                          #:init-keyword #:music-loop?))

(define-method (on-enter (scene <scene>))
  (when (audio? (background-music scene))
    (set-source-volume! (background-music-source scene)
                        (background-music-volume scene))
    (set-source-audio! (background-music-source scene)
                       (background-music scene))
    (source-play (background-music-source scene))))

(define-method (on-exit (scene <scene>))
  (source-stop (background-music-source scene)))

;; Input event handler methods
(define-method (on-quit (scene <scene>))
  (abort-game))

(define-method (on-key-press (scene <scene>) key scancode modifiers repeat?)
  #t)

(define-method (on-key-release (scene <scene>) key scancode modifiers)
  #t)

(define-method (on-text-input (scene <scene>) text)
  #t)

(define-method (on-mouse-press (scene <scene>) button clicks x y)
  #t)

(define-method (on-mouse-release (scene <scene>) button x y)
  #t)

(define-method (on-mouse-move (scene <scene>) x y x-rel y-rel buttons)
  #t)

(define-method (on-controller-add (scene <scene>) controller)
  #t)

(define-method (on-controller-remove (scene <scene>) controller)
  #t)

(define-method (on-controller-press (scene <scene>) controller button)
  #t)

(define-method (on-controller-release (scene <scene>) controller button)
  #t)

(define-method (on-controller-move (scene <scene>) controller axis value)
  #t)


;;;
;;; Scene Multiplexer
;;;

(define-class <scene-mux> (<node>)
  (scenes #:accessor scenes #:init-form '()))

(define-method (current-scene (mux <scene-mux>))
  (match (scenes mux)
    ((s . _) s)
    (() #f)))

(define-method (previous-scene (mux <scene-mux>))
  (match (scenes mux)
    ((_ s . _) s)
    (_ #f)))

(define-method (push-scene (mux <scene-mux>) (scene <scene>))
  (let ((old (current-scene mux)))
    (set! (scenes mux) (cons scene (scenes mux)))
    (when old (detach old))
    (attach-to mux scene)))

(define-method (replace-scene (mux <scene-mux>) (scene <scene>))
  (match (scenes mux)
    ((old . rest)
     (set! (scenes mux) (cons scene rest))
     (detach old)
     (attach-to mux scene))
    (()
     (error "no scene to replace!" mux))))

(define-method (pop-scene (mux <scene-mux>))
  (match (scenes mux)
    ((old)
     (set! (scenes mux) '())
     (detach old)
     (on-scenes-empty mux))
    ((and (old new . _)
          (_ . rest))
     (set! (scenes mux) rest)
     (detach old)
     (attach-to mux new))
    (()
     (error "no scene to pop!" mux))))

(define-method (on-scenes-empty (mux <scene-mux>))
  #t)

(define-method (on-quit (mux <scene-mux>))
  (on-quit (current-scene mux)))

(define-method (on-key-press (mux <scene-mux>) key scancode modifiers repeat?)
  (on-key-press (current-scene mux) key scancode modifiers repeat?))

(define-method (on-key-release (mux <scene-mux>) key scancode modifiers)
  (on-key-release (current-scene mux) key scancode modifiers))

(define-method (on-text-input (mux <scene-mux>) text)
  (on-text-input (current-scene mux) text))

(define-method (on-mouse-press (mux <scene-mux>) button clicks x y)
  (on-mouse-press (current-scene mux) button clicks x y))

(define-method (on-mouse-release (mux <scene-mux>) button x y)
  (on-mouse-release (current-scene mux) button x y))

(define-method (on-mouse-move (mux <scene-mux>) x y x-rel y-rel buttons)
  (on-mouse-move (current-scene mux) x y x-rel y-rel buttons))

(define-method (on-controller-add (mux <scene-mux>) controller)
  (on-controller-add (current-scene mux) controller))

(define-method (on-controller-remove (mux <scene-mux>) controller)
  (on-controller-remove (current-scene mux) controller))

(define-method (on-controller-press (mux <scene-mux>) controller button)
  (on-controller-press (current-scene mux) controller button))

(define-method (on-controller-release (mux <scene-mux>) controller button)
  (on-controller-release (current-scene mux) controller button))

(define-method (on-controller-move (mux <scene-mux>) controller axis value)
  (on-controller-move (current-scene mux) controller axis value))