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

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

import java.util.ArrayList;

import loader.data_projectile;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

public class weapon implements Cloneable
{
  public String           name, display_name;
  private int             width, height;
  // Firing
  private int             rpm;
  private int             delay_length, reload_length;
  private long            delay_start, delay_end, reload_start, reload_end;
  private boolean         automatic, triggerd, reloading;
  private int             clip_size, ammunition;
  // Sprite sheets
  private SpriteSheet     sheet;
  // Buy menu
  public Image            buy_menu_texture, buy_menu_texture_gray;
  public boolean          buy_menu_buyable;
  public int              buy_menu_index;
  public int              price;
  // Projectile
  private data_projectile projectile;

  public weapon( final String name, final ArrayList<String[]> data ) throws SlickException
  {
    this.name = name;

    delay_start = 0;
    delay_end = 0;
    reload_start = 0;
    reload_end = 0;

    automatic = false;
    triggerd = false;
    reloading = false;
    clip_size = -1;
    ammunition = clip_size;

    buy_menu_buyable = false;
    buy_menu_index = 0;
    price = 0;

    load( data );
  }

  @Override
  public weapon clone()
  {
    try {
      return (weapon) super.clone();
    }
    catch ( final Exception e ) {
      e.printStackTrace();
    }

    return null;
  }

  public void stop()
  {
    delay_start = 0;
    delay_end = 0;

    triggerd = false;
  }

  public boolean fire( final long time )
  {
    if ( ( !reloading ) && ( ammunition == 0 ) ) {
      start_reload( time );
    }
    else if ( ( reloading ) && ( reload_end <= time ) ) {
      ammunition = clip_size;
      reloading = false;
    }
    else if ( ( !reloading ) && ( delay_end <= time ) ) {
      delay_start = time;
      delay_end = delay_start + delay_length;
      if ( ammunition != -1 ) {
        ammunition--;

        if ( ammunition == 0 ) {
          start_reload( time );
        }
      }

      return true;
    }

    return false;
  }

  private void start_reload( final long time )
  {
    reload_start = time;
    reload_end = reload_start + reload_length;
    ammunition = 0;
    reloading = true;
  }

  public void start_automatic()
  {
    if ( automatic ) {
      triggerd = true;
    }
  }

  public void stop_automatic()
  {
    if ( automatic ) {
      triggerd = false;
    }
  }

  public int calculate_ms_per_frame()
  {
    if ( ( rpm == -1 ) || ( sheet == null ) ) {
      return 150;
    }

    return (int) ( ( 1000.0f ) / ( ( rpm / 60.0f ) * sheet.getHorizontalCount() * sheet.getVerticalCount() ) );
  }

  private void load( final ArrayList<String[]> data ) throws SlickException
  {
    String[] tmp;
    final ArrayList<String[]> projectile_data = new ArrayList<String[]>( 0 );
    for ( final String[] s : data ) {
      if ( s[ 0 ].equals( "display_name" ) ) {
        display_name = s[ 1 ];
      }
      else if ( s[ 0 ].equals( "buy_menu_buyable" ) ) {
        buy_menu_buyable = false;
        if ( s[ 1 ].equals( "true" ) ) {
          buy_menu_buyable = true;
        }
      }
      else if ( s[ 0 ].equals( "buy_menu_index" ) ) {
        buy_menu_index = Integer.parseInt( s[ 1 ] );
      }
      else if ( s[ 0 ].equals( "buy_menu_texture" ) ) {
        if ( !s[ 1 ].equals( "null" ) ) {
          buy_menu_texture = loader.textures.load_texture( "buymenu", s[ 1 ] );
          buy_menu_texture_gray = loader.textures.load_texture( "buymenu", s[ 1 ] + "gray" );
        }
      }
      else if ( s[ 0 ].equals( "rpm" ) ) {
        rpm = Integer.parseInt( s[ 1 ] );
        delay_length = Math.round( ( 60.0f / rpm ) * 1000.0f );
      }
      else if ( s[ 0 ].equals( "reload_time" ) ) {
        reload_length = Integer.parseInt( s[ 1 ] );
      }
      else if ( s[ 0 ].equals( "clip_size" ) ) {
        clip_size = Integer.parseInt( s[ 1 ] );
        ammunition = clip_size;
      }
      else if ( s[ 0 ].equals( "automatic" ) ) {
        if ( s[ 1 ].equals( "true" ) ) {
          automatic = true;
        }
      }
      else if ( s[ 0 ].equals( "sheet" ) ) {
        if ( !s[ 1 ].equals( "null" ) ) {
          tmp = s[ 1 ].split( "," );

          width = Integer.parseInt( tmp[ 1 ] );
          height = Integer.parseInt( tmp[ 2 ] );

          sheet = loader.sprites.load_sprite( "weapon", tmp[ 0 ], width, height );
        }
      }
      else if ( s[ 0 ].equals( "price" ) ) {
        price = Integer.parseInt( s[ 1 ] );
      }
      else if ( s[ 0 ].startsWith( "projectile_" ) ) {
        projectile_data.add( s );
      }

      projectile = new data_projectile( projectile_data );
    }
  }

  /*
   * Getters and Setters
   */
  public float get_ammo_fraction()
  {
    return (float) ammunition / (float) clip_size;
  }

  public float get_reload_fraction( final long time )
  {
    if ( time > 0 ) {
      if ( reload_end > time ) {
        return (float) ( reload_end - time ) / (float) reload_length;
      }
    }

    return 0;
  }

  public float get_round_delay_fraction( final long time )
  {
    if ( ( time > 0 ) && ( delay_start > 0 ) ) {
      if ( delay_end > time ) {
        return (float) ( delay_end - time ) / (float) delay_length;
      }
    }

    return 0;
  }

  public boolean is_reloading()
  {
    return reloading;
  }

  public boolean is_automatic()
  {
    return automatic;
  }

  public boolean is_triggerd()
  {
    return triggerd;
  }

  public boolean has_clips()
  {
    return ( ammunition != -1 );
  }

  public data_projectile projectile()
  {
    return projectile;
  }

  public SpriteSheet sheet()
  {
    return sheet;
  }
}
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.