Android Open Source - diploma-assignment Ballista Arrow






From Project

Back to project page diploma-assignment.

License

The source code is released under:

MIT License

If you think the Android project diploma-assignment 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.me.battlescreen;
// w ww.  j  a v  a 2 s.  co m
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.TimeUtils;

public class BallistaArrow extends Actor{
  
  private BattleScreen screen;
  
  private float velocity,velocityX,velocityY;
  private float startX,startY;
  private float currX,currY;
  private float angle;
  private float time;
  long startTime;
  private Texture texture;
  private int width,height;
  private int attack;
  
  public BallistaArrow(BattleScreen s,int v,float x, float y,float a,Texture t){
    screen=s;
    texture=t;
    height=(int)(screen.getHeight()*Constants.BALLISTA_ARROW_HEIGHT);
    width=(int) (height*texture.getWidth()/texture.getHeight());
    this.setSize(width, height);
    velocity=v;
    currX=startX=x;
    currY=startY=y;
    angle=a;
    time=0;
    velocityX=(float) (Math.cos(angle*Math.PI/180)*velocity);
    velocityY=(float) (Math.sin(angle*Math.PI/180)*velocity);
    attack=Constants.BALLISTA_DAMAGE;
    startTime=TimeUtils.millis();
  }
  
  @Override
  public void draw(Batch batch,float alpha) {
    // TODO Auto-generated method stub
    calculateCoordinates();
    if(currY<screen.getHeight()-screen.getBg().getHeight()){
      screen.removeArrow(this);
      return;
    }
    batch.draw(texture, currX-screen.getBg().getCoordsX(), currY, 
        this.getWidth()/2,0, this.getWidth(), this.getHeight(), 1, 1,
        angle,0, 0,texture.getWidth(), texture.getHeight(), false, false);
  }
  
  private void calculateCoordinates() {
    // TODO Auto-generated method stub
    time=((float)(TimeUtils.millis()-startTime))/1000;
    float velY=velocityY+Constants.GRAVITY*time;
    float currDisplacementY=velocityY*time+Constants.GRAVITY*time*time/2;
    float currDisplacementX=velocityX*time;
    angle=(float) (Math.atan(velY/velocityX)*180/Math.PI);
    currX=startX+currDisplacementX;
    currY=startY+currDisplacementY;
  }

  public void arrowHit() {
    if(angle<0){
      Vector2 v=new Vector2(this.getWidth(),0);
      v.rotate(angle);
      float x1=currX;
      float y1=currY+this.getHeight()/2;
      float x2=x1+v.x;
      float y2=y1+v.y;
      screen.arrowHit(x1,y1,x2,y2,attack,true);
    }
  }

  
}




Java Source Code List

com.me.battlescreen.BallistaArrow.java
com.me.battlescreen.Ballista.java
com.me.battlescreen.BattlePlayer.java
com.me.battlescreen.BattleScreen.java
com.me.battlescreen.Bot.java
com.me.battlescreen.Castle.java
com.me.battlescreen.CleanBackground.java
com.me.battlescreen.ComputerPlayer.java
com.me.battlescreen.Constants.java
com.me.battlescreen.HumanPlayer.java
com.me.battlescreen.Monster.java
com.me.battlescreen.MyButton.java
com.me.battlescreen.MyLabel.java
com.me.battlescreen.OpponentMonster.java
com.me.battlescreen.PlayerMonster.java
com.me.battlescreen.TimeLabel.java
com.me.battlescreen.UnitButton.java
com.me.battlescreen.getTextures.java
com.me.battlescreen.iAttacking.java
com.me.battlescreen.iHittable.java
com.me.battlescreen.iUpdateable.java
com.me.main.MyGame.java
com.me.main.Player.java
com.me.mygdxgame.MainActivity.java
com.me.openingscreen.AbstractMenu.java
com.me.openingscreen.Constants.java
com.me.openingscreen.FirstMenu.java
com.me.openingscreen.FloatingBackground.java
com.me.openingscreen.OpeningScreen.java
com.me.openingscreen.PlayerMenu.java
com.me.openingscreen.ResetMenu.java
com.me.openingscreen.StatisticsMenu.java
com.me.openingscreen.UpgradeButton.java
com.me.openingscreen.UpgradeMenu.java