Android Open Source - feup-lpoo-android-tower-defense Projectile






From Project

Back to project page feup-lpoo-android-tower-defense.

License

The source code is released under:

MIT License

If you think the Android project feup-lpoo-android-tower-defense 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 pt.up.fe.lpoo.towerdefense;
//from  ww w .java 2 s  . c  om
import java.util.ArrayList;

import pt.up.fe.lpoo.framework.Image;

/**
 * Abstract class to define Projectile objects
 * @author Joao Marinheiro
 * @author Luis Cleto
 * @see GameObj
 */
public abstract class Projectile extends GameObj {
  private static final double PROJECTILE_UPDATE_INTERVAL = 0.02;
  protected int centerX, centerY, movementSpeed, projectileDamage;
  protected int targetX, targetY, xSpeed, ySpeed;
  protected Enemy.DamageType damageType;
  /** Indicates if the projectile is to be removed or not */
  public boolean toRemove = false;
  private float elapsedSeconds;

  /**
   * Constructor for projectile objects.
   *  Determines the movement in each direction with every time slice for the game.
   * 
   * @param iniX initial x position
   * @param iniY initial y position
   * @param goalX destination x position
   * @param goalY destination y position
   * @param moveSpeed movement speed for the projectile (number of pixels it travels 50 times every second)
   * @param damage damage the projectile will inflict upon collision with an enemy
   */
  public Projectile(int iniX, int iniY, int goalX, int goalY, int moveSpeed, int damage){
    this.elapsedSeconds = 0;
    this.centerX = iniX;
    this.centerY = iniY;
    this.movementSpeed = moveSpeed;
    this.projectileDamage = damage;
    double xShift = goalX - iniX; double yShift = goalY-iniY;

    double moveAngle = Math.atan2(yShift, xShift);
    double cosMoveAngle = Math.cos(moveAngle);
    double sinMoveAngle = Math.sin(moveAngle);
    xSpeed = (int) (movementSpeed*cosMoveAngle);
    ySpeed = (int) (movementSpeed*sinMoveAngle);
  }

  /**
   * Abstract method to determine the sprite of the projectile. Deppends on the subclass.
   * 
   * @return returns the image to be drawn to represent the projectile
   */
  public abstract Image getCurrentImage();
  
  /**
   * @return returns all the status effects thay on the object
   */
  @Override
  public ArrayList<StatusEffect> getStatusEffects(){
    return null;
  }
  /**
   * @return returns the x position of the projectile
   */
  @Override
  public int getCenterX(){
    return centerX;
  }
  /**
   * @return returns the x position of the projectile
   */
  @Override
  public int getCenterY(){
    return centerY;
  }
  /**
   * @return returns the x position of the upper left corner of the projectile (for drawing)
   */
  public int getX(){
    return centerX-getCurrentImage().getWidth()/2;
  }
  /**
   * @return returns the y position of the upper left corner of the projectile (for drawing)
   */
  public int getY(){
    return centerY-getCurrentImage().getWidth()/2;
  }

  /**
   * Moves the projectile and check for collision with an enemy. In case of collision, the enemy is damaged
   *  and the projectile is set to be removed
   * @param deltaTime elapsed time since last update
   * @param enemies array of enemies to verify for collision
   */
  public void update(float deltaTime, ArrayList<Enemy> enemies){
    if(!toRemove){
      elapsedSeconds += (deltaTime/100.0f);
      while(elapsedSeconds > PROJECTILE_UPDATE_INTERVAL){
        elapsedSeconds-=PROJECTILE_UPDATE_INTERVAL;
        centerX += xSpeed;
        centerY += ySpeed;

        if(centerX < 0 || centerX > LevelScreen.MAP_WIDTH || centerY < LevelScreen.INFO_BAR_HEIGHT 
            || centerY > LevelScreen.INFO_BAR_HEIGHT+LevelScreen.MAP_HEIGHT){
          toRemove = true;
          this.xSpeed = 0;
          this.ySpeed = 0;
          return;
        }

        for(int i=0; i < enemies.size(); i++){
          Enemy curEnemy = enemies.get(i);
          if(collidedWith(curEnemy)){
            curEnemy.damage(projectileDamage, damageType);
            this.toRemove = true;
            this.xSpeed = 0;
            this.ySpeed = 0;
            this.centerX = -100;
            this.centerY = -100;
            break;
          }
        }
      }
    }
  }

