Android Open Source - ubisoldiers Battle






From Project

Back to project page ubisoldiers.

License

The source code is released under:

MIT License

If you think the Android project ubisoldiers listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.gobbisanches.ubisoldiers.mechanics;
//  w ww .j ava  2s  .co m
import java.util.Random;

import static com.github.gobbisanches.ubisoldiers.mechanics.BattleLogEntry.BattleResultType.*;
import static com.github.gobbisanches.ubisoldiers.mechanics.BattleLogEntry.ShootingDirection.*;

/**
 * Created by Sanches on 29/06/2014.
 */
public class Battle {
    private final BattleSquad attackerSquad;
    private final BattleSquad defenderSquad;
    private final Random random;
    private final BattleLog log;
    private int currentRound;

    private Battle(Random random, Squad attackerSquad, Squad defenderSquad) {
        this.random = random;
        this.attackerSquad = new BattleSquad(attackerSquad);
        this.defenderSquad = new BattleSquad(defenderSquad);
        this.currentRound = 0;
        this.log = new BattleLog();
    }

    public static BattleLog performBattleAndReturnLog(Random random, Squad attackerSquad, Squad defenderSquad) {
        Battle battle = new Battle(random, attackerSquad, defenderSquad);
        battle.performBattle();
        return battle.log;
    }

    private void performBattle() {
        while (bothSidesHaveLivingUnits()) {
            performBattleRound();
        }

        checkBattleResult();
    }

    private void checkBattleResult() {
        BattleLogEntry.BattleResultType result;

        if      (didAttackerWon()) { result = ATTACKER_WON; }
        else if (didDefenderWon()) { result = DEFENDER_WON; }
        else                       { result = DRAW; }

        log.addBattleResultEntry(result);
    }

    private boolean didAttackerWon() {
        return ((!attackerSquad.isEveryoneDead()) &&
                 defenderSquad.isEveryoneDead());
    }

    private boolean didDefenderWon() {
        return (attackerSquad.isEveryoneDead() &&
                (!defenderSquad.isEveryoneDead()));
    }

    private boolean bothSidesHaveLivingUnits() {
        return (attackerSquad.isThereLivingUnits() &&
                defenderSquad.isThereLivingUnits());
    }

    private void performBattleRound() {
        startNewRound();
        performShootings();
        removeDeadUnits();
    }

    private void startNewRound() {
        ++currentRound;
    }

    private void performShootings() {
        performAttackerShootingsForRound();
        performDefenderShootingsForRound();
    }

    private void performAttackerShootingsForRound() {
        performShootingsForRound(attackerSquad, defenderSquad, FROM_ATTACKER);
    }

    private void performDefenderShootingsForRound() {
        performShootingsForRound(defenderSquad, attackerSquad, FROM_DEFENDER);
    }

    private void performShootingsForRound(BattleSquad shooters, BattleSquad targets,
                                          BattleLogEntry.ShootingDirection direction) {
        for (BattleUnit shooter : shooters.getUnits()) {
            BattleUnit target = targets.getRandomUnit(random);
            performShooting(shooter, target, direction);
        }
    }

    private void performShooting(BattleUnit shooter, BattleUnit target, BattleLogEntry.ShootingDirection direction) {
        int damage = calculateDamage(shooter, target);
        target.takeDamage(damage);
        log.addShootingEntry(currentRound, direction, shooter, target, damage);
    }

    private int calculateDamage(BattleUnit attacker, BattleUnit defender) {
        return PolicyManager.getRules().calculateDamage(
                random,
                attacker.getUnit(),
                defender.getUnit());
    }

    private void removeDeadUnits() {
        attackerSquad.removeDeadUnits();
        defenderSquad.removeDeadUnits();
    }
}




Java Source Code List

com.github.gobbisanches.ubisoldiers.app.ArmyFragment.java
com.github.gobbisanches.ubisoldiers.app.BattleActivity.java
com.github.gobbisanches.ubisoldiers.app.BattleFragment.java
com.github.gobbisanches.ubisoldiers.app.ItemListFragment.java
com.github.gobbisanches.ubisoldiers.app.MainActivity.java
com.github.gobbisanches.ubisoldiers.app.ResourceManager.java
com.github.gobbisanches.ubisoldiers.app.SingleFragmentActivity.java
com.github.gobbisanches.ubisoldiers.app.UbisoldierUosApplication.java
com.github.gobbisanches.ubisoldiers.app.UbisoldiersDriver.java
com.github.gobbisanches.ubisoldiers.app.UnitCustomizationActivity.java
com.github.gobbisanches.ubisoldiers.app.UnitCustomizationFragment.java
com.github.gobbisanches.ubisoldiers.app.UnitFragment.java
com.github.gobbisanches.ubisoldiers.app.UosManager.java
com.github.gobbisanches.ubisoldiers.mechanics.Armor.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLogEntry.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLogParser.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLog.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleSquad.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleUnit.java
com.github.gobbisanches.ubisoldiers.mechanics.Battle.java
com.github.gobbisanches.ubisoldiers.mechanics.DefaultGameRules.java
com.github.gobbisanches.ubisoldiers.mechanics.GameRules.java
com.github.gobbisanches.ubisoldiers.mechanics.General.java
com.github.gobbisanches.ubisoldiers.mechanics.ItemCollection.java
com.github.gobbisanches.ubisoldiers.mechanics.Item.java
com.github.gobbisanches.ubisoldiers.mechanics.MechanicsEngine.java
com.github.gobbisanches.ubisoldiers.mechanics.PolicyManager.java
com.github.gobbisanches.ubisoldiers.mechanics.SearchEngine.java
com.github.gobbisanches.ubisoldiers.mechanics.Soldier.java
com.github.gobbisanches.ubisoldiers.mechanics.Squad.java
com.github.gobbisanches.ubisoldiers.mechanics.Unit.java
com.github.gobbisanches.ubisoldiers.mechanics.Weapon.java