Search in sources :

Example 1 with Missile

use of spacesettlers.objects.weapons.Missile in project spacesettlers by amymcgovern.

the class CollisionHandler method missileCollision.

/**
 * Collide with a missile
 * @param object1
 * @param object2
 */
private void missileCollision(Missile missile, AbstractObject object2) {
    // get the ship that fired this
    Ship firingShip = missile.getFiringShip();
    firingShip.decrementWeaponCount();
    // did it hit a ship?
    if (object2.getClass() == Ship.class) {
        Ship ship = (Ship) object2;
        // only take damageInflicted if not shielded
        if (!ship.isShielded()) {
            double initialEnergy = ship.getEnergy();
            ship.updateEnergy(missile.getDamage());
            if (ship.getEnergy() <= 0) {
                // if you killed the ship, only count the final amount of damage needed to kill it
                firingShip.incrementDamageInflicted((int) initialEnergy);
                ship.incrementDamageReceived(-(int) initialEnergy);
            } else {
                // otherwise a missile is a fixed amount of damage
                firingShip.incrementDamageInflicted(-missile.getDamage());
                ship.incrementDamageReceived(missile.getDamage());
            }
            // it hit a ship
            firingShip.incrementHitsInflicted();
        }
        // if the bullet killed the ship, credit the ship that hit it
        if (ship.getEnergy() <= 0) {
            // System.out.println("ship " + firingShip.getTeamName() + " stealing resourcesAvailable " + shipMoney + " from " + ship.getTeamName() + ship.getId());
            // it killed a ship
            firingShip.incrementKillsInflicted();
            ship.incrementKillsReceived();
        }
    }
    // did it hit a base?
    if (object2.getClass() == Base.class) {
        Base base = (Base) object2;
        // only take damageInflicted if not shielded
        if (!base.isShielded()) {
            double initialEnergy = base.getEnergy();
            base.updateEnergy(missile.getDamage());
            if (base.getEnergy() <= 0) {
                // if the base is dead, you can only count the energy it had prior to being dead
                firingShip.incrementDamageInflicted((int) initialEnergy);
                base.incrementDamageReceived(-(int) initialEnergy);
            // System.out.println("Firing at a dead base - should give only " + (int) -initialEnergy + " in damage");
            } else {
                // otherwise the missles count constant
                firingShip.incrementDamageInflicted(-missile.getDamage());
                base.incrementDamageReceived(missile.getDamage());
            }
            // it hit a base
            firingShip.incrementHitsInflicted();
        }
    }
    // Handle a bullet hitting a bullet
    if (object2.getClass() == Missile.class) {
        object2.setAlive(false);
        Ship otherFiringShip = ((Missile) object2).getFiringShip();
        otherFiringShip.decrementWeaponCount();
    }
    // Did the missile hit an AiCore? If so, damage the AiCore.
    if (object2.getClass() == AiCore.class) {
        AiCore core = (AiCore) object2;
        core.updateEnergy(-missile.getDamage());
    }
    // make the missile die
    missile.setAlive(false);
}
Also used : Missile(spacesettlers.objects.weapons.Missile) AiCore(spacesettlers.objects.AiCore) Ship(spacesettlers.objects.Ship) Base(spacesettlers.objects.Base)

Aggregations

AiCore (spacesettlers.objects.AiCore)1 Base (spacesettlers.objects.Base)1 Ship (spacesettlers.objects.Ship)1 Missile (spacesettlers.objects.weapons.Missile)1