forked from concord-consortium/agentscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtles.html
263 lines (173 loc) · 11.5 KB
/
turtles.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
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
<!DOCTYPE html>
<html>
<head>
<title>turtles.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>turtles.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h3 id="turtles">Turtles</h3>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Class Turtles is a subclass of AgentSet which stores instances of Turtle or
Breeds, which are subclasses of Turtle</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Turtles</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AgentSet</span></span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Constructor creates the empty AgentSet instance and installs
the agentClass (breed) variable shared by all the turtles in this set.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">constructor</span>: <span class="hljs-function">-></span> <span class="hljs-comment"># model, agentClass, name, mainSet</span>
<span class="hljs-keyword">super</span> <span class="hljs-comment"># call super with all the args I was called with</span></pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>@useSprites = false</p>
</div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Have turtles cache the links with them as a node.
Optimizes Turtle a.myLinks method. Call before any turtles created.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">cacheLinks</span>: <span class="hljs-function">-></span> <span class="hljs-property">@agentClass</span>::cacheLinks = <span class="hljs-literal">true</span> <span class="hljs-comment"># all turtles, not individual breeds</span></pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Use sprites rather than drawing</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setUseSprites</span>: <span class="hljs-function"><span class="hljs-params">(useSprites=<span class="hljs-literal">true</span>)</span> -></span>
u.deprecated <span class="hljs-string">'Turtles.setUseSprites: use turtles.setDefault("useSprites",bool)'</span>
<span class="hljs-property">@setDefault</span>(<span class="hljs-string">"useSprites"</span>, useSprites)</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Factory: create num new turtles stored in this agentset. The optional init
function is called on the new turtle after inserting in its agentset.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">create</span>: <span class="hljs-function"><span class="hljs-params">(num, init = ->)</span> -></span> <span class="hljs-comment"># returns array of new turtles too</span>
(<span class="hljs-function"><span class="hljs-params">(o)</span> -></span> init(o); o) <span class="hljs-property">@add</span> <span class="hljs-keyword">new</span> <span class="hljs-property">@agentClass</span> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">1.</span>.num] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span> <span class="hljs-comment"># too tricky?</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Remove all turtles from set via turtle.die()
Note call in reverse order to optimize list restructuring.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">clear</span>: <span class="hljs-function">-></span> <span class="hljs-property">@last</span>().die() <span class="hljs-keyword">while</span> <span class="hljs-property">@any</span>(); <span class="hljs-literal">null</span> <span class="hljs-comment"># tricky, each die modifies list</span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Filter to return all instances of this breed. Note: if used by
the mainSet, returns just the turtles that are not subclassed breeds.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">breedsIn</span>: <span class="hljs-function"><span class="hljs-params">(array)</span> -></span> <span class="hljs-property">@asSet</span> (o <span class="hljs-keyword">for</span> o <span class="hljs-keyword">in</span> array <span class="hljs-keyword">when</span> o.breed <span class="hljs-keyword">is</span> @)</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Return an agentset of this breed within the patch array</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inPatches</span>: <span class="hljs-function"><span class="hljs-params">(patches)</span> -></span>
array = []
array.push p.turtlesHere()... <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> patches <span class="hljs-comment"># concat measured slower</span></pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Possibly faster to filter by breed in p.breedsHere?</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> <span class="hljs-property">@mainSet</span>? <span class="hljs-keyword">then</span> <span class="hljs-property">@breedsIn</span> array <span class="hljs-keyword">else</span> <span class="hljs-property">@asSet</span> array</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Return an agentset of turtles/breeds within the patchRect, dx/y integers</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inRect</span>: <span class="hljs-function"><span class="hljs-params">(p, dx, dy=dx)</span> -></span>
rect = <span class="hljs-property">@model</span>.patches.patchRect p, dx, dy <span class="hljs-comment">#, true</span>
<span class="hljs-property">@inPatches</span> rect</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Return the members of this agentset that are within radius distance
from me, using patch topology</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inRadius</span>: <span class="hljs-function"><span class="hljs-params">(agent, radius)</span> -></span>
as = <span class="hljs-property">@inRect</span>( (agent.p ? agent), Math.ceil(radius) )
as.inRadius agent, radius</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Return the members of this agentset that are within radius distance
from agent, and within cone of angle, heading from agent
using patch topology</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">inCone</span>: <span class="hljs-function"><span class="hljs-params">(agent, radius, angle, heading)</span> -></span>
as = <span class="hljs-property">@inRect</span>( (agent.p ? agent), Math.ceil(radius) )
as.inCone agent, radius, angle, heading
<span class="hljs-attribute">setDraggable</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@on</span> <span class="hljs-string">'dragstart'</span>, <span class="hljs-function"><span class="hljs-params">(mouseEvent)</span> =></span>
mouseEvent.target.dragging = <span class="hljs-literal">true</span>
<span class="hljs-property">@on</span> <span class="hljs-string">'dragend'</span>, <span class="hljs-function"><span class="hljs-params">(mouseEvent)</span> =></span>
mouseEvent.target.dragging = <span class="hljs-literal">false</span>
<span class="hljs-property">@on</span> <span class="hljs-string">'drag'</span>, <span class="hljs-function"><span class="hljs-params">(mouseEvent)</span> =></span>
mouseEvent.target.setXY(mouseEvent.patchX, mouseEvent.patchY)</pre></div></div>
</li>
</ul>
</div>
</body>
</html>