first commit
This commit is contained in:
@@ -0,0 +1,218 @@
|
||||
package edu.gatech.cs2335.lemmings.graphics;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Class TileSetManager: This guy will allow us to manage tilesets in
|
||||
* such a fashion that every tileset is only loaded once.
|
||||
*
|
||||
* <PRE>
|
||||
* Revision History:
|
||||
* v1.0 (Mar. 12, 2004) - Created the TileSetManager class
|
||||
* </PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:gtg308i@mail.gatech.edu">Vladimir Urazov</A>
|
||||
* @version Version 1.0, Mar. 12, 2004
|
||||
*/
|
||||
public class TileSetManager {
|
||||
|
||||
/**
|
||||
* Show debug output?
|
||||
*/
|
||||
private static final boolean VERBOSE = false;
|
||||
|
||||
/**
|
||||
* The name of the file, where the mappings between the resource
|
||||
* names and their paths reside.
|
||||
*/
|
||||
public static final String RESOURCE_MAP_FILE = "resources.txt";
|
||||
|
||||
/**
|
||||
* Singleton implementation.
|
||||
*/
|
||||
private static TileSetManager instance;
|
||||
|
||||
/**
|
||||
* This map will contain the tilesets currently loaded.
|
||||
*/
|
||||
private Map tileSets;
|
||||
|
||||
/**
|
||||
* This contains the mappings between the paths and the ids of the
|
||||
* resources.
|
||||
*/
|
||||
private Map paths;
|
||||
|
||||
/**
|
||||
* Creates a new <code>TileSetManager</code> instance. Since this
|
||||
* class is a songleton, there is no need for the constructor to be
|
||||
* public.
|
||||
*/
|
||||
private TileSetManager() {
|
||||
tileSets = new HashMap();
|
||||
paths = new HashMap();
|
||||
|
||||
loadPaths();
|
||||
}
|
||||
|
||||
/**
|
||||
* Singleton Implementation.
|
||||
*
|
||||
* @return a <code>TileSetManager</code> value
|
||||
*/
|
||||
public static synchronized TileSetManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new TileSetManager();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will load the mapping between the paths and ids of
|
||||
* the resources.
|
||||
*/
|
||||
private void loadPaths() {
|
||||
FileInputStream fis = null;
|
||||
Properties props = null;
|
||||
|
||||
paths.clear();
|
||||
|
||||
//Open up the resource properties file:
|
||||
try {
|
||||
fis = new FileInputStream(RESOURCE_MAP_FILE);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println("TileSetManager: Could not open settings file...");
|
||||
System.err.println("\tIOException: " + ioe.getMessage());
|
||||
System.err.flush();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
props = new Properties();
|
||||
props.load(fis);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println("TileSetManager: Could not "
|
||||
+ "read the properties file...");
|
||||
System.err.println("\tIOException: " + ioe.getMessage());
|
||||
System.err.flush();
|
||||
return;
|
||||
}
|
||||
|
||||
if (VERBOSE) {
|
||||
System.out.println("TileSetManager: Loaded the following properties");
|
||||
System.out.println(props);
|
||||
System.out.flush();
|
||||
}
|
||||
|
||||
paths.putAll(props);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the resource if needed.
|
||||
*
|
||||
* @param name a <code>String</code> value
|
||||
* @param path a <code>String</code> value
|
||||
* @return a <code>boolean</code> value
|
||||
*/
|
||||
public boolean loadResource(String name, String path) {
|
||||
String newName = name;
|
||||
String p = path;
|
||||
if (newName == null) {
|
||||
|
||||
//Check if the path is valid:
|
||||
if (p == null) { return false; }
|
||||
|
||||
File f = new File(p);
|
||||
if (!f.exists()) { return false; }
|
||||
|
||||
newName = f.getName();
|
||||
|
||||
//Check if a resource with the same name already exists:
|
||||
if (this.paths.containsKey(newName)) { return false; }
|
||||
|
||||
//If we are still here, need to add the new entry to the paths:
|
||||
this.paths.put(newName, f.getPath());
|
||||
|
||||
p = (String) this.paths.get(newName);
|
||||
|
||||
} else {
|
||||
if (this.paths.containsKey(newName)) {
|
||||
if (p != null) {
|
||||
//Need to update the entry in the table:
|
||||
File f = new File(p);
|
||||
if (!f.exists()) { return false; }
|
||||
|
||||
this.paths.remove(newName);
|
||||
this.paths.put(newName, f.getPath());
|
||||
|
||||
//Need to also check if we have already loaded the item into
|
||||
//the table:
|
||||
if (this.tileSets.containsKey(newName)) {
|
||||
this.tileSets.remove(newName);
|
||||
}
|
||||
|
||||
p = (String) this.paths.get(newName);
|
||||
|
||||
} else {
|
||||
p = "images/" + (String) this.paths.get(newName);
|
||||
this.paths.put(name, p);
|
||||
|
||||
}
|
||||
} else {
|
||||
if (p == null) {
|
||||
return false;
|
||||
} else {
|
||||
File f = new File(p);
|
||||
if (!f.exists()) { return false; }
|
||||
|
||||
this.paths.put(newName, f.getPath());
|
||||
|
||||
p = (String) this.paths.get(newName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If we are still here, the paths and everything are good. Time to
|
||||
//actually load the resource:
|
||||
TileSet set = new ImprovedTileSet();
|
||||
if (!set.loadTileset(p)) { return false; }
|
||||
|
||||
this.tileSets.put(newName, set);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the tileset is loaded and false if it is not.
|
||||
*
|
||||
* @param name a <code>String</code> value
|
||||
* @return a <code>boolean</code> value
|
||||
*/
|
||||
public boolean tileSetLoaded(String name) {
|
||||
return this.tileSets.containsKey(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tileset with the specified name. Loads it if necessary.
|
||||
*
|
||||
* @param name a <code>String</code> value
|
||||
* @return a <code>TileSet</code> value
|
||||
*/
|
||||
public TileSet getTileSet(String name) {
|
||||
if (!tileSetLoaded(name)) {
|
||||
if (!loadResource(name, null)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return (TileSet) this.tileSets.get(name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user