unit.java :  » Java-2D » polhemthegame » elements » Java Open Source

Java Open Source » Java 2D » polhemthegame 
polhemthegame » elements » unit.java
package elements;

import org.newdawn.slick.SlickException;

import ui.infobar.info_bar;
import basics.object;

public class unit extends object
{
  protected int      hitpoints     = 0;
  protected int      hitpoints_max = 0;
  protected info_bar info_bar;

  protected enum STATES {
    WALKING, STANDING, DYING, DEAD;
  }

  protected STATES state;

  public unit()
  {
    super();

    state = STATES.STANDING;
  }

  protected void init_info_bar() throws SlickException
  {
    info_bar = new info_bar( this );
  }

  protected void draw()
  {
    if ( info_bar != null ) {
      info_bar.draw();
    }
  }

  protected void update( final long time )
  {
    if ( info_bar != null ) {
      info_bar.update( time );
    }
  }

  public void kill()
  {
    hitpoints = 0;
    info_bar = null; // Remove the infobar
    state = STATES.DYING;
  }

  public void damage( final int damage )
  {
    hitpoints -= damage;

    if ( info_bar != null ) {
      info_bar.update_hp();
    }

    if ( hitpoints <= 0 ) {
      kill();
    }
  }

  /*
   * Getters and Setters
   */
  public int get_hp()
  {
    return hitpoints;
  }

  public float get_hp_fraction()
  {
    if ( hitpoints_max != 0 ) {
      return (float) hitpoints / (float) hitpoints_max;
    }

    return 1.0f;
  }

  public void set_full_hitpoints( final int value )
  {
    hitpoints = value;
    hitpoints_max = value;
  }

  public void set_hitpoints( final int value )
  {
    hitpoints = value;
  }

  public boolean is_standing()
  {
    return state == STATES.STANDING;
  }

  public boolean is_walking()
  {
    return state == STATES.WALKING;
  }

  public boolean is_dead()
  {
    return state == STATES.DEAD;
  }

  public boolean is_dying()
  {
    return state == STATES.DYING;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.