public void endTurn() { for (Building building : buildings) building.endTurn(); for (Army army : armies) army.endTurn(); if (king.getHitPoints() <= 0) { kingDead = true; if (lives-- <= 0) lose(); } }
public void newTurn() { if (kingDead) newKing(); for (Building building : buildings) building.newTurn(); for (Army army : armies) army.newTurn(); finished = false; try { player.newTurn(); } catch (Exception ex) { ex.printStackTrace(); System.out.println(name + " loses because he threw an exception."); lose(); } }
/** * 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()
public void newKing() { king = new Hero(people.getKing(), 2, name); Vector<Troop> heroTroops = new Vector<Troop>(); UnitType worker = game.getWorker(people); heroTroops.add(new Troop(worker, 0, king)); Army.buildArmy(heroTroops, start, this); }
@Test public void test() { Army forcesOfLight = new Army("Forces of Light", null, Color.RED); final int NUM_ELF = 5; forcesOfLight.populate(ActorFactory.Type.ELF, NUM_ELF); final int NUM_HOBBIT = 6; forcesOfLight.populate(ActorFactory.Type.HOBBIT, NUM_HOBBIT); final int NUM_ORC = 7; forcesOfLight.populate(ActorFactory.Type.ORC, NUM_ORC); final int NUM_WIZARD = 8; forcesOfLight.populate(ActorFactory.Type.WIZARD, NUM_WIZARD); forcesOfLight.display(); assertTrue( "Number Actors created mismatches number stored", forcesOfLight.getSize() == NUM_ELF + NUM_HOBBIT + NUM_ORC + NUM_WIZARD); }
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); } }
public Side(String name, boolean human, People people, Player player, Position start) { this.name = name; this.human = human; this.people = people; map = new ForestMap(this); messages = new Vector<String>(); this.player = player; // Get the default start stuff for the given people. ressources = new Ressources(this.people.getStartingRessources()); System.out.println( "Ressourcenlaenge: " + ressources.getRessources().size() + " Ressourcen: " + ressources); armies = new Vector<Army>(); UnitType[] startingUnits = people.getStartingUnits(); int[] numbers = people.getNumbers(); Vector<Troop> startingTroops = new Vector<Troop>(); int i; kingType = people.getKing(); king = new Hero(kingType, 2, name); startingTroops.add(new Troop(startingUnits[0], numbers[0], king)); for (i = 1; i < numbers.length; i++) { startingTroops.add(new Troop(startingUnits[i], numbers[i])); } Army.buildArmy(startingTroops, start, this); ResearchType[] startingResearchs = people.getStartingResearchs(); int[] levels = people.getLevels(); researchs = new Vector<Research>(); for (i = 0; i < levels.length; i++) { researchs.add(new Research(startingResearchs[i], levels[i])); } buildings = new Vector<Building>(); try { addBuilding(new Building(people.getHeadQuader(), start, this)); } catch (IsAlreadyBuildException ex) { ex.printStackTrace(); System.exit(1); } }
public void addArmy(Army army) { armies.add(army); map.addArmy(army.getPosition(), army.getSightRange()); }
public void removeArmy(Army army) { armies.remove(army); Position position = army.getPosition(); map.removeArmy(position, army.getSightRange()); }