/** * Data class for a single bird entity * @author Thomas Bak **/ import java.awt.*; class Bird { int iX, iY, iTheta; Point pAim; // The spot a little ahead where the bird plans to go (relative) int iTooClose; // Says whether the bird is too close to something Color cFeathers; // The color of the bird (species, also) /** * Creates a bird * @param x X coordinate of the bird * @param y Y coordinate of the bird * @param theta The angle of the bird * @param feath The color of the bird's feathers **/ Bird(int x, int y, int theta, Color feath) { iX = x; iY = y; iTheta = theta; cFeathers = feath; pAim = new Point(iX,iY); } /** * Move the bird * @param iHeading The general heading around this bird **/ public void move(int iHeading) { // Make a funky table of movement values for the different angles // Much faster, I'd assume double iMovement[] = { 5.0, 0.0, 4.8, 1.3, 4.3, 2.5, 3.5, 3.5, 2.5, 4.3, 1.3, 4.8, 0.0, 5.0, // 90 degrees -1.3, 4.8, -2.5, 4.3, -3.5, 3.5, -4.3, 2.5, -4.8, 1.3, -5.0, 0.0, // 180 -4.8, -1.3, -4.3, -2.5, -3.5, -3.5, -2.5, -4.3, -1.3, -4.8, 0.0, -5.0, // 270 1.3, -4.8, 2.5, -4.3, 3.5, -3.5, 4.3, -2.5, 4.8, -1.3, 5.0, 0 }; int iDist; // The distance from current theta to goal int iChange = 0; if ( cFeathers != Color.black ) { if (iTheta > iHeading) { if ( Math.abs(iTheta-iHeading) < Math.abs(iTheta-(iHeading+360)) ) iChange= -30; else iChange= 30; } if (iTheta < iHeading) { if ( Math.abs(iTheta-iHeading) < Math.abs( (iTheta+360) -iHeading) ) iChange = 30; else iChange = -30; } // Go in other direction if too close (split up) if (iTooClose == 1) { iChange*= 0; } iTheta+=iChange; iTheta = (iTheta+360) % 360; iX += (int) iMovement[ (iTheta/15) * 2 ]; iY += (int) -iMovement[ (iTheta/15) * 2 + 1]; iX = (iX+300) % 300; iY = (iY+260) % 260; // Calculate where the bird is "aiming" for use by generalHeading() pAim.x = iX + (int) (20 * Math.cos(iTheta * Math.PI / 180) ); pAim.y = iY + (int) (-20 * Math.sin(iTheta * Math.PI / 180) ); // Reset the too close value iTooClose = 0; } // From the feathers chck } /** * Tell the bird that it's too close to stuff in the flock * This will make it turn in the opposite direction than it * normally would turn (To try to split apart a little **/ public void isTooClose() { iTooClose = 1; } /** * Draw the bird * @param g Where to draw it **/ public void display( Graphics g) { // Just draw a simple pie slice g.setColor(cFeathers); // Check if it's a static object (drawn differently) if (cFeathers != Color.black) g.fillArc( iX, iY, 25, 25, iTheta+180-15, 30); else g.fillArc( iX, iY, 10, 10, 0, 360); //g.drawString("Foo: " + iX + ", " + iY, 25, 25); } /** * Find out how far away this bird is from another bird * @param bOther The other bird * @return The distance **/ public int getDistance(Bird bOther) { int iDX = bOther.getX() - iX; int iDY = bOther.getY() - iY; return (int) Math.sqrt( (iDX * iDX) + (iDY * iDY) ); } /** * @return Heading of this bird **/ public int getTheta() { return iTheta; } /** * @return X Coord of this bird **/ public int getX() { return iX; } /** * @return Y Coord of this bird **/ public int getY() { return iY; } /** * @return The color of the birds feathers **/ public Color getFeathers() { return cFeathers; } /** * @return The relative point where the bird is aiming **/ public Point getAim() { return pAim; } }