Advertisement
Hanksha

Weapon Class

Apr 22nd, 2015
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. public class Weapon {
  2.    
  3.     public class WeaponAttack{
  4.         public int attackRangeAngle; //range in degree
  5.         public int startAngle; //starting angle of the attack 0 top 180 down
  6.         public int attackRange; //distance from the owner
  7.         public int attackSpeed; //speed in degree for vertical attack or pixel for horizontal attack
  8.         public Attack type; //vertical or horizontal
  9.         public Vector2f offsets; //offset x and y from the center of the AABB
  10.         public Rectangle rect; //rectangle of the weapon
  11.         public int damage;
  12.         public int startFrame; //animation frame at which the attack start to be updated
  13.         public Effect effect; //effect of the weapon applied when touches (fire, poison...etc)
  14.         public Vector3f movement; //movement applied to the owner by the attack x/y direction vector, z = animation frame
  15.        
  16.         /*WeaponAttack Contructor*/
  17.         /*...*/
  18.     }
  19.    
  20.     private boolean attack;
  21.     //if the current attack touched a target this boolean is turn to true
  22.     private boolean touched;
  23.     private int dx,dy, angle;
  24.     private ArrayList<WeaponAttack> attacks; //contains all the attacks
  25.     public static enum Attack{VERTICAL, HORIZONTAL};
  26.     private Attack currentType;
  27.     private int index; //index of the current attack in the list
  28.     private LivingEntity owner;
  29.    
  30.     public Weapon(LivingEntity owner){
  31.         this.owner = owner;
  32.         line = new Line(0, 0);
  33.         attacks = new ArrayList<Weapon.WeaponAttack>();
  34.     }
  35.    
  36.     /*method to add an attack*/
  37.     public void addWeaponAttack(Attack type, int attackRange, int attackSpeed, int attackRangeAngle, int startAngle, int damage, int startFrame, Effect effect, Rectangle weaponRect, Vector2f offsets){
  38.         attacks.add(new WeaponAttack(type, attackRange, attackSpeed, attackRangeAngle, startAngle, damage, startFrame, effect, weaponRect, offsets));
  39.     }
  40.  
  41.     /*
  42.     .
  43.     .
  44.     .
  45.     */
  46.    
  47.     /*update method sync with animation frame*/
  48.     public void update(double dt, int frameIndex){
  49.         if(!attack) return;
  50.        
  51.         //update when the animation reach the start frame
  52.         if(frameIndex >= attacks.get(index).startFrame)
  53.             update(dt);
  54.        
  55.         //frame at which the movement is applied
  56.         if(frameIndex == movement.z){
  57.             //apply movement
  58.             owner.setxExtV(owner.getFacing()?movement.x:-movement.x);
  59.             owner.setyExtV(movement.y);
  60.             movement.z = -1;
  61.         }
  62.     }
  63.    
  64.     /*update method*/
  65.     public void update(double dt){
  66.         //increment the attack angle (in the case of an horizontal attack it increment the distance)
  67.         attackAngle += attackSpeed * dt;
  68.        
  69.         if(currentType == Attack.VERTICAL){
  70.             //check max value with the range angle
  71.             if(attackSpeed > 0){
  72.                 if(attackAngle >= attackRangeAngle){
  73.                     attackAngle = attackRangeAngle;
  74.                     attack = false;
  75.                 }
  76.             }
  77.             else{
  78.                 if(attackAngle <= -attackRangeAngle){
  79.                     attackAngle = -attackRangeAngle;
  80.                     attack = false;
  81.                 }
  82.             }
  83.             //get the current angle
  84.             angle = (int)(owner.getFacing()?startAngle - attackAngle:-startAngle + attackAngle);
  85.            
  86.             //calculate distance x and y
  87.             dx = (int) (Math.sin(Math.toRadians(angle)) * attackRange);
  88.             dy = (int) (Math.cos(Math.toRadians(angle)) * attackRange);
  89.            
  90.             //assign the new values
  91.             line.set(new float[]{owner.getRect().getCenterX() + (owner.getFacing()?offsets.x:-offsets.x),
  92.                          owner.getRect().getCenterY() + offsets.y},
  93.                 new float[]{ owner.getRect().getCenterX() + (owner.getFacing()?offsets.x:-offsets.x) + dx,
  94.                          owner.getRect().getCenterY() + offsets.y + dy});
  95.            
  96.             rect.setCenterX(owner.getRect().getCenterX() + dx + (owner.getFacing()?offsets.x:-offsets.x));
  97.             rect.setCenterY(owner.getRect().getCenterY() + dy + offsets.y);
  98.         }
  99.         else if(currentType == Attack.HORIZONTAL){
  100.             //check the max value with the attack range distance
  101.             if(attackAngle >= attackRange){
  102.                 attackAngle = attackRange;
  103.                 attack = false;
  104.             }
  105.            
  106.             //assign the new values
  107.             line.set(new float[]{owner.getRect().getCenterX() + (owner.getFacing()?offsets.x:-offsets.x),
  108.                          owner.getRect().getCenterY() + offsets.y},
  109.                  new float[]{owner.getRect().getCenterX() + (owner.getFacing()?offsets.x:-offsets.x) + (owner.getFacing()?attackAngle:-attackAngle)),
  110.                          owner.getRect().getCenterY() + offsets.y});
  111.                
  112.             rect.setCenterX((float) (owner.getRect().getCenterX() + (owner.getFacing()?attackAngle:-attackAngle) +          (owner.getFacing()?offsets.x:-offsets.x)));
  113.             rect.setCenterY(owner.getRect().getCenterY() + offsets.y);
  114.         }
  115.     }
  116.     //completion of the current attack
  117.     public float getCompletion(){
  118.         if(!attack) return 0;
  119.        
  120.         return (attackAngle / (attacks.get(index).type == Attack.VERTICAL?attackRangeAngle:attackRange)) * 100;
  121.     }
  122.     /*
  123.     .
  124.     .
  125.     .
  126.     */
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement