Don't move Bobules more than once per step.
Chris Pressey
7 years ago
40 | 40 | |
41 | 41 | Bobule = function() { |
42 | 42 | this.pressure = 1; |
43 | ||
44 | this.move = function(pf, x, y) { | |
43 | this.serial = 0; | |
44 | ||
45 | this.move = function(serial, pf, x, y) { | |
46 | if (this.serial > serial) | |
47 | return; | |
48 | ||
45 | 49 | this.pressure++; |
46 | 50 | |
47 | 51 | var newX = x + (Math.floor(Math.random() * 3) - 1); |
62 | 66 | pf.put(x, y, undefined); |
63 | 67 | this.pressure = 1; |
64 | 68 | pf.put(newX, newY, this); |
69 | this.serial = serial + 1; | |
65 | 70 | }; |
66 | 71 | |
67 | 72 | this.draw = function(ctx, px, py, x, y, w, h) { |
230 | 235 | * This function ensures a particular order. |
231 | 236 | */ |
232 | 237 | this.foreach = function(fun) { |
233 | /* TODO have less knowledge of the innards of yoob.Playfield */ | |
234 | for (var y = this.worldPf.minY; y <= this.worldPf.maxY; y++) { | |
235 | for (var x = this.worldPf.minX; x <= this.worldPf.maxX; x++) { | |
238 | var lowerY = this.getLowerY(); | |
239 | var upperY = this.getUpperY(); | |
240 | var lowerX = this.getLowerX(); | |
241 | var upperX = this.getUpperX(); | |
242 | for (var y = lowerY; y <= upperY; y++) { | |
243 | for (var x = lowerX; x <= upperX; x++) { | |
236 | 244 | var value = this.get(x, y); |
237 | 245 | if (value === undefined) |
238 | 246 | continue; |
287 | 295 | |
288 | 296 | var proto = new yoob.Controller(); |
289 | 297 | WorbController = function() { |
298 | this.init = function(cfg) { | |
299 | proto.init.apply(this, [cfg]); | |
300 | this.pf = new WorbPlayfield().init({}); | |
301 | cfg.view.setPlayfield(this.pf); | |
302 | this.view = cfg.view; | |
303 | this.serial = 0; | |
304 | return this; | |
305 | }; | |
306 | ||
290 | 307 | this.step = function() { |
291 | 308 | var underLoad = false; |
292 | 309 | var pf = this.pf; |
293 | this.view.draw(); | |
294 | pf.foreachBobule(function (x, y, elem) { | |
295 | elem.move(pf, x, y); | |
310 | var serial = this.serial; | |
311 | pf.foreachBobule(function(x, y, elem) { | |
312 | elem.move(serial, pf, x, y); | |
296 | 313 | }); |
297 | pf.foreachWorld(function (x, y, elem) { | |
314 | pf.foreachWorld(function(x, y, elem) { | |
298 | 315 | var b; |
299 | 316 | if (elem instanceof Sink) { |
300 | 317 | b = pf.get(x, y); |
316 | 333 | if (this.onstep !== undefined) { |
317 | 334 | this.onstep(underLoad); |
318 | 335 | } |
336 | this.view.draw(); | |
337 | this.serial++; | |
319 | 338 | }; |
320 | 339 | |
321 | 340 | this.reset = function(text) { |
323 | 342 | this.pf.load(0, 0, text); |
324 | 343 | this.view.draw(); |
325 | 344 | }; |
326 | ||
327 | this.init = function(cfg) { | |
328 | proto.init.apply(this, [cfg]); | |
329 | this.pf = new WorbPlayfield().init({}); | |
330 | cfg.view.setPlayfield(this.pf); | |
331 | this.view = cfg.view; | |
332 | return this; | |
333 | }; | |
334 | 345 | }; |
335 | 346 | WorbController.prototype = proto; |