Vehicular simulation game example
const Vehicle = {
speed: 0,
maxSpeed: 100,
accelerate: (amount) => {
this.speed += amount;
if (this.speed > this.maxSpeed) {
this.speed = this.maxSpeed;
}
},
decelerate: (amount) => {
this.speed -= amount;
if (this.speed < 0) {
this.speed = 0;
}
}
};
const Car = {
name: 'Car',
maxSpeed: 120
};
const Bike = {
name: 'Bike',
maxSpeed: 60
};
Car.accelerate(30);
console.log(Car.speed); // 30
Car.accelerate(80);
console.log(Car.speed); // 100
Bike.accelerate(30);
console.log(Bike.speed); // 30
Bike.decelerate(20);
console.log(Bike.speed); // 10Last updated