  protected boolean collidedWith(Enemy curEnemy) {
    int enemyIniX = curEnemy.getX(); int enemyIniY = curEnemy.getY();
    int enemyFinalX = enemyIniX+curEnemy.getCurrentImage().getWidth();
    int enemyFinalY = enemyIniY+curEnemy.getCurrentImage().getHeight();

    if(centerX >= enemyIniX && centerX <= enemyFinalX && centerY >= enemyIniY && centerY <= enemyFinalY)
      return true;
    else
      return false;
  }
  
  /**
   * This indicates if the game object is part of the ground terrain or not (for visibility purposes)
   * 
   * @return always returns false for projectiles
   */
  @Override
  public boolean isGroundObject() {
    return false;
  }
}




Java Source Code List

pt.up.fe.lpoo.framework.Audio.java
pt.up.fe.lpoo.framework.FileIO.java
pt.up.fe.lpoo.framework.Game.java
pt.up.fe.lpoo.framework.Graphics.java
pt.up.fe.lpoo.framework.Image.java
pt.up.fe.lpoo.framework.Input.java
pt.up.fe.lpoo.framework.Music.java
pt.up.fe.lpoo.framework.Pool.java
pt.up.fe.lpoo.framework.Screen.java
pt.up.fe.lpoo.framework.Sound.java
pt.up.fe.lpoo.framework.implementation.AndroidAudio.java
pt.up.fe.lpoo.framework.implementation.AndroidFastRenderView.java
pt.up.fe.lpoo.framework.implementation.AndroidFileIO.java
pt.up.fe.lpoo.framework.implementation.AndroidGame.java
pt.up.fe.lpoo.framework.implementation.AndroidGraphics.java
pt.up.fe.lpoo.framework.implementation.AndroidImage.java
pt.up.fe.lpoo.framework.implementation.AndroidInput.java
pt.up.fe.lpoo.framework.implementation.AndroidMusic.java
pt.up.fe.lpoo.framework.implementation.AndroidSound.java
pt.up.fe.lpoo.framework.implementation.MultiTouchHandler.java
pt.up.fe.lpoo.framework.implementation.SingleTouchHandler.java
pt.up.fe.lpoo.framework.implementation.TouchHandler.java
pt.up.fe.lpoo.towerdefense.Animation.java
pt.up.fe.lpoo.towerdefense.Assets.java
pt.up.fe.lpoo.towerdefense.BarricadeDefense.java
pt.up.fe.lpoo.towerdefense.BoulderProjectile.java
pt.up.fe.lpoo.towerdefense.BoulderTower.java
pt.up.fe.lpoo.towerdefense.BuildingOption.java
pt.up.fe.lpoo.towerdefense.DefenseObject.java
pt.up.fe.lpoo.towerdefense.DescriptionObject.java
pt.up.fe.lpoo.towerdefense.Enemy.java
pt.up.fe.lpoo.towerdefense.FreezeTower.java
pt.up.fe.lpoo.towerdefense.GameElements.java
pt.up.fe.lpoo.towerdefense.GameObj.java
pt.up.fe.lpoo.towerdefense.Golem.java
pt.up.fe.lpoo.towerdefense.InstructionsScreen.java
pt.up.fe.lpoo.towerdefense.LevelScreen.java
pt.up.fe.lpoo.towerdefense.LevelSelectionScreen.java
pt.up.fe.lpoo.towerdefense.LoadingScreen.java
pt.up.fe.lpoo.towerdefense.MainMenuScreen.java
pt.up.fe.lpoo.towerdefense.Messages.java
pt.up.fe.lpoo.towerdefense.MusicPlayer.java
pt.up.fe.lpoo.towerdefense.PathCluster.java
pt.up.fe.lpoo.towerdefense.PoisonTower.java
pt.up.fe.lpoo.towerdefense.Projectile.java
pt.up.fe.lpoo.towerdefense.Skull.java
pt.up.fe.lpoo.towerdefense.Slime.java
pt.up.fe.lpoo.towerdefense.SplashLoadingScreen.java
pt.up.fe.lpoo.towerdefense.StatusEffect.java
pt.up.fe.lpoo.towerdefense.TarPoolDefense.java
pt.up.fe.lpoo.towerdefense.TextParser.java
pt.up.fe.lpoo.towerdefense.Tile.java
pt.up.fe.lpoo.towerdefense.TowerDefenseGame.java
pt.up.fe.lpoo.towerdefense.TurretProjectile.java
pt.up.fe.lpoo.towerdefense.TurretTower.java
pt.up.fe.lpoo.towerdefense.Wave.java