Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. class Ship {
  2. constructor(name, crew, fuel, hull, speed, img) {
  3.  
  4. this.name = name,
  5. this.crew = crew,
  6. this.fuel = fuel,
  7. this.maxFuel = fuel,
  8. this.hull = hull,
  9. this.maxHull = hull
  10. this.speed = this.checkSpeed(speed),
  11. this.credits = 500,
  12. this.img = img,
  13. this.isWorking = false,
  14. this.isDamaged = false,
  15. this.isDestroyed = false,
  16. this.dockedPlanet = null
  17. }
  18. checkSpeed(speed) {
  19. return ((speed <= 1) && (speed > 0)) ? speed : false
  20.  
  21. }
  22. start(planet) {
  23. console.log('the ship started its journey');
  24. if (!(planet instanceof Planet)) {
  25.  
  26. return false
  27.  
  28. }
  29.  
  30.  
  31. if (this.dockedPlanet) {
  32. return false
  33. }
  34.  
  35. if ((this.isDamaged) || (this.isDestroyed) || (!this.crew) || (!this.fuel)) {
  36. return false
  37. }
  38.  
  39.  
  40. this.isWorking = true;
  41. this.fuel = this.fuel - (planet.distance * 20);
  42. let TimeToPlanet = Number(((planet.distance) * 1000)/this.speed);
  43.  
  44. setTimeout(() => {
  45. // console.log();
  46. this.dock(planet)
  47. console.log('the ship arrived');
  48. }, TimeToPlanet);
  49.  
  50. }
  51.  
  52. dock(planet) {
  53.  
  54. setTimeout(() => {
  55. console.log('the ship started docking');
  56. (planet.shipsDocked).push(this);
  57. this.isWorking = false;
  58. this.dockedPlanet = planet;
  59. console.log('the ship FINISHED docking');
  60. }, 2000);
  61.  
  62.  
  63. }
  64.  
  65.  
  66. }
  67.  
  68.  
  69. class Planet {
  70.  
  71. constructor(name, size, population, distance, development) {
  72. this.name = name,
  73. this.size = size,
  74. this.population = population,
  75. this.distance = distance,
  76. this.shipsDocked = [],
  77. this.development = this.developmentLevel(development)
  78. }
  79. developmentLevel(development) {
  80. return ((development > 0) && (development < 4)) ? development : false
  81. }
  82.  
  83. getMarketPrice(price) {
  84. price = this.development * price / Math.floor(this.population / this.size)
  85. return price
  86.  
  87. }
  88.  
  89. repair(ship) {
  90. this.check(ship)
  91.  
  92. if (ship.credits < this.getMarketPrice(price.repair)) {
  93. return false
  94. }
  95.  
  96. if (ship.maxHull === ship.hull) {
  97. return `max hull`;
  98.  
  99. }
  100.  
  101.  
  102. ship.hull = this.maxHull;
  103. ship.isDamaged = false;
  104. ship.credits = ship.credits - this.getMarketPrice(price.repair)
  105.  
  106. }
  107.  
  108.  
  109. refuel(ship) {
  110. console.log(ship)
  111.  
  112. this.check(ship)
  113.  
  114. if (ship.credits < this.getMarketPrice(price.fuel)) {
  115. return false
  116. }
  117. if (ship.maxFuel === ship.fuel) {
  118. return false;
  119.  
  120. }
  121.  
  122.  
  123. ship.fuel =ship.maxFuel;
  124.  
  125. ship.credits = ship.credits - this.getMarketPrice(price.fuel)
  126.  
  127.  
  128. }
  129.  
  130. hireCrewMember(ship) {
  131. this.check(ship);
  132.  
  133. if (ship.credits < this.getMarketPrice(price.crew)) {
  134. return false
  135. }
  136. ship.crew += 1;
  137. ship.credits = ship.credits - this.getMarketPrice(price.crew)
  138.  
  139. }
  140.  
  141. check(ship) {
  142. if (!(ship instanceof Ship)) {
  143.  
  144. return false
  145.  
  146. }
  147. if (!(ship.dockedPlanet)) {
  148. return false
  149. }
  150.  
  151.  
  152. }
  153.  
  154.  
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. let ships= [
  166. new Ship("StarFighter", 3, 380, 500, 0.5, "img/StarFighter.png"),
  167. new Ship("Crushinator", 5, 540, 400, 0.2, "img/Crushinator.png"),
  168. new Ship("Scouter", 1, 300, 300, 0.9, "img/Scouter.png")
  169. ]
  170. let planets=[
  171. new Planet("Rubicon9", 300000, 2000000, 4, 2, "img/Rubicon9.png"),
  172. new Planet("R7", 120000, 4000000, 7, 3, "img/R7.png"),
  173. new Planet("Magmus", 500000, 10000000, 6, 1, "img/Magmus.png"),
  174. new Planet("Dextriaey", 50000, 500000, 9, 3, "img/Dextriaey.png"),
  175. new Planet("B18-1", 250000, 4000000, 12, 2, "img/B18-1.png")
  176. ]
  177.  
  178. let price= {
  179. fuel: 50,
  180. repair: 60,
  181. crew: 80
  182. }
  183.  
  184. // planets.forEach(planet => {
  185. // ships.forEach(ship => {
  186. // console.log(ship.start(planet))
  187. // });
  188.  
  189. // });
  190.  
  191.  
  192.  
  193. // console.log(firstShip.start(firstPlanet))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement