import java.awt.*; /* *---------------------------------------------------------------------- * * Obstacle.java */ /** Instances of this class are ammo boxes and rocks. Trees extends this class. @see Tree */ /* *---------------------------------------------------------------------- */ public class Obstacle { private Point coord; private Image texture; /* * Border of the obstacle. */ private Polygon poly; /* *---------------------------------------------------------------------- * * Obstacle */ /** @param t texture with which the obstacle is drawn. @param x x-coordinate @param y y-coordinate @param p the border of the obstacle. */ /* *---------------------------------------------------------------------- */ public Obstacle(Image t, int x, int y, Polygon p) { texture = t; coord = new Point(x, y); poly = new Polygon(p.xpoints, p.ypoints, p.npoints); /* * Translate the polygon to the screen coordinate. */ for (int n = 0; n < poly.npoints; n++) { poly.xpoints[n] += x; poly.ypoints[n] += y; } } /* *---------------------------------------------------------------------- * * draw */ /** Draws the obstacle. Can also draw the help polygons for collision detection. See Obstacle.java for more details. */ /* *---------------------------------------------------------------------- */ public void draw(Graphics g) { g.drawImage(texture, coord.x, coord.y, null); /* * Removing the comments on the following lines will * draw the help polygons for collision detection. */ // g.drawPolygon(poly); // Rectangle r = poly.getBoundingBox(); // g.drawRect(r.x, r.y, r.width, r.height); // for (int i = 0; i < poly.npoints - 1; i++) { // g.drawPolygon(helpPoly(i)); // } } /* *---------------------------------------------------------------------- * * getPolygon */ /** @return the polygon that defines the border of the obstacle. */ /* *---------------------------------------------------------------------- */ public Polygon getPolygon() { return poly; } /* *---------------------------------------------------------------------- * * getCentre */ /** @return centre of the obstacle. */ /* *---------------------------------------------------------------------- */ public Point getCentre() { int sumX = 0; int sumY = 0; int n; for (n = 1; n < poly.npoints; n++) { sumX += poly.xpoints[n]; sumY += poly.ypoints[n]; } return new Point (sumX / (n - 1), sumY / (n - 1)); } /* *---------------------------------------------------------------------- * * helpPoly */ /** @param i which point a help polygon is wanted for @return help polygon for point i and i+1 */ /* *---------------------------------------------------------------------- */ public Polygon helpPoly(int i) { Point center = getCentre(); Polygon helpPoly = new Polygon(); helpPoly.addPoint(center.x, center.y); helpPoly.addPoint(poly.xpoints[i], poly.ypoints[i]); helpPoly.addPoint(poly.xpoints[i + 1], poly.ypoints[i + 1]); helpPoly.addPoint(center.x, center.y); return helpPoly; } }