-
Notifications
You must be signed in to change notification settings - Fork 318
/
documentation.html
164 lines (156 loc) · 12.7 KB
/
documentation.html
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
<!DOCTYPE html>
<html>
<head>
<title>Elevator Saga - help and API documentation</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="font-awesome-4.1-1.0/css/font-awesome.min.css" type="text/css" media="all">
<link href='//fonts.googleapis.com/css?family=Oswald:400,700,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="style.css" type="text/css" media="all">
<link rel="stylesheet" href="libs/highlight/styles/default.css" type="text/css" media="all">
<link rel="stylesheet" href="libs/highlight/styles/solarized_light.css" type="text/css" media="all">
<script type="text/javascript" src="libs/highlight/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<div class="container" style="width: 960px">
<div class="header">
<h1>Elevator Saga <em class="emphasis-color">Help and API documentation</em></h1>
</div>
<div class="help">
<h2>About the game</h2>
<p>
This is a game of programming!<br>
Your task is to program the movement of elevators, by writing a program in <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide">JavaScript</a>.
</p>
<p>
The goal is to transport people in an efficient manner.<br>
Depending on how well you do it, you can progress through the ever more difficult challenges.<br>
Only the very best programs will be able to complete all the challenges.
</p>
<h2>How to play</h2>
<p>
Enter your code in the input window below the game view, and press the <span class="emphasis-color">Apply</span> button to start the challenge.<br>
You can increase or decrease the speed of time by pressing the <i class="fa fa-plus emphasis-color"></i> and <i class="fa fa-minus emphasis-color"></i> buttons.
</p>
<p>
If your program contains an error, you can use the developer tools in your web browser to try and debug it.
If you want to start over with the code, press the <span class="emphasis-color">Reset</span> button. This will revert the code to a working but simplistic implementation.<br>
If you have a favorite text editor, such as <a href="http://www.sublimetext.com/">Sublime Text</a>, feel free to edit the code there and paste it into the game editor.<br>
Your code is automatically saved in your local storage, so don't worry - it doesn't disappear if you accidentally close the browser.
</p>
<h2>Basics</h2>
<p>
Your code must declare an object containing at least two functions called <span class="emphasis-color">init</span> and <span class="emphasis-color">update</span>. Like this:
</p>
<div><pre><code>{
init: function(elevators, floors) {
// Do stuff with the elevators and floors, which are both arrays of objects
},
update: function(dt, elevators, floors) {
// Do more stuff with the elevators and floors
// dt is the number of game seconds that passed since the last time update was called
}
}</code></pre></div>
<p>These functions will then be called by the game during the challenge.<br>
<span class="emphasis-color">init</span> will be called when the challenge starts, and <span class="emphasis-color">update</span> repeatedly during the challenge.
</p>
<p>
Normally you will put most of your code in the <span class="emphasis-color">init</span> function, to set up event listeners and logic.
</p>
<h2>Code examples</h2>
<h3>How to control an elevator</h3>
<dl>
<dt><pre><code>elevator.goToFloor(1);</code></pre></dt>
<dd>Tell the elevator to move to floor 1 after completing other tasks, if any. Note that this will have no effect if the elevator is already queued to go to that floor.</dd>
<dt><pre><code>if(elevator.currentFloor() > 2) { ... }</code></pre></dt>
<dd>Calling currentFloor gets the floor number that the elevator currently is on. Note that this is a rounded number and does not necessarily mean the elevator is in a stopped state.</dd>
</dl>
<h3>Listening for events</h3>
<p>It is possible to listen for events, like when stopping at a floor, or a button has been pressed.</p>
<dl>
<dt><pre><code>elevator.on("idle", function() { elevator.goToFloor(0); });</code></pre></dt>
<dd>Listen for the "idle" event issued by the elevator, when the task queue has been emptied and the elevator is doing nothing. In this example we tell it to move to floor 0.</dd>
<dt><pre><code>elevator.on("floor_button_pressed", function(floorNum) { ... } );</code></pre></dt>
<dd>Listen for the "floor_button_pressed" event, issued when a passenger pressed a button inside the elevator. This indicates that the passenger wants to go to that floor.</dd>
<dt><pre><code>floor.on("up_button_pressed", function() { ... } );</code></pre></dt>
<dd>Listen for the "up_button_pressed" event, issued when a passenger pressed the up button on the floor they are waiting on. This indicates that the passenger wants to go to another floor.</dd>
</dl>
<h2 id="docs">API documentation</h2>
<h3>Elevator object</h3>
<table class="doctable">
<thead><th width="150">Property</th><th width="90">Type</th><th width="190">Explanation</th><th width="*">Example</th></thead>
<tbody>
<tr><td>goToFloor</td><td>function</td><td><small>Queue the elevator to go to specified floor number. If you specify true as second argument, the elevator will go to that floor directly, and then go to any other queued floors.</small></td><td><pre><code>elevator.goToFloor(3); // Do it after anything else
elevator.goToFloor(2, true); // Do it before anything else</code></pre></td></tr>
<tr><td>stop</td><td>function</td><td><small>Clear the destination queue and stop the elevator if it is moving. Note that you normally don't need to stop elevators - it is intended for advanced solutions with in-transit rescheduling logic. Also, note that the elevator will probably not stop at a floor, so passengers will not get out.</small></td><td><pre><code>elevator.stop();</code></pre></td></tr>
<tr><td>currentFloor</td><td>function</td><td><small>Gets the floor number that the elevator currently is on.</small></td><td><pre><code>if(elevator.currentFloor() === 0) {
// Do something special?
}</code></pre></td></tr>
<tr><td>goingUpIndicator</td><td>function</td><td><small>Gets or sets the going up indicator, which will affect passenger behaviour when stopping at floors.</small></td><td><pre><code>if(elevator.goingUpIndicator()) {
elevator.goingDownIndicator(false);
}</code></pre></td></tr>
<tr><td>goingDownIndicator</td><td>function</td><td><small>Gets or sets the going down indicator, which will affect passenger behaviour when stopping at floors.</small></td><td><pre><code>if(elevator.goingDownIndicator()) {
elevator.goingUpIndicator(false);
}</code></pre></td></tr>
<tr><td>maxPassengerCount</td><td>function</td><td><small>Gets the maximum number of passengers that can occupy the elevator at the same time.</small></td><td><pre><code>if(elevator.maxPassengerCount() > 5) {
// Use this elevator for something special, because it's big
}</code></pre></td></tr>
<tr><td>loadFactor</td><td>function</td><td><small>Gets the load factor of the elevator. 0 means empty, 1 means full. Varies with passenger weights, which vary - not an exact measure.</small></td><td><pre><code>if(elevator.loadFactor() < 0.4) {
// Maybe use this elevator, since it's not full yet?
}</code></pre></td></tr>
<tr><td>destinationDirection</td><td>function</td><td><small>Gets the direction the elevator is currently going to move toward. Can be "up", "down" or "stopped".</small></td></tr>
<tr><td>destinationQueue</td><td>array</td><td><small>The current destination queue, meaning the floor numbers the elevator is scheduled to go to. Can be modified and emptied if desired. Note that you need to call checkDestinationQueue() for the change to take effect immediately.</small></td><td><pre><code>elevator.destinationQueue = [];
elevator.checkDestinationQueue();</code></pre></td></tr>
<tr><td>checkDestinationQueue</td><td>function</td><td><small>Checks the destination queue for any new destinations to go to. Note that you only need to call this if you modify the destination queue explicitly.</small></td><td><pre><code>elevator.checkDestinationQueue();</code></pre></td></tr>
<tr><td>getPressedFloors</td><td>function</td><td><small>Gets the currently pressed floor numbers as an array.</small></td><td><pre><code>if(elevator.getPressedFloors().length > 0) {
// Maybe go to some chosen floor first?
}</code></pre></td></tr>
</tbody>
</table>
<table class="doctable">
<thead><th width="150">Event</th><th width="280">Explanation</th><th>Example</th></thead>
<tbody>
<tr><td>idle</td><td><small>Triggered when the elevator has completed all its tasks and is not doing anything.</small></td><td><pre><code>elevator.on("idle", function() { ... });</code></pre></td></tr>
<tr><td>floor_button_pressed</td><td><small>Triggered when a passenger has pressed a button inside the elevator.</small></td><td><pre><code>elevator.on("floor_button_pressed", function(floorNum) {
// Maybe tell the elevator to go to that floor?
})</code></pre></td></tr>
<tr><td>passing_floor</td><td><small>Triggered slightly before the elevator will pass a floor. A good time to decide whether to stop at that floor. Note that this event is not triggered for the destination floor. Direction is either "up" or "down".</small></td><td><pre><code>elevator.on("passing_floor", function(floorNum, direction) { ... });</code></pre></td></tr>
<tr><td>stopped_at_floor</td><td><small>Triggered when the elevator has arrived at a floor.</small></td><td><pre><code>elevator.on("stopped_at_floor", function(floorNum) {
// Maybe decide where to go next?
})</code></pre></td></tr>
</tbody>
</table>
<h3>Floor object</h3>
<table class="doctable">
<thead><th width="150">Property</th><th width="90">Type</th><th width="190">Explanation</th><th width="*">Example</th></thead>
<tbody>
<tr><td>floorNum</td><td>function</td><td><small>Gets the floor number of the floor object.</small></td><td><pre><code>if(floor.floorNum() > 3) { ... }</code></pre></td></tr>
</tbody>
</table>
<table class="doctable">
<thead><th width="150">Event</th><th width="280">Explanation</th><th>Example</th></thead>
<tbody>
<tr><td>up_button_pressed</td><td><small>Triggered when someone has pressed the up button at a floor. Note that passengers will press the button again if they fail to enter an elevator.</small></td><td><pre><code>floor.on("up_button_pressed", function() {
// Maybe tell an elevator to go to this floor?
})</code></pre></td></tr>
<tr><td>down_button_pressed</td><td><small>Triggered when someone has pressed the down button at a floor. Note that passengers will press the button again if they fail to enter an elevator.</small></td><td><pre><code>floor.on("down_button_pressed", function() {
// Maybe tell an elevator to go to this floor?
})</code></pre></td></tr>
</tbody>
</table>
</div>
<div class="footer">
<h4>Made by Magnus Wolffelt and contributors</h4>
<h4><a href="https://github.com/magwo/elevatorsaga">Source code</a> on GitHub</h4>
</div>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-56810935-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>