567b691afc8a0d0f4c2fde80a8f607caedc3ada3
[applet-bots.git] / src / appletbots / gatherers / GatherersWorld.java
blob567b691afc8a0d0f4c2fde80a8f607caedc3ada3
1 /*
2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserved
3 */
4 package appletbots.gatherers;
6 import appletbots.World;
7 import appletbots.WorldObject;
8 import appletbots.geometry.Point;
10 import java.awt.*;
11 import java.util.Iterator;
13 /**
14 * This class represents a world for Gatherers to collect food. It consists
15 * of two "home bases" in opposite corners.
17 * @author Erik Rasmussen
19 public class GatherersWorld extends World
21 /**
22 * The size of the home base zone
24 private int zoneSize;
26 /**
27 * Constructs a new gatherers world with the given dimensions
29 * @param width The width of the world in pixels
30 * @param height The height of the world in pixels
31 * @param zoneSize The size of the home base zones measured by the number of
32 * pixels from world corners where the zone boundary hits
33 * the edge of the world
35 public GatherersWorld(final int width, final int height, final int zoneSize)
37 super(width, height);
38 this.zoneSize = zoneSize;
41 /**
42 * Paints the objects in the world
44 * @param g The graphics object on which to paint the objects
46 protected void paintObjects(final Graphics g)
48 g.setColor(Color.green);
49 g.drawLine(0, zoneSize, zoneSize, 0);
50 g.drawLine(getWorldWidth() - zoneSize, getWorldHeight(), getWorldWidth(), getWorldHeight() - zoneSize);
51 super.paintObjects(g);
54 /**
55 * Returns whether or not the given object is in the home base zone of
56 * team specified by the bottomRightTeam boolean parameter
58 * @param object The object to check
59 * @param bottomRightTeam Checks the home base zone in the bottom right if
60 * true, and the zone in the top left if false
61 * @return Whether or not the given object is in the home base zone of
62 * team specified by the bottomRightTeam boolean parameter
64 public boolean isInHome(final WorldObject object, final boolean bottomRightTeam)
66 final Point location = getData(object).getLocation();
67 if (bottomRightTeam)
69 return (getWorldWidth() - location.x) + (getWorldHeight() - location.y) < zoneSize;
71 else
73 return location.x + location.y < zoneSize;
77 /**
78 * Called by the WorldThread. Checks if one team is has won by getting all
79 * the food into its home base zone, and pauses the world if a win has
80 * occurred. (Also causes World.incrementTime().)
82 public void incrementTime()
84 super.incrementTime();
85 boolean blueWins = true;
86 boolean redWins = true;
87 for (Iterator iterator = objectsTable.keySet().iterator(); iterator.hasNext();)
89 final WorldObject object = (WorldObject) iterator.next();
90 if (object instanceof Food)
92 if (isInHome(object, true))
93 redWins = false;
94 else if (isInHome(object, false))
95 blueWins = false;
96 else
98 blueWins = false;
99 redWins = false;
103 if (blueWins || redWins)
104 stopThread();