-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.js
3289 lines (2745 loc) · 79 KB
/
game.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Checks whether an object is an array.
Object.isArray([1, 2, 4])
# => true
Object.isArray({key: "value"})
# => false
@name isArray
@methodOf Object
@param {Object} object The object to check for array-ness.
@returns {Boolean} A boolean expressing whether the object is an instance of Array
*/
var __slice = Array.prototype.slice;
Object.isArray = function(object) {
return Object.prototype.toString.call(object) === "[object Array]";
};
/**
Checks whether an object is a string.
Object.isString("a string")
# => true
Object.isString([1, 2, 4])
# => false
Object.isString({key: "value"})
# => false
@name isString
@methodOf Object
@param {Object} object The object to check for string-ness.
@returns {Boolean} A boolean expressing whether the object is an instance of String
*/
Object.isString = function(object) {
return Object.prototype.toString.call(object) === "[object String]";
};
/**
Merges properties from objects into target without overiding.
First come, first served.
I =
a: 1
b: 2
c: 3
Object.reverseMerge I,
c: 6
d: 4
I # => {a: 1, b:2, c:3, d: 4}
@name reverseMerge
@methodOf Object
@param {Object} target The object to merge the properties into.
@returns {Object} target
*/
Object.defaults = Object.reverseMerge = function() {
var name, object, objects, target, _i, _len;
target = arguments[0], objects = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
for (_i = 0, _len = objects.length; _i < _len; _i++) {
object = objects[_i];
for (name in object) {
if (!target.hasOwnProperty(name)) target[name] = object[name];
}
}
return target;
};
/**
Merges properties from sources into target with overiding.
Last in covers earlier properties.
I =
a: 1
b: 2
c: 3
Object.extend I,
c: 6
d: 4
I # => {a: 1, b:2, c:6, d: 4}
@name extend
@methodOf Object
@param {Object} target The object to merge the properties into.
@returns {Object} target
*/
Object.extend = function() {
var name, source, sources, target, _i, _len;
target = arguments[0], sources = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
source = sources[_i];
for (name in source) {
target[name] = source[name];
}
}
return target;
};
/**
Helper method that tells you if something is an object.
object = {a: 1}
Object.isObject(object)
# => true
@name isObject
@methodOf Object
@param {Object} object Maybe this guy is an object.
@returns {Boolean} true if this guy is an object.
*/
Object.isObject = function(object) {
return Object.prototype.toString.call(object) === '[object Object]';
};
/**
Calculate the average value of an array. Returns undefined if some elements
are not numbers.
[1, 3, 5, 7].average()
# => 4
@name average
@methodOf Array#
@returns {Number} The average (arithmetic mean) of the list of numbers.
*/
var _base,
__slice = Array.prototype.slice;
Array.prototype.average = function() {
return this.sum() / this.length;
};
/**
Returns a copy of the array without null and undefined values.
[null, undefined, 3, 3, undefined, 5].compact()
# => [3, 3, 5]
@name compact
@methodOf Array#
@returns {Array} A new array that contains only the non-null values.
*/
Array.prototype.compact = function() {
return this.select(function(element) {
return element != null;
});
};
/**
Creates and returns a copy of the array. The copy contains
the same objects.
a = ["a", "b", "c"]
b = a.copy()
# their elements are equal
a[0] == b[0] && a[1] == b[1] && a[2] == b[2]
# => true
# but they aren't the same object in memory
a === b
# => false
@name copy
@methodOf Array#
@returns {Array} A new array that is a copy of the array
*/
Array.prototype.copy = function() {
return this.concat();
};
/**
Empties the array of its contents. It is modified in place.
fullArray = [1, 2, 3]
fullArray.clear()
fullArray
# => []
@name clear
@methodOf Array#
@returns {Array} this, now emptied.
*/
Array.prototype.clear = function() {
this.length = 0;
return this;
};
/**
Flatten out an array of arrays into a single array of elements.
[[1, 2], [3, 4], 5].flatten()
# => [1, 2, 3, 4, 5]
# won't flatten twice nested arrays. call
# flatten twice if that is what you want
[[1, 2], [3, [4, 5]], 6].flatten()
# => [1, 2, 3, [4, 5], 6]
@name flatten
@methodOf Array#
@returns {Array} A new array with all the sub-arrays flattened to the top.
*/
Array.prototype.flatten = function() {
return this.inject([], function(a, b) {
return a.concat(b);
});
};
/**
Invoke the named method on each element in the array
and return a new array containing the results of the invocation.
[1.1, 2.2, 3.3, 4.4].invoke("floor")
# => [1, 2, 3, 4]
['hello', 'world', 'cool!'].invoke('substring', 0, 3)
# => ['hel', 'wor', 'coo']
@param {String} method The name of the method to invoke.
@param [arg...] Optional arguments to pass to the method being invoked.
@name invoke
@methodOf Array#
@returns {Array} A new array containing the results of invoking the named method on each element.
*/
Array.prototype.invoke = function() {
var args, method;
method = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return this.map(function(element) {
return element[method].apply(element, args);
});
};
/**
Randomly select an element from the array.
[1, 2, 3].rand()
# => 2
@name rand
@methodOf Array#
@returns {Object} A random element from an array
*/
Array.prototype.rand = function() {
return this[rand(this.length)];
};
/**
Remove the first occurrence of the given object from the array if it is
present. The array is modified in place.
a = [1, 1, "a", "b"]
a.remove(1)
# => 1
a
# => [1, "a", "b"]
@name remove
@methodOf Array#
@param {Object} object The object to remove from the array if present.
@returns {Object} The removed object if present otherwise undefined.
*/
Array.prototype.remove = function(object) {
var index;
index = this.indexOf(object);
if (index >= 0) {
return this.splice(index, 1)[0];
} else {
return;
}
};
/**
Returns true if the element is present in the array.
["a", "b", "c"].include("c")
# => true
[40, "a"].include(700)
# => false
@name include
@methodOf Array#
@param {Object} element The element to check if present.
@returns {Boolean} true if the element is in the array, false otherwise.
*/
Array.prototype.include = function(element) {
return this.indexOf(element) !== -1;
};
/**
Call the given iterator once for each element in the array,
passing in the element as the first argument, the index of
the element as the second argument, and <code>this</code> array as the
third argument.
word = ""
indices = []
["r", "a", "d"].each (letter, index) ->
word += letter
indices.push(index)
# => ["r", "a", "d"]
word
# => "rad"
indices
# => [0, 1, 2]
@name each
@methodOf Array#
@param {Function} iterator Function to be called once for each element in the array.
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
@returns {Array} this to enable method chaining.
*/
Array.prototype.each = function(iterator, context) {
var element, i, _len;
if (this.forEach) {
this.forEach(iterator, context);
} else {
for (i = 0, _len = this.length; i < _len; i++) {
element = this[i];
iterator.call(context, element, i, this);
}
}
return this;
};
/**
Call the given iterator once for each element in the array,
passing in the element as the first argument, the index of
the element as the second argument, and `this` array as the
third argument.
[1, 2, 3].map (number) ->
number * number
# => [1, 4, 9]
@name map
@methodOf Array#
@param {Function} iterator Function to be called once for each element in the array.
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
@returns {Array} An array of the results of the iterator function being called on the original array elements.
*/
(_base = Array.prototype).map || (_base.map = function(iterator, context) {
var element, i, results, _len;
results = [];
for (i = 0, _len = this.length; i < _len; i++) {
element = this[i];
results.push(iterator.call(context, element, i, this));
}
return results;
});
/**
Call the given iterator once for each pair of objects in the array.
[1, 2, 3, 4].eachPair (a, b) ->
# 1, 2
# 1, 3
# 1, 4
# 2, 3
# 2, 4
# 3, 4
@name eachPair
@methodOf Array#
@param {Function} iterator Function to be called once for each pair of elements in the array.
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
*/
Array.prototype.eachPair = function(iterator, context) {
var a, b, i, j, length, _results;
length = this.length;
i = 0;
_results = [];
while (i < length) {
a = this[i];
j = i + 1;
i += 1;
_results.push((function() {
var _results2;
_results2 = [];
while (j < length) {
b = this[j];
j += 1;
_results2.push(iterator.call(context, a, b));
}
return _results2;
}).call(this));
}
return _results;
};
/**
Call the given iterator once for each element in the array,
passing in the element as the first argument and the given object
as the second argument. Additional arguments are passed similar to
<code>each</code>.
@see Array#each
@name eachWithObject
@methodOf Array#
@param {Object} object The object to pass to the iterator on each visit.
@param {Function} iterator Function to be called once for each element in the array.
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
@returns {Array} this
*/
Array.prototype.eachWithObject = function(object, iterator, context) {
this.each(function(element, i, self) {
return iterator.call(context, element, object, i, self);
});
return object;
};
/**
Call the given iterator once for each group of elements in the array,
passing in the elements in groups of n. Additional argumens are
passed as in each.
results = []
[1, 2, 3, 4].eachSlice 2, (slice) ->
results.push(slice)
# => [1, 2, 3, 4]
results
# => [[1, 2], [3, 4]]
@see Array#each
@name eachSlice
@methodOf Array#
@param {Number} n The number of elements in each group.
@param {Function} iterator Function to be called once for each group of elements in the array.
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
@returns {Array} this
*/
Array.prototype.eachSlice = function(n, iterator, context) {
var i, len;
if (n > 0) {
len = (this.length / n).floor();
i = -1;
while (++i < len) {
iterator.call(context, this.slice(i * n, (i + 1) * n), i * n, this);
}
}
return this;
};
/**
Pipe the input through each function in the array in turn. For example, if you have a
list of objects you can perform a series of selection, sorting, and other processing
methods and then receive the processed list. This array must contain functions that
accept a single input and return the processed input. The output of the first function
is fed to the input of the second and so on until the final processed output is returned.
@name pipeline
@methodOf Array#
@param {Object} input The initial input to pass to the first function in the pipeline.
@returns {Object} The result of processing the input by each function in the array.
*/
Array.prototype.pipeline = function(input) {
return this.inject(input, function(input, fn) {
return fn(input);
});
};
/**
Returns a new array with the elements all shuffled up.
a = [1, 2, 3]
a.shuffle()
# => [2, 3, 1]
a # => [1, 2, 3]
@name shuffle
@methodOf Array#
@returns {Array} A new array that is randomly shuffled.
*/
Array.prototype.shuffle = function() {
var shuffledArray;
shuffledArray = [];
this.each(function(element) {
return shuffledArray.splice(rand(shuffledArray.length + 1), 0, element);
});
return shuffledArray;
};
/**
Returns the first element of the array, undefined if the array is empty.
["first", "second", "third"].first()
# => "first"
@name first
@methodOf Array#
@returns {Object} The first element, or undefined if the array is empty.
*/
Array.prototype.first = function() {
return this[0];
};
/**
Returns the last element of the array, undefined if the array is empty.
["first", "second", "third"].last()
# => "third"
@name last
@methodOf Array#
@returns {Object} The last element, or undefined if the array is empty.
*/
Array.prototype.last = function() {
return this[this.length - 1];
};
/**
Returns an object containing the extremes of this array.
[-1, 3, 0].extremes()
# => {min: -1, max: 3}
@name extremes
@methodOf Array#
@param {Function} [fn] An optional funtion used to evaluate each element to calculate its value for determining extremes.
@returns {Object} {min: minElement, max: maxElement}
*/
Array.prototype.extremes = function(fn) {
var max, maxResult, min, minResult;
if (fn == null) fn = Function.identity;
min = max = void 0;
minResult = maxResult = void 0;
this.each(function(object) {
var result;
result = fn(object);
if (min != null) {
if (result < minResult) {
min = object;
minResult = result;
}
} else {
min = object;
minResult = result;
}
if (max != null) {
if (result > maxResult) {
max = object;
return maxResult = result;
}
} else {
max = object;
return maxResult = result;
}
});
return {
min: min,
max: max
};
};
Array.prototype.maxima = function(valueFunction) {
if (valueFunction == null) valueFunction = Function.identity;
return this.inject([-Infinity, []], function(memo, item) {
var maxItems, maxValue, value;
value = valueFunction(item);
maxValue = memo[0], maxItems = memo[1];
if (value > maxValue) {
return [value, [item]];
} else if (value === maxValue) {
return [value, maxItems.concat(item)];
} else {
return memo;
}
}).last();
};
Array.prototype.maximum = function(valueFunction) {
return this.maxima(valueFunction).first();
};
Array.prototype.minima = function(valueFunction) {
var inverseFn;
if (valueFunction == null) valueFunction = Function.identity;
inverseFn = function(x) {
return -valueFunction(x);
};
return this.maxima(inverseFn);
};
Array.prototype.minimum = function(valueFunction) {
return this.minima(valueFunction).first();
};
/**
Pretend the array is a circle and grab a new array containing length elements.
If length is not given return the element at start, again assuming the array
is a circle.
[1, 2, 3].wrap(-1)
# => 3
[1, 2, 3].wrap(6)
# => 1
["l", "o", "o", "p"].wrap(0, 16)
# => ["l", "o", "o", "p", "l", "o", "o", "p", "l", "o", "o", "p", "l", "o", "o", "p"]
@name wrap
@methodOf Array#
@param {Number} start The index to start wrapping at, or the index of the sole element to return if no length is given.
@param {Number} [length] Optional length determines how long result array should be.
@returns {Object} or {Array} The element at start mod array.length, or an array of length elements, starting from start and wrapping.
*/
Array.prototype.wrap = function(start, length) {
var end, i, result;
if (length != null) {
end = start + length;
i = start;
result = [];
while (i < end) {
result.push(this[i.mod(this.length)]);
i += 1;
}
return result;
} else {
return this[start.mod(this.length)];
}
};
/**
Partitions the elements into two groups: those for which the iterator returns
true, and those for which it returns false.
[evens, odds] = [1, 2, 3, 4].partition (n) ->
n.even()
evens
# => [2, 4]
odds
# => [1, 3]
@name partition
@methodOf Array#
@param {Function} iterator
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
@returns {Array} An array in the form of [trueCollection, falseCollection]
*/
Array.prototype.partition = function(iterator, context) {
var falseCollection, trueCollection;
trueCollection = [];
falseCollection = [];
this.each(function(element) {
if (iterator.call(context, element)) {
return trueCollection.push(element);
} else {
return falseCollection.push(element);
}
});
return [trueCollection, falseCollection];
};
/**
Return the group of elements for which the return value of the iterator is true.
@name select
@methodOf Array#
@param {Function} iterator The iterator receives each element in turn as the first agument.
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
@returns {Array} An array containing the elements for which the iterator returned true.
*/
Array.prototype.select = function(iterator, context) {
return this.partition(iterator, context)[0];
};
/**
Return the group of elements that are not in the passed in set.
[1, 2, 3, 4].without ([2, 3])
# => [1, 4]
@name without
@methodOf Array#
@param {Array} values List of elements to exclude.
@returns {Array} An array containing the elements that are not passed in.
*/
Array.prototype.without = function(values) {
return this.reject(function(element) {
return values.include(element);
});
};
/**
Return the group of elements for which the return value of the iterator is false.
@name reject
@methodOf Array#
@param {Function} iterator The iterator receives each element in turn as the first agument.
@param {Object} [context] Optional context parameter to be used as `this` when calling the iterator function.
@returns {Array} An array containing the elements for which the iterator returned false.
*/
Array.prototype.reject = function(iterator, context) {
return this.partition(iterator, context)[1];
};
/**
Combines all elements of the array by applying a binary operation.
for each element in the arra the iterator is passed an accumulator
value (memo) and the element.
@name inject
@methodOf Array#
@returns {Object} The result of a
*/
Array.prototype.inject = function(initial, iterator) {
this.each(function(element) {
return initial = iterator(initial, element);
});
return initial;
};
/**
Add all the elements in the array.
[1, 2, 3, 4].sum()
# => 10
@name sum
@methodOf Array#
@returns {Number} The sum of the elements in the array.
*/
Array.prototype.sum = function() {
return this.inject(0, function(sum, n) {
return sum + n;
});
};
/**
Multiply all the elements in the array.
[1, 2, 3, 4].product()
# => 24
@name product
@methodOf Array#
@returns {Number} The product of the elements in the array.
*/
Array.prototype.product = function() {
return this.inject(1, function(product, n) {
return product * n;
});
};
/**
Merges together the values of each of the arrays with the values at the corresponding position.
['a', 'b', 'c'].zip([1, 2, 3])
# => [['a', 1], ['b', 2], ['c', 3]]
@name zip
@methodOf Array#
@returns {Array} Array groupings whose values are arranged by their positions in the original input arrays.
*/
Array.prototype.zip = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.map(function(element, index) {
var output;
output = args.map(function(arr) {
return arr[index];
});
output.unshift(element);
return output;
});
};
/**
Bindable module.
player = Core
x: 5
y: 10
player.bind "update", ->
updatePlayer()
# => Uncaught TypeError: Object has no method 'bind'
player.include(Bindable)
player.bind "update", ->
updatePlayer()
# => this will call updatePlayer each time through the main loop
@name Bindable
@module
@constructor
*/
var Bindable,
__slice = Array.prototype.slice;
Bindable = function(I, self) {
var eventCallbacks;
if (I == null) I = {};
eventCallbacks = {};
return {
bind: function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return self.on.apply(self, args);
},
unbind: function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return self.off.apply(self, args);
},
/**
Adds a function as an event listener.
# this will call coolEventHandler after
# yourObject.trigger "someCustomEvent" is called.
yourObject.on "someCustomEvent", coolEventHandler
#or
yourObject.on "anotherCustomEvent", ->
doSomething()
@name on
@methodOf Bindable#
@param {String} event The event to listen to.
@param {Function} callback The function to be called when the specified event
is triggered.
*/
on: function(namespacedEvent, callback) {
var event, namespace, _ref;
_ref = namespacedEvent.split("."), event = _ref[0], namespace = _ref[1];
if (namespace) {
callback.__PIXIE || (callback.__PIXIE = {});
callback.__PIXIE[namespace] = true;
}
eventCallbacks[event] || (eventCallbacks[event] = []);
eventCallbacks[event].push(callback);
return this;
},
/**
Removes a specific event listener, or all event listeners if
no specific listener is given.
# removes the handler coolEventHandler from the event
# "someCustomEvent" while leaving the other events intact.
yourObject.off "someCustomEvent", coolEventHandler
# removes all handlers attached to "anotherCustomEvent"
yourObject.off "anotherCustomEvent"
@name off
@methodOf Bindable#
@param {String} event The event to remove the listener from.
@param {Function} [callback] The listener to remove.
*/
off: function(namespacedEvent, callback) {
var callbacks, event, key, namespace, _ref;
_ref = namespacedEvent.split("."), event = _ref[0], namespace = _ref[1];
if (event) {
eventCallbacks[event] || (eventCallbacks[event] = []);
if (namespace) {
eventCallbacks[event] = eventCallbacks.select(function(callback) {
var _ref2;
return !(((_ref2 = callback.__PIXIE) != null ? _ref2[namespace] : void 0) != null);
});
} else {
if (callback) {
eventCallbacks[event].remove(callback);
} else {
eventCallbacks[event] = [];
}
}
} else if (namespace) {
for (key in eventCallbacks) {
callbacks = eventCallbacks[key];
eventCallbacks[key] = callbacks.select(function(callback) {
var _ref2;
return !(((_ref2 = callback.__PIXIE) != null ? _ref2[namespace] : void 0) != null);
});
}
}
return this;
},
/**
Calls all listeners attached to the specified event.
# calls each event handler bound to "someCustomEvent"
yourObject.trigger "someCustomEvent"
@name trigger
@methodOf Bindable#
@param {String} event The event to trigger.
@param {Array} [parameters] Additional parameters to pass to the event listener.
*/
trigger: function() {
var callbacks, event, parameters;
event = arguments[0], parameters = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
callbacks = eventCallbacks[event];
if (callbacks && callbacks.length) {
self = this;
return callbacks.each(function(callback) {
return callback.apply(self, parameters);
});
}
}
};
};
(typeof global !== "undefined" && global !== null ? global : this)["Bindable"] = Bindable;
var CommandStack;
CommandStack = function() {
var index, stack;
stack = [];
index = 0;
return {
execute: function(command) {
stack[index] = command;
command.execute();
return stack.length = index += 1;
},
undo: function() {
var command;
if (this.canUndo()) {
index -= 1;
command = stack[index];
command.undo();
return command;
}
},
redo: function() {
var command;
if (this.canRedo()) {
command = stack[index];
command.execute();
index += 1;
return command;
}
},
current: function() {
return stack[index - 1];
},
canUndo: function() {
return index > 0;
},
canRedo: function() {
return stack[index] != null;
}
};
};