Archived
1
0

-Worked on loading of drones into the game at runtime

Signed-off-by: Tony Grosinger <github2@grosinger.net>
This commit is contained in:
Tony Grosinger 2011-04-21 17:05:18 -07:00
parent 8cc1c9a0dc
commit 0530ecec96
4 changed files with 105 additions and 14 deletions

View File

@ -1,40 +1,124 @@
package net.grosinger.nomads;
/*
* Various methods used when first setting up the game and loading everything into the world.
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Various methods used when first setting up the game and loading everything
* into the world.
*/
public class InitializeGame {
/**
* Search through ___ directory for any drone classes Load them, find the
* name, and return a linkedlist with all the drones
* name, and return a linked-list with all the drones
*
* Must set Nomads.firstDrone and Nomads.allTeams
*
* Each drone type will be a new DroneTeam
*
* @throws IOException
* @throws ClassNotFoundException
*/
public static void initializeDrones() {
public static void initializeDrones() throws ClassNotFoundException, IOException {
if (Nomads.DEBUGSTATUS)
System.out.println("Loading the drones into world...");
/*
* Steps
*/
// Discover a new Drone
// Obtain the class loader
ClassLoader classLoader = InitializeGame.class.getClassLoader();
// Create a DroneListItem with proper previous and next depending on its
// position
// Obtain a list of the classes that exist in the directory
String[] classesToLoad = generateList();
// Loop through the list of filenames
for (int i = 0; i < classesToLoad.length; i++) {
String filename = classesToLoad[i];
// Load the class
if (Nomads.DEBUGSTATUS)
System.out.println("Loading " + filename);
String className = filename.substring(0, filename.length() - 4);
File file = new File(System.getProperty("user.dir") + "/drones/" + filename);
URLClassLoader clazzLoader = URLClassLoader.newInstance(new URL[] { file.toURI().toURL() });
// System.class.getClassLoader()
JarFile jarFile = new JarFile(file);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry element = entries.nextElement();
if (element.getName().endsWith(".class")) {
try {
Class c = clazzLoader.loadClass(element.getName().replaceAll(".class", "").replaceAll("/", "."));
System.out.println("Class loaded sucessfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
// Create an object from it
// Create a DroneListItem with proper previous and next depending on
// its position
// Create a new DroneTeam which will automatically add it as the
// first and last drone.
}
// Create a new DroneTeam which will automatically add it as the first
// and last drone.
if (Nomads.DEBUGSTATUS)
System.out.println("Drone loading complete");
}
/*
* Buildings to create: Town Hall, Repair Shop, Upgrade Shop, Police Station
/**
* Creates a list of drones that are in the "drones" directory If directory
* does not exist it will create it
*
* @return <code>String[]</code> of all the file names
*/
private static String[] generateList() {
File dir = new File("drones");
// if the directory does not exist, create it
if (!dir.exists()) {
if (Nomads.DEBUGINITIALIZE)
System.out.println("creating directory: drones");
boolean result = dir.mkdir();
if (result && Nomads.DEBUGINITIALIZE) {
System.out.println("DIR created");
}
}
String[] children = dir.list();
if (children == null) {
// Either directory does not exist or is not a directory
return null;
} else {
return children;
}
}
/**
* Generates and places required buildings in the world
*
* @param awesomeWorld
* Main world the game uses
*/
public static void initializeBuildngs(World awesomeWorld) {
// Buildings to create: Town Hall, Repair Shop, Upgrade Shop, Police
// Station
if (Nomads.DEBUGSTATUS)
System.out.println("Generating and placing required buildings...");

View File

@ -1,7 +1,6 @@
//TODO - I think most of the comments before methods will have to be reformatted to fit in with how the API tool works. I will have to check
package net.grosinger.nomads;
import java.io.IOException;
import java.util.ArrayList;
public class Nomads {
@ -12,6 +11,7 @@ public class Nomads {
// Debugging Modes
public static final boolean DEBUGSTATUS = true;
public static final boolean DEBUGINITIALIZE = true;
public static final boolean DEBUGMOVES = true;
public static final boolean DEBUGDEATHS = true;
public static final boolean DEBUGCREATIONS = true;
@ -26,7 +26,14 @@ public class Nomads {
// Initialize and save all the drones
// This will update firstDrone and allTeams
InitializeGame.initializeDrones();
try {
InitializeGame.initializeDrones();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Generate and place all required buildings into world
if (DEBUGSTATUS)