Constructor
# new Vehicle ()
Example
Example of a vehicle going back and forth between cities A, B and C
function init (vehicles, cities, passengers, game) {
let vehicle = vehicles[0];
vehicle.moveTo(['B', 'C']);
vehicle.on('visitCity', function(city) {
if (city.id == 'C')
vehicle.moveTo(['B', 'A']);
else if (city.id == 'C')
vehicle.moveTo(['B', 'A']);
});
}
# Members
# Vehicle.id {String}
constant
Type: |
String
|
Description
The vehicle's id. It is a letter between "A" and "Z".
# Vehicle.type {VehicleType}
constant
Type: |
VehicleType
|
Description
Contains the information about this type of vehicle: capacity and type name.
Example
game.log.debug(vehicle.type.name); // outputs "car" or "bus"
game.log.debug(vehicle.type.capacity); // outputs 4 or 10
# Vehicle.passengersCapacity {Integer}
constant
Type: |
Integer
|
Description
Integer that informs how many Passengers can fit inside this vehicle based on this VehicleType. Basically a shortcut to Vehicle.type.capacity
.
# Vehicle.passengers {Array.<Passenger>}
Type: |
Array.<Passenger>
|
Description
List of Passengers that are currently inside this vehicle.
# Vehicle.lastCity {City}
Type: |
City
|
Description
The last City that this vehicle has been through.
# Vehicle.currentCity {City}
Type: |
City
|
Description
Current City that this vehicle is in. This property is undefined
if the vehicle is travelling over Roads.
# Vehicle.currentRoute {Array.<City>}
Type: |
Array.<City>
|
Description
Destination route of this vehicle, a list of cities. Each call of moveTo(city) adds a city to the end of this list. The city is only removed when the vehicle reaches it.
# Methods
# Vehicle.moveTo (targetCity)
Parameters:
Name | Type | Description |
---|---|---|
targetCity
|
City
|
Array.<City>
|
String
|
Array.<String>
|
Instance(s) of City or only their ID(s). |
Description
Method responsable for setting the destination of this vehicle. If the vehicle already has planned moves, the argument City is appended to this vehicle's current route. This method can receive both instances of City or only their IDs.
Example
// using a City instance
vehicle.moveTo(city);
// or by using only id
vehicle.moveTo('C');
# Vehicle.load (passengers)
Parameters:
Name | Type | Description |
---|---|---|
passengers
|
Passenger
|
Array.<Passenger>
|
Passenger(s) to load |
Description
# Vehicle.unload (passengers)
Parameters:
Name | Type | Description |
---|---|---|
passengers
|
Passenger
|
Array.<Passenger>
|
Passenger(s) to unload |
Description
Unloads a Passenger or a list of them to the vehicle's current city. They vehicle must be visiting the city to be able to drop passengers.
# Events
# Vehicle.on:idle
Parameters:
Name | Type | Description |
---|---|---|
name
|
'idle'
|
Name of this event |
onIdle
|
function
|
Function that is called when this vehicle is idle for more then 1 second, receives the current City by param. |
Description
Event that is triggered whenever this vehicle visits a city and stops moving for more than a second.
Example
function init (vehicles, cities, passengers, game) {
let vehicle = vehicles[0];
vehicle.on('idle', function(city) {
vehicle.moveTo('A');
});
}
# Vehicle.on:visitCity
Parameters:
Name | Type | Description |
---|---|---|
name
|
'visitCity'
|
Name of this event |
onVisitCity
|
function
|
Function that is called when this vehicle visits a city, receives the visited City by param. |
Description
Event that is triggered whenever this vehicle visits a city
Example
function init (vehicles, cities, passengers, game) {
let vehicle = vehicles[0];
vehicle.on('visitCity', function(city) {
if (city.id === 'A') {
vehicle.moveTo('B');
}
});
}
# Vehicle.on:loadPassenger
Parameters:
Name | Type | Description |
---|---|---|
name
|
'loadPassenger'
|
Name of this event |
onLoadPassenger
|
function
|
Function that is called when this vehicle loads a Passenger, receives the loaded Passenger and his City by param. |
Description
Event that is triggered whenever this vehicle loads a Passenger
Example
function init (vehicles, cities, passengers, game) {
let vehicle = vehicles[0];
vehicle.on('loadPassenger', function(passenger, city) {
// do something
});
}