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.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.spaceproject.components.PhysicsComponent;
import com.spaceproject.components.ShieldComponent;
import com.spaceproject.generation.BodyFactory;
import com.spaceproject.utility.Mappers;

public class ShieldSystem extends IteratingSystem {
    
    public ShieldSystem() {
        super(Family.all(ShieldComponent.class).get());
    }
    
    @Override
    protected void processEntity(Entity entity, float deltaTime) {
        ShieldComponent shield = Mappers.shield.get(entity);
        float shieldReactivateThreshold = 0.3f;
        
        switch (shield.state) {
            case off:
                if (shield.defend) {
                    //reactivate if still enough charge
                    if (shield.animTimer.ratio() >= shieldReactivateThreshold) {
                        shield.animTimer.flipRatio();
                    } else {
                        shield.animTimer.reset();
                    }
                    
                    shield.state = ShieldComponent.State.charge;
                }
                break;
            case on:
                if (!shield.defend) {
                    shield.state = ShieldComponent.State.discharge;
                    
                    //destroy shield fixture
                    Body body = entity.getComponent(PhysicsComponent.class).body;
                    Fixture circleFixture = body.getFixtureList().get(body.getFixtureList().size - 1);
                    body.destroyFixture(circleFixture);
    
                    shield.animTimer.flipRatio();
                }
                break;
            case charge:
                if (!shield.defend) {
                    shield.state = ShieldComponent.State.discharge;
                    break;
                }
                
                //charge
                shield.radius = shield.maxRadius * shield.animTimer.ratio();
                
                //or if (shield.animTimer.canDoEvent())
                if (shield.radius == shield.maxRadius) {
                    shield.state = ShieldComponent.State.on;
                    
                    //add shield fixture to body for protection
                    Body body = entity.getComponent(PhysicsComponent.class).body;
                    BodyFactory.addShieldFixtureToBody(body, shield.radius);
                }
                break;
            case discharge:
                if (shield.defend) {
                    //reactivate
                    if (shield.animTimer.ratio() >= shieldReactivateThreshold) {
                        shield.animTimer.flipRatio();
                    } else {
                        shield.animTimer.reset();
                    }
                    
                    shield.state = ShieldComponent.State.charge;
                    break;
                }
                
                //discharge
                shield.radius = shield.maxRadius * (1 - shield.animTimer.ratio());
                
                if (shield.radius <= 0) {
                    shield.state = ShieldComponent.State.off;
                }
                break;
        }
    }
    
}

@@ -744,56 +744,8 @@
 y);%0A
-        float shieldReactivateThreshold = 0.3f;%0A
     
@@ -864,893 +864,168 @@
     
-//reactivate if still enough charge%0A                    if (shield.animTimer.ratio() %3E= shieldReactivateThreshold) %7B%0A                        shield.animTimer.flipRatio();%0A                    %7D else %7B%0A                        shield.animTimer.reset();%0A                    %7D%0A                    %0A                    shield.state = ShieldComponent.State.charge;%0A                %7D%0A                break;%0A            case on:%0A                if (!shield.defend) %7B%0A                    shield.state = ShieldComponent.State.discharge;%0A                    %0A                    //destroy shield fixture%0A                    Body body = entity.getComponent(PhysicsComponent.class).body;%0A                    Fixture circleFixture = body.getFixtureList().get(body.getFixtureList().size - 1);%0A                    body.destroyFixture(circleFixture);%0A    %0A                    shield.animTimer.flipRatio(
+engage(shield);%0A                %7D%0A                break;%0A            case on:%0A                if (!shield.defend) %7B%0A                    disengage(entity, shield
 );%0A 
@@ -1277,24 +1277,37 @@
     //charge
+: gain energy
 %0A           
@@ -1376,81 +1376,8 @@
 ();%0A
-                %0A                //or if (shield.animTimer.canDoEvent())%0A
     
@@ -1425,16 +1425,49 @@
 axRadius
+ %7C%7C shield.animTimer.canDoEvent()
 ) %7B%0A    
@@ -1897,341 +1897,22 @@
     
-//reactivate%0A                    if (shield.animTimer.ratio() %3E= shieldReactivateThreshold) %7B%0A                        shield.animTimer.flipRatio();%0A                    %7D else %7B%0A                        shield.animTimer.reset();%0A                    %7D%0A                    %0A                    shield.state = ShieldComponent.State.charge
+engage(shield)
 ;%0A  
@@ -1998,16 +1998,30 @@
 ischarge
+: loose energy
 %0A       
@@ -2096,33 +2096,16 @@
 tio());%0A
-                %0A
         
@@ -2134,16 +2134,50 @@
 ius %3C= 0
+  %7C%7C shield.animTimer.canDoEvent()
 ) %7B%0A    
@@ -2296,10 +2296,839 @@
  %7D%0A    %0A
+    private void engage(ShieldComponent shield) %7B%0A        float shieldReactivateThreshold = 0.3f;%0A        %0A        //reactivate if still enough charge%0A        if (shield.animTimer.ratio() %3E= shieldReactivateThreshold) %7B%0A            shield.animTimer.flipRatio();%0A        %7D else %7B%0A            shield.animTimer.reset();%0A        %7D%0A        %0A        shield.state = ShieldComponent.State.charge;%0A    %7D%0A    %0A    private void disengage(Entity entity, ShieldComponent shield) %7B%0A        shield.state = ShieldComponent.State.discharge;%0A        %0A        //destroy shield fixture%0A        Body body = entity.getComponent(PhysicsComponent.class).body;%0A        Fixture circleFixture = body.getFixtureList().get(body.getFixtureList().size - 1);%0A        body.destroyFixture(circleFixture);%0A        %0A        shield.animTimer.flipRatio();%0A    %7D%0A    %0A
 %7D%0A

Update ShieldSystem.java