/*
 * FoodCluster.java
 *
 * Created on 12 November 2006, 22:23
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ants;

import java.awt.Point;
import java.util.Vector;

/**
 *
 * @author James Hamilton
 */
public class FoodCluster implements Runnable {
    
    private int radius = 5;
    private int x, y;
    private Vector<GridSquare> gridSquares;
    private int strengthRadius = 0;
    
    private Thread move;
    
    /** Creates a new instance of FoodCluster */
    public FoodCluster(int x, int y, int radius) {
        this.setX(x);
        this.setY(y);
        this.radius = radius;
        
        try {
            gridSquares = Grid.getInstance().getGridSquare(x, y).getGridSquares(radius, true);

            for(GridSquare gs : gridSquares)
                gs.setFood(new Food(gs));

        }catch (Exception e) {
            
        }
  
      //  move = new Thread(this);
    //    move.setDaemon(true);
     //   move.start();
        
        // recalculateStrengths();
    }
    
    public void recalculateStrengths() {
        recalculateStrengths(false);
    }
    
    public void recalculateStrengths(boolean reset) {

        try {
            Vector<GridSquare> gridSquares = Grid.getInstance().getGridSquare(x, y).getGridSquares(getRadius() + getStrengthRadius(), true);

            for(GridSquare gs : gridSquares) {
               // if(reset) gs.setFoodStrength(0);
                Point p = gs.getCoordinateXY();

                double c2 = Math.pow(p.x - getX(), 2) + Math.pow(p.y - getY(), 2);
                //System.out.println(Math.sqrt(c2));
                if(Math.sqrt(c2) <= 100)//getRadius() +  getStrengthRadius())
                    gs.setFoodStrength((1 / c2*300));//gs.getFoodStrength() + 
            }
        }catch (Exception e) {
            
        }
    }
            
    public void run() {

        while(move != null) {
            try {
                
                recalculateStrengths(true);
                
                move.sleep(1000);
            }catch (InterruptedException e) {}
        }
    }    
    
    public int getRadius() {
        return radius;
    }

    public void setRadius(int size) {
        this.radius = size;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getStrengthRadius() {
        
        return strengthRadius;
    }

    public void setStrengthRadius(int strengthRadius) {
         recalculateStrengths(true);
       // System.out.println("Setting strength raduis: " + strengthRadius);
        this.strengthRadius = strengthRadius;
    }
    

}

