电梯传奇 帮助和 API 文档

关于游戏

这是一个编程游戏!
你的任务是通过用 JavaScript编写程序来编程电梯的运动。

目标是以有效的方式运送人员。
根据你做得有多好,你可以在越来越困难的挑战中取得进步。
只有最好的程序才能完成所有挑战。

怎么玩

在游戏视图下方的输入窗口中输入您的代码,然后按 应用 按钮开始挑战。
您可以通过按 按钮来增加或减少时间速度。

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 Reset button. This will revert the code to a working but simplistic implementation.
如果您有最喜欢的文本编辑器,例如 Sublime Text, 请随时编辑其中的代码并将其粘贴到游戏编辑器中。
您的代码会自动保存在本地存储中,因此不用担心 - 如果您不小心关闭了浏览器,它不会消失。

基本

您的代码必须声明一个对象,其中包含至少两个名为 init and update. 的函数。喜欢这个:

{
    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
    }
}

These functions will then be called by the game during the challenge.
init will be called when the challenge starts, and update repeatedly during the challenge.

Normally you will put most of your code in the init function, to set up event listeners and logic.

代码示例

如何控制电梯

elevator.goToFloor(1);
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.
if(elevator.currentFloor() > 2) { ... }
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.

侦听事件

It is possible to listen for events, like when stopping at a floor, or a button has been pressed.

elevator.on("idle", function() { elevator.goToFloor(0); });
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.
elevator.on("floor_button_pressed", function(floorNum) { ... } );
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.
floor.on("up_button_pressed", function() { ... } );
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.

API 文档

电梯对象

PropertyTypeExplanationExample
goToFloorfunctionQueue 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.
elevator.goToFloor(3); // Do it after anything else
elevator.goToFloor(2, true); // Do it before anything else
stopfunctionClear 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.
elevator.stop();
currentFloorfunctionGets the floor number that the elevator currently is on.
if(elevator.currentFloor() === 0) {
    // Do something special?
}
goingUpIndicatorfunctionGets or sets the going up indicator, which will affect passenger behaviour when stopping at floors.
if(elevator.goingUpIndicator()) {
    elevator.goingDownIndicator(false);
}
goingDownIndicatorfunctionGets or sets the going down indicator, which will affect passenger behaviour when stopping at floors.
if(elevator.goingDownIndicator()) {
    elevator.goingUpIndicator(false);
}
maxPassengerCountfunctionGets the maximum number of passengers that can occupy the elevator at the same time.
if(elevator.maxPassengerCount() > 5) {
    // Use this elevator for something special, because it's big
}
loadFactorfunctionGets the load factor of the elevator. 0 means empty, 1 means full. Varies with passenger weights, which vary - not an exact measure.
if(elevator.loadFactor() < 0.4) {
    // Maybe use this elevator, since it's not full yet?
}
destinationDirectionfunctionGets the direction the elevator is currently going to move toward. Can be "up", "down" or "stopped".
destinationQueuearrayThe 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.
elevator.destinationQueue = [];
elevator.checkDestinationQueue();
checkDestinationQueuefunctionChecks 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.
elevator.checkDestinationQueue();
getPressedFloorsfunctionGets the currently pressed floor numbers as an array.
if(elevator.getPressedFloors().length > 0) {
    // Maybe go to some chosen floor first?
}
EventExplanationExample
idleTriggered when the elevator has completed all its tasks and is not doing anything.
elevator.on("idle", function() { ... });
floor_button_pressedTriggered when a passenger has pressed a button inside the elevator.
elevator.on("floor_button_pressed", function(floorNum) {
    // Maybe tell the elevator to go to that floor?
})
passing_floorTriggered 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".
elevator.on("passing_floor", function(floorNum, direction) { ... });
stopped_at_floorTriggered when the elevator has arrived at a floor.
elevator.on("stopped_at_floor", function(floorNum) {
    // Maybe decide where to go next?
})

Floor object

PropertyTypeExplanationExample
floorNumfunctionGets the floor number of the floor object.
if(floor.floorNum() > 3) { ... }
EventExplanationExample
up_button_pressedTriggered 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.
floor.on("up_button_pressed", function() {
    // Maybe tell an elevator to go to this floor?
})
down_button_pressedTriggered 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.
floor.on("down_button_pressed", function() {
    // Maybe tell an elevator to go to this floor?
})