-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.lisp
406 lines (311 loc) · 9.93 KB
/
classes.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
;;;; classes.lisp
(in-package #:game2)
;; https://learnopengl.com/In-Practice/2D-Game/Collisions/Collision-detection
;;;;;;;;;;;;;;;;;;;;;;;;
;; KILLABLE INTERFACE ;;
;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric dead-p (obj))
(defclass killable ()
((dead :initform nil
:accessor dead)))
(defmethod dead-p ((obj killable))
(with-slots (dead) obj
(when dead t)))
(defmethod dead-p (obj)
nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; IN-GAME OBJECT INTERFACE ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric display (object)
(:documentation "A method used to display according to its state."))
(defgeneric update (object)
(:documentation "Update an object according to its state."))
;;;;;;;;;;;;;;;;;;;;;;;
;; PHYSICS INTERFACE ;;
;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric move (object)
(:documentation "Move a object according to its state."))
(defgeneric move-to (obj destination)
(:documentation "Moves an object to destination"))
(defgeneric move-by (obj delta)
(:documentation "Moves an object by delta"))
;; default implementation
(defclass movable ()
((coords :initform (vec2 0 0)
:accessor coords)
(speed :initform 0
:documentation "In pixels/frame.")
(heading :initform (vec2 0 0)
:accessor heading)))
(defmethod move ((obj movable))
(with-slots (coords speed heading) obj
(setf coords (add coords
(mult (normalize heading)
speed)))))
(defmethod move-to ((obj movable) (delta vec2))
)
(defmethod move-by ((obj movable) (delta vec2))
)
;;;;;;;;;;;;;;;;;;;;;;;;;
;; COLLISION INTERFACE ;;
;;;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric collide-p (a b)
(:documentation "Checks if A and B collide."))
(defmethod collide-p (a b))
(defgeneric collide (a b)
(:documentation "Performs actions when objects collide"))
;;;;;;;;;;;;;;;;;;;
;; BOX COLLISION ;;
;;;;;;;;;;;;;;;;;;;
;; TODO
(defclass collidable2-box ()
((height :initform 0 ;; 0 pixels
:initarg :collision-y
:accessor collision-y
:documentation "Used for collision detection.")
(widht :initform 0 ;; 0 pixels
:initarg :collision-x
:accessor collision-x
:documentation "Used for collision detection."))
(:documentation "Used to detect 2D collision. Assumes, that method DISTANCE is definde for this objcect"))
;;;;;;;;;;;;;;;;;;;;;;
;; CIRCLE COLLISION ;;
;;;;;;;;;;;;;;;;;;;;;;
(defclass collidable2-circle ()
((collision-radius :initform 0 ;; 0 pixels
:initarg :collision-radius
:accessor collision-radius
:documentation "Used for collision detection."))
(:documentation "Used to detect 2D collision. Assumes, that method DISTANCE is definde for this objcect"))
;; collision for circle with circle
(defmethod collide-p ((a collidable2-circle) (b collidable2-circle))
(> (+ (collision-radius a)
(collision-radius b))
(distance (coords a)
(coords b))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PLAYER IMPLEMENTATION ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass player (movable collidable2-circle killable)
((coords :initform (vec2 100 100)
:accessor coords)
(collision-radius :initform 5)
(heading :initform (vec2 0 0))
(speed :initform 5)
(hp :initform 3
:accessor hp)
(score :initform 0
:accessor score)
(weapon :initform (make-instance 'player-bullet-spawner)
:accessor weapon))
(:documentation "Represents player. Inherits X, Y from VEC2, collidable."))
(defmethod display ((player player))
(draw-text "'()" (coords player)
:fill-color (color :aqua)
:font *font*))
(defmethod update ((player player))
(with-slots (hp dead weapon) player
(when (<= hp 0)
(setf dead t)
(setf weapon nil)))
(move player))
(defmethod move-to ((player player) (where vec2))
(setf (coords player)
where))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; hp bonus implementation ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass hp-box (movable collidable2-circle killable)
((coords :initarg :coords
:accessor coords)
(collision-radius :initform 5)
(heading :initform (vec2 -1 0))
(speed :initform 2)))
(defmethod update ((box hp-box))
(move box))
(defmethod display ((box hp-box))
(draw-text "<3"
(coords box)
:font *font*
:fill-color (color :pink)))
;;;;;;;;;;;;;;;;;;;;;;;;;
;; BOTS IMPLEMENTATION ;;
;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass bot (movable collidable2-circle killable spawner)
((coords :initform (vec2 *canvas-width*
(random *canvas-width*))
:initarg :coords)
(heading :initform (vec2 -1 0)
:initarg :heading)
(speed :initform (+ 1 (random 2)))
(collision-radius :initform 5)
(initial :initform (+ 60 (random 300)))
(hp :initform (+ 1 (random 2)))))
(defmethod maybe-spawn ((bot bot))
(make-instance 'bot-bullet
:heading (vec2 -1 0)
:coords (add (coords bot)
(vec2 -6 0))))
(defmethod update ((bot bot))
(with-slots (coords heading hp dead) bot
(when (<= hp 0)
(setf dead t)
(incf (score *player*)))
(unless dead
(move bot))
(when (< (x coords) 0)
(setf dead t))))
(defmethod display ((bot bot))
(draw-text "@"
(coords bot)
:fill-color (color :red)
:font *font*))
;;;;;;;;;;;;;;;;;;;;;;;;;;
;; STARS IMPLEMENTATION ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass star (movable killable)
((coords :initform (vec2 *canvas-width*
(random *canvas-width*))
:initarg :coords)
(heading :initform (vec2 -1 0)
:initarg :heading)
(speed :initform (+ 1 (random 2)))))
(defmethod update ((star star))
(with-slots (coords heading dead) star
(unless dead
(move star))
(when (< (x coords) 0)
(setf dead t))))
(defmethod display ((star star))
(draw-text "*"
(coords star)
:font *font*
:fill-color (color :grey)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BULLET IMPLEMENTATION ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass bullet (movable killable collidable2-circle)
((coords :initarg :coords
:accessor coords)
(heading :initarg :heading
:accessor heading)
(collision-radius :initform 5)
(speed :initform 6)))
(defmethod display ((bullet bullet))
(draw-text "->"
(coords bullet)
:fill-color (color :pink)
:font *font*))
(defmethod update ((bullet bullet))
(with-slots (coords heading dead) bullet
(unless dead
(move bullet))
(when (> (x coords) *canvas-width*)
(setf dead t))))
(defclass player-bullet (bullet) ())
(defclass bot-bullet (bullet) ())
(defmethod display ((bullet bot-bullet))
(draw-text "<-"
(coords bullet)
:fill-color (color :red)
:font *font*))
;;;;;;;;;;;;;;;;;;;;;;;
;; SPAWNER INTERFACE ;;
;;;;;;;;;;;;;;;;;;;;;;;
(defgeneric maybe-spawn (spawner)
(:documentation "Indeterministically spawns an object."))
(defclass spawner (counter)
((initial :initform 100)
(remains :initform 5)
(x-start :initarg :x-start)
(x-end :initarg :x-end)
(y-start :initarg :y-start)
(y-end :initarg :y-end)))
(defmethod maybe-spawn :around ((spawner spawner))
(with-slots (x-start x-end y-start y-end cooldown initial delta) spawner
(counter-tick spawner)
(when (counter-elapsed-p spawner)
(progn
(counter-reset spawner)
(call-next-method)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BOT SPAWNER IMPLEMENTATION ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass bot-spawner (spawner) ())
(defmethod maybe-spawn ((spawner bot-spawner))
(with-slots (x-start x-end y-start y-end cooldown initial delta) spawner
(decf initial (/ 1 (+ 1 (score *player*))))
(make-instance 'bot
:coords (vec2 (+ x-start (random (+ 1 (- x-end x-start))))
(+ y-start (random (+ 1 (- y-end y-start))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; STAR SPAWNER IMPLEMENTATION ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass star-spawner (spawner)
((initial :initform 25)))
(defmethod maybe-spawn (obj) nil)
(defmethod maybe-spawn ((spawner star-spawner))
(with-slots (x-start x-end y-start y-end cooldown initial delta) spawner
(make-instance 'star
:coords (vec2 (+ x-start (random (+ 1 (- x-end x-start))))
(+ y-start (random (+ 1 (- y-end y-start))))))))
;;;;;;;;;;;;;;;;;;;;
;; BULLET SPAWNER ;;
;;;;;;;;;;;;;;;;;;;;
(defclass player-bullet-spawner (spawner)
((initial :initform 30)))
(defmethod maybe-spawn ((spawner player-bullet-spawner))
(with-slots (x-start x-end y-start y-end cooldown initial delta) spawner
(make-instance 'player-bullet
:heading (vec2 1 0)
:coords (add (coords *player*)
(vec2 6 0)))))
;;;;;;;;;;;;;;;;
;; HP SPAWNER ;;
;;;;;;;;;;;;;;;;
(defclass hp-spawner (spawner)
((initial :initform 720)))
(defmethod maybe-spawn ((spawner hp-spawner))
(with-slots (x-start x-end y-start y-end cooldown initial delta) spawner
(make-instance 'hp-box
:coords (vec2 (+ x-start (random (+ 1 (- x-end x-start))))
(+ y-start (random (+ 1 (- y-end y-start))))))))
;;;;;;;;;;;;;;;;
;; COLLISIONS ;;
;;;;;;;;;;;;;;;;
(defmethod collide ((player player) (bot bot))
(decf (hp *player*))
(setf (dead bot) t))
(defmethod collide ((bot bot) (player player))
(collide player bot))
(defmethod collide ((bot bot) (bot2 bot)))
(defmethod collide ((bullet player-bullet) (bot bot))
(setf (dead bullet) t)
(with-slots (hp) bot
(decf hp)))
(defmethod collide ((bot bot) (bullet player-bullet))
(collide bullet bot))
(defmethod collide ((player player) (bullet player-bullet))
)
(defmethod collide ((bullet player-bullet) (player player))
)
(defmethod collide ((bullet bot-bullet) (player player))
(with-slots (hp) player
(decf hp))
(setf (dead bullet) t))
(defmethod collide ((player player) (bullet bot-bullet))
(collide bullet player))
(defmethod collide ((bot-bullet bot-bullet) (player-bullet player-bullet))
(setf (dead player-bullet) t
(dead bot-bullet) t))
(defmethod collide ((bot-bullet bot-bullet) (bot bot))
)
(defmethod collide ((bot bot) (bot-bullet bot-bullet))
)
(defmethod collide ((player player) (hp-box hp-box))
(with-slots (hp) player
(incf hp)
(with-slots (dead) hp-box
(setf dead t))))
(defmethod collide ((hp-box hp-box) (player player))
(collide player hp-box))