/
logic.js
241 lines (196 loc) · 5.3 KB
/
logic.js
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
var model = require('./model')
var inspect = require('util').inspect
module.exports = api
function isFunction (f) {
return 'function' === typeof f
}
function isObject (o) {
return 'object' === typeof o
}
var timer = null
//gonna
var RADIUS = 50
function left(r, radius) {
r = model.get(r)
return r.get('x') - (radius || RADIUS) / 2
}
function right(r, radius) {
r = model.get(r)
return r.get('x') + (radius || RADIUS) / 2
}
function dist (a, b) {
a = a.get ? a : model.get(a)
b = b.get ? b : model.get(b)
var x = a.get('x') - b.get('x')
var y = a.get('y') - b.get('y')
var l = Math.sqrt(x*x+y*y)
// console.log("x", x, "y", y, a.state, b.state)
return {x: x, y: y, length: l}
}
model.on('row_update', function detect () {
if(timer)
return
//detect collisions.
var rows = model.rows
//sort by x
var sorted = Object.keys(rows).sort(function (a, b) {
left(a) - left(b)
})
//iterate over sorted..
sorted.forEach(function (e, i) {
//check things that maybe intersect.
var j = i + 1
//sort the elements by their left bound,
//so it is easy to check whether there has been a collision.
while(sorted[j] && left(sorted[j]) < right(sorted[i])) {
if(dist(sorted[i], sorted[j]).length < RADIUS*2) {
model.emit('touch', sorted[i], sorted[j])
}
j ++
}
})
// setTimeout(detect, 50)
})
function api (row) {
function safe (fun) {
return function () {
console.log(JSON.stringify([ 'start', row.id ]))
var f
try {
f = fun.apply(this, arguments)
} catch (err) {
console.log(JSON.stringify([ 'error', row.id ]))
//TODO: present this back to the user somehow...
//or, make the entity die.
row.set('message', {text: err.toString().toUpperCase(), stroke: 'red', fill: 'black'})
console.error('user-error', err.toString().toUpperCase())
return
}
console.log(JSON.stringify([ 'end', row.id ]))
return f
}
}
var thinker = null, hearer = null
function createListener (event, wrapper) {
wrapper = wrapper || function (l) {
return l
}
var _listener
return function (listener) {
model.removeListener(event, listener)
if(isFunction(listener)) {
var wrapped =
model.on(event, _listener = safe(wrapper(listener)))
}
return self
}
}
var self = {
id: function () {
return row.id
},
whatDist: function (other) {
other = model.rows[other]
if(!other) return
var coord = dist(other, row)
// console.log("c", coord)
return coord
},
set: function () {
return row.set.apply(row, arguments)
},
get: function () {
return row.get.apply(row, arguments)
},
say: function (text, opts) {
if(!opts) opts = {}
if(typeof text === 'object') {
opts = text
}
else opts.text = text
if(opts.text) {
if(String(opts.text).length > 140)
opts.text = String(text).substring(0, 140)
row.set('message', opts)
model.emit('say', text, self.id())
}
return self
},
move: function (x, y) {
if(isObject(x))
y = x.y, x=x.x
//check for max X,Y
var _x = row.get('x') + x
, _y = row.get('y') + y;
// console.log('['+_x+', '+_y+']--'+row.get('id'))
row.set('x', _x)
row.set('y', _y)
return self
},
think: function (think) {
clearInterval(thinker)
//console.log('THINK')
//depending on how 'smart' the entity is,
//CURRENTLY, just hard code to 500 ms
if(isFunction(think))
thinker = setInterval(safe(think), 500)
return self
},
hear: createListener('say', function (f) {
return function (message, id) {
if(self.whatDist(id).length < 1000 && id !== self.id()) {
f(message, id)
}
}
}),
touch: createListener('touch', function (fun) {
return function (a, b) {
if (row.id == a) fun(b)
else
if (row.id == b) fun(a)
}
}),
bless: function (id, atm) {
//transfer energy to another ID
amt = amt || 1
var other = model.rows[id], back
if(!other) return
//transfer energy to the other...
//other.set('energy', other.get('energy') + amt)
if(isFunction(other._cursed))
back = other._cursed(self.id(), amt) || 0
else
//just give in if you don't refuse.
back = amt * -1
amt = amt * Math.random()
back = back * Math.random()
//okay, you can attack + or -
//what if the user gives in?
//
var diff
if(amt > 0 && back > 0) {
//the winner gains the diff,
//the looser looses the diff
diff = amt - back
} else {
diff = (amt - back) / 2
}
row.set('energy', row.get('energy') + diff)
energy.set('energy', energy.get('energy') + diff)
},
//a curse is just a negative blessing.
curse: function (id, amt) {
return self.bless(id, (amt || 1) * -1)
},
cursed: function (func) {
//when you are cursed, curse them back.
//or bless them back... whatever...
row._cursed = safe(func)
}
}
//prevent context reassign
for (var k in self) {
self[k] = self[k].bind(self)
}
return self
}