This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
elevator.js
1649 lines (1431 loc) · 70 KB
/
elevator.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
/*******************************
AS3JS Version 0.3.3
AS3 to JS converter for use with ImportJS and OOPS.js.
The MIT License (MIT)
Copyright (c) 2017 Greg McLeod <cleod9{at}gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************/
(function ( ) {
var AS3JS = {
Utils: {
// Helper for default args
getDefaultValue: function getDefaultValue(value, fallback) {
return (typeof value != 'undefined') ? value : fallback;
},
// Helper for Vector/Array constructors
createArray: function (size, val) {
var arr = [];
for (var i = 0; i < size; i++) {
arr.push(val);
}
return arr;
}
},
load: function ( params ) {
// Loads program specified by params
params = params || {};
params.entryMode = params.entryMode || 'instance';
// Shim just in case
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
}
};
// Some temps / helper
var i, j, tmpPkg;
var getPackageInfo = function ( name ) {
// Splits package path into separate package and class name
var pkg = name.split('.');
var className = pkg[pkg.length-1];
pkg.splice(pkg.length-1, 1);
var packageName = pkg.join('.');
return {
packageName: packageName,
className: className
};
};
// This hash map contains each package, each package contains its classes
var packages = {};
// Converts supplied package hash to packageName.className.{ source: moduleFn }
for (i in params.program) {
tmpPkg = getPackageInfo(i);
packages[tmpPkg.packageName] = packages[tmpPkg.packageName] || {};
packages[tmpPkg.packageName][tmpPkg.className] = { compiled: false, source: params.program[i] };
}
// This helper will execute the module source specified by "name" and return its exports object
var imports = function ( packageName, className ) {
// Only run source() if it hasn't been compiled yet
if (!packages[packageName][className].compiled) {
packages[packageName][className].compiled = true;
packages[packageName][className].module = { exports: null, inject: null, import: imports };
//This next line actually compiles the module
packages[packageName][className].source(packages[packageName][className].module, packages[packageName][className].module.exports);
}
// Returns the compiled module
return packages[packageName][className].module.exports;
};
// Compiles all packages
for (i in packages) {
for (j in packages[i]) {
imports(i, j);
}
}
// Run inject() and $cinit() functions as the final step (this trivializes circular dependencies)
for (i in packages) {
// Execute the injection functions
for (j in packages[i]) {
if (typeof packages[i][j].module.inject === 'function') {
packages[i][j].module.inject();
}
}
}
for (i in packages) {
// Execute the $cinit functions
for (j in packages[i]) {
if (typeof packages[i][j].module.exports.$cinit === 'function') {
packages[i][j].module.exports.$cinit();
}
}
}
// Initializes application
var entryPkgInfo = getPackageInfo(params.entry);
var entryPoint = imports(entryPkgInfo.packageName, entryPkgInfo.className);
if (params.entryMode === "instance") {
return new entryPoint();
} else if (params.entryMode === "static") {
return entryPoint;
}
}
};
if (typeof module !== 'undefined') {
//CommonJS
module.exports = AS3JS;
} else {
//Browser Global
window.AS3JS = AS3JS;
}
})();
(function(){var Program={}; Program["com.mcleodgaming.elevator.core.Elevator"]=function(module, exports){ var ElevatorState, PersonState, ElevatorEvent, EventDispatcher, Debug, FrameTimer;
module.inject = function () {
ElevatorState = module.import('com.mcleodgaming.elevator.core', 'ElevatorState');
PersonState = module.import('com.mcleodgaming.elevator.core', 'PersonState');
ElevatorEvent = module.import('com.mcleodgaming.elevator.events', 'ElevatorEvent');
EventDispatcher = module.import('com.mcleodgaming.elevator.events', 'EventDispatcher');
Debug = module.import('com.mcleodgaming.elevator.util', 'Debug');
FrameTimer = module.import('com.mcleodgaming.elevator.util', 'FrameTimer');
};
var Elevator = function(params) {
this.$init(); //Get settings and set defaults params = params || {}; this.name = params.name || "[NoName]"; this.capacity = (params.capacity) ? params.capacity : 10; this.state = params.state || ElevatorState.IDLE; this.floors = params.floors || []; this.targetFloors = params.targetFloors || []; this.currentFloor = (this.floors.length > 0) ? params.currentFloor || this.floors[0] : null; this.entranceFloor = (this.floors.length > 0) ? params.entranceFloor || this.floors[0] : null; this.speedTimer = new FrameTimer(params.speedTimer || 30); this.waitTimer = new FrameTimer(params.waitTimer || 60); this.people = []; this.targetFloorsMemory = []; };
Elevator.prototype.$init = function() {
this.targetFloors = null;
this.targetFloorsMemory = null;
this.floors = null;
this.currentFloor = null;
this.entranceFloor = null;
this.people = null;
this.speedTimer = null;
this.waitTimer = null;};
Elevator.prototype.name = null;
Elevator.prototype.state = 0;
Elevator.prototype.targetFloors = null;
Elevator.prototype.targetFloorsMemory = null;
Elevator.prototype.floors = null;
Elevator.prototype.currentFloor = null;
Elevator.prototype.entranceFloor = null;
Elevator.prototype.people = null;
Elevator.prototype.speedTimer = null;
Elevator.prototype.waitTimer = null;
Elevator.prototype.capacity = 0;
Elevator.prototype.tick = function() { //Function to perform each game tick var index; if(this.state == ElevatorState.IDLE) { //Idling on the entrance floor } else if(this.state == ElevatorState.RISING || this.state == ElevatorState.FALLING) { //Elevator is currently rising or falling this.speedTimer.tick(); //If the movement timer reaches 0 if(this.speedTimer.get_IsComplete()) { //Reset timer and validate the next floor this.speedTimer.reset(); if(this.nextFloor()) { //Verify direction state if((this.nextFloor().index > this.currentFloor.index && this.state == ElevatorState.FALLING) || (this.nextFloor().index < this.currentFloor.index && this.state == ElevatorState.RISING)) { this.state = (this.nextFloor().index > this.currentFloor.index) ? ElevatorState.RISING : ElevatorState.FALLING; Debug.warn("WARNING: HAD TO FORCE FLIP DIRECTION STATE", this); } } //Proceed towards the next floor var increase = (this.state == ElevatorState.RISING) ? 1 : -1; //Verify floor destination exists if(this.currentFloor.index + increase >= this.floors.length || this.currentFloor.index + increase < 0) { Debug.warn("Problem with elevator: ", this); //throw new Error("sorta almost went outta bounds", this); } //Get next floor index and force in bounds to prevent errors index = this.targetFloors.indexOf(this.nextFloor()); this.currentFloor = this.floors[Math.max(0, Math.min(this.currentFloor.index + increase, this.floors.length-1))]; //Check to see if this is a target floor if(this.targetFloors[index] == this.currentFloor) { this.waitTimer.reset(); //Remove this floor from the targets list this.targetFloors.splice(index, 1); //Open doors this.state = ElevatorState.WAITING; //Get 1 pass in early this.processFloor(); //Trigger ARRIVE event EventDispatcher.dispatcher.dispatchEvent(new ElevatorEvent(ElevatorEvent.ARRIVE, { elevator: this, floor: this.currentFloor })); } } } else if(this.state == ElevatorState.RETURNING) { //Elevator is returning towards the entrance this.speedTimer.tick(); //If the movement timer reaches 0 if(this.speedTimer.get_IsComplete()) { this.speedTimer.reset(); //Get direction towards entrance floor var dir = (this.currentFloor < this.entranceFloor) ? 1 : -1; //Verify floor destination exists if(this.currentFloor.index + dir >= this.floors.length || this.currentFloor.index + dir < 0) { Debug.warn("Problem with elevator: ", this); //throw new Error("sorta almost went outta bounds", this); } //Go to the next consecutive floor this.currentFloor = this.floors[Math.max(0, Math.min(this.currentFloor.index + dir, this.floors.length-1))]; if(this.currentFloor == this.entranceFloor) this.state = ElevatorState.IDLE; //Idle at entrance this.waitTimer.reset(); } } else if(this.state == ElevatorState.WAITING) { //Elevator is waiting on a floor for passengers, processes the people that want to board each frame this.processFloor(); this.waitTimer.tick(); if(this.waitTimer.get_IsComplete()) { //Done waiting this.waitTimer.reset(); this.speedTimer.reset(); if(this.targetFloors.length <= 0) { //TODO: Figure out if this is needed (relates to people "pressing buttons" while inside of the elevator) //dumpFloorMemory(); } if(!this.nextFloor()) { //There are no people in the elevator, back to entrance if(this.people.length > 0) { //TODO: Consider moving/removing this check throw new Error("People were left on the elevator while it prepared to go back to the entrance :< ", this) } if(this.currentFloor == this.entranceFloor) { this.state = ElevatorState.IDLE; //At the entrance (TODO: Figure out when can this occur?) } else { //Dispatch the RELEASE event EventDispatcher.dispatcher.dispatchEvent(new ElevatorEvent(ElevatorEvent.RELEASE, { elevator: this })); //Head towards the entrance floor this.state = ElevatorState.RETURNING; //Go towards the entrance //Dispatch the DEPART event EventDispatcher.dispatcher.dispatchEvent(new ElevatorEvent(ElevatorEvent.DEPART, { elevator: this, floor: this.currentFloor })); } } else { //Decide what to do if we have a floor in our queue //TODO: Consider removing people length check, the elevator shouldn't be able to know this information //TODO: Consider removing this if statement altogehter, it may be impossible to enter if(this.currentFloor == this.entranceFloor && this.people.length <= 0 && this.targetFloors.length <= 0) { //No one got on the elevator at the entrance floor, begin idling this.state = ElevatorState.IDLE; EventDispatcher.dispatcher.dispatchEvent(new ElevatorEvent(ElevatorEvent.NOPASSENGERS, { elevator: this, floor: this.currentFloor })); } else { //Move towards the next room this.state = (this.nextFloor().index > this.currentFloor.index) ? ElevatorState.RISING : ElevatorState.FALLING; EventDispatcher.dispatcher.dispatchEvent(new ElevatorEvent(ElevatorEvent.DEPART, { elevator: this, floor: this.currentFloor })); } } } } };
Elevator.prototype.dumpFloorMemory = function() { //Grab floors from memory if there are any while(this.targetFloorsMemory.length > 0) { //if(targetFloors.length == 0) this.queueFloor(this.targetFloorsMemory.splice(0, 1)[0]); //Just add the floor //else if((isRising() && targetFloorsMemory[index].index > 0) || (isFalling() && targetFloorsMemory[index])) //queueFloor(targetFloorsMemory.splice(index--, 1)[0]); //Floor is in the same direction we're going already, we can add it } };
Elevator.prototype.nextFloor = function() { //TODO: Code this to not need the people.length? (use a special targetFloor array or toggle?) if(this.targetFloors.length <= 0) return null; //No target floors if(this.isRising()) return (this.people.length > 0) ? this.targetFloors[0] : this.targetFloors[this.targetFloors.length - 1]; //With people inside next floor is the lowest, otherwise highest else if(this.isFalling()) return (this.people.length > 0) ? this.targetFloors[this.targetFloors.length - 1] : this.targetFloors[0]; //With people inside next floor is the highest, otherwise lowest else return null; //No next floor };
Elevator.prototype.isHeadingTowards = function(floor, upwards) { //Determines if the elevator is headed towards a specific floor in the same queued direction (up or down) if(upwards) return (floor && this.isRising() && floor.index > this.currentFloor.index); else return (floor && this.isFalling() && floor.index < this.currentFloor.index); };
Elevator.prototype.isFree = function() { //Determine if the elevator is in a state to move immediately return (this.state == ElevatorState.IDLE || this.state == ElevatorState.RETURNING); };
Elevator.prototype.addPerson = function(person) { //Add a person into the elevator and have them queue their floor if(this.people.indexOf(person) < 0) { this.people.push(person); this.queueFloor(person.targetFloor); } };
Elevator.prototype.removePerson = function(person) { //Remove a person from the elevator var index = this.people.indexOf(person); if(index >= 0) this.people.splice(index, 1); };
Elevator.prototype.queueFloor = function(floor) { //Attempt to queue a floor if(!floor || this.targetFloors.indexOf(floor) >= 0) return; //Cancel if already queued this floor or the floor provided is null var self = this; //Function to enqueue a floor via simple insertion sort var enqueue = function() { if(self.targetFloors.length <= 0) { //No targetted floors, free to queue self.targetFloors.push(floor); if(self.people.length <= 0) { //Trigger RESPOND event that a person's button press has been acknowledged EventDispatcher.dispatcher.dispatchEvent(new ElevatorEvent(ElevatorEvent.RESPOND, { elevator: self, floor: floor })); } } else { //Adds a floor in ascending order for(var i = 0; i < self.targetFloors.length; i++) { if(self.targetFloors[i] == floor) { break; //Already queued this floor } else if(i < self.targetFloors.length && self.targetFloors[i].index > floor.index) { self.targetFloors.splice(i, 0, floor); break; //Insert at this index } else if(i + 1 >= self.targetFloors.length){ self.targetFloors.push(floor); break; //Insert at the end of the array } } } }; if(this.isRising()) { //If rising, only queue if the pressed floor is in our direction if(floor.index > this.currentFloor.index) enqueue(); else { if(this.targetFloorsMemory.indexOf(floor) < 0) this.targetFloorsMemory.push(floor); //Someone inside the elevator pressed a floor in the opposite direction } } else if (this.isFalling()) { //If falling, only queue if the pressed floor is in our direction if(floor.index < this.currentFloor.index) enqueue(); else { if(this.targetFloorsMemory.indexOf(floor) < 0) this.targetFloorsMemory.push(floor); //Someone inside the elevator pressed a floor in the opposite direction } } else if(this.isIdling()) { if(this.currentFloor == floor) { //Already on the floor so we can just enter waiting state this.state = ElevatorState.WAITING; } else { //We're not already on the floor so enqueue it enqueue(); if(this.isFree()) { //We're free so we can go immediately this.state = (floor.index > this.currentFloor.index) ? ElevatorState.RISING : ElevatorState.FALLING; EventDispatcher.dispatcher.dispatchEvent(new ElevatorEvent(ElevatorEvent.DEPART, { elevator: this, floor: this.currentFloor })); } } } };
Elevator.prototype.hasQueued = function(floor) { //Determine if a floor has been queued or not return (this.targetFloors.indexOf(floor) >= 0); };
Elevator.prototype.processFloor = function() { var i; //Let people off the elevator first for(i = 0; i < this.people.length; i++) { //If their target floor is here if(this.people[i].targetFloor == this.currentFloor) { //Remove them from the elevator this.people[i].exitElevator(this.currentFloor); this.removePerson(this.people[i--]); } } //Allow boarding for(i = 0; i < this.currentFloor.people.length; i++) { if(this.people.length >= this.capacity) { //Trigger FULL event, no more space in elevator EventDispatcher.dispatcher.dispatchEvent(new ElevatorEvent(ElevatorEvent.FULL, { elevator: this, floor: this.currentFloor, person: this.currentFloor.people[i] })); break; } //Make sure the person wants to get on the elevator if(this.currentFloor.people[i].state == PersonState.ENTERING || this.currentFloor.people[i].state == PersonState.LEAVING) { //Make sure going the right direction if((this.currentFloor.people[i].targetFloor.index > this.currentFloor.index && (this.targetFloors.length <= 0 || this.isRising())) || (this.currentFloor.people[i].targetFloor.index < this.currentFloor.index && (this.targetFloors.length <= 0 || this.isFalling()))) { this.addPerson(this.currentFloor.people[i]); this.currentFloor.people[i].boardElevator(this); this.currentFloor.people.splice(i--, 1); //Floor member count decremented } } } };
Elevator.prototype.isRising = function() { //TODO: Check this, probably should be nextFloor()? //Return true if the next target floor is above this one return (this.targetFloors.length > 0 && this.targetFloors[0].index > this.currentFloor.index); };
Elevator.prototype.isFalling = function() { //TODO: Check this, probably should be nextFloor()? //Return true if the next target floor is below this one return (this.targetFloors.length > 0 && this.targetFloors[0].index < this.currentFloor.index); };
Elevator.prototype.isIdling = function() { //If the elevator is in a non-moving state return (this.state == ElevatorState.IDLE || this.state == ElevatorState.WAITING || this.state == ElevatorState.RETURNING); };
Elevator.prototype.comparePriority = function(otherElevator, targetFloor, upwards) { var self = this; //Returns the beest elevator given a target floor, and the direction towards that floor. Swap out this compare function with your own :) var genericCompare = function() { if(!otherElevator || otherElevator == self || !targetFloor) return self; var nextFloor1 = self.nextFloor(); var nextFloor2 = otherElevator.nextFloor(); var distance1 = targetFloor.index - self.currentFloor.index; var distance2 = targetFloor.index - otherElevator.currentFloor.index; //Perform some basic tests to see which elevator is best if(self.isHeadingTowards(targetFloor, upwards) && otherElevator.isHeadingTowards(targetFloor, upwards)) { if(self.hasQueued(targetFloor)) { return self; //This elevator already has the floor queued } else if(otherElevator.hasQueued(targetFloor)) { return otherElevator; //This elevator already has the floor queued } else { return (Math.abs(distance1) <= Math.abs(distance2)) ? self : otherElevator; //Choose the closer elevator } } else if(self.isHeadingTowards(targetFloor, upwards)) { if(otherElevator.isFree()) { return (Math.abs(distance1) <= Math.abs(distance2)) ? self : otherElevator; //Choose the closer elevator } else { return self; //This elevator is available } } else if(otherElevator.isHeadingTowards(targetFloor, upwards)) { if(self.isFree()) { return (Math.abs(distance1) <= Math.abs(distance2)) ? self : otherElevator; //Choose the closer elevator } else { return otherElevator; //Other elevator is available } } else if(self.isFree() && otherElevator.isFree()) { return (Math.abs(distance1) <= Math.abs(distance2)) ? self : otherElevator; //Choose the closer elevator } else if(self.isFree()) { return self; //Other elevator must be busy } else if(otherElevator.isFree()) { return otherElevator; //This elevator must be busy } else { //Debug.console.warn('Hm.... Some state was not managed in Elevator.comparePriority() ', self); return null; } }; return genericCompare(); }
module.exports = Elevator;
};Program["com.mcleodgaming.elevator.core.ElevatorEngine"]=function(module, exports){ var Main, Elevator, ElevatorState, Floor, Person, PersonState, ElevatorEngineEvent, ElevatorEvent, EventDispatcher, PersonEvent, Debug, RandomNameGenerator;
module.inject = function () {
Main = module.import('com.mcleodgaming.elevator', 'Main');
Elevator = module.import('com.mcleodgaming.elevator.core', 'Elevator');
ElevatorState = module.import('com.mcleodgaming.elevator.core', 'ElevatorState');
Floor = module.import('com.mcleodgaming.elevator.core', 'Floor');
Person = module.import('com.mcleodgaming.elevator.core', 'Person');
PersonState = module.import('com.mcleodgaming.elevator.core', 'PersonState');
ElevatorEngineEvent = module.import('com.mcleodgaming.elevator.events', 'ElevatorEngineEvent');
ElevatorEvent = module.import('com.mcleodgaming.elevator.events', 'ElevatorEvent');
EventDispatcher = module.import('com.mcleodgaming.elevator.events', 'EventDispatcher');
PersonEvent = module.import('com.mcleodgaming.elevator.events', 'PersonEvent');
Debug = module.import('com.mcleodgaming.elevator.util', 'Debug');
RandomNameGenerator = module.import('com.mcleodgaming.elevator.util', 'RandomNameGenerator');
};
var ElevatorEngine = function(params) {
this.$init(); //Get settings params = params || {}; this.elevators = params.elevators || []; this.floors = params.floors || []; this.people = params.people || []; this.entranceFloor = (params.entranceFloor) ? params.entranceFloor : (this.floors.length > 0) ? this.floors[0] : null; this.queue = []; Debug.log("An ElevatorEngine has been created. { elevators: " + this.elevators.length + ", floors: " + this.floors.length + ", people: " + this.people.length + " } "); };
ElevatorEngine.generateFloors = function(amount) { //Generate given amount of floors var floors = []; for(var i = 0; (i <= amount && amount >= 12) || (i < amount && amount < 12); i++) if(amount < 12 || i != 12) /* Skip Floor 13!!! */ floors.push(new Floor({index: i, name: "" +(i+1) })); return floors; };
ElevatorEngine.generateElevators = function(amount, floors, entranceFloor, currentFloor) { //Generate given amount of elevators var elevators = []; for(var i = 0; i < amount; i++) elevators.push(new Elevator({ name: "" + (i + 1), floors: floors, entranceFloor: entranceFloor, currentFloor: (currentFloor) ? currentFloor : null })); return elevators; };
ElevatorEngine.generatePeople = function(amount, floors, entranceFloor, currentFloor) { //Generate given amount of people var people = []; for(var i = 0; i < amount; i++) { //Make a random floor var targetFloor = floors[Math.round(Math.random()*(floors.length-1))]; while(targetFloor == currentFloor || targetFloor == entranceFloor) targetFloor = floors[Math.round(Math.random()*(floors.length-1))]; var person = new Person({ name: RandomNameGenerator.getRandomName(), floorTimeMax: Math.round(Math.random()*(1000 - 250) + 250), entranceFloor: entranceFloor, currentFloor: (currentFloor) ? currentFloor : null, targetFloor: targetFloor }); people.push(person); currentFloor.people.push(person); } return people; };
ElevatorEngine.$cinit = function () {
};
ElevatorEngine.prototype.$init = function() {
this.ticker = null;
this.elevators = null;
this.floors = null;
this.people = null;
this.queue = null;
this.entranceFloor = null;};
ElevatorEngine.prototype.started = false;
ElevatorEngine.prototype.ticker = null;
ElevatorEngine.prototype.elevators = null;
ElevatorEngine.prototype.floors = null;
ElevatorEngine.prototype.people = null;
ElevatorEngine.prototype.queue = null;
ElevatorEngine.prototype.entranceFloor = null;
ElevatorEngine.prototype.start = function() { //Starts the ticker if(!this.started) { //Add all event listeners EventDispatcher.dispatcher.addEventListener(PersonEvent.ENTERING, Main.eventHelper(this, this.onPersonEntering)); EventDispatcher.dispatcher.addEventListener(PersonEvent.LEAVING, Main.eventHelper(this, this.onPersonLeaving)); EventDispatcher.dispatcher.addEventListener(PersonEvent.BOARDED, Main.eventHelper(this, this.onPersonBoarded)); EventDispatcher.dispatcher.addEventListener(PersonEvent.DEPARTED, Main.eventHelper(this, this.onPersonDeparted)); EventDispatcher.dispatcher.addEventListener(PersonEvent.DONE, Main.eventHelper(this, this.onPersonDone)); EventDispatcher.dispatcher.addEventListener(ElevatorEvent.ARRIVE, Main.eventHelper(this, this.onElevatorArrive)); EventDispatcher.dispatcher.addEventListener(ElevatorEvent.DEPART, Main.eventHelper(this, this.onElevatorDepart)); EventDispatcher.dispatcher.addEventListener(ElevatorEvent.FULL, Main.eventHelper(this, this.onElevatorFull)); EventDispatcher.dispatcher.addEventListener(ElevatorEvent.RESPOND, Main.eventHelper(this, this.onElevatorRespond)); //Enable ticker this.started = true; this.ticker = setInterval(Main.eventHelper(this, this.tick), (1000 / Main.FPS)); Debug.log("ElevatorEngine started."); } else { Debug.log("Engine already started"); } };
ElevatorEngine.prototype.stop = function() { //Stops the ticker if(this.started) { //Remove all event listeners EventDispatcher.dispatcher.removeEventListener(PersonEvent.ENTERING); EventDispatcher.dispatcher.removeEventListener(PersonEvent.LEAVING); EventDispatcher.dispatcher.removeEventListener(PersonEvent.BOARDED); EventDispatcher.dispatcher.removeEventListener(PersonEvent.DEPARTED); EventDispatcher.dispatcher.removeEventListener(PersonEvent.DONE); EventDispatcher.dispatcher.removeEventListener(ElevatorEvent.ARRIVE); EventDispatcher.dispatcher.removeEventListener(ElevatorEvent.DEPART); EventDispatcher.dispatcher.removeEventListener(ElevatorEvent.FULL); EventDispatcher.dispatcher.removeEventListener(ElevatorEvent.RESPOND); //Turn off ticker this.started = false; clearInterval(this.ticker); this.ticker = null; Debug.log("ElevatorEngine stopped."); } else { Debug.log("Engine already stopped"); } };
ElevatorEngine.prototype.tick = function(e) {
e = AS3JS.Utils.getDefaultValue(e, null); //Function to perform each tick (the main loop) var i; //Evaluate queue this.processQueue(); //Run ticks for(i = 0; i < this.elevators.length; i++) this.elevators[i].tick(); for(i = 0; i < this.people.length; i++) { this.people[i].tick(); } //Get rid of all people who have left for(i = 0; i < this.people.length; i++) if(this.people[i].state == PersonState.DONE) this.people.splice(i--, 1); EventDispatcher.dispatcher.dispatchEvent(new ElevatorEngineEvent(ElevatorEngineEvent.UPDATE, { engine: this })); };
ElevatorEngine.prototype.addPerson = function(person) { //Person enters the building this.people.push(person); };
ElevatorEngine.prototype.processQueue = function() { //Process the floor queue (basically simulates the user hitting the up or down button) var e, p; for(p = 0; p < this.queue.length; p++) { if(!this.queue[p].currentFloor) { //They must have already boarded an elevator this.queue.splice(p--, 1); continue; } //Use the Elevator's comparePriority() function to determine the best elevator //Note: I'm looping backwards since my "best elevator" logic will priortize elevators on the "opposite" end of where it starts checking var bestElevator = null; for(e = this.elevators.length - 1; e >= 0; e--) bestElevator = this.elevators[e].comparePriority(bestElevator, this.queue[p].currentFloor, (this.queue[p].targetFloor.index > this.queue[p].currentFloor.index)); //If a best elevator was found if(bestElevator) { //Only queue floor if there is not already another elevator on this floor already that's accepting people for(e = 0; e < this.elevators.length; e++) if(this.elevators[e] != bestElevator && this.elevators[e].state == ElevatorState.WAITING && this.elevators[e].currentFloor == this.queue[p].currentFloor) bestElevator = this.elevators[e]; //Officially queue the elevator bestElevator.queueFloor(this.queue[p].currentFloor); this.queue.splice(p--, 1); } } };
ElevatorEngine.prototype.onPersonEntering = function(event) { this.queue.push(event.data.person); Debug.log(event.data.person.name + " entered the building on floor: " + event.data.floor.name + " (target floor: " + event.data.person.targetFloor.name + ")"); };
ElevatorEngine.prototype.onPersonLeaving = function(event) { this.queue.push(event.data.person); Debug.log(event.data.person.name + " is ready to leave floor: " + event.data.floor.name); };
ElevatorEngine.prototype.onPersonBoarded = function(event) { Debug.log(event.data.person.name + " boarded elevator " + event.data.elevator.name + ' from floor ' + event.data.elevator.currentFloor.name + ' (going to floor ' + event.data.person.targetFloor.name + ')'); };
ElevatorEngine.prototype.onPersonDeparted = function(event) { Debug.log(event.data.person.name + " departed elevator " + event.data.elevator.name + ' (from floor ' + event.data.person.currentFloor.name + ')'); };
ElevatorEngine.prototype.onPersonDone = function(event) { Debug.log(event.data.person.name + " has left the building."); };
ElevatorEngine.prototype.onElevatorRespond = function(event) { //When an elevator fills up Debug.log("Elevator " + event.data.elevator.name + " responded to the request from floor " + event.data.floor.name + ' (currently at floor ' + event.data.elevator.currentFloor.name + ' )'); };
ElevatorEngine.prototype.onElevatorArrive = function(event) { //When an elevator arrives on a floor, let other elevators know perhaps? for(var i = 0; i < this.elevators.length; i++) { if(this.elevators[i] != event.data.elevator && this.elevators[i].targetFloors.indexOf(event.data.floor) >= 0) { //TODO: Allow elevator to change course or something under certain conditions? } } var nextFloor = (event.data.elevator.nextFloor()) ? event.data.elevator.nextFloor().name : null; Debug.log("Elevator " + event.data.elevator.name + " arrived on floor " + event.data.floor.name + ' (nextFloor: ' + nextFloor + ', people: ' + event.data.elevator.people.length + ', targetFloors: ' + event.data.elevator.targetFloors.length + ' )'); };
ElevatorEngine.prototype.onElevatorDepart = function(event) { //When an elevator departs a floor var nextFloor = (event.data.elevator.nextFloor()) ? event.data.elevator.nextFloor().name : null; Debug.log("Elevator " + event.data.elevator.name + " departed floor " + event.data.floor.name + ' (nextFloor: ' + nextFloor + ', people: ' + event.data.elevator.people.length + ', targetFloors: ' + event.data.elevator.targetFloors.length + ' )'); };
ElevatorEngine.prototype.onElevatorFull = function(event) { //When an elevator fills up Debug.log("Elevator " + event.data.elevator.name + " was full and denied " + event.data.person.name + ' access at floor ' + event.data.floor.name + ' )'); }
module.exports = ElevatorEngine;
};Program["com.mcleodgaming.elevator.core.ElevatorState"]=function(module, exports){
var ElevatorState = function ElevatorState() {};
ElevatorState.IDLE = 0;
ElevatorState.RISING = 1;
ElevatorState.FALLING = 2;
ElevatorState.WAITING = 3;
ElevatorState.RETURNING = 4;
ElevatorState.BROKEN = 5;
ElevatorState.asString = function(state) { //For debugging purposes return ["IDLE","RISING","FALLING","WAITING", "RETURNING", "BROKEN"][state]; };
ElevatorState.$cinit = function () {
ElevatorState.IDLE = 0;
ElevatorState.RISING = 1;
ElevatorState.FALLING = 2;
ElevatorState.WAITING = 3;
ElevatorState.RETURNING = 4;
ElevatorState.BROKEN = 5;
};
ElevatorState.prototype.$init = function() {}
module.exports = ElevatorState;
};Program["com.mcleodgaming.elevator.core.Floor"]=function(module, exports){
var Floor = function(params) {
this.$init(); //Get settings params = params || {}; this.index = (typeof params.index == "number") ? params.index : -1; this.name = params.name || "[No Name]"; this.people = []; };
Floor.prototype.$init = function() {
this.people = null;};
Floor.prototype.index = 0;
Floor.prototype.name = null;
Floor.prototype.people = null;
Floor.prototype.removePerson = function(person) { //Remove a person from the floor var i = this.people.indexOf(person); if(i >= 0) this.people.splice(i, 1) };
Floor.prototype.addPerson = function(person) { //Add a person to the floor if(this.people.indexOf(person) < 0) this.people.push(person); }
module.exports = Floor;
};Program["com.mcleodgaming.elevator.core.Person"]=function(module, exports){ var PersonState, EventDispatcher, PersonEvent, FrameTimer;
module.inject = function () {
PersonState = module.import('com.mcleodgaming.elevator.core', 'PersonState');
EventDispatcher = module.import('com.mcleodgaming.elevator.events', 'EventDispatcher');
PersonEvent = module.import('com.mcleodgaming.elevator.events', 'PersonEvent');
FrameTimer = module.import('com.mcleodgaming.elevator.util', 'FrameTimer');
};
var Person = function(params) {
this.$init(); //Get settings params = params || {}; this.name = params.name || "Unknown"; this.entranceFloor = (params.entranceFloor) ? params.entranceFloor : null; this.currentFloor = (params.currentFloor) ? params.currentFloor : null; this.targetFloor = (params.targetFloor) ? params.targetFloor : null; this.floorTimer = new FrameTimer(params.floorTimer || 1000); this.state = params.state || PersonState.QUEUEING; this.waitTimer = 0; };
Person.prototype.$init = function() {
this.entranceFloor = null;
this.currentFloor = null;
this.targetFloor = null;
this.currentElevator = null;
this.floorTimer = null;};
Person.prototype.name = null;
Person.prototype.state = 0;
Person.prototype.entranceFloor = null;
Person.prototype.currentFloor = null;
Person.prototype.targetFloor = null;
Person.prototype.currentElevator = null;
Person.prototype.floorTimer = null;
Person.prototype.waitTimer = 0;
Person.prototype.isReady = function() { //Returns true if the person is ready to leave their floor return this.floorTimer.get_IsComplete(); };
Person.prototype.resetTime = function() { //Resets the amount of time the user wants to remain on the floor this.floorTimer.reset(); };
Person.prototype.waitTime = function() { //Returns the total time the user has been in the isReady() state and been stuck waiting return this.waitTimer; };
Person.prototype.boardElevator = function(elevator) { //Leave floor, enter elevator var oldFloor = this.currentFloor; this.currentElevator = elevator; this.currentFloor.removePerson(this); this.currentFloor = null; EventDispatcher.dispatcher.dispatchEvent(new PersonEvent(PersonEvent.BOARDED, { elevator: elevator, person: this, floor: oldFloor })); };
Person.prototype.exitElevator = function(floor) { //Exit elevator, enter floor var tmpElevator = this.currentElevator; this.currentElevator = null; var tmpFloor = this.currentFloor = floor; this.currentFloor.addPerson(this); this.resetTime(); if(this.state == PersonState.ENTERING) { //They are just entering the building so now they want to stay IDLE on this floor this.state = PersonState.IDLE; EventDispatcher.dispatcher.dispatchEvent(new PersonEvent(PersonEvent.DEPARTED, { elevator: tmpElevator, person: this, floor: this.currentFloor })); EventDispatcher.dispatcher.dispatchEvent(new PersonEvent(PersonEvent.WAITING, { person: this })); } else if(this.state == PersonState.LEAVING) { //The person had been on their way out of the building, so have them enter their DONE state EventDispatcher.dispatcher.dispatchEvent(new PersonEvent(PersonEvent.DEPARTED, { elevator: tmpElevator, person: this, floor: tmpFloor })); this.currentFloor.removePerson(this); this.currentFloor = null; this.state = PersonState.DONE; EventDispatcher.dispatcher.dispatchEvent(new PersonEvent(PersonEvent.DONE, { person: this, floor: tmpFloor })); } else { //This state should be impossible throw new Error("[Person] Some Unhandled state in Person"); } };
Person.prototype.tick = function() { if(this.state == PersonState.QUEUEING) { //Immediately allow the person into their ENTERING state (QUEING state is just so we can do a one-time ENTERING event trigger) this.state = PersonState.ENTERING; EventDispatcher.dispatcher.dispatchEvent(new PersonEvent(PersonEvent.ENTERING, { person: this, floor: this.currentFloor })); } else if(this.state == PersonState.IDLE) { //The person is IDLING on a floor if(this.currentFloor) { this.floorTimer.tick(); if(this.floorTimer.get_IsComplete()) { //The person is done on the floor, they want to enter LEAVING state this.waitTimer = 0; this.floorTimer.reset(); this.state = PersonState.LEAVING; this.targetFloor = this.entranceFloor; EventDispatcher.dispatcher.dispatchEvent(new PersonEvent(PersonEvent.LEAVING, { person: this, floor: this.currentFloor })); } } } else if((this.state == PersonState.LEAVING || this.state ==PersonState.ENTERING) && !this.currentElevator) { this.waitTimer++; } }
module.exports = Person;
};Program["com.mcleodgaming.elevator.core.PersonState"]=function(module, exports){
var PersonState = function PersonState() {};
PersonState.QUEUEING = 0;
PersonState.ENTERING = 1;
PersonState.IDLE = 2;
PersonState.LEAVING = 3;
PersonState.DONE = 4;
PersonState.$cinit = function () {
PersonState.QUEUEING = 0;
PersonState.ENTERING = 1;
PersonState.IDLE = 2;
PersonState.LEAVING = 3;
PersonState.DONE = 4;
};
PersonState.prototype.$init = function() {}
module.exports = PersonState;
}; Program["com.mcleodgaming.elevator.events.ElevatorEngineEvent"]=function(module, exports){ var Event = module.import('com.mcleodgaming.elevator.events', 'Event');
var ElevatorEngineEvent = function(type, data) {
this.$init(); Event.call(this, type, data); };
ElevatorEngineEvent.prototype = Object.create(Event.prototype);
ElevatorEngineEvent.UPDATE = null;
ElevatorEngineEvent.$cinit = function () {
ElevatorEngineEvent.UPDATE = "elevatorEngineUpdate";
};
ElevatorEngineEvent.prototype.$init = function() {}
module.exports = ElevatorEngineEvent;
};Program["com.mcleodgaming.elevator.events.ElevatorEvent"]=function(module, exports){ var Event = module.import('com.mcleodgaming.elevator.events', 'Event');
var ElevatorEvent = function(typeVal, dataVal) {
this.$init(); Event.call(this, typeVal, dataVal); };
ElevatorEvent.prototype = Object.create(Event.prototype);
ElevatorEvent.ARRIVE = null;
ElevatorEvent.DEPART = null;
ElevatorEvent.RESPOND = null;
ElevatorEvent.RELEASE = null;
ElevatorEvent.NOPASSENGERS = null;
ElevatorEvent.OVERBURDENED = null;
ElevatorEvent.FULL = null;
ElevatorEvent.$cinit = function () {
ElevatorEvent.ARRIVE = "elevatorArrive";
ElevatorEvent.DEPART = "elevatorDepart";
ElevatorEvent.RESPOND = "elevatorRespond";
ElevatorEvent.RELEASE = "elevatorRelease";
ElevatorEvent.NOPASSENGERS = "elevatorNoPassengers";
ElevatorEvent.OVERBURDENED = "elevatorOverburdened";
ElevatorEvent.FULL = "elevatorFull";
};
ElevatorEvent.prototype.$init = function() {}
module.exports = ElevatorEvent;
};Program["com.mcleodgaming.elevator.events.Event"]=function(module, exports){
var Event = function(typeVal, dataVal) {
this.$init(); this.type = typeVal || "event"; this.data = dataVal || {}; };
Event.prototype.$init = function() {
this.data = null;};
Event.prototype.type = null;
Event.prototype.data = null
module.exports = Event;
};Program["com.mcleodgaming.elevator.events.EventDispatcher"]=function(module, exports){ var Debug;
module.inject = function () {
Debug = module.import('com.mcleodgaming.elevator.util', 'Debug');
};
var EventDispatcher = function() {
this.$init(); this._eventList = {}; };
EventDispatcher.debug = false;
EventDispatcher.dispatcher = null;
EventDispatcher.init = function() { EventDispatcher.dispatcher = new EventDispatcher(); };
EventDispatcher.$cinit = function () {
EventDispatcher.debug = false;
EventDispatcher.dispatcher = null;
};
EventDispatcher.prototype.$init = function() {
this._eventList = null;};
EventDispatcher.prototype._eventList = null;
EventDispatcher.prototype.addEventListener = function(type, listener) { if(typeof listener != 'function') throw new Error("[EventDispatcher] Error, provided event listener is not a function"); //Save the event under its name in our eventList Dictionary if(!this._eventList[type]) this._eventList[type] = []; this._eventList[type].push({ listener: listener}); };
EventDispatcher.prototype.removeEventListener = function(type, listener) {
listener = AS3JS.Utils.getDefaultValue(listener, null); //Check our event list for listeners of this type, and remove if(this._eventList[type]) { for(var i = 0; i < this._eventList[type].length; i++) if(!listener || listener == this._eventList[type][i].listener) this._eventList[type].splice(i--, 1); //Could delete events of the this type if there are none left, but not necessary since it can be reused (and would probably break removeAllEvents() anyway) /*if(_eventList[type].length <= 0) { //Delete this listener type from the dictionar _eventList[type] = null; delete _eventList[type]; }*/ } };
EventDispatcher.prototype.dispatchEvent = function(event) { //Trigger all event of this type if(this._eventList[event.type]) { for(var i = 0; i < this._eventList[event.type].length; i++) this._eventList[event.type][i].listener(event); } if(EventDispatcher.debug) Debug.log("[Event \"" + event.type + "\"]"); };
EventDispatcher.prototype.hasEvent = function(type, listener) {
listener = AS3JS.Utils.getDefaultValue(listener, null); //Determine if an event exists (can filter by listener) if(this._eventList[type]) { for(var i = 0; i < this._eventList[type].length; i++) if(!listener || listener == this._eventList[type][i].listener) return true; } return false; };
EventDispatcher.prototype.removeAllEvents = function() { //Remove all events for(var i in this._eventList) { this.removeEventListener(i); this._eventList[i] = null; } this._eventList = null; this._eventList = {}; };
EventDispatcher.prototype.getCount = function(type) {
type = AS3JS.Utils.getDefaultValue(type, null); if(!type) { //Total all events var total = 0; for(var i in this._eventList) total += this._eventList[i].length; return total; } else { //Total events of a specific type return (this._eventList[type]) ? this._eventList[type].length : 0; } }
module.exports = EventDispatcher;
};Program["com.mcleodgaming.elevator.events.PersonEvent"]=function(module, exports){ var Event = module.import('com.mcleodgaming.elevator.events', 'Event');
var PersonEvent = function(typeVal, dataVal) {
this.$init(); Event.call(this, typeVal, dataVal); };
PersonEvent.prototype = Object.create(Event.prototype);
PersonEvent.ENTERING = null;
PersonEvent.BOARDED = null;
PersonEvent.WAITING = null;
PersonEvent.LEAVING = null;
PersonEvent.DEPARTED = null;
PersonEvent.DONE = null;
PersonEvent.$cinit = function () {
PersonEvent.ENTERING = "personEntering";
PersonEvent.BOARDED = "personBoarded";
PersonEvent.WAITING = "personWaiting";
PersonEvent.LEAVING = "personLeaving";
PersonEvent.DEPARTED = "personDeparted";
PersonEvent.DONE = "personDone";
};
PersonEvent.prototype.$init = function() {}
module.exports = PersonEvent;
}; Program["com.mcleodgaming.elevator.Main"]=function(module, exports){ var ElevatorEngine, EventDispatcher, Debug, ElevatorViewJS;
module.inject = function () {
ElevatorEngine = module.import('com.mcleodgaming.elevator.core', 'ElevatorEngine');
EventDispatcher = module.import('com.mcleodgaming.elevator.events', 'EventDispatcher');
Debug = module.import('com.mcleodgaming.elevator.util', 'Debug');
ElevatorViewJS = module.import('com.mcleodgaming.elevator.views', 'ElevatorViewJS');
};
var Main = function() {
this.$init();
Debug.init();
Debug.log("Main class created.");
//Some simple inits
Main.ROOT = this;
EventDispatcher.init();
//Create floors, elevators, and people
var floors = ElevatorEngine.generateFloors(10);
var elevators = ElevatorEngine.generateElevators(5, floors, floors[0], floors[0]);
var people = ElevatorEngine.generatePeople(4, floors, floors[0], floors[0]);
//Create the engine
this.engine = new ElevatorEngine({
entranceFloor: floors[0],
elevators: elevators,
floors: floors,
people: people
});
//Start the engine
this.engine.start();
//Start the people adding timer
this.startAddingPeople();
//Attach a view so we can visualize
//view = new ElevatorView(engine); //For template version (no
//view = new ElevatorViewAS3(engine); //For AS3 Version
this.view = new ElevatorViewJS(this.engine); //For JS Version
};
Main.ROOT = null;
Main.FPS = 30;
Main.eventHelper = function(context, fn) {
//Helps with binding events to 'this'
return function(e) {
return fn.call(context, e);
};
};
Main.$cinit = function () {
Main.ROOT = null;
Main.FPS = 30;
};
Main.prototype.$init = function() {
this.engine = null;
this.view = null;
this.addPeopleInterval = null;};
Main.prototype.engine = null;
Main.prototype.view = null;
Main.prototype.addingPeople = false;
Main.prototype.addPeopleInterval = null;
Main.prototype.addPeople = function(e) {
e = AS3JS.Utils.getDefaultValue(e, null);
//Add a random amount of people to the elevator engine
var people = ElevatorEngine.generatePeople(Math.round(Math.random()*(5-1) + 1), this.engine.floors, this.engine.floors[0], this.engine.floors[0]);
for(var i = 0; i < people.length; i++)
this.engine.addPerson(people[i]);
};
Main.prototype.startAddingPeople = function() {
if(!this.addingPeople) {
//Turn on the people adding timer
this.addingPeople = true;
this.addPeopleInterval = setInterval(Main.eventHelper(this, this.addPeople), 5000);
Debug.log("Started adding people.")
} else {
Debug.log("Already adding people.")
}
};
Main.prototype.stopAddingPeople = function() {
if(this.addingPeople) {
//Turn off the people adding timer
this.addingPeople = false;
clearInterval(this.addPeopleInterval);
Debug.log("Stopped adding people.")
} else {
Debug.log("Not currently adding people.")
}
}
module.exports = Main;
}; Program["com.mcleodgaming.elevator.util.Debug"]=function(module, exports){ var DebugJS;
module.inject = function () {
DebugJS = module.import('com.mcleodgaming.elevator.util', 'DebugJS');
};
var Debug = function Debug() {};
Debug.log = null;
Debug.warn = null;
Debug.init = function() {
//Debug.log = DebugAS3.log; //For AS3
//Debug.warn = DebugAS3.warn; //For AS3
Debug.log = DebugJS.log;
Debug.warn = DebugJS.warn;
};
Debug.$cinit = function () {
Debug.log = null;
Debug.warn = null;
};
Debug.prototype.$init = function() {}
module.exports = Debug;
};Program["com.mcleodgaming.elevator.util.DebugAS3"]=function(module, exports){
var DebugAS3 = function DebugAS3() {};
DebugAS3.log = function() {
var rest = Array.prototype.slice.call(arguments).splice(0); trace.apply(null, rest); };
DebugAS3.warn = function() {
var rest = Array.prototype.slice.call(arguments).splice(0); trace.apply(null, rest); };
DebugAS3.$cinit = function () {
};
DebugAS3.prototype.$init = function() {}
module.exports = DebugAS3;
};Program["com.mcleodgaming.elevator.util.DebugJS"]=function(module, exports){
var DebugJS = function DebugJS() {};
DebugJS.log = function() { console.log.apply(console, arguments); };
DebugJS.warn = function() { console.warn(console, arguments); };
DebugJS.$cinit = function () {
};
DebugJS.prototype.$init = function() {}
module.exports = DebugJS;
};Program["com.mcleodgaming.elevator.util.FrameTimer"]=function(module, exports){
var FrameTimer = function(length) {
this.$init(); this.m_initTime = length; this.m_currentTime = 0; };
FrameTimer.prototype.get_IsComplete = function() { return Boolean(this.m_currentTime >= this.m_initTime); };
FrameTimer.prototype.get_MaxTime = function() { return this.m_initTime; };
FrameTimer.prototype.get_CurrentTime = function() { return this.m_currentTime; };
FrameTimer.prototype.set_MaxTime = function(value) { if (value < 0) { this.m_initTime = 0; } else { this.m_initTime = value; if (this.m_currentTime > this.m_initTime) { this.m_currentTime = this.m_initTime; } } };
FrameTimer.prototype.set_CurrentTime = function(value) { if (value < 0) { this.m_currentTime = 0; } else { this.m_currentTime = (value > this.get_MaxTime()) ? this.get_MaxTime() : value; } };
FrameTimer.prototype.$init = function() {};
FrameTimer.prototype.m_initTime = 0;
FrameTimer.prototype.m_currentTime = 0;
FrameTimer.prototype.tick = function(amount) {
amount = AS3JS.Utils.getDefaultValue(amount, 1); if (this.m_currentTime < this.m_initTime) { this.m_currentTime = Math.min(this.m_initTime, this.m_currentTime + amount);//Just making it a bit faster. Might matter if it ticks a lot to remove 1 read/math/store operation } };
FrameTimer.prototype.finish = function() { this.m_currentTime = this.m_initTime; };
FrameTimer.prototype.reset = function() { this.m_currentTime = 0; }
module.exports = FrameTimer;
};Program["com.mcleodgaming.elevator.util.RandomNameGenerator"]=function(module, exports){
var RandomNameGenerator = function RandomNameGenerator() {};
RandomNameGenerator.names = null;
RandomNameGenerator.getRandomName = function() { return RandomNameGenerator.names[Math.round(Math.random() * (RandomNameGenerator.names.length-1))]; };
RandomNameGenerator.$cinit = function () {
RandomNameGenerator.names = ["Derek","Reginald","Hubert","Woodrow","Norma","Cheryl","Allison","Jamie","Bessie","Corey","Emily","Jeremiah","Lana","Rosemary","Willie","Alfred","Rex","Tomas","Carol","Bernard","Dean","Jan","Salvador","Antonio","John","Spencer","Bill","Angel","Clarence","Bethany","Joan","Jenna","Loretta","Marie","Jonathan","Kristi","Dolores","Eloise","Latoya","Krista","Earnest","Eduardo","Darin","Francis","Pauline","Arturo","Preston","Rosie","Sandra","Denise","Kari","Amanda","James","Darla","Alexis","Leon","Penny","Roland","Mark","Evan","Amos","Jennie","Susie","Peter","Johanna","Frederick","Christopher","Kerry","Taylor","Luz","Laverne","Greg","Marcus","Luke","Toby","Olivia","Patsy","Paula","Alex","Ernest","Myrtle","Ernesto","Eleanor","Nicole","Ivan","Claudia","Ray","Byron","Grace","Jenny","Glenda","Pablo","Maryann","Perry","Susan","Virginia","Jan","Ellen","Elsie","Hugh"];
};
RandomNameGenerator.prototype.$init = function() {}
module.exports = RandomNameGenerator;
}; Program["com.mcleodgaming.elevator.views.ElevatorView"]=function(module, exports){ var ElevatorState;
module.inject = function () {
ElevatorState = module.import('com.mcleodgaming.elevator.core', 'ElevatorState');
};
var ElevatorView = function(e) {
this.$init();
this.engine = e;
};
ElevatorView.prototype.$init = function() {
this.engine = null;};
ElevatorView.prototype.engine = null;
ElevatorView.prototype.onUpdate = function(event) {
};
ElevatorView.prototype.getInfo = function(elevator) {
//Information to display on the elevators themselves
var info = "";
var i = 0;
info += 'Floor: ' + elevator.currentFloor.name + '<br />';
info += 'State: ' + ElevatorState.asString(elevator.state) + '<br />';
info += 'Occupants: <br />';
if(elevator.people.length <= 0)
info += '[empty]';
for(i = 0; i < elevator.people.length; i++)
info += '-' + elevator.people[i].name + ' (' + elevator.people[i].targetFloor.name + ') ' + '<br />'
return info;
}
module.exports = ElevatorView;
};Program["com.mcleodgaming.elevator.views.ElevatorViewAS3"]=function(module, exports){ var ElevatorView = module.import('com.mcleodgaming.elevator.views', 'ElevatorView');
var Main, PersonState, ElevatorEngineEvent, EventDispatcher;
module.inject = function () {
Main = module.import('com.mcleodgaming.elevator', 'Main');
PersonState = module.import('com.mcleodgaming.elevator.core', 'PersonState');
ElevatorEngineEvent = module.import('com.mcleodgaming.elevator.events', 'ElevatorEngineEvent');
EventDispatcher = module.import('com.mcleodgaming.elevator.events', 'EventDispatcher');
};
var ElevatorViewAS3 = function(e) {
this.$init(); ElevatorView.call(this, e); var self = this; var i, mc, txtField; //Create display elements this.elevatorContainer = new MovieClip(); this.elevatorContainer.graphics.beginFill(0xffffff); this.elevatorContainer.graphics.lineStyle(1, 0x000000); this.elevatorContainer.graphics.drawRect(0, 0, Main.ROOT.stage.width, Main.ROOT.stage.height); this.elevatorContainer.graphics.endFill(); this.elevatorMCs = []; for(i = 0; i < this.engine.elevators.length; i++) { mc = new MovieClip(); //New elevator MC mc.x = 100 * i + 100; mc.y = 300; mc.graphics.beginFill(0xffffff); mc.graphics.lineStyle(1, 0x000000); mc.graphics.drawRect(0, 0, 100, 150); mc.graphics.endFill(); txtField = new TextField(); //Text field txtField.width = 100; txtField.height = 150; txtField.name = "txt"; mc.addChild(txtField); //Add text field to elevator MC this.elevatorMCs.push(mc); //Add elevator MC to list this.elevatorContainer.addChild(mc); //Add to main container } //Build floor table this.floorTable = new MovieClip(); this.floorMCs = []; for(i = 0; i < this.engine.floors.length; i++) { mc = new MovieClip(); //New elevator MC mc.y = 45 * i; mc.graphics.beginFill(0xffffff); mc.graphics.lineStyle(1, 0x000000); mc.graphics.drawRect(0, 0, 100, 45); mc.graphics.endFill(); txtField = new TextField(); txtField.width = 100; txtField.height = 45; txtField.multiline = true; txtField.wordWrap = true; txtField.name = "txt"; mc.addChild(txtField); this.floorTable.addChild(mc); this.floorMCs.push(mc); } //Make pause button to start/stop the engine and people adding timer this.pauseButton = new MovieClip(); this.pauseButton.ispaused = false; this.pauseButton.x = 600; this.pauseButton.graphics.beginFill(0xffffff); this.pauseButton.graphics.lineStyle(1, 0xffffff); this.pauseButton.graphics.drawRect(0, 0, 100, 45); this.pauseButton.graphics.endFill(); this.pauseButton.useHandCursor = true; this.pauseButton.mouseChildren = false; this.pauseButton.buttonMode = true; txtField = new TextField(); txtField.width = 200; txtField.height = 300; txtField.selectable = false; txtField.name = "txt"; txtField.text = "Pause"; txtField.mouseEnabled = false; this.pauseButton.addEventListener(MouseEvent.CLICK, function(e) { this.pauseButton.ispaused = !this.pauseButton.ispaused; if(this.pauseButton.ispaused) { TextField(this.pauseButton.getChildByName('txt')).text = "Play"; Main.ROOT.stopAddingPeople(); self.engine.stop(); } else { TextField(this.pauseButton.getChildByName('txt')).text = "Pause"; self.engine.start(); Main.ROOT.startAddingPeople(); } }); this.pauseButton.addChild(txtField); //Add to ROOT Main.ROOT.addChild(this.elevatorContainer); Main.ROOT.addChild(this.floorTable); Main.ROOT.addChild(this.pauseButton); //Ready to accept events EventDispatcher.dispatcher.addEventListener(ElevatorEngineEvent.UPDATE, this.onUpdate); trace('Initialized ElevatorView'); };
ElevatorViewAS3.prototype = Object.create(ElevatorView.prototype);
ElevatorViewAS3.prototype.$init = function() {
this.elevatorContainer = null;
this.elevatorMCs = null;
this.pauseButton = null;
this.floorTable = null;
this.floorMCs = null;};
ElevatorViewAS3.prototype.elevatorContainer = null;
ElevatorViewAS3.prototype.elevatorMCs = null;
ElevatorViewAS3.prototype.pauseButton = null;
ElevatorViewAS3.prototype.floorTable = null;
ElevatorViewAS3.prototype.floorMCs = null;
ElevatorViewAS3.prototype.onUpdate = function(event) { var i, j; //For each floor (Floor Table View) for(i = 0; i < this.engine.floors.length; i++) { TextField(this.floorMCs[this.engine.floors.length - i - 1].getChildByName('txt')).text = 'Floor ' + (i+1) + ': '; //For each person on this floor for(j = 0; j < this.engine.floors[i].people.length; j++) { var prepend = (j > 0) ? ", " : ""; if(this.engine.queue.indexOf(this.engine.floors[i].people[j]) >= 0) prepend += "*"; //Button press yet to be acknowledged else if(this.engine.floors[i].people[j].state == PersonState.LEAVING || this.engine.floors[i].people[j].state == PersonState.ENTERING) prepend += "+"; //Some elevator has responded to their button press //Concatenate all of the people (placing in the table from the bottom up) TextField(this.floorMCs[this.engine.floors.length - i - 1].getChildByName('txt')).text += (prepend + this.engine.floors[i].people[j].name + '(' + this.engine.floors[i].people[j].targetFloor.name + ')'); } } //Render elevators (Elevator View) for(i = 0; i < this.engine.elevators.length; i++) { var height = (this.elevatorMCs[i].height / this.elevatorContainer.height * 84); //<-Weird guessing to get the elevators to fit in the window var offset = (this.engine.elevators[i].currentFloor.index / this.engine.floors.length * height); this.elevatorMCs[i].y = 300 - Math.round(offset) * 10; //Lol guessing position here TextField(this.elevatorMCs[i].getChildByName('txt')).text = this.getInfo(this.engine.elevators[i]); } };
ElevatorViewAS3.prototype.getInfo = function(elevator) { return ElevatorView.prototype.getInfo.call(this, elevator).split("<br />").join("\n"); }
module.exports = ElevatorViewAS3;
};Program["com.mcleodgaming.elevator.views.ElevatorViewJS"]=function(module, exports){ var ElevatorView = module.import('com.mcleodgaming.elevator.views', 'ElevatorView');
var Main, PersonState, ElevatorEngineEvent, EventDispatcher, Debug;
module.inject = function () {
Main = module.import('com.mcleodgaming.elevator', 'Main');
PersonState = module.import('com.mcleodgaming.elevator.core', 'PersonState');
ElevatorEngineEvent = module.import('com.mcleodgaming.elevator.events', 'ElevatorEngineEvent');
EventDispatcher = module.import('com.mcleodgaming.elevator.events', 'EventDispatcher');
Debug = module.import('com.mcleodgaming.elevator.util', 'Debug');
};
var ElevatorViewJS = function(e) {
this.$init();
ElevatorView.call(this, e);
var i;
var self = this;
this.engine = e;
//Create table elements
this.elevatorTable = $('.elevator-view tbody');
this.elevatorRow = $('<tr/>'); //Store this for easy direct reference to rows
this.elevatorColumns = []; //Will place the columns to display elevators in here
this.elevatorTable.append(this.elevatorRow); //Add the row to the elevator table
//Build floor table
this.floorTable = $('.floor-view');
for(i = this.engine.floors.length - 1; i >= 0; i--) {
this.floorTable.append('<tr><td class="title">Floor ' + this.engine.floors[i].name + ':</td><td class="data"></td></tr>')
}
//Make pause button to start/stop the engine and people adding timer
this.pauseToggle = $('.btn-pause');
this.pauseToggle.click(function() {
$('.btn-pause').toggleClass('paused');
if($('.btn-pause').hasClass('paused')) {
$('.btn-pause').text('Play');
Main.ROOT.stopAddingPeople();
self.engine.stop();
} else {
$('.btn-pause').text('Pause');
self.engine.start();
Main.ROOT.startAddingPeople();
}
});
//Add elevator elements
for(i = 0; i < this.engine.elevators.length; i++) {
var col = $('<td/>').append($('<div/>').addClass('elevator'));
this.elevatorColumns.push(col);
this.elevatorRow.append(col);
}
//Ready to accept events
EventDispatcher.dispatcher.addEventListener(ElevatorEngineEvent.UPDATE, Main.eventHelper(this, this.onUpdate));
Debug.log('Initialized ElevatorView');
};
ElevatorViewJS.prototype = Object.create(ElevatorView.prototype);
ElevatorViewJS.prototype.$init = function() {
this.elevatorTable = null;
this.elevatorRow = null;
this.elevatorColumns = null;
this.floorTable = null;};
ElevatorViewJS.prototype.pauseToggle = false;
ElevatorViewJS.prototype.elevatorTable = null;
ElevatorViewJS.prototype.elevatorRow = null;
ElevatorViewJS.prototype.elevatorColumns = null;
ElevatorViewJS.prototype.floorTable = null;
ElevatorViewJS.prototype.onUpdate = function(event) {
var i, j;
//For each floor (Floor Table View)
for(i = 0; i < this.engine.floors.length; i++) {
$('tr', this.floorTable).eq(this.engine.floors.length - i - 1).find('.data').text('');
//For each person on this floor
for(j = 0; j < this.engine.floors[i].people.length; j++) {
var prepend = (j > 0) ? ", " : "";
if(this.engine.queue.indexOf(this.engine.floors[i].people[j]) >= 0)
prepend += "*"; //Button press yet to be acknowledged
else if(this.engine.floors[i].people[j].state == PersonState.LEAVING || this.engine.floors[i].people[j].state == PersonState.ENTERING)
prepend += "+"; //Some elevator has responded to their button press
//Concatenate all of the people (placing in the table from the bottom up)
$('tr', this.floorTable).eq(this.engine.floors.length - i - 1).find('.data').append(prepend + this.engine.floors[i].people[j].name + '(' + this.engine.floors[i].people[j].targetFloor.name + ')');
}
}
//Render elevators (Elevator View)
for(i = 0; i < this.engine.elevators.length; i++) {
var height = (this.elevatorColumns[i].height() / this.elevatorTable.height() * 84); //<-Weird guessing to get the elevators to fit in the window
var offset = (this.engine.elevators[i].currentFloor.index / this.engine.floors.length * height);
this.elevatorColumns[i].find('.elevator').css('bottom', Math.round(offset) + '%').html(this.getInfo(this.engine.elevators[i])); //<-TODO: Change this later I guess, % is weird
}
}
module.exports = ElevatorViewJS;
};if(typeof module !== 'undefined'){module.exports=AS3JS.load({program:Program,entry:"com.mcleodgaming.elevator.Main",entryMode:"instance"});}else if(typeof window!=='undefined'&&typeof AS3JS!=='undefined'){window['com.mcleodgaming.elevator.Main']=AS3JS.load({program:Program,entry:"com.mcleodgaming.elevator.Main",entryMode:"instance"});}})();