sashomaga

Simple inheritance

Apr 28th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a hierarchy of classes to simulate vehicles
  2. 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
  3. Each propulsion unit can produce acceleration (change in speed). There should be three types of propulsion units - wheels, propelling nozzles and propellers
  4. A wheel should have a radius and the acceleration it produces should be equal to its perimeter
  5. 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.
  6. 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.
  7. 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).
  8. Air vehicles should have the ability to switch on/off their afterburners.
  9. Water vehicles should have the ability to change the spin direction of their propellers.
  10. 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 */
  11.  
  12.  
  13.  
  14. 'use strict';
  15.  
  16. var vehicles = (function () {
  17.     // Easy inheritance
  18.     Function.prototype.inherit = function (parent) {
  19.         this.prototype = new parent();
  20.         this.prototype.constructor = parent;
  21.     }
  22.     // Enumerations
  23.     var afterBurnerState = {
  24.         "ON": 0,
  25.         "OFF": 1
  26.     }
  27.     var rotateDirection = {
  28.         "CLOCKWISE": 0,
  29.         "COUNTERCLOCKWISE": 1
  30.     }
  31.     var travelMode = {
  32.         "LAND": 0,
  33.         "WATER": 1
  34.     }
  35.  
  36.     // Base class for propultion units
  37.     function PropultionUnit(motorCount) {
  38.         this.motorCount = motorCount;
  39.     }
  40.     // Wheels
  41.     function Wheel(radius, motorCount) {
  42.         this.radius = radius;
  43.         PropultionUnit.call(this, motorCount);
  44.     }
  45.  
  46.     Wheel.inherit(PropultionUnit);
  47.  
  48.     Wheel.prototype.accelerate = function () {
  49.         var speed = this.radius * 2 * Math.PI;
  50.         speed *= this.motorCount;
  51.         return speed;
  52.     }
  53.  
  54.     // Propelling Nozzle
  55.     function Nozzle(power, afterBurner, motorCount) {
  56.         this.power = power;
  57.         this.afterBurner = afterBurner;
  58.         PropultionUnit.call(this, motorCount);
  59.     }
  60.  
  61.     Nozzle.inherit(PropultionUnit);
  62.  
  63.     Nozzle.prototype.accelerate = function () {
  64.         var speed = 0;
  65.         if (this.afterBurner === afterBurnerState.ON) {
  66.             speed = this.power * 2;
  67.         } else {
  68.             speed = this.power;
  69.         }
  70.         speed *= this.motorCount;
  71.         return speed;
  72.     }
  73.  
  74.     // Propeler
  75.     function Propeler(finsCount, spinDirection, motorCount) {
  76.         this.finsCount = finsCount;
  77.         this.spinDirection = spinDirection;
  78.         PropultionUnit.call(this, motorCount);
  79.     }
  80.  
  81.     Propeler.inherit(PropultionUnit);
  82.  
  83.     Propeler.prototype.accelerate = function () {
  84.         var speed;
  85.         if (this.spinDirection === rotateDirection.CLOCKWISE) {
  86.             speed = this.finsCount;
  87.         } else {
  88.             speed = this.finsCount * -1;
  89.         }
  90.         speed *= this.motorCount;
  91.         return speed;
  92.     }
  93.  
  94.     // Vehicle bace class
  95.     function Vehicle(propulsionUnits) {
  96.         this.propulsionUnits = propulsionUnits;
  97.         this.speed = 0;
  98.     }
  99.  
  100.     Vehicle.prototype.getAcceleration = function () {
  101.         var speed = 0;
  102.         for (var i = 0; i < this.propulsionUnits.length; i++) {
  103.             speed += this.propulsionUnits[i].accelerate();
  104.         }    
  105.         this.speed = speed;    
  106.     }
  107.  
  108.     // Land vehicle
  109.     function LandVehicle(propulsionUnits) {
  110.         Vehicle.call(this, propulsionUnits);
  111.     }
  112.  
  113.     LandVehicle.inherit(Vehicle);
  114.  
  115.     // Air vehicle
  116.     function AirVehicle(propulsionUnits) {
  117.         Vehicle.call(this, propulsionUnits);
  118.     }
  119.  
  120.     AirVehicle.inherit(Vehicle);
  121.  
  122.     AirVehicle.prototype.afterBurnerSwitch = function () {
  123.         for (var i = 0; i < this.propulsionUnits.length; i++) {
  124.             var self = this.propulsionUnits[i];
  125.             if(self instanceof Nozzle){
  126.                 if (self.afterBurner === afterBurnerState.ON) {
  127.                     self.afterBurner = afterBurnerState.OFF;
  128.                 } else {
  129.                     self.afterBurner = afterBurnerState.ON;
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135.     // Water vechicle
  136.     function WaterVehicle(propulsionUnits) {
  137.         Vehicle.call(this, propulsionUnits);
  138.     }
  139.  
  140.     WaterVehicle.inherit(Vehicle);
  141.  
  142.     WaterVehicle.prototype.spinDirectionSwitch = function () {
  143.         for (var i = 0; i < this.propulsionUnits.length; i++) {
  144.             var self = this.propulsionUnits[i];
  145.             if (self.spinDirection === rotateDirection.CLOCKWISE) {
  146.                 self.spinDirection = rotateDirection.COUNTERCLOCKWISE;
  147.             } else {
  148.                 self.spinDirection = rotateDirection.CLOCKWISE;
  149.             }
  150.         }      
  151.     }
  152.  
  153.     // Amphibious vechicle
  154.     function AmphibiousVehicle(mode, propulsionUnits) {
  155.         this.mode = mode;
  156.         Vehicle.call(this, propulsionUnits);
  157.     }
  158.  
  159.     AmphibiousVehicle.inherit(Vehicle);
  160.     AmphibiousVehicle.inherit(WaterVehicle);
  161.  
  162.     AmphibiousVehicle.prototype.switchMode = function () {
  163.         if (this.mode === travelMode.LAND) {
  164.             this.mode = travelMode.WATER;
  165.         } else {
  166.             this.mode = travelMode.LAND;
  167.         }
  168.     }
  169.  
  170.     AmphibiousVehicle.prototype.getAcceleration = function () {
  171.         this.speed = 0;
  172.         for (var i = 0; i < this.propulsionUnits.length; i++) {
  173.             var self = this.propulsionUnits[i];
  174.                      
  175.             if (self instanceof Wheel && this.mode === travelMode.LAND) {
  176.                 alert(self.accelerate());
  177.                 this.speed += self.accelerate();
  178.             }
  179.             if (self instanceof Propeler && this.mode === travelMode.WATER) {
  180.                 this.speed += self.accelerate();
  181.             }
  182.         }
  183.     }
  184.     // Public
  185.     return {
  186.         afterBurnerState: afterBurnerState,
  187.         rotateDirection: rotateDirection,
  188.         travelMode: travelMode,
  189.         Wheel: Wheel,
  190.         Nozzle: Nozzle,
  191.         Propeler: Propeler,
  192.         LandVehicle: LandVehicle,
  193.         AirVehicle: AirVehicle,
  194.         WaterVehicle: WaterVehicle,
  195.         AmphibiousVehicle: AmphibiousVehicle,
  196.     }
  197. }());
Advertisement
Add Comment
Please, Sign In to add comment