level.java :  » Java-2D » polhemthegame » levels » Java Open Source

Java Open Source » Java 2D » polhemthegame 
polhemthegame » levels » level.java
package levels;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import loader.file;

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

import ui.hud;
import elements.boss;
import elements.creep;

public class level
{
  public int              width, height;
  public int              y, x;
  public String           name;
  public constraints      constraints;
  public ArrayList<creep> creeps;
  public boss             boss;
  public Image            background, text_start, text_boss, text_finished, text_game_over;

  level( final String name, final int x, final int y ) throws FileNotFoundException, IOException, SlickException
  {
    this.name = name;

    creeps = new ArrayList<creep>( 0 );
    boss = null;

    width = 1024;
    height = 618;

    this.x = x;
    this.y = y;

    load( file.relative_path( new String[] { "data", "levels" }, name + ".lvl" ) );
  }

  public void draw()
  {
    background.draw( x, y );
  }

  public boolean has_boss()
  {
    return boss != null;
  }

  private void load( final String file ) throws FileNotFoundException, IOException, SlickException
  {
    final ArrayList<String> t_creeps = new ArrayList<String>( 0 );
    String t_boss = "";

    String group = "";
    String line = "";
    String[] s, tmp;
    final file f = new file( file );
    try {
      while ( ( line = f.read_line() ) != null ) {
        if ( line.startsWith( "[" ) ) {
          group = line;
        }
        else if ( !line.isEmpty() && !line.startsWith( "#" ) ) {
          s = line.toLowerCase().split( "=" );
          if ( group.equals( "[data]" ) ) {
            if ( s[ 0 ].equals( "name" ) ) {
              // display_name = s[1];
            }
            else if ( s[ 0 ].equals( "constraints" ) ) {
              tmp = s[ 1 ].split( "," );
              constraints = new constraints( Integer.parseInt( tmp[ 0 ] ), Integer.parseInt( tmp[ 1 ] ) + hud.height,
                                             Integer.parseInt( tmp[ 2 ] ), Integer.parseInt( tmp[ 3 ] ) );
            }
            else if ( s[ 0 ].equals( "background" ) ) {
              background = loader.textures.load_texture( "level", s[ 1 ] );
            }
            else if ( s[ 0 ].equals( "boss" ) ) {
              t_boss = s[ 1 ];
            }
            else if ( s[ 0 ].equals( "text_start" ) ) {
              if ( !s[ 1 ].equals( "null" ) ) {
                text_start = loader.textures.load_texture( "text", s[ 1 ] );
              }
            }
            else if ( s[ 0 ].equals( "text_boss" ) ) {
              if ( !s[ 1 ].equals( "null" ) ) {
                text_boss = loader.textures.load_texture( "text", s[ 1 ] );
              }
            }
            else if ( s[ 0 ].equals( "text_finished" ) ) {
              if ( !s[ 1 ].equals( "null" ) ) {
                text_finished = loader.textures.load_texture( "text", s[ 1 ] );
              }
            }
            else if ( s[ 0 ].equals( "text_game_over" ) ) {
              if ( !s[ 1 ].equals( "null" ) ) {
                text_game_over = loader.textures.load_texture( "text", s[ 1 ] );
              }
            }
          }
          else if ( group.equals( "[creeps]" ) ) {
            t_creeps.add( line );
          }
        }
      }

      // Load creeps and boss here to ensure it's done after reading level constraints
      if ( !t_creeps.isEmpty() ) {
        for ( final String str : t_creeps ) {
          tmp = str.split( "," );
          creeps.add( new creep( tmp[ 0 ], Integer.parseInt( tmp[ 1 ] ), constraints ) );
        }
      }
      if ( !t_boss.isEmpty() ) {
        boss = new boss( t_boss, constraints );
      }
    }
    finally {
      f.close();
    }
  }
}
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.