/** * Ok, fine, "mob" is a bit of an overstatement. This class helps manage * a pair of deadly turtles. * * @author Brad Richards * @version 1.0 */ public class TurtleMob { Turtle comet; // Our first, ferocious turtle Turtle cupid; // Don't let the name fool you... /** * This copy constructor sets up our "mob" to look exactly like the * TurtleMob instance passed in as an input. We need to make sure * that the two turtles we manage are completely separate from the two * turtles in the other "mob", and that the Circles in our two turtles * are separate from their Circles but have the same state. * * @param otherMob A reference to another TurtleMob object we want to copy */ public TurtleMob(TurtleMob otherMob) { // This seems way too easy, but most of the hard work is hidden in // the Turtle class's copy constructor. comet = new Turtle(otherMob.comet); // Use Turtle's copy constructor! cupid = new Turtle(otherMob.cupid); } /** * Default constructor for objects of class TurtleMob. It makes a * pair of Turtle instances with the same "speed", then calls splitUp() * to make sure they're not at exactly the same position on the screen. */ public TurtleMob() { comet = new Turtle(40); // Make a new Turtle cupid = new Turtle(40); // Make an identical second Turtle splitUp(); // Move them apart } /** * Causes the two Turtles to move apart horizontally, then leaves them * both facing south. */ public void splitUp() { comet.faceWest(); cupid.faceEast(); comet.forward(); cupid.forward(); comet.faceSouth(); cupid.faceSouth(); } /** * Moves both turtles forward one "step". */ public void advance() { comet.forward(); cupid.forward(); } /** * Compares "us" to another TurtleMob. It returns true if *all* details * of our first turtle match theirs (including the Circle within it), and * the same is true for our second turtle and theirs. We get off easy * here since there's an equals() method in the Turtle class that can do * most of the work for us. * * @param other A reference to the TurtleMob to which we will compare ourselces * @return True if our turtles match their turtles, false otherwise. */ public boolean equals(TurtleMob other) { return comet.equals(other.comet) && cupid.equals(other.cupid); } /** * Compares our two turtles to see if they're at the same position on the * screen. There's no method in the Turtle class that can help us here, * so we need to go look into their Circle objects, get their X and Y * positions, and compare those. * * @return True if our circles are at the same spot on the screen. */ public boolean sameSpot() { // System.out.println(comet.shell.xPosition); Circle c1 = comet.getCircle(); // A reference to comet's Circle Circle c2 = cupid.getCircle(); // A reference to cupid's Circle return c1.getXPosition() == c2.getXPosition() && c1.getYPosition() == c2.getYPosition(); } /** * Moves our turtles so that they end up on the same spot on the screen. * We can't set their positions directly, so we need to calculate how * far apart they are and do some moves to line them up. The code below * has comet close the gap vertically, and cupid close it horizontally. */ public void comeTogether() { int xDifference = comet.getCircle().getXPosition() - cupid.getCircle().getXPosition(); int yDifference = comet.getCircle().getYPosition() - cupid.getCircle().getYPosition(); // First we'll make comet move north or south to line up with cupid. // Given the way Turtles work, I can just always turn north and move // the distance I determined above, even if it's negative comet.faceNorth(); comet.setDistance(yDifference); comet.forward(); // Now have cupid do the same in the east-west direction cupid.faceEast(); cupid.setDistance(xDifference); cupid.forward(); // Put our turtles back into a reasonable state again comet.faceNorth(); cupid.faceNorth(); comet.setDistance(40); cupid.setDistance(40); } }