/
asteroid.lisp
254 lines (226 loc) · 8.74 KB
/
asteroid.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
(defvar *width* 800)
(defvar *height* 600)
(defvar *max-speed* 10)
(defvar *ship-size* 25)
(defvar *accel* 0.5)
(defvar *decel* 0.02)
(defvar *angle-step* 0.1)
(defvar *bullet-size* 10)
(defvar *bullet-speed* 10)
(defvar *bullet-life* 50)
(defvar *asteroid-shapes*)
(defvar *min-size* 15)
(defun random-elt (list)
(elt list (random (length list))))
;;; Shapes
(defmacro make-shape (&rest points)
`(list
,@(loop for p in points
collect `(cons ,(car p)
,(cadr p)))))
(defmacro deftrans (name args x y)
(let ((shape (gensym))
(point (gensym)))
`(defmethod ,name (,@args (,shape list))
(mapcar (lambda (,point)
(let ((p-x (car ,point))
(p-y (cdr ,point)))
(cons ,x ,y)))
,shape))))
(deftrans translate (x y)
(+ p-x x)
(+ p-y y))
(deftrans rotate (angle)
(- (* (cos angle) p-x) (* (sin angle) p-y))
(+ (* (sin angle) p-x) (* (cos angle) p-y)))
(deftrans scale (factor)
(* p-x factor)
(* p-y factor))
(defmethod draw ((shape list))
(when shape
(sdl:draw-polygon
(mapcar (lambda (point)
(sdl:point :x (car point) :y (cdr point)))
(cons (car (last shape)) shape)))))
(defmethod print-shape ((shape list))
(princ "(make-shape") (princ #\Newline)
(mapcar (lambda (pt)
(format t " (~a ~a)~%" (car pt) (cdr pt)))
shape)
(princ ")") (princ #\Newline))
(defclass item ()
((x :accessor pos-x :i:nitarg :x :initform 0)
(y :accessor pos-y :initarg :y :initform 0)
(vx :accessor vel-x :initarg :vx :initform 0)
(vy :accessor vel-y :initarg :vy :initform 0)
(direction :accessor dir :initarg :dir :initform 0)
(shape :accessor shape :initform nil)))
(defmethod update :after ((item item))
(setf (pos-x item)
(mod (round (+ (pos-x item) (vel-x item))) *width*))
(setf (pos-y item)
(mod (round (+ (pos-y item) (vel-y item))) *height*)))
(defclass asteroid (item)
((size :accessor size :initarg :size :initform 25)))
(defmethod initialize-instance :after ((asteroid asteroid) &rest initargs)
; TODO: write some macro to avoid repetitions like that
(when (not (find :dir initargs))
(setf (dir asteroid) (random (* 2 pi))))
(when (not (find :x initargs))
(setf (pos-x asteroid) (random *width*)))
(when (not (find :y initargs))
(setf (pos-y asteroid) (random *height*)))
(when (not (find :vx initargs))
(setf (vel-x asteroid) (random *max-speed*)))
(when (not (find :vy initargs))
(setf (vel-y asteroid) (random *max-speed*)))
(setf (shape asteroid) (random-elt *asteroid-shapes*)))
(defmethod draw ((asteroid asteroid))
(draw (translate (pos-x asteroid) (pos-y asteroid)
(scale (size asteroid)
(rotate (dir asteroid) (shape asteroid))))))
(defmethod update ((asteroid asteroid))
(incf (dir asteroid) 0.1))
(defun point-in-circle (x y r point)
(<= (+ (expt (- (car point) x) 2)
(expt (- (cdr point) y) 2))
(expt r 2)))
; TODO: it assume the asteroid as some sort of circular shape, we
; should use better collision detection, like point-in-polygon algorithm
(defmethod collides ((asteroid asteroid) (shape list))
(when shape
(if (point-in-circle (pos-x asteroid) (pos-y asteroid) (size asteroid)
(first shape))
t
(collides asteroid (rest shape)))))
(defmethod collides ((asteroid asteroid) (item item))
(collides asteroid
(translate (pos-x item) (pos-y item)
(rotate (dir item) (shape item)))))
(defmethod next-size ((asteroid asteroid))
(if (< (size asteroid) *min-size*)
0
(/ (size asteroid) 2)))
(defmacro spawn-asteroids (n &rest initargs)
`(loop for i from 1 to ,n
collect (make-instance 'asteroid ,@initargs)))
(defclass bullet (item)
((direction :accessor dir :initarg :dir)
(shape :initform (make-shape (0 0) (*bullet-size* 0)))
(life :accessor life :initform *bullet-life*)))
(defmethod initialize-instance :after ((bullet bullet) &rest initargs)
(declare (ignore initargs))
(setf (vel-x bullet) (* *bullet-speed* (cos (dir bullet))))
(setf (vel-y bullet) (* *bullet-speed* (sin (dir bullet)))))
(defmethod update ((bullet bullet))
(decf (life bullet)))
(defmethod draw ((bullet bullet))
(draw (translate (pos-x bullet) (pos-y bullet)
(rotate (dir bullet) (shape bullet)))))
(defclass ship (item)
((direction :accessor dir :initarg :dir :initform 0)
(shape :initform (make-shape (0 (- (/ *ship-size*) 4))
(0 (/ *ship-size* 4))
(*ship-size* 0)))))
(defmethod update ((ship ship))
(when (sdl:key-held-p :SDL-KEY-UP)
(incf (vel-x ship) (* *accel* (cos (dir ship))))
(incf (vel-y ship) (* *accel* (sin (dir ship)))))
(when (sdl:key-held-p :SDL-KEY-DOWN)
(incf (vel-x ship) (- (* (/ *accel* 8) (vel-x ship))))
(incf (vel-y ship) (- (* (/ *accel* 8) (vel-y ship)))))
(when (sdl:key-held-p :SDL-KEY-RIGHT)
(incf (dir ship) *angle-step*))
(when (sdl:key-held-p :SDL-KEY-LEFT)
(decf (dir ship) *angle-step*))
(incf (vel-x ship) (- (* *decel* (vel-x ship))))
(incf (vel-y ship) (- (* *decel* (vel-y ship)))))
(defmethod draw ((ship ship))
(draw (translate (pos-x ship) (pos-y ship)
(rotate (dir ship)
(shape ship)))))
(defun start ()
(let ((asteroids (spawn-asteroids 4))
(ship (make-instance 'ship
:x (/ *width* 2)
:y (/ *width* 2)
:vx 0
:vy 0))
bullets)
(sdl:with-init ()
(sdl:window *width* *height*)
(sdl:with-events ()
(:quit-event () t)
(:key-down-event (:key key)
(case key
(:SDL-KEY-SPACE
(push (make-instance 'bullet
:x (pos-x ship)
:y (pos-y ship)
:dir (dir ship))
bullets))))
(:idle ()
(sdl:clear-display sdl:*black*)
(mapcar
(lambda (bullet)
(let ((asteroid (first
(remove-if-not (lambda (x) (collides x bullet))
asteroids))))
(when asteroid
; collision between asteroid and bullet
(setf (life bullet) 0) ; destroy the bullet
(let ((new-size (next-size asteroid)))
; decrease the size and spawn new asteroids
(setf (size asteroid) new-size)
(nconc asteroids (spawn-asteroids 1
:x (pos-x asteroid)
:y (pos-y asteroid)
:size new-size))
))))
bullets)
; remove end-of-life'd bullets and zero-sized asteroids
(setf bullets (remove-if-not (lambda (bullet) (> (life bullet) 0))
bullets))
(setf asteroids (remove-if-not (lambda (asteroid)
(> (size asteroid) 0))
asteroids))
(mapcar #'update bullets)
(mapcar #'draw bullets)
(mapcar #'update asteroids)
(mapcar #'draw asteroids)
(when (remove-if-not (lambda (x) (collides x ship)) asteroids)
(setf (pos-x ship) (/ *width* 2))
(setf (pos-y ship) (/ *height* 2))
(setf (vel-x ship) 0)
(setf (vel-y ship) 0))
(update ship)
(draw ship)
(sdl:update-display))))))
(defun shape-editor (&rest shape)
(let* ((ofx (/ *width* 2))
(ofy (/ *height* 2))
(axis-x (make-shape (ofx 0) (ofx *height*)))
(axis-y (make-shape (0 ofy) (*width* ofy))))
(sdl:with-init ()
(sdl:window *width* *height*)
(sdl:with-events ()
(:quit-event () t)
(:mouse-button-down-event (:button button :x x :y y)
(case button
; left click
(1 (push (cons (- x ofx) (- y ofy)) shape))
(2 (format t "~a~%" (contain-point shape
(cons (- x ofx) (- y ofy)))))
; right click
(3 (setf shape nil))
; wheel up
(4 (setf shape (scale 1.1 shape)))
; wheel down
(5 (setf shape (scale 0.9 shape))))
(print-shape shape))
(:idle ()
(sdl:clear-display sdl:*black*)
(draw axis-x)
(draw axis-y)
(draw (translate ofx ofy shape))
(sdl:update-display))))))