/** * returns the amount of asteroids in the game * * @return the amount of asteroids in the game */ public int getAsteroidCount() { int i = 0; for (SpaceObject object : this.objects) { if (object.getClass() == Asteroid.class) i++; } return i; }
/** * returns the points which can be earned if all asteroids were shot down * * @return the points which can be earned if all asteroids were shot down */ public int getTotalPoints() { int totalPoints = 0; for (SpaceObject object : this.objects) { totalPoints += object.getPoints(); } return totalPoints; }
/** * Generates the printable vision of each object in the game * * @return each object in the game as printable in an arraylist */ public ArrayList<Printable> getPrintables() { ArrayList<Printable> list = new ArrayList<Printable>(); for (SpaceObject object : this.objects) { list.add(object.getPrintable()); } list.add(player.getPrintable()); return list; }
/** * handles objects, so deletes them if they should be deleted, all changes are made recursively * * @param objects the objects which should be handled * @return the rest of the objects which shouldn't be deleted */ public ArrayList<SpaceObject> handleObjects(ArrayList<SpaceObject> objects) { if (objects.size() <= 0) return objects; SpaceObject thisObject = objects.get(0); ArrayList<SpaceObject> remainingObjects = getRestOfList(objects); ArrayList<SpaceObject> thisOverlappingObjects = thisObject.overlapingObjects(objects); if (thisObject.shouldBeDeletedIfOverlaps(thisOverlappingObjects)) { for (SpaceObject overlappingObject : thisObject.shouldBeBothDeletedIfOverlaps(thisOverlappingObjects)) { this.score += overlappingObject.getPoints(); remainingObjects.remove(overlappingObject); } this.score += thisObject.getPoints(); return handleObjects(remainingObjects); } else if (thisObject.shouldBeDeletedAsItCrashsWithWall(this.resolution)) { return handleObjects(remainingObjects); } else { remainingObjects = handleObjects(remainingObjects); remainingObjects.add(thisObject); return remainingObjects; } }
/* -------------------------------------------------------------*/ private void gravityEffect(ArrayList movingObjects) { // find effect of gravity on this objects from all other objects for (int i = 0; i < movingObjects.size(); i++) { // reset variables double add_vx = 0.0; double add_vy = 0.0; SpaceObject object = (SpaceObject) movingObjects.get(i); if (object.getObjCount() != objectNum && // ignore yourself (distance = 0!) !object.isBullet() && // ignore bullets !object.isSpaceShip()) // ignore spaceships { // find distance vector between planet and ball int x = pos_x - object.getXPos(); int y = pos_y - object.getYPos(); double distance = Math.sqrt(x * x + y * y); // find effect of planet on velocity double add_vec = (SpaceObject.G * k * object.getMass() * ballMass) / (distance * distance); add_vx = -(x / distance) * add_vec; add_vy = -(y / distance) * add_vec; // add objects speeds onto spaceship speed (clip speed if too large) x_speed += add_vx; y_speed += add_vy; } } }
/* -------------------------------------------------------------*/ private void objectCollision(ArrayList movingObjects) { for (int i = 0; i < movingObjects.size(); i++) { // make sure not testing if collided with yourself :P if (((SpaceObject) movingObjects.get(i)).getObjCount() != objectNum) { SpaceObject object = (SpaceObject) movingObjects.get(i); // find distance vector between two objects int x = pos_x - object.getXPos(); int y = pos_y - object.getYPos(); double distance = Math.sqrt(x * x + y * y); // has it collided with the object? if (distance < (radius + object.getRadius())) { // has it collided with a BULLET (or MISSILE)? if (object.isBullet()) { // do nothing } // is it another SPACESHIP? (INSTANT DEATH) else if (object.isSpaceShip()) { // do nothing } // collided with anything else (e.g PLANET): (INSTANT DEATH) else { collision.play(); kill(movingObjects); // object has died } } } } // end for loop }
@Override public void setPos(int x, int y) { super.setPos(x, y); img.setLocation(x, y); }