Search in sources :

Example 21 with Ship

use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.

the class Toroidal2DPhysics method advanceTime.

/**
 * Move all moveable objects and handle power ups.
 */
public void advanceTime(int currentTimeStep, Map<UUID, SpaceSettlersPowerupEnum> powerups) {
    this.currentTimeStep = currentTimeStep;
    // heal any base injuries
    for (Base base : bases) {
        base.updateEnergy(base.getHealingIncrement());
    }
    // detect collisions across all objects
    detectCollisions();
    // get the power ups and create any objects (weapons) as necessary
    for (UUID key : powerups.keySet()) {
        AbstractObject swobject = getObjectById(key);
        // if the object is not alive or it is not actionable, then ignore this
        if (!swobject.isAlive() || (!(swobject instanceof AbstractActionableObject))) {
            continue;
        }
        // otherwise, handle the power up
        handlePowerup((AbstractActionableObject) swobject, powerups.get(key));
    }
    // now move all objects that are moveable (which may include weapons)
    for (AbstractObject object : allObjects) {
        // skip non-moveable objects or dead object
        if (!object.isMoveable() || !object.isAlive()) {
            continue;
        }
        Position currentPosition = object.getPosition();
        // is it a ship that can be controlled?
        if (object.isControllable()) {
            Ship ship = (Ship) object;
            AbstractAction action = ship.getCurrentAction();
            // handle a null action
            if (action == null) {
                action = new DoNothingAction();
            }
            // need to clone the ship and space because otherwise the ship can affect
            // itself inside AbstractAction
            Movement actionMovement = action.getMovement(this.deepClone(), ship.deepClone());
            Position newPosition = applyMovement(currentPosition, actionMovement, timeStep);
            if (newPosition.isValid()) {
                ship.setPosition(newPosition);
            } else {
                ship.setPosition(currentPosition);
            }
            // spend ship energy proportional to its acceleration (old formula used velocity) and mass (new for space settlers
            // since resources cost mass)
            // double penalty = ENERGY_PENALTY * -Math.abs(ship.getPosition().getTotalTranslationalVelocity());
            double angularAccel = Math.abs(actionMovement.getAngularAccleration());
            double angularInertia = (3.0 * ship.getMass() * ship.getRadius() * angularAccel) / 2.0;
            double linearAccel = actionMovement.getTranslationalAcceleration().getMagnitude();
            double linearInertia = ship.getMass() * linearAccel;
            int penalty = (int) Math.floor(ENERGY_PENALTY * (angularInertia + linearInertia));
            ship.updateEnergy(-penalty);
            // this isn't the most general fix but it will work for now (also has to be done for bases)
            if (ship.isShielded()) {
                ship.updateEnergy(-PowerupToggleShield.SHIELD_STEP_COST);
            }
        // if (!ship.isAlive()) {
        // System.out.println("Ship " + ship.getTeamName() + ship.getId() + " is dead");
        // }
        } else {
            // move all other types of objects
            Position newPosition = moveOneTimestep(currentPosition);
            object.setPosition(newPosition);
        }
        // if any ships or bases are frozen, decrement their frozen count
        if (object instanceof AbstractActionableObject && !object.isControllable()) {
            AbstractActionableObject actionable = (AbstractActionableObject) object;
            actionable.decrementFreezeCount();
        }
    }
    // go through and see if any bases have died
    Set<Base> basesClone = new LinkedHashSet<Base>(bases);
    for (Base base : basesClone) {
        // this isn't the most general fix but it will work for now (also has to be done for bases)
        if (base.isShielded()) {
            base.updateEnergy(-PowerupToggleShield.SHIELD_STEP_COST);
        }
        if (!base.isAlive()) {
            base.setAlive(false);
            removeObject(base);
            base.getTeam().removeBase(base);
        }
    }
    // from when it was called inside updateEnergy
    for (Ship ship : ships) {
        if (ship.getEnergy() <= 0 && ship.isAlive() == true) {
            // drop any resources that the ship was carrying
            ResourcePile resources = ship.getResources();
            // Spawn a new AiCore with the same velocity magnitude and direction as its parent ship.
            // handle dropping the core if the ship died
            Position corePosition = ship.getPosition();
            corePosition.setTranslationalVelocity(ship.getPosition().getTranslationalVelocity());
            corePosition.setAngularVelocity(ship.getPosition().getAngularVelocity());
            AiCore shipCore = new AiCore(corePosition, ship.getTeamName(), ship.getTeamColor());
            this.addObject(shipCore);
            if (resources.getTotal() > 0) {
            // Position newPosition = ship.getPosition();
            // newPosition.setTranslationalVelocity(new Vector2D(0,0));
            // newPosition.setAngularVelocity(0.0);
            // Asteroid newAsteroid = new Asteroid(newPosition, true, ship.getRadius(), true, resources);
            // this.addObject(newAsteroid);
            // distributeResourcesToNearbyAsteroids(ship.getPosition(), resources);
            // System.out.println("Adding a new asteroid with resources " + newAsteroid.getResources().getTotal() +
            // " due to death, total is " + asteroids.size());
            // System.out.println("Ship died and " + resources.getTotal() + " has been added to an asteroid");
            }
            // set the ship to dead last (so we can grab its resources first)
            ship.setAlive(false);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResourcePile(spacesettlers.objects.resources.ResourcePile) AbstractActionableObject(spacesettlers.objects.AbstractActionableObject) Movement(spacesettlers.utilities.Movement) Position(spacesettlers.utilities.Position) Base(spacesettlers.objects.Base) AiCore(spacesettlers.objects.AiCore) AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction)

Example 22 with Ship

use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.

the class TestCollisionHandler method testCollidingInsideRadius.

@Test
public void testCollidingInsideRadius() {
    Ship ship;
    Asteroid asteroid;
    Position ship1Pos = new Position(100, 0, Math.PI / 4);
    ship1Pos.setTranslationalVelocity(new Vector2D(20, 0));
    ship = new Ship("team1", Color.BLUE, ship1Pos);
    Position asteroid2Pos = new Position(134, 0, -Math.PI / 4);
    asteroid2Pos.setTranslationalVelocity(new Vector2D(-10, 0));
    asteroid = new Asteroid(asteroid2Pos, false, 20, true, .33, .33, .34);
    ship.setMass(asteroid.getMass());
    // ship radius is 15 and is at 100,0
    // asteroid radius is 20 here and at 134,0
    // ship should bounce back to -99.33,0 (edge of collision) and asteroid to 134.33 (edge of collision, based on velocities)
    collisionHandler.collide(ship, asteroid, space);
    assertEquals(ship.getPosition().getTranslationalVelocityX(), -10, 0.01);
    assertEquals(ship.getPosition().getTranslationalVelocityY(), 0, 0.01);
    assertEquals(asteroid.getPosition().getTranslationalVelocityX(), 20, 0.01);
    assertEquals(asteroid.getPosition().getTranslationalVelocityY(), 0, 0.01);
    assertEquals(ship.getPosition().getX(), 99.33, 0.01);
    assertEquals(ship.getPosition().getY(), 0, 0.01);
    assertEquals(asteroid.getPosition().getX(), 134.33, 0.01);
    assertEquals(asteroid.getPosition().getY(), 0, 0.01);
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Ship(spacesettlers.objects.Ship) Test(org.junit.Test)

Example 23 with Ship

use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.

the class TestCollisionHandler method testElasticCollisionsShipToAsteroidX.

@Test
public void testElasticCollisionsShipToAsteroidX() {
    Ship ship1;
    Asteroid asteroid;
    Position ship1Pos = new Position(0, 0, Math.PI / 4);
    ship1Pos.setTranslationalVelocity(new Vector2D(20, 0));
    ship1 = new Ship("team1", Color.BLUE, ship1Pos);
    Position asteroid2Pos = new Position(10, 0, -Math.PI / 4);
    asteroid2Pos.setTranslationalVelocity(new Vector2D(-10, 0));
    asteroid = new Asteroid(asteroid2Pos, false, 10, true, .33, .33, .34);
    ship1.setMass(asteroid.getMass());
    collisionHandler.collide(ship1, asteroid, space);
    assertEquals(ship1.getPosition().getTranslationalVelocityX(), -10, 0.01);
    assertEquals(ship1.getPosition().getTranslationalVelocityY(), 0, 0.01);
    assertEquals(asteroid.getPosition().getTranslationalVelocityX(), 20, 0.01);
    assertEquals(asteroid.getPosition().getTranslationalVelocityY(), 0, 0.01);
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Ship(spacesettlers.objects.Ship) Test(org.junit.Test)

Example 24 with Ship

use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.

the class TestCollisionHandler method testCollidingWithMineableAsteroid.

@Test
public void testCollidingWithMineableAsteroid() {
    Ship ship1;
    Asteroid asteroid;
    Position ship1Pos = new Position(0, 0, Math.PI / 4);
    ship1Pos.setTranslationalVelocity(new Vector2D(20, 0));
    ship1 = new Ship("team1", Color.BLUE, ship1Pos);
    Position asteroid2Pos = new Position(10, 0, -Math.PI / 4);
    asteroid2Pos.setTranslationalVelocity(new Vector2D(-10, 0));
    asteroid = new Asteroid(asteroid2Pos, true, 10, true, .33, .33, .34);
    ResourcePile asteroidResources = asteroid.getResources();
    collisionHandler.collide(ship1, asteroid, space);
    assertEquals(ship1.getResources().getResourceQuantity(ResourceTypes.FUEL), asteroidResources.getResourceQuantity(ResourceTypes.FUEL), 0.0);
    assertEquals(ship1.getResources().getResourceQuantity(ResourceTypes.WATER), asteroidResources.getResourceQuantity(ResourceTypes.WATER), 0.0);
    assertEquals(ship1.getResources().getResourceQuantity(ResourceTypes.METALS), asteroidResources.getResourceQuantity(ResourceTypes.METALS), 0.0);
}
Also used : ResourcePile(spacesettlers.objects.resources.ResourcePile) Asteroid(spacesettlers.objects.Asteroid) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Ship(spacesettlers.objects.Ship) Test(org.junit.Test)

Example 25 with Ship

use of spacesettlers.objects.Ship in project spacesettlers by amymcgovern.

the class ResourcesPanel method updateData.

public void updateData(AbstractObject object) {
    ResourcePile avail = null;
    ResourcePile total = null;
    if (object.getClass() == Asteroid.class) {
        Asteroid asteroid = (Asteroid) object;
        avail = asteroid.getResources();
        total = avail;
    } else if (object.getClass() == Ship.class) {
        Ship ship = (Ship) object;
        avail = ship.getResources();
        total = avail;
    } else if (object.getClass() == Base.class) {
        Base base = (Base) object;
        avail = base.getResources();
        total = base.getResources();
    } else if (object.getClass() == Beacon.class) {
        Beacon beacon = (Beacon) object;
    }
    if (avail != null) {
        waterAvail.setText("" + avail.getResourceQuantity(ResourceTypes.WATER));
        fuelAvail.setText("" + avail.getResourceQuantity(ResourceTypes.FUEL));
        metalsAvail.setText("" + avail.getResourceQuantity(ResourceTypes.METALS));
        waterTotal.setText("" + total.getResourceQuantity(ResourceTypes.WATER));
        fuelTotal.setText("" + total.getResourceQuantity(ResourceTypes.FUEL));
        metalsTotal.setText("" + total.getResourceQuantity(ResourceTypes.METALS));
    } else {
        waterAvail.setText("0");
        fuelAvail.setText("0");
        metalsAvail.setText("0");
        waterTotal.setText("0");
        fuelTotal.setText("0");
        metalsTotal.setText("0");
    }
}
Also used : ResourcePile(spacesettlers.objects.resources.ResourcePile) Asteroid(spacesettlers.objects.Asteroid) Beacon(spacesettlers.objects.Beacon) Ship(spacesettlers.objects.Ship) Base(spacesettlers.objects.Base)

Aggregations

Ship (spacesettlers.objects.Ship)49 UUID (java.util.UUID)20 Base (spacesettlers.objects.Base)19 Position (spacesettlers.utilities.Position)18 HashMap (java.util.HashMap)16 AbstractAction (spacesettlers.actions.AbstractAction)15 DoNothingAction (spacesettlers.actions.DoNothingAction)15 AbstractObject (spacesettlers.objects.AbstractObject)15 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)9 Asteroid (spacesettlers.objects.Asteroid)9 Vector2D (spacesettlers.utilities.Vector2D)8 Test (org.junit.Test)7 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)7 PurchaseTypes (spacesettlers.actions.PurchaseTypes)7 Beacon (spacesettlers.objects.Beacon)7 AiCore (spacesettlers.objects.AiCore)6 Flag (spacesettlers.objects.Flag)5 Team (spacesettlers.clients.Team)4 ResourcePile (spacesettlers.objects.resources.ResourcePile)4 ExecutionException (java.util.concurrent.ExecutionException)2