-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtutorial-traveling-at-light-speed.html
More file actions
350 lines (321 loc) · 15.1 KB
/
tutorial-traveling-at-light-speed.html
File metadata and controls
350 lines (321 loc) · 15.1 KB
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
<html>
<head>
<title>DragonRuby - Warp Drive Tutorial</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/default.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/highlight.min.js"></script>
<script src="tutorial.js"></script>
<link rel="stylesheet" href="tutorial.css" />
</head>
<body>
<h1>DragonRuby Game Toolkit - Warp Drive Tutorial</h1>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="1">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Traveling at Light Speed</h1>
In this tutorial, we'll be creating a movement effect that makes it look like you're traveling at light speed.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
# entry point to game
def tick args
# set the game's background color
# try changing these numbers and seeing how
# the background color changes
args.outputs.background_color = [40, 44, 52]
end
# reset the game on save
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="2">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Drawing Crosshairs</h1>
We are going to set our coordinate system to <code>origin_center</code> which means that <code>0, 0</code> is in the center of the screen.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
# set the coordinate system to have the
# origin in the center
args.grid.origin_center!
# draw a horizontal and vertical
# line through the origin
# horizontal line
args.outputs.lines << [ args.grid.left, # X
0, # Y
args.grid.right, # X2
0] # Y2
# vertical line
args.outputs.lines << [ 0, # X
args.grid.top, # Y
0, # X2
args.grid.bottom] # Y2
args.outputs.background_color = [40, 44, 52]
end
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="3">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Initializing State</h1>
We'll use RNG, and DragonRuby Game Toolkit's <code>Numeric#vector_(x|y)</code>
function to create 100 points around the origin.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
# set the coordinate system to have the
# origin in the center
args.grid.origin_center!
# set the radius of the warp tunnel
# this area will be void of stars
args.state.warp_tunnel_size = 10;
# set stars to 100 stars by first
args.state.stars ||= 100.map do |i|
# generating 100 random angles between
# 0 and 360
rand(360)
end.map do |random_angle|
# create a Hash with initial
# starting locations, color
# and speed (between 3 and 13 pixels)
{ initial_x: random_angle.vector_x *
args.state.warp_tunnel_size,
initial_y: random_angle.vector_y *
args.state.warp_tunnel_size,
angle: random_angle,
r: 128,
g: rand(128) + 128,
b: rand(128) + 128,
s: rand(13) + 3 }
end.map do |star|
# using the initial hash
# generate the the starting
# position as a pixel
star.merge x: star[:initial_x],
y: star[:initial_y],
w: 1,
h: 1
end
# black background color
args.outputs.background_color = [0, 0, 0]
# render the stars as solid pixels
args.outputs.solids << args.state.stars
end
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="4">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Moving Things</h1>
Now that we have the stars created. Let's move them (we take this opportunity to refactor
the <code>tick</code> method into multiple parts).
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
# refactor the code to seperate
# methods
def tick args
# initialize default values
defaults args
# render the game
render args
# calculate the next step of the game
calc args
end
def calc args
# for each star move it along the
# normal vector denoted by its angle
args.state.stars.each do |star|
star[:x] += star[:angle].vector_x * star[:s]
star[:y] += star[:angle].vector_y * star[:s]
end
end
def render args
args.outputs.background_color = [0, 0, 0]
args.outputs.solids << args.state.stars
end
def defaults args
args.grid.origin_center!
args.state.warp_tunnel_size = 10;
args.state.stars ||= 100.map do |i|
rand(360)
end.map do |random_angle|
{ initial_x: random_angle.vector_x *
args.state.warp_tunnel_size,
initial_y: random_angle.vector_y *
args.state.warp_tunnel_size,
angle: random_angle,
r: 128,
g: rand(128) + 128,
b: rand(128) + 128,
s: 0.1 + rand * 2 }
end.map do |star|
star.merge x: star[:initial_x],
y: star[:initial_y],
w: 1,
h: 1
end
end
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="5">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Reset Initial Star State</h1>
There was a sudden explosion of movement, and then nothing.
Once a star is off the screen, we'll want to reset its
position back to its starting point.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
defaults args
render args
calc args
end
def calc args
args.state.stars.each do |star|
star[:x] += star[:angle].vector_x * star[:s]
star[:y] += star[:angle].vector_y * star[:s]
end
args.state.stars.find_all do |star|
# find all stars that no longer
# intersect the game's render area
!star.intersect_rect? args.grid.rect
end.each do |star|
# for those stars, reset them back to their
# original position
star[:x] = star[:initial_x]
star[:y] = star[:initial_y]
end
end
def render args
args.outputs.background_color = [0, 0, 0]
args.outputs.solids << args.state.stars
end
def defaults args
args.grid.origin_center!
args.state.warp_tunnel_size = 10;
args.state.stars ||= 100.map do |i|
rand(360)
end.map do |random_angle|
{ initial_x: random_angle.vector_x *
args.state.warp_tunnel_size,
initial_y: random_angle.vector_y *
args.state.warp_tunnel_size,
angle: random_angle,
r: 128,
g: rand(128) + 128,
b: rand(128) + 128,
s: 0.1 + rand * 2 }
end.map do |star|
star.merge x: star[:initial_x],
y: star[:initial_y],
w: 1,
h: 1
end
end
$gtk.reset
</code>
</pre>
</div>
</div>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="6">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>3D Effects</h1>
You can give the movement a bit more depth by accelerating the
star and increasing its size over time. This is the final result.
Play around with the properties and see how altering the numbers gives
you different results.
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
defaults args
render args
calc args
end
def calc args
args.state.stars.each do |star|
star[:x] += star[:angle].vector_x * star[:s]
star[:y] += star[:angle].vector_y * star[:s]
# track the distance the star has moved
star[:d] += star[:s]
# increase the speed of the star by 20%
star[:s] *= 1.2
# change to width and height of the
# star to grow as the distance increases
# as if it's moving towards you
star[:w] = 1 + 15 * star[:d].fdiv(100)
star[:h] = 1 + 15 * star[:d].fdiv(100)
star[:x] += star[:angle].vector_x *
star[:w].fdiv(2)
star[:y] += star[:angle].vector_y *
star[:h].fdiv(2)
end
args.state.stars.find_all do |star|
!star.intersect_rect? args.grid.rect
end.each do |star|
# reset stars, but initialize
# them with a new random speed
star[:x] = star[:initial_x]
star[:y] = star[:initial_y]
star[:s] = 0.1 * rand
star[:d] = 0
star[:w] = 1
star[:h] = 1
end
end
def render args
args.outputs.background_color = [0, 0, 0]
args.outputs.solids << args.state.stars
end
def defaults args
args.grid.origin_center!
args.state.warp_tunnel_size = 10;
args.state.stars ||= 100.map do |i|
rand(360)
end.map do |random_angle|
{ initial_x: random_angle.vector_x *
args.state.warp_tunnel_size,
initial_y: random_angle.vector_y *
args.state.warp_tunnel_size,
angle: random_angle,
r: 128,
g: rand(128) + 128,
b: rand(128) + 128 }
end.map do |star|
star.merge x: star[:initial_x],
y: star[:initial_y],
w: 1, h: 1,
# make the speed a very small
# random number
s: 0.1 * rand,
# initialize a property
# to keep track of distance
# traveled
d: 0
end
end
$gtk.reset
</code>
</pre>
</div>
</div>
<footer id="bottom"></footer>
</body>
</html>