/**
 * Agent.java
 *
 * Agents model actors in the simulation's Grid.
 * 
 * @author Daniel Davenport, Grant Hensel, Elliot Penson, and Simon Swenson
 * Wheaton College, CSCI 335, Spring 2013
 */

package edu.wheaton.simulator.entity;

import java.awt.Color;
import java.util.NoSuchElementException;

import net.sourceforge.jeval.EvaluationException;
import edu.wheaton.simulator.datastructure.ElementAlreadyContainedException;
import edu.wheaton.simulator.datastructure.Field;
import edu.wheaton.simulator.simulation.Grid;
import edu.wheaton.simulator.simulation.Layer;

public abstract class GridEntity extends Entity {

	/**
	 * A pointer to the environment so new Agents can be added or removed.
	 */
	private Grid grid;

	/**
	 * Bitmask for storing an entity's customized appearance, initially set
	 */
	private byte[] design;

	/**
	 * Constructor
	 * 
	 * @param g
	 *            The grid object
	 * @param c
	 *            This entity's defaut color
	 * 
	 */
	public GridEntity(Grid g) {
		super();
		grid = g;
		Color c = Color.black;
		try {
			addField("colorRed", new Integer(c.getRed()));
			addField("colorBlue", new Integer(c.getBlue()));
			addField("colorGreen", new Integer(c.getGreen()));
			addField("x", 0);
			addField("y", 0);
		} catch (ElementAlreadyContainedException e) {
			e.printStackTrace();
		}

		design = new byte[8];
		for (int i = 0; i < 8; i++)
			design[i] = 127; // sets design to a solid image
	}

	/**
	 * Constructor
	 * 
	 * @param g
	 *            The grid object
	 * @param c
	 *            This entity's defaut color
	 * 
	 */
	public GridEntity(Grid g, Color c) {
		super();
		grid = g;

		try {
			addField("colorRed", new Integer(c.getRed()));
			addField("colorBlue", new Integer(c.getBlue()));
			addField("colorGreen", new Integer(c.getGreen()));
			addField("x", 0);
			addField("y", 0);
		} catch (ElementAlreadyContainedException e) {
			e.printStackTrace();
		}

		design = new byte[8];
		for (int i = 0; i < 8; i++)
			design[i] = 127; // sets design to a solid image
	}

	/**
	 * Constructor
	 * 
	 * @param g
	 *            The grid object
	 * @param c
	 *            This entity's defaut color
	 * @param d
	 *            The bitmask design chosen by the user
	 */
	public GridEntity(Grid g, Color c, byte[] d) {
		super();
		grid = g;

		try {
			addField("colorRed", new Integer(c.getRed()));
			addField("colorBlue", new Integer(c.getBlue()));
			addField("colorGreen", new Integer(c.getGreen()));
		} catch (ElementAlreadyContainedException e) {
			e.printStackTrace();
		}

		design = d;
	}

	/**
	 * Set the default color of this entity to a new value
	 * 
	 * @param c
	 */
	public void setColor(Color c) {
		updateField("colorRed", new Integer(c.getRed()));
		updateField("colorBlue", new Integer(c.getBlue()));
		updateField("colorGreen", new Integer(c.getGreen()));
	}

	/**
	 * Returns the object's default color
	 * 
	 */
	public Color getColor() {
		return new Color(getField("colorRed").getIntValue(), getField(
				"colorGreen").getIntValue(), getField("colorBlue")
				.getIntValue());
	}

	/**
	 * Gets a color for the entity based on the Field being examined by the
	 * Layer object. Returns null if the entity does not contain the Field.
	 * 
	 * @return The specific Color to represent the value of this entity's Field
	 * @throws EvaluationException
	 */
	public Color getLayerColor() throws EvaluationException {

		Field field = getField(Layer.getInstance().getFieldName());
		if (field == null)
			throw new NoSuchElementException(
					"Entity.getLayerColor() could not find a valid field for return");
		return Layer.getInstance().newShade(field);
	}

	/**
	 * Sets the image bitmask array to a new design
	 */
	public void setDesign(byte[] design) {
		this.design = design;
	}

	/**
	 * Returns the image bitmask array
	 */
	public byte[] getDesign() {
		return design;
	}

	/**
	 * Provides the grid that this entity is in.
	 * 
	 * @return the Grid object
	 */
	public Grid getGrid() {
		return grid;
	}

}

@@ -1100,160 +1100,19 @@
 %0A%09%09%09
-addField(%22colorRed%22, new Integer(c.getRed()));%0D%0A%09%09%09addField(%22colorBlue%22, new Integer(c.getBlue()));%0D%0A%09%09%09addField(%22colorGreen%22, new Integer(c.getGreen())
+initColor(c
 );%0D%0A
@@ -1578,160 +1578,19 @@
 %0A%09%09%09
-addField(%22colorRed%22, new Integer(c.getRed()));%0D%0A%09%09%09addField(%22colorBlue%22, new Integer(c.getBlue()));%0D%0A%09%09%09addField(%22colorGreen%22, new Integer(c.getGreen())
+initColor(c
 );%0D%0A
@@ -2122,16 +2122,173 @@
 try %7B%0D%0A%09
+%09%09initColor(c);%0D%0A%09%09%7D catch (ElementAlreadyContainedException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%0D%0A%09%09design = d;%0D%0A%09%7D%0D%0A%09%0D%0A%09private void initColor(Color c) %7B%0D%0A
 %09%09addFie
@@ -2325,25 +2325,24 @@
 etRed()));%0D%0A
-%09
 %09%09addField(%22
@@ -2379,25 +2379,24 @@
 lue()));%0D%0A%09%09
-%09
 addField(%22co
@@ -2439,105 +2439,8 @@
 );%0D%0A
-%09%09%7D catch (ElementAlreadyContainedException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%0D%0A%09%09design = d;%0D%0A
 %09%7D%0D%0A

Update GridEntity.java