SolarSystem.js 959 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. var ns = require('ns');
  3. var Utils = require('./Utils');
  4. var definitions = require('./Definitions');
  5. var CelestialBody = require('./CelestialBody');
  6. var bodies = definitions.map(function(def){
  7. var body = Object.create(CelestialBody);
  8. Utils.extend(body, def);
  9. body.init();
  10. return body;
  11. });
  12. var names = bodies.reduce(function(carry, body){
  13. carry[body.name] = body;
  14. return carry;
  15. }, {});
  16. var central = bodies.reduce(function(carry, body){
  17. carry = carry && carry.mass > body.mass ? carry : body;
  18. return carry;
  19. }, null);
  20. console.log(bodies);
  21. module.exports = {
  22. getBody: function(name){
  23. return names[name] || central;
  24. },
  25. getPositions: function(userDate, calculateVelocity){
  26. var epochTime = ns.getEpochTime(userDate);
  27. return bodies.map(function(body){
  28. body.setPositionFromDate(epochTime, calculateVelocity);
  29. return {
  30. name: body.name,
  31. position: body.getPosition(),
  32. velocity: body.getVelocity()
  33. };
  34. });
  35. }
  36. };