package com.fundynamic.d2tm.game.entities.units.states;


import com.fundynamic.d2tm.game.entities.Entity;
import com.fundynamic.d2tm.game.entities.EntityRepository;
import com.fundynamic.d2tm.game.entities.units.Unit;
import com.fundynamic.d2tm.game.map.Map;
import com.fundynamic.d2tm.math.MapCoordinate;

/**
 * Determine what to do to get to the given goal.
 */
public class GoalResolverState extends UnitState {

    public GoalResolverState(Unit unit, EntityRepository entityRepository, Map map) {
        super(unit, entityRepository, map);
    }

    @Override
    public void update(float deltaInSeconds) {
        if (!unit.shouldMove()) {
            unit.log("No need to move anywhere (target == coordinate), going to idle mode.");
            unit.idle();
            return;
        }

        if (unit.hasEnemyToAttack()) {
            Entity entityToAttack = unit.getEntityToAttack();

            // forget about entity when it is destroyed
            if (entityToAttack.isDestroyed()) {
                unit.forgetEntityToAttack();
            } else {
                if (unit.isEntityToAttackInRange()) {
                    unit.fireAt(entityToAttack);
                    return;
                }
                // get closer
                unit.setMoveTarget(entityToAttack.getCenteredCoordinate());
            }
        }

        if (unit.hasNoNextCellToMoveTo()) {
            unit.stopAndResetAnimating();
            MapCoordinate nextIntendedCoordinatesToMoveTo = unit.getNextIntendedCellToMoveToTarget();
            if (unit.isCellPassableForMe(nextIntendedCoordinatesToMoveTo)) {
                if (!unit.isHarvester()) {
                    unit.startAnimating();
                }
                unit.moveToCell(nextIntendedCoordinatesToMoveTo.toCoordinate());
            } else {
                unit.log("I have no where to go.");
            }
        } else {
            // still have a target, keep moving
            // we can get here when a unit is mid-way moving and is ordered to move somewhere else,
            // which will set the unit state to the GoalResolverState again.
            unit.log("Has next cell to move to " + unit.getNextTargetToMoveTo());
            unit.moveToCell(unit.getNextTargetToMoveTo());
        }
    }

    @Override
    public String toString() {
        return "GoalResolverState [TARGET=" + unit.getTarget() + "]";
    }

}

@@ -1052,32 +1052,35 @@
             
-%7D else %7B
+    return;
 %0A           
@@ -1072,32 +1072,39 @@
 rn;%0A        
+    %7D%0A%0A
         
 if (unit.isE
@@ -1083,32 +1083,36 @@
      %7D%0A%0A        
+    
 if (unit.isEntit
@@ -1145,28 +1145,24 @@
             
-    
 unit.fireAt(
@@ -1186,36 +1186,32 @@
                 
-   
- 
 return;%0A        
@@ -1210,38 +1210,39 @@
             
+%7D%0A%0A
     
-%7D%0A
         
         // g
@@ -1233,34 +1233,149 @@
     
-        // get closer%0A    
+// update (if applicable) the coordinate to move to, and continue below to move to next cell%0A            // in order to get closer to target%0A
     
@@ -1442,30 +1442,16 @@
 ate());%0A
-            %7D%0A
         

Increase some readability to remove some unneeded brackets