summaryrefslogtreecommitdiff
path: root/catbird/mixins.scm
blob: 8f4ec7c457f1ea9f4d2be805d47c08d4afa8e09f (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
(define-module (catbird mixins)
  #:use-module (catbird config)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (ice-9 exceptions)
  #:use-module (oop goops)
  #:use-module (srfi srfi-9)
  #:export (<nameable>
            name

            <rankable>
            rank
            sort-by-rank/ascending

            <containable>
            parent
            attach
            detach
            on-enter
            on-exit
            on-attach
            on-detach

            <updatable>
            update
            update/around

            <scriptable>
            agenda
            on-pause
            on-resume
            paused?
            pause
            resume
            run-script
            stop-scripts

            <renderable>
            visible?
            on-show
            on-hide
            show
            hide
            render
            render/around
            render/before

            <movable-2d>
            <movable-3d>
            position)
  #:replace (pause))

(define-class <nameable> ()
  (name #:accessor name #:init-keyword #:name #:init-value #f))

;; For Z sorting objects and such.
(define-class <rankable> ()
  (rank #:accessor rank #:init-keyword #:rank #:init-value 0))

(define (sort-by-rank/ascending lst)
  (sort lst
        (lambda (a b)
          (< (rank a) (rank b)))))


;;;
;;; Containable
;;;

(define-class <containable> ()
  (parent #:accessor parent #:init-form #f))

(define-method (on-enter (child <containable>))
  #t)

(define-method (on-exit (child <containable>))
  #t)

(define-method (on-attach parent (child <containable>))
  #t)

(define-method (on-detach parent (child <containable>))
  #t)

(define-method (attach (obj <containable>) container)
  (when (parent obj)
    (raise-exception
     (make-exception-with-message "object already has a parent")))
  (set! (parent obj) container)
  (on-enter obj)
  (on-attach container obj))

(define-method (detach (obj <containable>))
  (unless (parent obj)
    (raise-exception
     (make-exception-with-message "object has no parent")))
  (on-detach (parent obj) obj)
  (on-exit obj)
  (set! (parent obj) #f))


;;;
;;; Updatable
;;;

(define-class <updatable> ())

(define-method (update (obj <updatable>) dt)
  #t)

(define-method (update/around (obj <updatable>) dt)
  (update obj dt))


;;;
;;; Scriptable
;;;

(define-class <scriptable> (<updatable>)
  (paused? #:accessor paused? #:init-form #f #:init-keyword #:paused?)
  (agenda #:getter agenda #:init-thunk make-agenda))

(define-method (on-pause (obj <scriptable>))
  #t)

(define-method (on-resume (obj <scriptable>))
  #t)

(define-method (pause (obj <scriptable>))
  (unless (paused? obj)
    (set! (paused? obj) #t)
    (on-pause obj)))

(define-method (resume (obj <scriptable>))
  (when (paused? obj)
    (set! (paused? obj) #f)
    (on-resume obj)))

(define-method (update/around (obj <scriptable>) dt)
  (unless (paused? obj)
    (with-agenda (agenda obj)
      (update-agenda dt)
      (next-method))))

(define-syntax-rule (run-script obj body ...)
  (with-agenda (agenda obj) (script body ...)))

(define-method (stop-scripts obj)
  (with-agenda (agenda obj) (clear-agenda)))


;;;
;;; Renderable
;;;

(define-class <renderable> ()
  (visible? #:accessor visible? #:init-form #t #:init-keyword #:visible?))

(define-method (on-show (obj <renderable>))
  #t)

(define-method (on-hide (obj <renderable>))
  #t)

(define-method (show (obj <renderable>))
  (set! (visible? obj) #t)
  (on-show obj))

(define-method (hide (obj <renderable>))
  (set! (visible? obj) #f)
  (on-hide obj))

(define-method (render (obj <renderable>) alpha)
  #t)

(define-method (render/before (obj <renderable>) alpha)
  #t)

(define-method (render/around (obj <renderable>) alpha)
  (when (visible? obj)
    (render/before obj alpha)
    (render obj alpha)))


;;;
;;; Movable
;;;

(define-class <movable-2d> ()
  (position #:accessor position #:init-keyword #:position
            #:init-form (vec2 0.0 0.0)))

(define-class <movable-3d> ()
  (position #:accessor position #:init-keyword #:position
            #:init-form (vec3 0.0 0.0 0.0)))