Package com.palepail.TestGame.Enemies

Source Code of com.palepail.TestGame.Enemies.Enemy

package com.palepail.TestGame.Enemies;

import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.math.Vector2;
import com.palepail.TestGame.Model.MovableEntity;
import com.palepail.TestGame.Model.Ship;
import com.palepail.TestGame.Model.Items.Item;
import com.palepail.TestGame.Utilities.Audio;
import com.palepail.TestGame.Utilities.Configuration;
import com.palepail.TestGame.View.World;

public abstract class Enemy extends MovableEntity {

  protected World world;
  protected int health;
  protected float attack;
  protected float timeBombed;
  protected float timeDeathGrazed;
  protected int ticks;
  protected float delta = 0;
  protected int prevCounter;
  protected int attackCounter;

  protected int dropCount = 1;

  protected boolean bombed = false;
  protected int value = 1;

  float damage = 1;

  Texture texture;
  Animation animation;

  final int typesOfItems = 3;

  public Enemy(float SPEED, float rotation, float width, float height, Vector2 position) {
    super(SPEED, rotation, width, height, position);

  }

  public Enemy(float SPEED, float rotation, float width, float height, Vector2 position, String variation) {
    this(SPEED, rotation, width, height, position);
    this.variation = variation;

  }

  public void update(Ship ship, World world) {
    time += Gdx.graphics.getDeltaTime();
    delta += (Gdx.graphics.getDeltaTime() * Configuration.frameRate);
    while (delta >= 1) {
      delta--;
      ticks++;
      attackCounter++;
      cooldownCounter++;
      prevCounter = world.getCounter();
    }

    if (isBombed()) {
      if (time - timeBombed > ship.getBombDuration() / Configuration.frameRate) {
        setBombed(false);
      }
    }

    if (isDeathGrazed()) {
      if (time - timeBombed > ship.getBombDuration() / Configuration.frameRate) {
        setDeathGrazed(false);
      }
    }

    if (health < 0) {
      die(world);
      // world.getShip().getInfoBox().addScore(world.getLevel().getName(),
      // this.getValue());
      world.addToScoreQueue(this.getValue());
      addExplosions(world);
      Audio.explosion();
    }

    setPreviousPosition(getPosition());

    super.update(ship);

  }

  /**
   * @return the variation
   */
  public String getVariation() {
    return variation;
  }

  /**
   * @param variation
   *            the variation to set
   */
  public void setVariation(String variation) {
    this.variation = variation;
  }

  /**
   * @return the texture
   */
  public Texture getTexture() {
    return texture;
  }

  /**
   * @param texture
   *            the texture to set
   */
  public void setTexture(Texture texture) {
    this.texture = texture;
  }

  /**
   * @return the animation
   */
  public Animation getAnimation() {
    return animation;
  }

  /**
   * @param animation
   *            the animation to set
   */
  public void setAnimation(Animation animation) {
    this.animation = animation;
  }

  /**
   * @return the name
   */
  public String getSpriteName() {
    return spriteName;
  }

  /**
   * @param name
   *            the name to set
   */
  public void setSpriteName(String name) {
    this.spriteName = name;
  }

  /**
   * @return the timeBombed
   */
  public float getTimeBombed() {
    return timeBombed;
  }

  /**
   * @param timeBombed
   *            the timeBombed to set
   */
  public void setTimeBombed(float timeBombed) {
    this.timeBombed = timeBombed;
  }

  /**
   * @return the timeDeathGrazed
   */
  public float getTimeDeathGrazed() {
    return timeDeathGrazed;
  }

  /**
   * @param timeDeathGrazed
   *            the timeDeathGrazed to set
   */
  public void setTimeDeathGrazed(float timeDeathGrazed) {
    this.timeDeathGrazed = timeDeathGrazed;
  }

  /**
   * @return the value
   */
  public int getValue() {
    return value;
  }

  /**
   * @param value
   *            the value to set
   */
  public void setValue(int value) {
    this.value = value;
  }

  public float getDamage() {
    return damage;
  }

  public float getAttack() {
    return attack;
  }

  public void upAttack(float ad) {
    attack += ad;
  }

  public int getHealth() {
    return health;
  }

  public void setHealth(int health) {
    this.health = health;
  }

  public void damage(float ad) {
    health -= ad;
  }

  /**
   * @return the bombed
   */
  public boolean isBombed() {
    return bombed;
  }

  /**
   * @param bombed
   *            the bombed to set
   */
  public void setBombed(boolean bombed) {
    this.bombed = bombed;
  }

  public Item getDrop() {

    Random rand = new Random();
    // rolls 1-100000
    // can have drops as low as .001%
    int roll = rand.nextInt(99999) + 1;

    if (isBetween(roll, 1, 1000)) {
      return Item.getBomb(this);
    } else if (isBetween(roll, 1001, 30000)) {
      return Item.getPower(this, .1f);
    } else if (isBetween(roll, 30001, 80000)) {
      return Item.getPoints(this, 20);
    } else {
      return null;
    }

  }

  private static boolean isBetween(int x, int lower, int upper) {
    return lower <= x && x <= upper;
  }

  public float getOrginX(float width) {

    return (width / 2);
  }

  public float getOrginY(float height) {
    return (height / 2);
  }

  protected void die(World world) {
    int dropNum = this.getNumberOfDrops();
    for (int x = 0; x < dropNum; x++) {
      Item item = this.getDrop();

      if (item != null) {
        if (dropCount > 1) {
          Random random = new Random();
          float angle = 20 - random.nextInt(40);

          item.setVelocity(new Vector2(new Vector2(angle * Configuration.gameScale,
              50 * Configuration.gameScale).sub(getPosition()).nor()));
        }
        world.putItem(item);
      }
    }

    this.setActive(false);

  }

  protected int getNumberOfDrops() {
    // TODO Auto-generated method stub
    return dropCount;
  }

}
TOP

Related Classes of com.palepail.TestGame.Enemies.Enemy

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.