From d3fce19421fa2db6aece07aa396a1a9c48c61fd9 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Mon, 2 Feb 2009 18:05:11 -0500 Subject: Added tiled ChipSet support Replaced previous chipset management (manual via a Java class) with "tiled" ChipSets. However, this support will be removed once a proper map editor/chipset editor is created for FourPuzzle. --- .../fourisland/fourpuzzle/PuzzleApplication.java | 2 + .../fourpuzzle/gamestate/mapview/ChipSet.java | 129 +++++++++++++++++---- 2 files changed, 106 insertions(+), 25 deletions(-) diff --git a/src/com/fourisland/fourpuzzle/PuzzleApplication.java b/src/com/fourisland/fourpuzzle/PuzzleApplication.java index 0b0e0cb..1c1dde2 100644 --- a/src/com/fourisland/fourpuzzle/PuzzleApplication.java +++ b/src/com/fourisland/fourpuzzle/PuzzleApplication.java @@ -5,6 +5,7 @@ package com.fourisland.fourpuzzle; import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState; +import com.fourisland.fourpuzzle.gamestate.mapview.ChipSet; import java.awt.GraphicsEnvironment; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; @@ -104,6 +105,7 @@ public class PuzzleApplication extends Application { public void run() { try { Audio.init(); + ChipSet.initalize(); Game.setGameState(new TitleScreenGameState()); long iTickCount = System.currentTimeMillis(); diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/ChipSet.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/ChipSet.java index 7ebb4eb..4fac1c0 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/ChipSet.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/ChipSet.java @@ -5,56 +5,135 @@ package com.fourisland.fourpuzzle.gamestate.mapview; +import com.fourisland.fourpuzzle.Layer; import com.fourisland.fourpuzzle.PuzzleApplication; import com.fourisland.fourpuzzle.util.ObjectLoader; -import com.fourisland.fourpuzzle.util.ResourceNotFoundException; import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.net.URL; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParserFactory; +import org.jdesktop.application.ResourceMap; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; /** * * @author hatkirby */ -public abstract class ChipSet { +public class ChipSet { - public abstract void initalize(); + private ChipSet() {} private BufferedImage chipSetImage; public BufferedImage getImage(int offset) { - int sx = (offset % 8) * 16; - int sy = (offset / 8) * 16; + int sx = (offset % 30) * 16; + int sy = (offset / 30) * 16; return chipSetImage.getSubimage(sx, sy, 16, 16); } - - public static ChipSet getChipSet(String chipSet) + + private HashMap chipSetData = new HashMap(); //162 + public HashMap getChipSetData() { - try { - Class chipSetClass = Class.forName(PuzzleApplication.INSTANCE.getGamePackage() + ".gamedata.chipset." + chipSet); - Object chipSetObject = chipSetClass.newInstance(); - ChipSet temp = (ChipSet) chipSetObject; - temp.initalize(); - temp.chipSetImage = ObjectLoader.getImage("ChipSet", chipSet); + return chipSetData; + } - return temp; - } catch (InstantiationException ex) { - Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex); - } catch (IllegalAccessException ex) { - Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex); - } catch (ClassNotFoundException ex) { - throw new ResourceNotFoundException("ChipSetData", chipSet); + public static void initalize() + { + ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); + File folder = new File(new File(rm.getResourcesDir()).getParent() + "/gamedata/chipset/"); + URL path = rm.getClassLoader().getResource(folder.toString()); + File[] files = new File(path.getPath().replace("%20", " ")).listFiles(); + for (File cs : files) + { + try { + SAXParserFactory.newInstance().newSAXParser().parse(cs, new ChipSetDefaultHandler()); + } catch (SAXException ex) { + Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex); + } catch (IOException ex) { + Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex); + } catch (ParserConfigurationException ex) { + Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex); + } } + } + + public static void initalize(String name, int trans, HashMap lower) + { + ChipSet temp = new ChipSet(); + temp.chipSetData = lower; + temp.chipSetImage = ObjectLoader.getImage("ChipSet", name); - return null; + chipSets.put(name, temp); } - - private HashMap chipSetData = new HashMap(); //162 - public HashMap getChipSetData() + + private static HashMap chipSets = new HashMap(); + public static ChipSet getChipSet(String name) { - return chipSetData; + return chipSets.get(name); } } + +class ChipSetDefaultHandler extends DefaultHandler { + + public ChipSetDefaultHandler() { + } + + private String name; + private int trans; + + private int curTile; + private HashMap lower = new HashMap(); + + private String terrain; + private Layer layer; + + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException + { + if (qName.equals("tileset")) + { + name = attributes.getValue("name"); + } else if (qName.equals("image")) + { + trans = Integer.decode("0x" + attributes.getValue("trans")); + } else if (qName.equals("tile")) + { + curTile = Integer.decode(attributes.getValue("id")); + lower.put(curTile, new ChipSetData("Grass", Layer.Below)); + } else if (qName.equals("property")) + { + if (attributes.getValue("name").equals("terrain")) + { + terrain = attributes.getValue("value"); + } else if (attributes.getValue("name").equals("layer")) + { + layer = Layer.valueOf(attributes.getValue("value")); + } + } + } + + @Override + public void endElement(String uri, String localName, String qName) throws SAXException + { + if (qName.equals("tile")) + { + lower.put(curTile, new ChipSetData(terrain, layer)); + } + } + + @Override + public void endDocument() throws SAXException + { + ChipSet.initalize(name, trans, lower); + } + +} \ No newline at end of file -- cgit 1.4.1