/** * 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()
/** Pauses a <i>TranslateTransition</i> if it is actively running. */ public void pauseMotion() { if (tt.getStatus() == Animation.Status.RUNNING) tt.pause(); } // end suspendMotion()