567b691afc8a0d0f4c2fde80a8f607caedc3ada3
2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserved
4 package appletbots
.gatherers
;
6 import appletbots
.World
;
7 import appletbots
.WorldObject
;
8 import appletbots
.geometry
.Point
;
11 import java
.util
.Iterator
;
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
22 * The size of the home base zone
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
)
38 this.zoneSize
= zoneSize
;
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
);
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();
69 return (getWorldWidth() - location
.x
) + (getWorldHeight() - location
.y
) < zoneSize
;
73 return location
.x
+ location
.y
< zoneSize
;
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))
94 else if (isInHome(object
, false))
103 if (blueWins
|| redWins
)