/* Copyright 2002 Graham Ritchie This file is part of Intellego. Intellego is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Intellego is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Intellego; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package simworldobjects; import interfaces.*; import java.util.*; import java.awt.*; import java.awt.geom.*; /** * Abstract class for SimWorlds, provides a base implementation of * the SimWorld interface which more complex worlds can extend. (e.g * by adding lights, walls etc.) * * @see interfaces.SimWorld * * @author Graham Ritchie */ public abstract class BasicSimWorld implements SimWorld { private int ticks; private int ambientLight; private LinkedList objectList; private long width, length, height; /** * Sets up the basic sim world * * @param x the largest x cooridnate in this world * @param y the largest y cooridnate in this world * @param z the largest z cooridnate in this world */ public BasicSimWorld(long x, long y, long z) { // initialise world size width=x; height=y; length=z; // initialise tick counter ticks=0; // initialise object list objectList=new LinkedList(); // set ambient light ambientLight=20; } /** * Performs one update loop */ public void tick() { updateObjects(); ticks++; } /** * Returns the number of 'ticks' since this world was started * * @return the number of ticks as a long */ public long getTime() { return ticks; } /** * Returns the light level at the specified co-ordinate. * * @return the brightness, this will always be an int between 0 and 100 */ public int getBrightness(double x, double y, double z) { double totalBrightness=0; // check through the object list for SimLights for(int i=0;i 100) { totalBrightness=100; } // add some random 'noise' to the light Random r=new Random(System.currentTimeMillis()); totalBrightness=totalBrightness+(2*r.nextFloat()); // return total brightness at this point as an int return (int) totalBrightness; } /** * Checks whether there is an obstacle in the specified co-ordinate * * @param x the x coordinate * @param y the y coordinate * @param z the z coordinate * * @return true or false accordingly */ public boolean hasObstacle(double x, double y, double z) { // examine each SimObject in turn for (int i=0;i0) { moveForward(o); } else if(o.getDesiredVelocity()<0) { moveBackward(o); } if(o.getDesiredBearingVelocityXZ()>0) { moveRight(o); } else if(o.getDesiredBearingVelocityXZ()<0) { moveLeft(o); } } else // objects are currently collided so ... { // do the opposite to what it wants to do if(o.getActualVelocity()>0 && o.getActualVelocity()!=o.getDesiredVelocity()) { moveBackward(o); } else if(o.getActualVelocity()<0 && o.getActualVelocity()!=o.getDesiredVelocity()) { moveForward(o); } if(o.getActualBearingVelocityXZ()>0 && o.getActualBearingVelocityXZ()!=o.getDesiredBearingVelocityXZ()) { moveLeft(o); } else if(o.getActualBearingVelocityXZ()<0 && o.getActualBearingVelocityXZ()!=o.getDesiredBearingVelocityXZ()) { moveRight(o); } } } } /** * Moves the object forward one step according to its current bearing * * @param o the SimObject to be moved */ private void moveForward(SimObject o) { o.setActualVelocity(1); o.setXCoord(o.getXCoord() + Math.sin(Math.toRadians(o.getActualBearingXZ()))); o.setZCoord(o.getZCoord() - Math.cos(Math.toRadians(o.getActualBearingXZ()))); } /** * Moves the object backward one step according to its current bearing * * @param o the SimObject to be moved */ private void moveBackward(SimObject o) { o.setActualVelocity(-1); o.setXCoord(o.getXCoord() - Math.sin( Math.toRadians(o.getActualBearingXZ()))); o.setZCoord(o.getZCoord() + Math.cos( Math.toRadians(o.getActualBearingXZ()))); } /** * Turns the object one step right (clockwise) * * @param o the SimObject to be moved */ private void moveRight(SimObject o) { o.setActualBearingVelocityXZ(1); o.setActualBearingXZ((o.getActualBearingXZ()+1)); } /** * Turns the object one step left (anticlockwise) * * @param o the SimObject to be moved */ private void moveLeft(SimObject o) { o.setActualBearingVelocityXZ(-1); o.setActualBearingXZ((o.getActualBearingXZ()-1)); } }