Package agent

Source Code of agent.Painter

package agent;

import constants.ResourceConstants;
import environment.Cell;
import environment.World;
import functions.ColorFunctions;
import functions.EnvironmentFunction;
import java.awt.Color;
import java.util.Set;
import resource.Resource;

/**
*
* @author marcocelesti
*/
public class Painter extends Agent {

    public Painter(short progenitor, short id, short idWorld) {
        super(progenitor, id, idWorld);
    }

    public Painter(World world, DNAparameters dnaParam, Cell cell, short id, short progenitor) {
        super(world, dnaParam, cell, id, progenitor);
    }

    protected void executeActions(String time) {
        if (time.equals("minute")) {
            if (getSpeedCounter() >= getDnaParams().getSpeed()) {
                setLastDirection(getDirection());
                setDirection((short) (Math.random() * 4));
                move();
                setSpeedCounter(0);
            } else {
                setSpeedCounter(getSpeedCounter() + 1);
            }
        } else if (time.equals("hour")) {
        } else if (time.equals("day")) {
        }
    }

    private void move() {
        switch (getDirection()) {
            case 0:
                move(-1, 0);
                break;
            case 1:
                move(0, 1);
                break;
            case 2:
                move(1, 0);
                break;
            case 3:
                move(0, -1);
                break;
            default:
                move(0, 0);
        }
    }

    private void move(int downMove, int rightMove) {
        getUnit().setLastOccupiedCell(getUnit().getCell());
        if (isAlive()) {
            int oldX = getUnit().getCell().getX();
            int oldY = getUnit().getCell().getY();
            Cell cell = getUnit().move(getDnaParams().getSpecies(), downMove, rightMove, getProgenitor());
            if (oldX != cell.getX() || oldY != cell.getY()) {
                Color resultantColor = null;
                Resource resource = getUnit().getCell().getResource();
                int artisticType = resource.getType();
                if (artisticType == ResourceConstants.ARTISTIC_COLOR_RESOURCE) {
                    // colore dell'agente in RGB
                    Color agentColor = getDnaParams().getColor();
                    int redAgent = agentColor.getRed();
                    int greenAgent = agentColor.getGreen();
                    int blueAgent = agentColor.getBlue();
                    // colore dell'agente in HSB
                    float[] hsbvalsAgent = Color.RGBtoHSB(redAgent, greenAgent, blueAgent, null);
                    float hAgent = hsbvalsAgent[0];
                    float sAgent = hsbvalsAgent[1];
                    float bAgent = hsbvalsAgent[2];

                    // colore della risorsa in RGB
                    Color resourceColor = resource.getColor();
                    int redResource = resourceColor.getRed();
                    int greenResource = resourceColor.getGreen();
                    int blueResource = resourceColor.getBlue();
                    // colore della risorsa in HSB
                    float[] hsbvalsRes = Color.RGBtoHSB(redResource, greenResource, blueResource, null);
                    float hRes = hsbvalsRes[0];
                    float sRes = hsbvalsRes[1];
                    // avvicinamento del colore dell'agente al colore della cella
                    if (sRes > 0.5) {
                        float diff = hAgent - hRes;
                        if (Math.abs(diff) > 0.5f) {
                            hAgent += diff / 50f;
                        } else {
                            hAgent -= diff / 50f;
                        }
                        // normalizzazione
                        if (hAgent > 1f) {
                            hAgent = hAgent - 1f;
                        } else if (hAgent < 0f) {
                            hAgent = hAgent + 1f;
                        }
                    }

                    if (getParams().isHunger()) {
                        // si verifica se c'e' almeno un bordo nelle vicinanze
                        boolean nearBorder = false;
                        Set<Cell> surroundings = EnvironmentFunction.getSurroundings(getWorld().getGrid(), getUnit().getCell(), 5, getWorld().getParams().getCols(), getWorld().getParams().getRows(), false);
                        for (Cell c : surroundings) {
                            Resource artres = c.getResource();
                            if (artres.getType() == ResourceConstants.ARTISTIC_TRACE_RESOURCE) {
                                nearBorder = true;
                                break;
                            }
                        }

                        if (nearBorder) {
                            sAgent += (sAgent * 0.1f);
                            if (sAgent > 1f) {
                                sAgent = 1f;
                            }
                        } else {
                            sAgent -= (sAgent * 0.1f);
                        }

                        if (sAgent < 0.05f) {
                            getUnit().getCell().setOccupied(getDnaParams().getSpecies(), false, getProgenitor());
                            getUnit().getCell().cellChanged();
                            stopThread();
                        }
                    }

                    agentColor = Color.getHSBColor(hAgent, sAgent, bAgent);
                    getDnaParams().setColor(agentColor);
                    // resultantColor e' il colore da impostare sulla cella
                    resultantColor = ColorFunctions.meanColor(agentColor, resourceColor);
                }
                getUnit().getCell().getResource().setColor(resultantColor);
            }
            getUnit().getCell().cellChanged();
        }
    }
}
TOP

Related Classes of agent.Painter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.