-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtutorial-collisions-many-to-one.html
119 lines (108 loc) · 5 KB
/
tutorial-collisions-many-to-one.html
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
<html>
<head>
<title>DragonRuby Game Toolkit - Collisions: Many to One</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 - Collisions: Many to One</h1>
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="1">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>Collisions: Many to One</h1>
<p>The small squares collide with the center square. The more collisions, the more red the center square gets.</p>
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
def tick args
# SET DEFAULTS ==============================
# Center body's values are set using an array
# All bodies that intersect with center body
# are stored in collisions collection
# calculations done to place body in center
args.state.target ||= {
x: args.grid.w.half - 25,
y: args.grid.h.half - 25,
w: 50,
h: 50,
collisions: []
}
# Numeric#map is used to set values of 50
# other bodies
# 50 bodies given random position on screen
args.state.squares ||= 50.map do
[args.grid.w * rand,
args.grid.h * rand,
3,
3]
end
# RENDER =============================
# transparency changes based on number
# of collisions the more collisions,
# the redder (more transparent) the
# box becomes
alpha = args.state
.target[:collisions]
.length * 5
args.solids << (args.state
.target
.merge r: 255,
g: 0,
b: 0,
a: alpha)
# outputs center body as a black border
args.borders << args.state.target
# other bodies are output as (black) solids
args.solids << args.state.squares
# MOVE BODIES =============================
# Bodies are returned to bottom left corner
# if positions exceed scope of screen
# for each body in the other_bodies collection
args.state.squares.each do |b|
# x and y are both incremented by 1
b.x += 1
b.y += 1
# x becomes 0 if star exceeds scope
# of screen (goes too far right)
# y becomes 0 if star exceeds scope
# of screen (goes too far up)
b.x = 0 if b.x > args.grid.w
b.y = 0 if b.y > args.grid.h
end
# PROCESS COLLISIONS =================
# finds all bodies that intersect with
# center body, stores them in collisions
args.state.target[:collisions] =
args.state
.squares
.find_all do |b|
args.state
.target
.intersect_rect? b
end
end
$gtk.reset
</code>
</pre>
</div>
</div>
<footer id="bottom"></footer>
<script type="text/template-sample">
<div itemscope="itemscope" itemtype="tutorial-step" data-step-number="STEP_NUMBER">
<div itemscope="itemscope" itemtype="tutorial-text">
<h1>STEP_TITLE</h1>
<p>STEP_TEXT</p>
</div>
<div itemscope="itemscope" itemtype="tutorial-code">
<pre>
<code class="language-ruby" itemprop="text">
# STEP_CODE
</code>
</pre>
</div>
</div>
</script>
</body>
</html>