-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskill.lisp
247 lines (221 loc) · 8.09 KB
/
skill.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
;;;; skill.lisp
(in-package :com.eliasfeijo.pong)
(defparameter +delay-red-skill+ 1.0)
(defparameter +delay-green-skill+ 2.0)
(defparameter +delay-blue-skill+ 2.5)
(define-image 'red-skill "images/skill-red.png")
(define-image 'green-skill "images/skill-green.png")
(define-image 'blue-skill "images/skill-blue.png")
(defparameter *anim-red-skill* (make-animation
'((0 0 50 50 0)
(50 0 100 50 0.1)
(100 0 150 50 0.2)
(150 0 200 50 0.3)
(200 0 250 50 0.4)
(250 0 300 50 0.5)
(300 0 350 50 0.6)
(350 0 400 50 0.7)
(400 0 450 50 0.8)
(450 0 500 50 0.9))
:looped-p t))
(defparameter *anim-green-skill* (make-animation
'((0 0 30 10 0)
(30 0 60 10 0.25)
(60 0 90 10 0.5))
:looped-p t))
(defparameter *anim-blue-skill* (make-animation
'((0 0 100 100 0)
(100 0 200 100 0.1)
(200 0 300 100 0.2)
(300 0 400 100 0.3)
(400 0 500 100 0.4)
(500 0 600 100 0.5)
(600 0 700 100 0.6)
(700 0 800 100 0.7)
(800 0 900 100 0.8)
(900 0 1000 100 0.9)
(1000 0 1100 100 1.0)
(1100 0 1200 100 1.1)
(1200 0 1300 100 1.2)
(1300 0 1400 100 1.3)
(1400 0 1500 100 1.4)
(1500 0 1600 100 1.5)
(1600 0 1700 100 1.6)
(1700 0 1800 100 1.7))
:looped-p t))
(defclass skill (positionable renderable)
((target :initarg :target :initform nil)
(size :initarg :size :initform nil :accessor size-of)
(speed :initarg :speed :initform nil :accessor speed-of)
(fill-color :initarg :fill-color :initform nil :accessor fill-color-of)
(collided-p :initform nil :reader collided-p)
(flipped-p :initarg :flipped-p :initform nil)))
(defgeneric update-skill (skill player1 player2 ball delta-time))
(defgeneric move-skill (skill direction delta-time &key &allow-other-keys))
(defmethod render ((this skill))
(with-slots (position size fill-color) this
(draw-rect position (x size) (y size) :fill-paint fill-color)))
(defun center-of (skill)
(let ((position (position-of skill))
(size (size-of skill)))
(vec2
(+ (x position) (/ (x size) 2))
(+ (y position) (/ (y size) 2)))))
;;; Red skill
(defclass red-skill (skill)
((size :initform (vec2 25 25))
(speed :initform 700)
(fill-color :initform (vec4 1 0 0 1))))
(defmethod initialize-instance :after ((this red-skill) &key)
(with-slots (position size) this
(setf position
(vec2
(x position)
(- (y position) (/ (y size) 2))))))
(defmethod render ((this red-skill))
(with-slots (position size fill-color flipped-p) this
(let* ((frame (get-frame *anim-red-skill* (real-time-seconds)))
(origin (keyframe-origin frame))
(end (keyframe-end frame)))
(with-pushed-canvas ()
(scale-canvas 0.5 0.5)
(if flipped-p
(with-pushed-canvas ()
(scale-canvas -1 1)
(draw-image (vec2 (* (+ (- (x position)) (- (x size))) 2) (* (y position) 2)) 'red-skill
:origin origin
:width (- (x end) (x origin))
:height (- (y end) (y origin))))
(draw-image (vec2 (* (x position) 2) (* (y position) 2)) 'red-skill
:origin origin
:width (- (x end) (x origin))
:height (- (y end) (y origin))))))))
(defmethod update-skill ((this red-skill) player1 player2 ball delta-time)
(with-slots (target speed collided-p) this
(if (eql target 'player1)
(progn
(move-skill this 'left delta-time)
(if (colliding-with this player1)
(let ((burn (make-instance 'burning :target player1)))
(setf collided-p t)
(push-effect player1 burn))))
(progn
(move-skill this 'right delta-time)
(if (colliding-with this player2)
(let ((burn (make-instance 'burning :target player2)))
(setf collided-p t)
(push-effect player2 burn)))))))
(defmethod move-skill ((this red-skill) direction delta-time &key)
(with-slots (speed position) this
(let ((real-speed (* speed delta-time)))
(if (eql direction 'left)
(setf (x position) (- (x position) real-speed))
(setf (x position) (+ (x position) real-speed))))))
;;; Green skill
(defclass green-skill (skill)
((size :initform (vec2 30 10))
(speed :initform 600)
(speed-y :initform 350)
(fill-color :initform (vec4 0 1 0 1))))
(defmethod initialize-instance :after ((this green-skill) &key)
(with-slots (position size) this
(setf position
(vec2
(x position)
(- (y position) (/ (y size) 2))))))
(defmethod render ((this green-skill))
(with-slots (position size fill-color flipped-p) this
(let* ((frame (get-frame *anim-green-skill* (real-time-seconds)))
(origin (keyframe-origin frame))
(end (keyframe-end frame)))
(if flipped-p
(with-pushed-canvas ()
(scale-canvas -1 1)
(draw-image (vec2 (+ (- (x position)) (- (x size))) (y position)) 'green-skill
:origin origin
:width (- (x end) (x origin))
:height (- (y end) (y origin))))
(draw-image position 'green-skill
:origin origin
:width (- (x end) (x origin))
:height (- (y end) (y origin)))))))
(defmethod update-skill ((this green-skill) player1 player2 ball delta-time)
(with-slots (target speed collided-p) this
(if (eql target 'player1)
(progn
(move-skill this 'left delta-time :player1 player1 :player2 player2)
(if (colliding-with this player1)
(let ((slow (make-instance 'slow :target player1)))
(setf collided-p t)
(push-effect player1 slow))))
(progn
(move-skill this 'right delta-time :player1 player1 :player2 player2)
(if (colliding-with this player2)
(let ((slow (make-instance 'slow :target player2)))
(setf collided-p t)
(push-effect player2 slow)))))))
(defmethod move-skill ((this green-skill) direction delta-time &key player1 player2)
(with-slots (speed speed-y position) this
(let ((real-speed (* speed delta-time))
(real-speed-y (* speed-y delta-time)))
(if (eql direction 'left)
(let ((target (y (center-of player1))))
(setf (x position) (- (x position) real-speed))
(if (< (y position) (- target 25))
(setf (y position) (+ (y position) real-speed-y))
(if (> (y position) (+ target 25))
(setf (y position) (- (y position) real-speed-y)))))
(let ((target (y (center-of player2))))
(setf (x position) (+ (x position) real-speed))
(if (< (y position) (- target 25))
(setf (y position) (+ (y position) real-speed-y))
(if (> (y position) (+ target 25))
(setf (y position) (- (y position) real-speed-y)))))))))
;;; Blue skill
(defclass blue-skill (skill)
((size :initform (vec2 20 100))
(speed :initform 400)
(fill-color :initform (vec4 0 0 1 1))))
(defmethod initialize-instance :after ((this blue-skill) &key)
(with-slots (position size) this
(setf position
(vec2
(x position)
(- (y position) (/ (y size) 2))))))
(defmethod render ((this blue-skill))
(with-slots (position size fill-color flipped-p) this
(let* ((frame (get-frame *anim-blue-skill* (real-time-seconds)))
(origin (keyframe-origin frame))
(end (keyframe-end frame)))
(if flipped-p
(with-pushed-canvas ()
(scale-canvas -1 1)
(draw-image (vec2 (+ (- (x position)) (- (x size)) -80) (y position)) 'blue-skill
:origin origin
:width (- (x end) (x origin))
:height (- (y end) (y origin))))
(draw-image (vec2 (- (x position) 80) (y position)) 'blue-skill
:origin origin
:width (- (x end) (x origin))
:height (- (y end) (y origin)))))))
(defmethod update-skill ((this blue-skill) player1 player2 ball delta-time)
(with-slots (target speed collided-p) this
(if (eql target 'player1)
(progn
(move-skill this 'left delta-time)
(if (colliding-with this ball)
(let ((slip (make-instance 'slip :target ball)))
(setf collided-p t)
(push-effect ball slip))))
(progn
(move-skill this 'right delta-time)
(if (colliding-with this ball)
(let ((slip (make-instance 'slip :target ball)))
(setf collided-p t)
(push-effect ball slip)))))))
(defmethod move-skill ((this blue-skill) direction delta-time &key)
(with-slots (speed position) this
(let ((real-speed (* speed delta-time)))
(if (eql direction 'left)
(setf (x position) (- (x position) real-speed))
(setf (x position) (+ (x position) real-speed))))))