package com.spaceproject.systems;


import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.spaceproject.components.AIComponent;
import com.spaceproject.components.ControlFocusComponent;
import com.spaceproject.components.AISpawnComponent;
import com.spaceproject.components.TransformComponent;
import com.spaceproject.generation.EntityFactory;
import com.spaceproject.screens.GameScreen;
import com.spaceproject.utility.Mappers;
import com.spaceproject.utility.Misc;
import com.spaceproject.utility.SimpleTimer;

public class PlanetarySystemEntitySpawner extends IteratingSystem {
    
    public PlanetarySystemEntitySpawner() {
        super(Family.all(AISpawnComponent.class, TransformComponent.class).get());
    }
    
    @Override
    protected void processEntity(Entity entity, float delta) {
        //goal: when in a solar system (loaded planets
        //get all plaenet entitys
        //if planet has life
    
        //add to planet SpawnerComponent
        //add spawning rules
        //eg:
        //spawn chance of xxxx everytick
        //lets say spawn timer = 30 seconds, 50% spawn chance
        //or timeer.setInterval(10 seconds from now, and
        //once timer / spawner satisfied
        //
        //could have a more busy planet with more spawners
        
        AISpawnComponent spawn = Mappers.spawn.get(entity);
        for (SimpleTimer timer : spawn.timers) {
            if (timer.canDoEvent()) {
                long nextInterval = MathUtils.random(spawn.min, spawn.max);
                timer.setInterval(nextInterval, true);//also reset
                
                
                if (spawn.state == AIComponent.State.attack) {
                    //spawn ship at planets location
                    TransformComponent transform = Mappers.transform.get(entity);
                    Entity aiShip = EntityFactory.createAIShip(transform.pos.x, transform.pos.y, GameScreen.inSpace());
                    AIComponent aiComponent = aiShip.getComponent(AIComponent.class);
                    aiComponent.state = spawn.state;
                    //find player entity, send angry ships at them
                    ImmutableArray<Entity> players = getEngine().getEntitiesFor(Family.all(ControlFocusComponent.class).get());
                    if (players.size() == 0) {
                        continue;
                    }
                    Entity attackTarget = players.first();
                    aiComponent.attackTarget = attackTarget;
    
                    getEngine().addEntity(aiShip);
                    Gdx.app.log(this.getClass().getSimpleName(), "spawned: " + Misc.objString(entity) + " source:" + Misc.objString(entity));
                }
                
                
                //if i am another type of object, then do my own spawn rules. like maybe I am on a planet and want to spawn characters near the player
                //TransformComponent transform = Mappers.transform.get(entity);
                //Entity ai = EntityFactory.createCharacterAI(transform.pos.x, transform.pos.y);
                //ai.getComponent(AIComponent.class).State = AIComponent.State.dumbwander;
                //getEngine().addEntity(ai);
            }
        }
    }
    
    
    
}

@@ -430,32 +430,84 @@
 SpawnComponent;%0A
+import com.spaceproject.components.PlanetComponent;%0A
 import com.space
@@ -1170,24 +1170,28 @@
 as life%0A    
+    
 %0A        //a
@@ -1870,84 +1870,168 @@
     
-%0A                if (spawn.state == AIComponent.State.attack) %7B%0A            
+spawn(entity, spawn);%0A            %7D%0A            %0A            %0A            %0A        %7D%0A    %7D%0A    %0A    private void spawn(Entity entity, AISpawnComponent spawn) %7B%0A
     
@@ -2067,28 +2067,16 @@
 ocation%0A
-            
         
@@ -2133,36 +2133,24 @@
 et(entity);%0A
-            
         Enti
@@ -2241,36 +2241,24 @@
 inSpace());%0A
-            
         AICo
@@ -2319,28 +2319,16 @@
 class);%0A
-            
         
@@ -2360,24 +2360,84 @@
 .state;%0A    
+    switch (aiComponent.state) %7B%0A            case attack: %7B%0A
             
@@ -2487,20 +2487,16 @@
 at them%0A
-    
         
@@ -2623,28 +2623,24 @@
             
- 
-   
 if (players.
@@ -2678,26 +2678,16 @@
     
-    continue;%0A    
+return;%0A
     
@@ -2692,36 +2692,32 @@
               %7D%0A
-    
                 
@@ -2755,20 +2755,16 @@
 irst();%0A
-    
         
@@ -2812,24 +2812,33 @@
 Target;%0A    
+        %7D
 %0A           
@@ -2838,23 +2838,221 @@
         
+break;%0A
        
+     case landOnPlanet: %7B%0A                //todo: find a planet that is not the one we spawned on%0A                //set it as target. go land.%0A                ImmutableArray%3CEntity%3E planets =
  getEngi
@@ -3060,39 +3060,331 @@
 e().
-add
+get
 Entit
-y(aiShip);%0A            
+iesFor(Family.all(PlanetComponent.class).get());%0A                Entity destinationPlanet = planets.random();%0A                if (destinationPlanet != null) %7B%0A                    aiComponent.planetTarget = destinationPlanet;%0A                %7D%0A            %7D%0A        %7D%0A        %0A        getEngine().addEntity(aiShip);%0A
     
@@ -3517,64 +3517,9 @@
     
-            %7D%0A                %0A                %0A        
+%0A
     
@@ -3657,24 +3657,16 @@
  player%0A
-        
         
@@ -3737,24 +3737,16 @@
         
-      
-  
 //Entity
@@ -3826,24 +3826,16 @@
         
-      
-  
 //ai.get
@@ -3909,24 +3909,16 @@
         
-     
-   
 //getEng
@@ -3946,47 +3946,8 @@
     
-        %7D%0A        %7D%0A    %7D%0A    %0A    %0A    
+%7D
 %0A%7D%0A

Update PlanetarySystemEntitySpawner.java