-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
interactable.lisp
376 lines (302 loc) · 14.8 KB
/
interactable.lisp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
(in-package #:org.shirakumo.fraf.kandria)
(defclass interactable (entity collider)
())
(defmethod action ((interactable interactable)) 'interact)
(defgeneric interact (with from))
(defgeneric interactable-p (entity))
(defgeneric interactable-priority (entity))
(defmethod interactable-priority ((interactable interactable)) 0)
(defmethod description ((interactable interactable)) "")
(defmethod interactable-p ((entity entity)) NIL)
(defmethod interactable-p ((block block)) NIL)
(defclass dialog-entity (interactable)
((interactions :initform () :accessor interactions)
(default-interaction :initform NIL :initarg :default-interaction :accessor default-interaction)))
(defmethod interactable-p ((entity dialog-entity))
(let ((default (default-interaction entity)))
(when (symbolp default)
(let ((defaults (default-interactions (storyline +world+))))
(setf (default-interaction entity)
(or (gethash default defaults)
(gethash (name entity) defaults)
;; KLUDGE: dirty
(when (typep entity 'npc)
(gethash 'npc defaults))))))
(or (interactions entity)
default)))
(defmethod interact ((entity dialog-entity) from)
(when (interactable-p entity)
;; KLUDGE: default interaction causes lookup on NPC to resolve profile.
;; we set it here to refer to whoever is being talked to.
(setf (gethash 'npc (name-map +world+)) entity)
(let ((interactions (or (interactions entity)
(default-interaction entity))))
(when interactions
(show (make-instance 'dialog :interactions (enlist interactions)))))))
(define-shader-entity interactable-sprite (ephemeral lit-sprite dialog-entity resizable creatable)
((name :initform (generate-name "INTERACTABLE"))))
(defmethod description ((sprite interactable-sprite))
(language-string 'examine))
(define-shader-entity interactable-animated-sprite (ephemeral lit-animated-sprite dialog-entity resizable creatable)
((name :initform (generate-name "INTERACTABLE"))
(trial:sprite-data :initform (asset 'kandria 'dummy) :type asset
:documentation "The sprite to display")
(layer-index :initarg :layer-index :initform +base-layer+ :accessor layer-index :type integer
:documentation "Which layer to render the sprite on")
(pending-animation :initarg :animation :initform NIL :accessor pending-animation :type symbol
:documentation "Which animation to play back at the start")))
(defmethod (setf animations) :after (value (sprite interactable-animated-sprite))
(when (pending-animation sprite)
(setf (animation sprite) (pending-animation sprite))))
(define-shader-entity leak (interactable-animated-sprite audible-entity)
((voice :initform (// 'sound 'ambience-water-pipe-leak))))
(defmethod (setf animation) :after (animation (leak leak))
(unless (active-p leak)
(when (allocated-p (voice leak))
(harmony:stop (voice leak)))))
(defmethod description ((leak leak))
(language-string 'leak))
(defmethod active-p ((leak leak))
(not (eq 'normal (name (animation leak)))))
(defclass profile ()
((profile-sprite-data :initform (error "PROFILE-SPRITE-DATA not set.") :accessor profile-sprite-data)
(nametag :initform (error "NAMETAG not set.") :accessor nametag)
(pitch :initform 1.0 :initarg :pitch :accessor pitch)))
(defmethod stage :after ((profile profile) (area staging-area))
(stage (resource (profile-sprite-data profile) 'texture) area)
(stage (resource (profile-sprite-data profile) 'vertex-array) area))
(define-shader-entity door (lit-animated-sprite interactable ephemeral)
((target :initform NIL :initarg :target :accessor target)
(bsize :initform (vec 11 20))
(primary :initform T :initarg :primary :accessor primary)
(facing-towards-screen-p :initform T :initarg :facing-towards-screen-p :accessor facing-towards-screen-p :type boolean
:documentation "Whether the door is facing the screen or facing away from the screen
Determines the animation used when entering it")))
(defmethod description ((door door))
(cond ((or (not (visible-on-map-p (find-chunk door)))
(not (visible-on-map-p (find-chunk (target door)))))
(language-string 'door))
((< (vy (location door)) (vy (location (target door))))
(language-string 'door-up))
(T
(language-string 'door-down))))
(defmethod interactable-p ((door door)) T)
(defmethod interactable-priority ((door door)) 1)
(defmethod default-tool ((door door)) (find-class 'freeform))
(defmethod enter :after ((door door) (region region))
(when (primary door)
(let ((other (etypecase (target door)
(vec2 (clone door :target door :primary NIL :location (target door)))
(null (clone door :target door :primary NIL :location (vec (+ (vx (location door)) (* 2 (vx (bsize door))))
(vy (location door)))))
(list (apply #'clone door :target door :primary NIL (target door))))))
(setf (target door) other)
(enter other region))))
(defmethod layer-index ((door door))
(1- +base-layer+))
(defmethod leave :after ((door door) (container container))
(when (container (target door))
(leave (target door) T)))
(define-shader-entity basic-door (door creatable)
()
(:default-initargs :sprite-data (asset 'kandria 'door)))
(defmethod stage :after ((door basic-door) (area staging-area))
(stage (// 'sound 'door-open) area))
(defmethod interact :before ((door basic-door) (entity game-entity))
(harmony:play (// 'sound 'door-open) :reset T))
(define-shader-entity train-door (basic-door)
()
(:default-initargs :sprite-data (asset 'kandria 'train-door)))
(define-shader-entity passage (door creatable)
()
(:default-initargs :sprite-data (asset 'kandria 'passage)))
(defmethod object-renderable-p ((passage passage) (pass shader-pass)) NIL)
(define-shader-entity locked-door (door creatable)
((name :initform (generate-name "LOCKED-DOOR"))
(key :initarg :key :initform NIL :accessor key :type symbol
:documentation "The name of the item required to unlock the door")
(unlocked-p :initarg :unlocked-p :initform NIL :accessor unlocked-p :type boolean
:documentation "Whether the door is locked or not"))
(:default-initargs :sprite-data (asset 'kandria 'electronic-door)))
(defmethod description ((door locked-door))
(if (unlocked-p door)
(call-next-method)
(language-string 'door-locked)))
(defmethod stage :after ((door locked-door) (area staging-area))
(dolist (sound '(door-access-denied door-unlock door-open-sliding-inside door-shut-sliding-inside))
(stage (// 'sound sound) area)))
(defmethod initargs append ((door locked-door)) '(:key :unlocked-p))
(defmethod interactable-p ((door locked-door)) T)
(defmethod (setf unlocked-p) :around (value (door locked-door))
(unless (eql value (unlocked-p door))
(call-next-method)
(setf (unlocked-p (target door)) value)
(when (/= 0 (length (animations door)))
(setf (animation door) (if value 'granted 'denied))))
value)
(defmethod interact ((door locked-door) (player player))
(cond ((unlocked-p door)
(harmony:play (// 'sound 'door-open-sliding-inside))
(call-next-method))
((have (key door) player)
(setf (unlocked-p door) T)
(harmony:play (// 'sound 'door-unlock)))
(T
(harmony:play (// 'sound 'door-access-denied))
(setf (animation door) 'denied))))
(define-shader-entity save-point (lit-animated-sprite interactable ephemeral creatable)
((bsize :initform (vec 8 18)))
(:default-initargs :sprite-data (asset 'kandria 'telephone)))
(defmethod stage :after ((point save-point) (area staging-area))
(stage (// 'sound 'telephone-save) area))
(defmethod interact :after ((save-point save-point) thing)
(harmony:play (// 'sound 'telephone-save)))
(defmethod (setf animations) :after (animations (save-point save-point))
(setf (next-animation (find 'call (animations save-point) :key #'name)) 'normal))
(defmethod description ((save-point save-point))
(language-string 'save-game))
(defmethod interactable-p ((save-point save-point)) (saving-possible-p))
(defmethod layer-index ((save-point save-point))
(1- +base-layer+))
(define-shader-entity station (lit-animated-sprite interactable ephemeral creatable)
((name :initform (generate-name "STATION"))
(bsize :initform (vec 24 16))
(train :initform (make-instance 'train) :accessor train)
(unlocked-p :initform NIL :initarg :unlocked-p :accessor unlocked-p
:type boolean :documentation "Whether the station has been unlocked or not"))
(:default-initargs :sprite-data (asset 'kandria 'station)))
(defmethod layer-index ((station station))
(1- +base-layer+))
(defmethod initialize-instance :after ((station station) &key train-location)
(v<- (location (train station)) (or train-location (location station))))
(defmethod enter :after ((station station) (container container))
(enter (train station) container))
(defmethod leave :after ((station station) (container container))
(leave (train station) container))
(defmethod description ((station station))
(language-string 'station))
(defmethod quest:activate ((station station))
(setf (unlocked-p station) T))
(defmethod stage :after ((station station) (area staging-area))
(when (name station)
(stage (// 'kandria (name station)) area)))
(defmethod interact ((station station) (player player))
(unless (unlocked-p station)
(status :important "~a" (@formats 'new-station-unlocked (language-string (name station))))
(quest:activate station))
(show-panel 'fast-travel-menu :current-station station))
(defun list-stations (&optional (region (region +world+)))
(sort (for:for ((entity over region)
(status when (typep entity 'station)
collect entity)))
#'> :key (lambda (station) (vy (location station)))))
(defmethod interactable-p ((station station)) T)
(defmethod trigger ((target station) (source station) &key)
(move :freeze (node 'player +world+))
(hide (prompt (node 'player +world+)))
(setf (move-time (train source)) 0.0)
(setf (state (train source)) :depart)
(setf (move-time (train target)) 0.0)
(setf (state (train target)) :arrive))
(defmethod handle :after ((ev tick) (station station))
(handle ev (train station)))
(define-shader-entity train (lit-sprite ephemeral)
((name :initform NIL)
(texture :initform (// 'kandria 'train))
(move-time :initform 0.0 :accessor move-time)
(visibility :initform 0.5 :accessor visibility)
(original-location :initform (vec 0 0) :accessor original-location)
(size :initform (vec 1094 109))
(state :initform :normal :accessor state)))
(defmethod layer-index ((train train))
(1+ +base-layer+))
(defmethod render :before ((train train) (program shader-program))
(setf (uniform program "visibility") (visibility train)))
(defmethod handle ((ev tick) (train train))
(let ((acc 5.0)
(arrive-time 2.0)
(loc (location train)))
(ecase (state train)
(:normal
(if (contained-p (location (node 'player +world+))
(tvec (vx loc) (vy loc) (/ (vx (size train)) 2.0) (/ (vy (size train)) 2.0)))
(when (< 0.5 (visibility train))
(decf (visibility train) (dt ev)))
(when (< (visibility train) 1.0)
(incf (visibility train) (dt ev)))))
(:depart
(when (<= 1.0 (incf (visibility train) (dt ev)))
;; KLUDGE: absolutely terrible
(let* ((player (node 'player +world+))
(sentinel (make-instance 'sentinel :location (vcopy (location player)))))
(enter sentinel +world+)
(setf (target (camera +world+)) sentinel)
(incf (vy (location player)) 200))
(setf (move-time train) 0.0)
(v<- (original-location train) loc)
(setf (state train) :departing)))
(:departing
(decf (vx loc) (* (move-time train) acc))
(when (<= 2.5 (incf (move-time train) (dt ev)))
(v<- loc (original-location train))
(setf (state train) :normal)))
(:arrive
(when (<= 2.0 (incf (move-time train) (dt ev)) 3.0)
;; KLUDGE: ensure that we trigger the transition only once.
(setf (move-time train) 3.1)
(transition
:kind :black
(v<- (original-location train) loc)
(when (typep (target (camera +world+)) 'sentinel)
(leave (target (camera +world+)) +world+))
(setf (target (camera +world+)) (node 'player +world+))
(snap-to-target (camera +world+) train)
(incf (vx loc) (* 0.5 acc (expt arrive-time 2.0) 100.0))
(setf (state train) :arriving)
(setf (move-time train) 0.0))))
(:arriving
(decf (vx loc) (* (- arrive-time (move-time train)) acc))
(when (<= arrive-time (incf (move-time train) (dt ev)))
(let ((player (node 'player +world+)))
(v<- loc (original-location train))
(interrupt-movement-trace player)
(v<- (location player) loc)
(place-on-ground player (location player))
(stop player)
(setf (active-p (find-class 'in-game)) T)
(setf (target (camera +world+)) player)
(setf (state train) :normal)))))))
(define-class-shader (train :fragment-shader)
"uniform float visibility = 0.5;
out vec4 color;
void main(){
maybe_call_next_method();
color.a *= visibility;
}")
(defclass place-marker (sized-entity resizable ephemeral dialog-entity creatable)
((name :accessor name)))
(defmethod interactable-p ((marker place-marker))
(interactions marker))
(defmethod description ((marker place-marker))
(if (eql :complete (quest:status (first (interactions marker))))
(language-string 'examine-again)
(language-string 'examine)))
(defmethod (setf location) ((marker located-entity) (entity located-entity))
(setf (location entity) (location marker)))
(defmethod (setf location) ((name symbol) (entity located-entity))
(setf (location entity) (location (node name +world+))))
(defclass audio-marker (sized-entity resizable ephemeral audible-entity creatable)
((voice :initarg :voice :accessor voice :initform (asset 'sound 'ambience-fluorescent-light-1) :type trial-harmony:sound
:documentation "The sound to play back")))
(defclass bomb-marker (place-marker)
((active-p :initform T :accessor quest:active-p)))
(defmethod interactable-p ((marker bomb-marker))
(and (call-next-method)
(quest:active-p marker)))
(defmethod description ((marker bomb-marker))
(language-string 'place-bomb))
(defmethod interact :after ((marker bomb-marker) (player player))
(harmony:play (// 'sound 'bomb-plant))
(start-animation 'place-bomb 'player)
(mark-as-spawned (spawn player 'bomb) T)
(setf (quest:active-p marker) NIL))