/** * Defines the characteristics of a <i>TranslateTransition</i>. Each call results in ONE segment * of motion. When that segment is finished, it "chains" another call to <i>startMotion()</i> * (which is NOT recursion)! The initial call is made by the managing <i>Army</i> object; * subsequent calls are made through the "chaining" process described here. * * @param engageInCombat TODO */ public void startMotion(boolean engageInCombat) { Army opposingArmy = armyAllegiance.getOpposingArmy(); Actor opponent = opposingArmy.findNearestOpponent( this); // could legitimately return a null: 1) no one is visible 2) no Actors in // opposing army Point2D newLocation; if (opponent != null) { System.out.printf( "ToMove:[%.1f:%.1f] Opponent:[%.1f:%.1f]\n", getAvatar().getTranslateX(), getAvatar().getTranslateY(), opponent.getAvatar().getTranslateX(), opponent.getAvatar().getTranslateX()); double DISTANCE_FOR_BATTLE = 50.0; if (engageInCombat && distanceTo(opponent) < DISTANCE_FOR_BATTLE) { double h1, h2, h3, h4; // debug code h1 = this.getHealth(); h2 = opponent.getHealth(); combatRound(opponent); h3 = this.getHealth(); h4 = opponent.getHealth(); h4 = h4; if (this.getHealth() <= 0.0) { armyAllegiance.removeNowDeadActor(this); } if (opponent.getHealth() <= 0.0) { opponent.armyAllegiance.removeNowDeadActor(opponent); } } // end if (combat) newLocation = findNewLocation(opponent); } else // end if (test for null opponent) newLocation = meander(); // null opponent means we wander around close to our current location if (tt.getStatus() != Animation.Status.RUNNING) { // if NOT yet RUNNING, start . . . otherwise, do nothing. // tt.setToX(Math.random()*getAvatar().getScene().getWidth()); // tt.setToY(Math.random()*getAvatar().getScene().getHeight()); tt.setToX(validateCoordinate(newLocation).getX()); tt.setToY(validateCoordinate(newLocation).getY()); tt.setDuration( Duration.seconds(MAX_SPEED / (getSpeed() * (armyAllegiance.getSpeedControllerValue())))); tt.setOnFinished(event -> startMotion(true)); // NOT RECURSION!!!! tt .play(); // give assembled object to the render engine (of course, play() is an // object-oriented method which has access to "this" inside, and it can use // "this" to give to the render engine. } } // end startMotion()
private Point2D validateCoordinate(Point2D possibleNewLocation) { double maxY = armyAllegiance.getScene().getHeight(); double maxX = armyAllegiance.getScene().getWidth(); double myX = possibleNewLocation.getX(); double myY = possibleNewLocation.getY(); double newX = 0.0; double newY = 0.0; if ((myX < 0) && (myY < 0)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxX), 2.0); newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxY), 2.0); return new Point2D(newX, newY); } else if ((0 < myX) && (myX < maxX) && (myY < 0)) { newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxY), 2.0); return new Point2D(myX, newY); } else if ((myX > maxX) && (myY < 0)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxX), 2.0); newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxY), 2.0); return new Point2D(newX, newY); } else if ((0 < myX) && (myY < maxY) && (myY > 0)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxX), 2.0); return new Point2D(newX, myY); } else if ((myX > maxX) && (myY < maxY) && (myY > 0)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxX), 2.0); return new Point2D(newX, myY); } if ((myX < 0) && (myY > maxY)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.25 * maxX), 2.0); newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxY), 2.0); return new Point2D(newX, newY); } else if ((0 < myX) && (myX < maxX) && (myY > maxY)) { newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxY), 2.0); return new Point2D(myX, newY); } else if ((myX > maxX) && (myY > maxY)) { newX = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxX), 2.0); newY = SingletonRandom.instance.getNormalDistribution(0.0, (0.75 * maxY), 2.0); return new Point2D(newX, newY); } else { return new Point2D(myX, myY); } }