-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (72 loc) · 2.62 KB
/
index.js
File metadata and controls
86 lines (72 loc) · 2.62 KB
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
document.addEventListener('DOMContentLoaded', function(){
initialize();
}, false);
function initialize() {
console.log("Initializing");
var canvas = document.getElementById("canvas");
renderer.initializeCanvas(canvas);
var initialState = generateInitialState();
console.log(initialState);
smartLogic.initialize(initialState);
var logic;
var requestedLogic = getUrlParam("logic", "smart");
if (requestedLogic == "random") {
logic = randomLogic;
console.log("Using random logic");
} else if (requestedLogic == "simple") {
logic = simpleLogic;
console.log("Using simple logic");
} else {
logic = smartLogic;
console.log("Using smart logic");
}
looper.initialize(initialState, logic);
var speedFactor = getUrlParam("speedFactor", 25);
looper.setSpeedFactor(speedFactor);
document.getElementById("slider").value = speedFactor;
looper.startLooping();
renderer.startRenderingStates();
}
// generates a state that will be used for
// initializing the looper
function generateInitialState() {
var peopleCount = getUrlParam("peopleCount", 100);
var elevatorCount = getUrlParam("elevatorCount", 4);
var levelCount = getUrlParam("levelCount", 6);
var initialState = generator.initialize(peopleCount, elevatorCount, levelCount);
return initialState;
}
// generates a state that can be used to test
// the state rendering
function generateFakeState() {
var initialState = generateInitialState();
for (var elevatorIndex = 0; elevatorIndex < initialState.elevators.length; elevatorIndex++) {
var people = Math.floor(Math.random() * 10);
for (var peopleIndex = 0; peopleIndex < people; peopleIndex++) {
initialState.elevators[elevatorIndex].people.push(peopleIndex);
}
var level = Math.floor(Math.random() * (initialState.levels.length - 1));
initialState.elevators[elevatorIndex].currentLevel = level;
}
for (var levelIndex = 0; levelIndex < initialState.levels.length; levelIndex++) {
var people = Math.floor(Math.random() * 10);
for (var peopleIndex = 0; peopleIndex < people; peopleIndex++) {
initialState.levels[levelIndex].people.push(peopleIndex);
}
}
return initialState;
}
function getRandomNumberBetween(minMin, maxMin) {
return Math.floor(Math.random() * (maxMin - minMin + 1)) + minMin;
}
function getUrlParam(sParam, defaultValue) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
return defaultValue;
}