Package eas.users.demos.pacman

Source Code of eas.users.demos.pacman.RealPacmanAgent

/*
* Datei:            PacmanAgent.java
* Autor(en):        Lukas König
* Java-Version:     6.0
* Erstellt:         2010-10-27
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.users.demos.pacman;

import java.awt.Color;

import eas.math.geometry.Polygon2D;
import eas.math.geometry.Vector2D;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.simulation.agent.GenericSensor;
import eas.simulation.event.EASEvent;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D;
import eas.startSetup.ParCollection;

public class RealPacmanAgent extends AbstractAgent2D<AbstractEnvironment2D<?>> {

    /**
     *
     */
    private static final long serialVersionUID = -7846777526952617626L;
    private Polygon2D pacman;
    private Color color;

    public RealPacmanAgent(int id, AbstractEnvironment2D<?> env, ParCollection params) {
        super(id, env, params);
       
        this.addSensor(new GenericSensor<Float, AbstractEnvironment2D<AbstractAgent2D<?>>, AbstractAgent2D<?>>() {

            /**
             *
             */
            private static final long serialVersionUID = -850570281361687891L;

            @Override
            public Float sense(AbstractEnvironment2D<AbstractAgent2D<?>> env, AbstractAgent2D<?> agent) {
                return (float) env.getWheelSpeedLeft(agent.id());
            }

            @Override
            public String id() {
                return "Wheel-Speed-Left";
            }
        });

        this.addSensor(new GenericSensor<Float, AbstractEnvironment2D<AbstractAgent2D<?>>, AbstractAgent2D<?>>() {

            /**
             *
             */
            private static final long serialVersionUID = -953172563969605174L;

            @Override
            public Float sense(AbstractEnvironment2D<AbstractAgent2D<?>> env, AbstractAgent2D<?> agent) {
                return (float) env.getWheelSpeedRight(agent.id());
            }

            @Override
            public String id() {
                return "Wheel-Speed-Right";
            }
        });

        this.color = Color.yellow;
    }

    @Override
    public Polygon2D getAgentShape() {
        if (pacman == null) {
            final double precision = 40;
            final double radius = 1;
            pacman = new Polygon2D();
           
            for (double d = 0; d < Math.PI * 1.75; d += Math.PI * 2 / precision) {
                pacman.add(new Vector2D(Math.sin(d) * radius, Math.cos(d) * radius));
            }
            pacman.add(new Vector2D(0, 0));
            pacman.rotate(Vector2D.NULL_VECTOR, -Math.PI * 0.125);
        }
       
        return pacman;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public synchronized void step(Wink simTime) {
        handleEventStatic(this.lastEvent);
        this.lastEvent = null;

        if (!((AbstractEnvironment2D) this.getEnvironment()).differentialDriveForward(this.id())) {
            this.pacmanDead();
        }
       
        this.getEnvironment().setWheelSpeedLeft(this.id(), this.getEnvironment().getWheelSpeedLeft(this.id()) / 1.03);
        this.getEnvironment().setWheelSpeedRight(this.id(), this.getEnvironment().getWheelSpeedRight(this.id()) / 1.03);
    }

    private EASEvent lastEvent;
   
    @Override
    public synchronized void handleEvent(EASEvent e, Wink lastActionTime) {
        this.lastEvent = e;
    }

    @SuppressWarnings("rawtypes")
    private void handleEventStatic(EASEvent e) {
        if (e == null) {
            return;
        }
       
        if (e.getEventDescription().equals(ConstantsSimulation.KEY_EVENT_IDENTIFIER + " ")) { // SPACE
            ((PacmanWorld) ((AbstractEnvironment2D) this.getEnvironment())).resetPacman();
            this.color = Color.yellow;
        }

        if (this.color.equals(Color.red)) {
            return;
        }
       
        final double speedChangeForward = 0.15;
        final double speedChangeTurn = 0.01;
       
        if (e.getEventDescription().equals(ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_UP)) { // UP
            ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedLeft(
                    this.id(),
                    ((AbstractEnvironment2D) this.getEnvironment()).getWheelSpeedLeft(this.id()) + speedChangeForward);
            ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedRight(
                    this.id(),
                    ((AbstractEnvironment2D) this.getEnvironment()).getWheelSpeedRight(this.id()) + speedChangeForward);
        }
        if (e.getEventDescription().equals(ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_DOWN)) { // DOWN
            ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedLeft(
                    this.id(),
                    ((AbstractEnvironment2D) this.getEnvironment()).getWheelSpeedLeft(this.id()) - speedChangeForward);
            ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedRight(
                    this.id(),
                    ((AbstractEnvironment2D) this.getEnvironment()).getWheelSpeedRight(this.id()) - speedChangeForward);
        }
        if (e.getEventDescription().equals(ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_LEFT)) { // LEFT
            ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedLeft(
                    this.id(),
                    ((AbstractEnvironment2D) this.getEnvironment()).getWheelSpeedLeft(this.id()) - speedChangeTurn);
            ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedRight(
                    this.id(),
                    ((AbstractEnvironment2D) this.getEnvironment()).getWheelSpeedRight(this.id()) + speedChangeTurn);
        }
        if (e.getEventDescription().equals(ConstantsSimulation.KEY_EVENT_ARROW_KEY_ENCODING_RIGHT)) { // RIGHT
            ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedLeft(
                    this.id(),
                    ((AbstractEnvironment2D) this.getEnvironment()).getWheelSpeedLeft(this.id()) + speedChangeTurn);
            ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedRight(
                    this.id(),
                    ((AbstractEnvironment2D) this.getEnvironment()).getWheelSpeedRight(this.id()) - speedChangeTurn);
        }
    }

    @SuppressWarnings("rawtypes")
    public void pacmanDead() {
        this.color = Color.red;
        ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedLeft(this.id(), 0);
        ((AbstractEnvironment2D) this.getEnvironment()).setWheelSpeedRight(this.id(), 0);
    }
   
    @Override
    public Color getAgentColor() {
        return this.color;
    }
}
TOP

Related Classes of eas.users.demos.pacman.RealPacmanAgent

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.