Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Weapon {
- public class WeaponAttack{
- public int attackRangeAngle; //range in degree
- public int startAngle; //starting angle of the attack 0 top 180 down
- public int attackRange; //distance from the owner
- public int attackSpeed; //speed in degree for vertical attack or pixel for horizontal attack
- public Attack type; //vertical or horizontal
- public Vector2f offsets; //offset x and y from the center of the AABB
- public Rectangle rect; //rectangle of the weapon
- public int damage;
- public int startFrame; //animation frame at which the attack start to be updated
- public Effect effect; //effect of the weapon applied when touches (fire, poison...etc)
- public Vector3f movement; //movement applied to the owner by the attack x/y direction vector, z = animation frame
- /*WeaponAttack Contructor*/
- /*...*/
- }
- private boolean attack;
- //if the current attack touched a target this boolean is turn to true
- private boolean touched;
- private int dx,dy, angle;
- private ArrayList<WeaponAttack> attacks; //contains all the attacks
- public static enum Attack{VERTICAL, HORIZONTAL};
- private Attack currentType;
- private int index; //index of the current attack in the list
- private LivingEntity owner;
- public Weapon(LivingEntity owner){
- this.owner = owner;
- line = new Line(0, 0);
- attacks = new ArrayList<Weapon.WeaponAttack>();
- }
- /*method to add an attack*/
- public void addWeaponAttack(Attack type, int attackRange, int attackSpeed, int attackRangeAngle, int startAngle, int damage, int startFrame, Effect effect, Rectangle weaponRect, Vector2f offsets){
- attacks.add(new WeaponAttack(type, attackRange, attackSpeed, attackRangeAngle, startAngle, damage, startFrame, effect, weaponRect, offsets));
- }
- /*
- .
- .
- .
- */
- /*update method sync with animation frame*/
- public void update(double dt, int frameIndex){
- if(!attack) return;
- //update when the animation reach the start frame
- if(frameIndex >= attacks.get(index).startFrame)
- update(dt);
- //frame at which the movement is applied
- if(frameIndex == movement.z){
- //apply movement
- owner.setxExtV(owner.getFacing()?movement.x:-movement.x);
- owner.setyExtV(movement.y);
- movement.z = -1;
- }
- }
- /*update method*/
- public void update(double dt){
- //increment the attack angle (in the case of an horizontal attack it increment the distance)
- attackAngle += attackSpeed * dt;
- if(currentType == Attack.VERTICAL){
- //check max value with the range angle
- if(attackSpeed > 0){
- if(attackAngle >= attackRangeAngle){
- attackAngle = attackRangeAngle;
- attack = false;
- }
- }
- else{
- if(attackAngle <= -attackRangeAngle){
- attackAngle = -attackRangeAngle;
- attack = false;
- }
- }
- //get the current angle
- angle = (int)(owner.getFacing()?startAngle - attackAngle:-startAngle + attackAngle);
- //calculate distance x and y
- dx = (int) (Math.sin(Math.toRadians(angle)) * attackRange);
- dy = (int) (Math.cos(Math.toRadians(angle)) * attackRange);
- //assign the new values
- line.set(new float[]{owner.getRect().getCenterX() + (owner.getFacing()?offsets.x:-offsets.x),
- owner.getRect().getCenterY() + offsets.y},
- new float[]{ owner.getRect().getCenterX() + (owner.getFacing()?offsets.x:-offsets.x) + dx,
- owner.getRect().getCenterY() + offsets.y + dy});
- rect.setCenterX(owner.getRect().getCenterX() + dx + (owner.getFacing()?offsets.x:-offsets.x));
- rect.setCenterY(owner.getRect().getCenterY() + dy + offsets.y);
- }
- else if(currentType == Attack.HORIZONTAL){
- //check the max value with the attack range distance
- if(attackAngle >= attackRange){
- attackAngle = attackRange;
- attack = false;
- }
- //assign the new values
- line.set(new float[]{owner.getRect().getCenterX() + (owner.getFacing()?offsets.x:-offsets.x),
- owner.getRect().getCenterY() + offsets.y},
- new float[]{owner.getRect().getCenterX() + (owner.getFacing()?offsets.x:-offsets.x) + (owner.getFacing()?attackAngle:-attackAngle)),
- owner.getRect().getCenterY() + offsets.y});
- rect.setCenterX((float) (owner.getRect().getCenterX() + (owner.getFacing()?attackAngle:-attackAngle) + (owner.getFacing()?offsets.x:-offsets.x)));
- rect.setCenterY(owner.getRect().getCenterY() + offsets.y);
- }
- }
- //completion of the current attack
- public float getCompletion(){
- if(!attack) return 0;
- return (attackAngle / (attacks.get(index).type == Attack.VERTICAL?attackRangeAngle:attackRange)) * 100;
- }
- /*
- .
- .
- .
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement