Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a hierarchy of classes to simulate vehicles
- All vehicles should have speed and propulsion units (things which make them move) and a Accelerate method, which should update their speed by summing it with the acceleration of their propulsion units
- Each propulsion unit can produce acceleration (change in speed). There should be three types of propulsion units - wheels, propelling nozzles and propellers
- A wheel should have a radius and the acceleration it produces should be equal to its perimeter
- A propelling nozzle should have power and an afterburner switch. The acceleration the nozzle should produce as much acceleration as it has power, but if the afterburner is on it should produce double acceleration.
- A propeller should have a number of fins and a spin direction. The acceleration a propeller produces should by default be equal to the number of fins it has. The spin direction should be clockwise and counter-clockwise. If the spin direction is counter-clockwise, the acceleration the propeller produces should be negative, if the spin direction is clockwise, the acceleration should be positive.
- There should be land, air and water vehicles. Land vehicles should have 4 wheels, air vehicles should have 1 propelling nozzles and water vehicles should have a customizable number of propellers (passed in the constructor).
- Air vehicles should have the ability to switch on/off their afterburners.
- Water vehicles should have the ability to change the spin direction of their propellers.
- Implement one additional amphibious vehicle. It should both have a propeller (so it can move on water) and wheels (so it can move on land). The amphibious vehicle should be able to switch between land and water mode and it's speed property and Accelerate method should respectively depend on its wheels in the first case and on its propeller in the second case */
- 'use strict';
- var vehicles = (function () {
- // Easy inheritance
- Function.prototype.inherit = function (parent) {
- this.prototype = new parent();
- this.prototype.constructor = parent;
- }
- // Enumerations
- var afterBurnerState = {
- "ON": 0,
- "OFF": 1
- }
- var rotateDirection = {
- "CLOCKWISE": 0,
- "COUNTERCLOCKWISE": 1
- }
- var travelMode = {
- "LAND": 0,
- "WATER": 1
- }
- // Base class for propultion units
- function PropultionUnit(motorCount) {
- this.motorCount = motorCount;
- }
- // Wheels
- function Wheel(radius, motorCount) {
- this.radius = radius;
- PropultionUnit.call(this, motorCount);
- }
- Wheel.inherit(PropultionUnit);
- Wheel.prototype.accelerate = function () {
- var speed = this.radius * 2 * Math.PI;
- speed *= this.motorCount;
- return speed;
- }
- // Propelling Nozzle
- function Nozzle(power, afterBurner, motorCount) {
- this.power = power;
- this.afterBurner = afterBurner;
- PropultionUnit.call(this, motorCount);
- }
- Nozzle.inherit(PropultionUnit);
- Nozzle.prototype.accelerate = function () {
- var speed = 0;
- if (this.afterBurner === afterBurnerState.ON) {
- speed = this.power * 2;
- } else {
- speed = this.power;
- }
- speed *= this.motorCount;
- return speed;
- }
- // Propeler
- function Propeler(finsCount, spinDirection, motorCount) {
- this.finsCount = finsCount;
- this.spinDirection = spinDirection;
- PropultionUnit.call(this, motorCount);
- }
- Propeler.inherit(PropultionUnit);
- Propeler.prototype.accelerate = function () {
- var speed;
- if (this.spinDirection === rotateDirection.CLOCKWISE) {
- speed = this.finsCount;
- } else {
- speed = this.finsCount * -1;
- }
- speed *= this.motorCount;
- return speed;
- }
- // Vehicle bace class
- function Vehicle(propulsionUnits) {
- this.propulsionUnits = propulsionUnits;
- this.speed = 0;
- }
- Vehicle.prototype.getAcceleration = function () {
- var speed = 0;
- for (var i = 0; i < this.propulsionUnits.length; i++) {
- speed += this.propulsionUnits[i].accelerate();
- }
- this.speed = speed;
- }
- // Land vehicle
- function LandVehicle(propulsionUnits) {
- Vehicle.call(this, propulsionUnits);
- }
- LandVehicle.inherit(Vehicle);
- // Air vehicle
- function AirVehicle(propulsionUnits) {
- Vehicle.call(this, propulsionUnits);
- }
- AirVehicle.inherit(Vehicle);
- AirVehicle.prototype.afterBurnerSwitch = function () {
- for (var i = 0; i < this.propulsionUnits.length; i++) {
- var self = this.propulsionUnits[i];
- if(self instanceof Nozzle){
- if (self.afterBurner === afterBurnerState.ON) {
- self.afterBurner = afterBurnerState.OFF;
- } else {
- self.afterBurner = afterBurnerState.ON;
- }
- }
- }
- }
- // Water vechicle
- function WaterVehicle(propulsionUnits) {
- Vehicle.call(this, propulsionUnits);
- }
- WaterVehicle.inherit(Vehicle);
- WaterVehicle.prototype.spinDirectionSwitch = function () {
- for (var i = 0; i < this.propulsionUnits.length; i++) {
- var self = this.propulsionUnits[i];
- if (self.spinDirection === rotateDirection.CLOCKWISE) {
- self.spinDirection = rotateDirection.COUNTERCLOCKWISE;
- } else {
- self.spinDirection = rotateDirection.CLOCKWISE;
- }
- }
- }
- // Amphibious vechicle
- function AmphibiousVehicle(mode, propulsionUnits) {
- this.mode = mode;
- Vehicle.call(this, propulsionUnits);
- }
- AmphibiousVehicle.inherit(Vehicle);
- AmphibiousVehicle.inherit(WaterVehicle);
- AmphibiousVehicle.prototype.switchMode = function () {
- if (this.mode === travelMode.LAND) {
- this.mode = travelMode.WATER;
- } else {
- this.mode = travelMode.LAND;
- }
- }
- AmphibiousVehicle.prototype.getAcceleration = function () {
- this.speed = 0;
- for (var i = 0; i < this.propulsionUnits.length; i++) {
- var self = this.propulsionUnits[i];
- if (self instanceof Wheel && this.mode === travelMode.LAND) {
- alert(self.accelerate());
- this.speed += self.accelerate();
- }
- if (self instanceof Propeler && this.mode === travelMode.WATER) {
- this.speed += self.accelerate();
- }
- }
- }
- // Public
- return {
- afterBurnerState: afterBurnerState,
- rotateDirection: rotateDirection,
- travelMode: travelMode,
- Wheel: Wheel,
- Nozzle: Nozzle,
- Propeler: Propeler,
- LandVehicle: LandVehicle,
- AirVehicle: AirVehicle,
- WaterVehicle: WaterVehicle,
- AmphibiousVehicle: AmphibiousVehicle,
- }
- }());
Advertisement
Add Comment
Please, Sign In to add comment