/ Published in: Java
OO Software Development Project
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * */ package burgertime; import java.awt.Color; import java.awt.Shape; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D.Double; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; import javax.imageio.ImageIO; /** * Guard class has one guard and handles all of the moving and collision * detection with the path * * @author xiaox. trowbrct. obrienm. */ private double VELOCITY; public boolean isAlive; private DrawableComponent drawComponent; private int xDelta; private int yDelta; private int levelNumber; private boolean stuck; /** * Takes the start point, width, heighth, and path list for the guard to * follow * * @param start * @param characterWidth * @param characterHeight * @param pathList * @param drawableComponent * @param levelNumber * */ ArrayList<Rectangle2D> pathList, DrawableComponent drawableComponent, int levelNumber, Score score) { super(start, characterWidth, characterHeight, pathList, score); this.VELOCITY = 0.65; this.stuck = false; this.drawComponent = drawableComponent; super.currentPosition.getY(), characterWidth, characterHeight); this.xDelta = 0; this.yDelta = 1; this.levelNumber = levelNumber; this.isAlive = true; this.currentPosition.getX(), this.currentPosition.getY() + super.height); this.currentPosition.getX() + super.width, this.currentPosition.getY() + super.height); this.pointList[0] = newPositionBottomLeft; this.pointList[1] = newPositionBottomRight; this.currentPosition.getX() - (this.xDelta * this.VELOCITY), this.currentPosition.getY() - (this.yDelta * this.VELOCITY) + super.height); this.currentPosition.getX() - (this.xDelta * this.VELOCITY) + super.width, this.currentPosition.getY() - (this.yDelta * this.VELOCITY) + super.height); this.prevPointList[0] = prevPositionBottomLeft; this.prevPointList[1] = prevPositionBottomRight; } /** * When the Hero dies, this resets the guard's position * */ public void resetPosition() { this.isAlive = true; super.STARTPOINT.getY(), super.width, super.height)); super.STARTPOINT.getY()); this.xDelta = 0; this.yDelta = 1; this.score.addScore(-100); } /* * (non-Javadoc) * * @see burgertime.AbstractCharacter#updatePosition(int, int) */ @Override public void updatePosition(double x, double y) { if (this.isAlive == false) { } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 5; k++) { if (this.character .intersects(this.drawComponent.burger[i].burger[j][k]) && this.drawComponent.burger[i].getIsMoving(j)) { this.isAlive = false; this.xDelta = 0; this.yDelta = 1; this.addScore(100); @Override public void run() { } }, 2500); break; } } } } this.currentPosition.getX() + (x * this.VELOCITY), this.currentPosition.getY() + (y * this.VELOCITY)); this.currentPosition.getX() + (x * this.VELOCITY), this.currentPosition.getY() + (y * this.VELOCITY) + super.height); this.currentPosition.getX() + (x * this.VELOCITY) + super.width, this.currentPosition.getY() + (y * this.VELOCITY) + super.height); this.pointList[0] = newPositionBottomLeft; this.pointList[1] = newPositionBottomRight; if (this.isOnPath(this.pointList)) { this.currentPosition = newPositionTopLeft; super.currentPosition.getX(), super.currentPosition.getY(), super.width, super.height); } } /** * TODO Put here a description of what this method does. * * @return * */ @SuppressWarnings("javadoc") public boolean previousInIntersection() { this.currentPosition.getX() - (this.xDelta * this.VELOCITY), this.currentPosition.getY() - (this.yDelta * this.VELOCITY) + super.height); this.currentPosition.getX() - (this.xDelta * this.VELOCITY) + super.width, this.currentPosition.getY() - (this.yDelta * this.VELOCITY) + super.height); this.prevPointList[0] = prevPositionBottomLeft; this.prevPointList[1] = prevPositionBottomRight; return this.isAtIntersection(this.prevPointList); } /** * TODO Put here a description of what this method does. * * @param x * @param y * @return */ public boolean isOption(int x, int y) { int xBuffer = x; int yBuffer = y; this.currentPosition.getX() + (this.xDelta * this.VELOCITY) + xBuffer, this.currentPosition.getY() + (this.yDelta * this.VELOCITY) + super.height + yBuffer); this.currentPosition.getX() + (this.xDelta * this.VELOCITY) + super.width + xBuffer, this.currentPosition.getY() + (this.yDelta * this.VELOCITY) + super.height + yBuffer); option[0] = nextPossiblePositionBottomLeft; option[1] = nextPossiblePositionBottomRight; return (this.isOnPath(option)); } /** * TODO Put here a description of what this method does. * */ public void moveGuard() { this.currentPosition.getX(), this.currentPosition.getY() + super.height); this.currentPosition.getX() + super.width, this.currentPosition.getY() + super.height); int type = 0; int posOrNegX = 0; int posOrNegY = 0; if (this.isAtIntersection(bottomCornersCurrent) && !this.previousInIntersection()) { if (this.drawComponent.hero.currentPosition.getX() > this.currentPosition .getX()) { posOrNegX = 1; } else { posOrNegX = -1; } if (this.drawComponent.hero.currentPosition.getY() > this.currentPosition .getY()) { posOrNegY = 1; } else { posOrNegY = -1; } if (num < 0.5) { if (!this.isOption(15 * posOrNegX, 0)) { if (this.isOption(0, 15 * posOrNegY)) { type = 1; } else { this.xDelta *= -1; this.yDelta *= -1; this.stuck = true; } } } else { type = 1; if (!this.isOption(0, 15 * posOrNegY)) { if (this.isOption(15 * posOrNegX, 0)) { type = 0; } else { this.xDelta *= -1; this.yDelta *= -1; this.stuck = true; } } } if (type == 0 && this.stuck == false) { if (posOrNegX > 0) { this.xDelta = 1; this.yDelta = 0; } else { this.xDelta = -1; this.yDelta = 0; } } else if (type == 1 && this.stuck == false) { if (posOrNegY > 0) { this.xDelta = 0; this.yDelta = 1; } else { this.xDelta = 0; this.yDelta = -1; } } } this.stuck = false; this.updatePosition(this.xDelta, this.yDelta); } /* * (non-Javadoc) * * @see burgertime.Drawable#getColor() */ @Override return this.guardColor; } /* * (non-Javadoc) * * @see burgertime.Drawable#getShape() */ @Override return this.character; } /* * (non-Javadoc) * * @see burgertime.AbstractCharacter#isOnPath(java.awt.geom.Point2D[]) */ @Override int count = 0; for (int i = 0; i < pointList.length; i++) { for (int j = 0; j < super.pathList.size(); j++) { if (super.pathList.get(j).contains(pointList[i])) { count++; break; } } } if (count == 2) { return true; } return false; } /** * TODO Put here a description of what this method does. * * @param pointList * @return */ int count = 0; for (int i = 0; i < pointList.length; i++) { count = 0; for (int j = 0; j < super.pathList.size(); j++) { if (super.pathList.get(j).contains(pointList[i]) - pointList[i].getY()) > 4 - pointList[i].getX()) > 4) { count++; if (count == 1 && i == 0) { firstRect1 = super.pathList.get(j); } else if (i == 0) { secondRect1 = super.pathList.get(j); if (firstRect1.equals(secondRect1)) { count--; } } else if (count == 1 && i == 1) { firstRect2 = super.pathList.get(j); } else if (i == 1) { secondRect2 = super.pathList.get(j); if (firstRect2.equals(secondRect2)) { count--; } } } else if (count == 2) { break; } } } if (firstRect1 == null) { } if (firstRect2 == null) { } if (secondRect1 == null) { } if (secondRect2 == null) { } if (firstRect1.equals(firstRect2) && secondRect1.equals(secondRect2)) { return true; } return false; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override public void run() { try { while (this.drawComponent.isLevelThreading[this.levelNumber]) { this.moveGuard(); } // do nothing here } } /* (non-Javadoc) * @see burgertime.AbstractCharacter#getImage() */ @Override try { return image; e.printStackTrace(); } return null; } }