⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 levelfactory.java

📁 非常好的java collapse游戏代码
💻 JAVA
字号:
/*
 * JCollapse - Java Collapse Game
 * Copyright (C) 2005 Erico Gon鏰lves Rimoli
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package sourceforge.net.projects.jcollapse.game.levels;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.StringTokenizer;

import sourceforge.net.projects.jcollapse.engine.block.Block;
import sourceforge.net.projects.jcollapse.game.blocks.BlockFactory;
import sourceforge.net.projects.jcollapse.game.components.DLHelper;

/**
 * Factory of levels
 * @author erico
  */
public class LevelFactory {
	private static final String LEVEL_NAME = "Name=";
	private static final String LEVEL_SIZE = "Size=";
	private static final String LEVEL_TIME_INTERVAL = "TimeInterval=";
	private static final String LEVEL_NOTES = "Notes=";
	private static final String LEVEL_BLOCK = "Block=";
	private static final String LEVEL_BACKGROUND_IMAGE = "BackgroundImage=";
	private static final String LEVEL_BACKGROUND_COLOR = "BackgroundColor=";
	
	/**
	 * Load level from specified resource.<br>
	 * Exemple:<br>
	 * Level level = lf.loadLevel( "/sourceforge/net/projects/collapse/game/levels/level1.cld", true ); <br>
	 * @param resourceLocation the resource location (URL)
	 * @param randomized if true, randomize the blocks in level
	 * @return the Level object.
	 * @throws LevelException fired when any error occurs loading level
	 */
	public static Level loadLevel( URL resourceLocation, boolean randomized ) throws LevelException {
		BufferedReader br;
		String line;
		String levelClass, levelName, blockParameter1, blockParameter2;
		int blockQuantity;
		Block block;
		Level level = new Level();

		try {
			br = new BufferedReader( new InputStreamReader( resourceLocation.openStream() ) );
			
			while( ( line = br.readLine() ) != null ) {
				//System.out.println( line );

				//#
				if( line.startsWith( "#" ) || line.length() == 0 )
					continue;
				
				//level name
				else if( line.startsWith( LEVEL_NAME ) )
					level.setName( line.substring( LEVEL_NAME.length() ) );
				
				//level size
				else if( line.startsWith( LEVEL_SIZE ) )
					level.setSize( Integer.parseInt( line.substring( LEVEL_SIZE.length(), line.indexOf( 'x' ) ) ),
								   Integer.parseInt( line.substring( line.indexOf( 'x' ) + 1 ) ) );

				//time interval
				else if( line.startsWith( LEVEL_TIME_INTERVAL ) )
					level.setRollInterval( Integer.parseInt( line.substring( LEVEL_TIME_INTERVAL.length() ) ) );
				
				//description
				else if( line.startsWith( LEVEL_NOTES ) )
					level.setNotes( line.substring( LEVEL_NOTES.length() ) );

				//background image
				else if( line.startsWith( LEVEL_BACKGROUND_IMAGE ) )
					level.setBackgroundImage( DLHelper.getInstance().getImage( line.substring( LEVEL_BACKGROUND_IMAGE.length() ) ) );
				
				//background color
				else if( line.startsWith( LEVEL_BACKGROUND_COLOR ) )
					level.setBackgroundColor( new Color( Integer.parseInt( line.substring( LEVEL_BACKGROUND_COLOR.length() + 0, LEVEL_BACKGROUND_COLOR.length() + 3 ) ),
														 Integer.parseInt( line.substring( LEVEL_BACKGROUND_COLOR.length() + 4, LEVEL_BACKGROUND_COLOR.length() + 7 ) ),
														 Integer.parseInt( line.substring( LEVEL_BACKGROUND_COLOR.length() + 8, LEVEL_BACKGROUND_COLOR.length() + 11 ) ) ) );
				//block
				else if( line.startsWith( LEVEL_BLOCK ) ) {
					StringTokenizer st = new StringTokenizer( line, ";" );
					
					if( st.countTokens() != 5 )
						continue;
					
					levelClass = st.nextToken().substring( LEVEL_BLOCK.length() );
					levelName = st.nextToken();
					blockQuantity = Integer.parseInt( st.nextToken() );
					blockParameter1 = st.nextToken();
					blockParameter2 = st.nextToken();
					
					block = BlockFactory.createBlock( 
													 levelClass,
													 levelName,
													 blockParameter1,
													 blockParameter2
								  	   				);
					
					for( int i = 0; i < blockQuantity; i++ )
						level.addBlock( block );
						
				}
			}//while
		} catch( IOException ex1 ) {
			throw new LevelException( "Error loading level: " + ex1.getMessage(), ex1 );
		}

		//randomize Blocks!
		if( randomized )
			level.randomize();
		
		return level;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -