Guest User

PlanetarySystemData.java

a guest
Jan 4th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.01 KB | None | 0 0
  1. package celestial.support;
  2.  
  3. import com.jme3.material.Material;
  4. import com.jme3.math.ColorRGBA;
  5. import com.jme3.math.FastMath;
  6. import com.jme3.math.Vector3f;
  7. import com.jme3.scene.Node;
  8. import com.jme3.scene.Spatial;
  9. import com.jme3.scene.shape.Sphere;
  10. import com.jme3.texture.Texture;
  11. import com.jme3.util.TangentBinormalGenerator;
  12.  
  13. import celestial.AsteroidField;
  14. import celestial.Nebula;
  15. import celestial.Planet;
  16. import celestial.Star;
  17. import celestial.Star.Type;
  18. import setup.MakeWorld;
  19. import station.SpaceStation;
  20. import util.Methods;
  21.  
  22. public class PlanetarySystemData extends Node{
  23.    
  24.     public enum Danger{
  25.         None, Minimal, Medium, High, Extreme
  26.     }
  27.    
  28.     public static Sphere ultra, high, medium;
  29.     public String tag;
  30.     public Star.Type startype;
  31.     public Vector3f loc;
  32.    
  33.     private SpaceStation[] spesteshun;
  34.    
  35.     public static void init(){
  36.         ultra = new Sphere(128,128,1, true, false);
  37.         high = new Sphere(96,96,1, true, false);
  38.         medium = new Sphere(64,64,1, true, false);
  39.         TangentBinormalGenerator.generate(ultra);
  40.         TangentBinormalGenerator.generate(high);
  41.         TangentBinormalGenerator.generate(medium);
  42.     }
  43.    
  44.     public PlanetarySystemData(int x, int y, int z, String tag, Node[] predefined, Type type)
  45.     {
  46.         loc = new Vector3f(x,y,z);
  47.         this.tag = tag;
  48.        
  49.         for (int i = 0; i < predefined.length; i++)
  50.             attachChild(predefined[i]);
  51.        
  52.         startype = type;
  53.     }
  54.  
  55.     public PlanetarySystemData(int x, int y, int z, String tag){
  56.         this(x,y,z,tag,randomStarType());
  57.     }
  58.    
  59.     public PlanetarySystemData(int x, int y, int z, String tag, Type type)
  60.     {
  61.         loc = new Vector3f(x,y,z);
  62.         this.tag = tag;
  63.  
  64.         generatePlanets();
  65.         SpaceObject p = (SpaceObject)getChild(FastMath.nextRandomInt(0, getChildren().size()-1));
  66.         float k = 2.5f;
  67.         if(p instanceof Nebula)
  68.             k = 1.5f;
  69.        
  70.        
  71.         SpaceStation st = new SpaceStation(p.getLocalTranslation().add(Methods.randomVector3f().multLocal(p.getSize()*k)));
  72.         if(st.getLocalTranslation().length() < 50000)
  73.             st.setLocalTranslation(st.getLocalTranslation().normalize().multLocal(80000));
  74.         attachChild(st);
  75.        
  76.         spesteshun = new SpaceStation[1];
  77.         spesteshun[0] = st;
  78.        
  79.         attachChild(new Star(tag, type));
  80.         startype = type;
  81.     }
  82.    
  83.     private static Type randomStarType() {
  84.         switch(FastMath.nextRandomInt(0, 7)){
  85.             case 0: return Star.Type.BlueGiant;
  86.             case 1: return Star.Type.Pulsar;
  87.             case 2: return Star.Type.Singularity;
  88.             case 3: return Star.Type.WhiteDwarf;
  89.             case 4: return Star.Type.RedGiant;
  90.             case 5: return Star.Type.Singularity;
  91.             case 6: return Star.Type.MainSequence;
  92.             default: return Star.Type.Singularity;
  93.         }
  94.     }
  95.  
  96.     private void generatePlanets() {
  97.         int count = FastMath.rand.nextInt(6)+5;
  98.         int nebcount = 0;
  99.         while(count > 0)
  100.         {
  101.             int type = FastMath.rand.nextInt(4);
  102.             while(nebcount >= 2 && type == 2)
  103.                 type = FastMath.rand.nextInt(4);
  104.            
  105.             int size;
  106.             if(type == 1)
  107.                 size = FastMath.rand.nextInt(3000)+3000;
  108.             else if(type == 2){
  109.                 size = FastMath.rand.nextInt(6000)+19000;
  110.                 nebcount++;
  111.             }else
  112.                 size = FastMath.rand.nextInt(3000)+500;
  113.            
  114.             Vector3f loc = new Vector3f(
  115.                     FastMath.rand.nextFloat()*450000-225000,
  116.                     FastMath.rand.nextFloat()*150000-75000,
  117.                     FastMath.rand.nextFloat()*450000-252000
  118.             );
  119.            
  120.             boolean clear = true;
  121.             for (Spatial object : getChildren()) {
  122.                 Vector3f vec = null;
  123.                 if(object instanceof Planet)
  124.                      vec = ((Planet)object).getLocalTranslation();
  125.                 else if (object instanceof Nebula)
  126.                     vec = ((Nebula)object).getLocalTranslation();
  127.                 else if (object instanceof AsteroidField)
  128.                     vec = ((AsteroidField)object).getLocalTranslation();
  129.                
  130.                 if(loc.distance(vec) < 60000)
  131.                     clear = false;
  132.             }
  133.             if(type != 2 && loc.length() < 60000)
  134.                 clear = false;
  135.            
  136.             if(clear)
  137.             {
  138.                 if(type == 2)
  139.                 {
  140.                     attachChild(new Nebula(loc,size*(FastMath.nextRandomFloat()/3+0.9f),Methods.randomColor().mult(0.8f)));
  141.                     count--;
  142.                 }
  143.                 else
  144.                 {
  145. /*                  if(FastMath.nextRandomInt(0, 4) == 0 && !asteroidfield)
  146.                     {
  147.                         attachChild(new AsteroidField(loc,size*2,size/20));
  148.                         asteroidfield = true;
  149.                     }
  150.                     else*/
  151.                     {
  152.                         Sphere Lod = getpolycount(size);
  153.                         Material ground = getMaterial(type);
  154.                         ColorRGBA col = randomRGBA();
  155.                         Planet planet = new Planet(loc.x,loc.y,loc.z,size,Lod,ground,getRings(type),tag,(float)(Math.random()/10)+0.001f,setAtmo(ground),col,col.mult(0.8f),this);
  156.                         attachChild(planet);
  157.                     }
  158.                     count--;
  159.                 }
  160.                
  161.             }
  162.         }
  163.     }
  164.    
  165.     private ColorRGBA randomRGBA() {
  166.         return new ColorRGBA(FastMath.nextRandomFloat(),FastMath.nextRandomFloat(),FastMath.nextRandomFloat(),1);
  167.     }
  168.  
  169.     private Texture setAtmo(Material m1) {
  170.         if(m1.getName() != null && m1.getName().equals("atmo"))
  171.             return MakeWorld.atmosimg.get((int)(Math.random()*MakeWorld.atmosimg.size()));
  172.         return null;
  173.     }
  174.  
  175.     private Material getRings(int type) {
  176.         if(type == 1)
  177.         {
  178.             if((int)(Math.random()*2) == 0)
  179.                 return MakeWorld.rings.get((int)(Math.random()*MakeWorld.rings.size()));
  180.             else
  181.                 return null;
  182.         }
  183.         else
  184.         {
  185.             if((int)(Math.random()*5) == 0)
  186.                 return MakeWorld.rings.get((int)(Math.random()*MakeWorld.rings.size()));
  187.             else
  188.                 return null;
  189.         }
  190.     }
  191.  
  192.     public void disablePhy(){
  193.         for (Spatial celestial : getChildren()) {
  194.             if(celestial instanceof SpaceObject)
  195.                 ((SpaceObject)celestial).disable();
  196.         }
  197.     }
  198.  
  199.     public void enablePhy(){
  200.         for (Spatial celestial : getChildren()) {
  201.             if(celestial instanceof SpaceObject)
  202.                 ((SpaceObject)celestial).enable();
  203.         }  
  204.     }
  205.    
  206.     private Material getMaterial(int type) {
  207.         if(type == 1)
  208.             return MakeWorld.gass.get((int)(Math.random()*MakeWorld.gass.size()));
  209.         return MakeWorld.rocky.get((int)(Math.random()*MakeWorld.rocky.size()));
  210.     }
  211.  
  212.     private Sphere getpolycount(int size) {
  213.         if(size > 3000)
  214.             return ultra;
  215.        
  216.         if(size > 2000)
  217.             return high;
  218.        
  219.         return medium;
  220.     }
  221.  
  222.     public String toString(){
  223.         return "["+tag+" x:"+loc.x+" y:"+loc.y+" z:"+loc.z+"]";
  224.     }
  225.    
  226.     public SpaceStation[] getStation(){
  227.         return spesteshun;
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment