package com.eogames.john.ecs.systems;

import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.eogames.john.ecs.components.EnemyComponent;
import com.eogames.john.ecs.components.JohnComponent;
import com.eogames.john.ecs.components.PhysicComponent;
import com.eogames.john.ecs.components.TransformComponent;

public class EnemySystem extends EntitySystem {

  private Entity john;
  private ImmutableArray<Entity> enemies;

  @Override
  public void addedToEngine(Engine engine) {
    john = engine.getEntitiesFor(Family.one(JohnComponent.class).get()).first();
    enemies = engine.getEntitiesFor(Family.one(EnemyComponent.class).get());
  }

  @Override
  public void update(float deltaTime) {
    Rectangle johnRectangle = new Rectangle(
        john.getComponent(TransformComponent.class).pos.x,
        john.getComponent(TransformComponent.class).pos.y,
        john.getComponent(PhysicComponent.class).width,
        john.getComponent(PhysicComponent.class).height
    );
    Rectangle enemyRectangle = new Rectangle();
    Rectangle intersection = new Rectangle();
    for (Entity enemy: enemies) {
      enemyRectangle.set(
          enemy.getComponent(TransformComponent.class).pos.x,
          enemy.getComponent(TransformComponent.class).pos.y,
          enemy.getComponent(PhysicComponent.class).width,
          enemy.getComponent(PhysicComponent.class).height
      );
      // TODO: Fix top collision
      if (Intersector.intersectRectangles(johnRectangle, enemyRectangle, intersection)) {
        if(intersection.y > enemyRectangle.y) {
          john.getComponent(JohnComponent.class).score += enemy.getComponent(EnemyComponent.class).score;
          getEngine().removeEntity(enemy);
          addedToEngine(getEngine());
        } else {
          // Dead
        }
      }
    }
  }
}

@@ -247,16 +247,45 @@
 eArray;%0A
+import com.badlogic.gdx.Gdx;%0A
 import c
@@ -459,32 +459,86 @@
 .JohnComponent;%0A
+import com.eogames.john.ecs.components.LifeComponent;%0A
 import com.eogam
@@ -569,32 +569,87 @@
 hysicComponent;%0A
+import com.eogames.john.ecs.components.StateComponent;%0A
 import com.eogam
@@ -690,16 +690,74 @@
 mponent;
+%0Aimport com.eogames.john.ecs.components.VelocityComponent;
 %0A%0Apublic
@@ -1145,56 +1145,42 @@
     
-Rectangle johnRectangle = new Rectangle(%0A       
+TransformComponent johnTransform =
  joh
@@ -1219,31 +1219,50 @@
 t.class)
-.pos.x,%0A       
+;%0A    PhysicComponent johnPhysic =
  john.ge
@@ -1268,33 +1268,30 @@
 etComponent(
-Transform
+Physic
 Component.cl
@@ -1294,31 +1294,54 @@
 t.class)
-.pos.y,%0A       
+;%0A    VelocityComponent johnVelocity =
  john.ge
@@ -1343,38 +1343,40 @@
 hn.getComponent(
-Physic
+Velocity
 Component.class)
@@ -1375,31 +1375,48 @@
 t.class)
-.width,%0A       
+;%0A    StateComponent johnState =
  john.ge
@@ -1418,38 +1418,37 @@
 hn.getComponent(
-Physic
+State
 Component.class)
@@ -1447,28 +1447,147 @@
 t.class)
+;%0A    Rectangle johnRectangle = new Rectangle(johnTransform.pos.x, johnTransform.pos.y,%0A        johnPhysic.width, johnPhysic
 .height
-%0A    
 );%0A    R
@@ -1671,24 +1671,25 @@
 ectangle();%0A
+%0A
     for (Ent
@@ -1720,37 +1720,43 @@
     
-enemyRectangle.set(%0A         
+TransformComponent enemyTransform =
  ene
@@ -1800,25 +1800,45 @@
 ass)
-.pos.x,%0A         
+;%0A      PhysicComponent enemyPhysic =
  ene
@@ -1849,33 +1849,30 @@
 etComponent(
-Transform
+Physic
 Component.cl
@@ -1879,175 +1879,129 @@
 ass)
-.pos.y,%0A          enemy.getComponent(PhysicComponent.class).width,%0A          enemy.getComponent(PhysicComponent.class).height%0A      );%0A      // TODO: Fix top collision
+;%0A      enemyRectangle.set(enemyTransform.pos.x, enemyTransform.pos.y,%0A          enemyPhysic.width, enemyPhysic.height);%0A
 %0A   
@@ -2117,26 +2117,105 @@
 n.y 
-%3E enemyRectangle.y
+- (johnVelocity.y - johnVelocity.gravity) * deltaTime %3E= enemyRectangle.y + enemyRectangle.height
 ) %7B%0A
@@ -2420,27 +2420,280 @@
 lse 
-%7B%0A          // Dead
+if (!johnState.isInvincible)%7B%0A          Gdx.app.error(%22intersection.y%22, intersection.y + %22%22);%0A          Gdx.app.error(%22enemyRect.y%22, enemyRectangle.y + %22%22);%0A          Gdx.app.error(%22enemyRect.height%22, enemyRectangle.height + %22%22);%0A          johnState.hasBeenTouched = true;
 %0A   

Fix enemy collision