import java.awt.geom.Rectangle2D; /* * Superclass of all objects in game. */ public class GameObject { protected double locationX, locationY; // Location of object (top-left corner of bounding box) protected double speedX, speedY; // Speed of object protected double sizeX, sizeY; // Size of object protected int degrees; // Angle of object. 0 is vertical. public GameObject() { // Will initialize all values to 0 by default } public GameObject(double locationX, double locationY, double speedX, double speedY, double sizeX, double sizeY) { this.locationX = locationX; this.locationY = locationY; this.speedX = speedX; this.speedY = speedY; this.sizeX = sizeX; this.sizeY = sizeY; } public String toString() { StringBuffer buf = new StringBuffer(100); buf.append("Location: ("+locationX+","+locationY+")"); buf.append(" Speed: ("+speedX+","+speedY+")"); buf.append(" Size: ("+sizeX+","+sizeY+")"); return buf.toString(); } public void move() { locationX += speedX; locationY += speedY; } public static boolean detectCollision(GameObject obj1, GameObject obj2) { // Returns true if two game objects collide. Determine by if there is an overlap in their bounding boxes. Rectangle2D.Double rect = new Rectangle2D.Double(obj1.locationX, obj1.locationY, obj1.sizeX, obj1.sizeY); return rect.intersects(obj2.locationX, obj2.locationY, obj2.sizeX, obj2.sizeY); } // Handle wrapping of object "around the screen" public void moveWrap(int maxx, int maxy) { move(); // Move the object // Now check if it is outside of the boundary and move it to the other side if (locationX < 0) locationX = maxx+locationX; if (locationX > maxx) locationX = locationX-maxx; if (locationY < 0) locationY = maxy+locationY; if (locationY > maxy) locationY = locationY-maxy; } public boolean moveDiscard(int maxx, int maxy) { move(); // Move the object // Now check if it is outside of the boundary. // If so, send back a flag to indicate so. if (locationX < 0 || locationX > maxx || locationY < 0 || locationY > maxy) return true; return false; } public void setLocationX(double locationX) { this.locationX = locationX; } public double getLocationX() { return locationX; } public void setLocationY(double locationY) { this.locationY = locationY; } public double getLocationY() { return locationY; } public void setSizeY(double sizeY) { this.sizeY = sizeY; } public double getSizeY() { return sizeY; } public void setSpeedX(double speedX) { this.speedX = speedX; } public double getSpeedX() { return speedX; } public void setSizeX(double sizeX) { this.sizeX = sizeX; } public double getSizeX() { return sizeX; } public void setSpeedY(double speedY) { this.speedY = speedY; } public double getSpeedY() { return speedY; } }