Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Ship {
- constructor(name, crew, fuel, hull, speed, img) {
- this.name = name,
- this.crew = crew,
- this.fuel = fuel,
- this.maxFuel = fuel,
- this.hull = hull,
- this.maxHull = hull
- this.speed = this.checkSpeed(speed),
- this.credits = 500,
- this.img = img,
- this.isWorking = false,
- this.isDamaged = false,
- this.isDestroyed = false,
- this.dockedPlanet = null
- }
- checkSpeed(speed) {
- return ((speed <= 1) && (speed > 0)) ? speed : false
- }
- start(planet) {
- console.log('the ship started its journey');
- if (!(planet instanceof Planet)) {
- return false
- }
- if (this.dockedPlanet) {
- return false
- }
- if ((this.isDamaged) || (this.isDestroyed) || (!this.crew) || (!this.fuel)) {
- return false
- }
- this.isWorking = true;
- this.fuel = this.fuel - (planet.distance * 20);
- let TimeToPlanet = Number(((planet.distance) * 1000)/this.speed);
- setTimeout(() => {
- // console.log();
- this.dock(planet)
- console.log('the ship arrived');
- }, TimeToPlanet);
- }
- dock(planet) {
- setTimeout(() => {
- console.log('the ship started docking');
- (planet.shipsDocked).push(this);
- this.isWorking = false;
- this.dockedPlanet = planet;
- console.log('the ship FINISHED docking');
- }, 2000);
- }
- }
- class Planet {
- constructor(name, size, population, distance, development) {
- this.name = name,
- this.size = size,
- this.population = population,
- this.distance = distance,
- this.shipsDocked = [],
- this.development = this.developmentLevel(development)
- }
- developmentLevel(development) {
- return ((development > 0) && (development < 4)) ? development : false
- }
- getMarketPrice(price) {
- price = this.development * price / Math.floor(this.population / this.size)
- return price
- }
- repair(ship) {
- this.check(ship)
- if (ship.credits < this.getMarketPrice(price.repair)) {
- return false
- }
- if (ship.maxHull === ship.hull) {
- return `max hull`;
- }
- ship.hull = this.maxHull;
- ship.isDamaged = false;
- ship.credits = ship.credits - this.getMarketPrice(price.repair)
- }
- refuel(ship) {
- console.log(ship)
- this.check(ship)
- if (ship.credits < this.getMarketPrice(price.fuel)) {
- return false
- }
- if (ship.maxFuel === ship.fuel) {
- return false;
- }
- ship.fuel =ship.maxFuel;
- ship.credits = ship.credits - this.getMarketPrice(price.fuel)
- }
- hireCrewMember(ship) {
- this.check(ship);
- if (ship.credits < this.getMarketPrice(price.crew)) {
- return false
- }
- ship.crew += 1;
- ship.credits = ship.credits - this.getMarketPrice(price.crew)
- }
- check(ship) {
- if (!(ship instanceof Ship)) {
- return false
- }
- if (!(ship.dockedPlanet)) {
- return false
- }
- }
- }
- let ships= [
- new Ship("StarFighter", 3, 380, 500, 0.5, "img/StarFighter.png"),
- new Ship("Crushinator", 5, 540, 400, 0.2, "img/Crushinator.png"),
- new Ship("Scouter", 1, 300, 300, 0.9, "img/Scouter.png")
- ]
- let planets=[
- new Planet("Rubicon9", 300000, 2000000, 4, 2, "img/Rubicon9.png"),
- new Planet("R7", 120000, 4000000, 7, 3, "img/R7.png"),
- new Planet("Magmus", 500000, 10000000, 6, 1, "img/Magmus.png"),
- new Planet("Dextriaey", 50000, 500000, 9, 3, "img/Dextriaey.png"),
- new Planet("B18-1", 250000, 4000000, 12, 2, "img/B18-1.png")
- ]
- let price= {
- fuel: 50,
- repair: 60,
- crew: 80
- }
- // planets.forEach(planet => {
- // ships.forEach(ship => {
- // console.log(ship.start(planet))
- // });
- // });
- // console.log(firstShip.start(firstPlanet))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement