summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-02-02 18:05:11 -0500
committerStarla Insigna <hatkirby@fourisland.com>2009-02-02 18:05:11 -0500
commitd3fce19421fa2db6aece07aa396a1a9c48c61fd9 (patch)
tree0a2af9b27120ad6c29a7231f1e2d5b9880d14a71 /src
parentf2738dc2e06f172a6db0822d4f94ccdcd6b52da8 (diff)
downloadfourpuzzle-d3fce19421fa2db6aece07aa396a1a9c48c61fd9.tar.gz
fourpuzzle-d3fce19421fa2db6aece07aa396a1a9c48c61fd9.tar.bz2
fourpuzzle-d3fce19421fa2db6aece07aa396a1a9c48c61fd9.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/com/fourisland/fourpuzzle/PuzzleApplication.java2
-rw-r--r--src/com/fourisland/fourpuzzle/gamestate/mapview/ChipSet.java129
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 @@
5package com.fourisland.fourpuzzle; 5package com.fourisland.fourpuzzle;
6 6
7import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState; 7import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState;
8import com.fourisland.fourpuzzle.gamestate.mapview.ChipSet;
8import java.awt.GraphicsEnvironment; 9import java.awt.GraphicsEnvironment;
9import java.awt.event.KeyAdapter; 10import java.awt.event.KeyAdapter;
10import java.awt.event.KeyEvent; 11import java.awt.event.KeyEvent;
@@ -104,6 +105,7 @@ public class PuzzleApplication extends Application {
104 public void run() { 105 public void run() {
105 try { 106 try {
106 Audio.init(); 107 Audio.init();
108 ChipSet.initalize();
107 Game.setGameState(new TitleScreenGameState()); 109 Game.setGameState(new TitleScreenGameState());
108 110
109 long iTickCount = System.currentTimeMillis(); 111 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 @@
5 5
6package com.fourisland.fourpuzzle.gamestate.mapview; 6package com.fourisland.fourpuzzle.gamestate.mapview;
7 7
8import com.fourisland.fourpuzzle.Layer;
8import com.fourisland.fourpuzzle.PuzzleApplication; 9import com.fourisland.fourpuzzle.PuzzleApplication;
9import com.fourisland.fourpuzzle.util.ObjectLoader; 10import com.fourisland.fourpuzzle.util.ObjectLoader;
10import com.fourisland.fourpuzzle.util.ResourceNotFoundException;
11import java.awt.image.BufferedImage; 11import java.awt.image.BufferedImage;
12import java.io.File;
13import java.io.IOException;
14import java.net.URL;
12import java.util.HashMap; 15import java.util.HashMap;
13import java.util.logging.Level; 16import java.util.logging.Level;
14import java.util.logging.Logger; 17import java.util.logging.Logger;
18import javax.xml.parsers.ParserConfigurationException;
19import javax.xml.parsers.SAXParserFactory;
20import org.jdesktop.application.ResourceMap;
21import org.xml.sax.Attributes;
22import org.xml.sax.SAXException;
23import org.xml.sax.helpers.DefaultHandler;
15 24
16/** 25/**
17 * 26 *
18 * @author hatkirby 27 * @author hatkirby
19 */ 28 */
20public abstract class ChipSet { 29public class ChipSet {
21 30
22 public abstract void initalize(); 31 private ChipSet() {}
23 32
24 private BufferedImage chipSetImage; 33 private BufferedImage chipSetImage;
25 public BufferedImage getImage(int offset) 34 public BufferedImage getImage(int offset)
26 { 35 {
27 int sx = (offset % 8) * 16; 36 int sx = (offset % 30) * 16;
28 int sy = (offset / 8) * 16; 37 int sy = (offset / 30) * 16;
29 38
30 return chipSetImage.getSubimage(sx, sy, 16, 16); 39 return chipSetImage.getSubimage(sx, sy, 16, 16);
31 } 40 }
32 41
33 public static ChipSet getChipSet(String chipSet) 42 private HashMap<Integer,ChipSetData> chipSetData = new HashMap<Integer,ChipSetData>(); //162
43 public HashMap<Integer,ChipSetData> getChipSetData()
34 { 44 {
35 try { 45 return chipSetData;
36 Class chipSetClass = Class.forName(PuzzleApplication.INSTANCE.getGamePackage() + ".gamedata.chipset." + chipSet); 46 }
37 Object chipSetObject = chipSetClass.newInstance();
38 ChipSet temp = (ChipSet) chipSetObject;
39 temp.initalize();
40 temp.chipSetImage = ObjectLoader.getImage("ChipSet", chipSet);
41 47
42 return temp; 48 public static void initalize()
43 } catch (InstantiationException ex) { 49 {
44 Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex); 50 ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap();
45 } catch (IllegalAccessException ex) { 51 File folder = new File(new File(rm.getResourcesDir()).getParent() + "/gamedata/chipset/");
46 Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex); 52 URL path = rm.getClassLoader().getResource(folder.toString());
47 } catch (ClassNotFoundException ex) { 53 File[] files = new File(path.getPath().replace("%20", " ")).listFiles();
48 throw new ResourceNotFoundException("ChipSetData", chipSet); 54 for (File cs : files)
55 {
56 try {
57 SAXParserFactory.newInstance().newSAXParser().parse(cs, new ChipSetDefaultHandler());
58 } catch (SAXException ex) {
59 Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex);
60 } catch (IOException ex) {
61 Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex);
62 } catch (ParserConfigurationException ex) {
63 Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex);
64 }
49 } 65 }
66 }
67
68 public static void initalize(String name, int trans, HashMap<Integer, ChipSetData> lower)
69 {
70 ChipSet temp = new ChipSet();
71 temp.chipSetData = lower;
72 temp.chipSetImage = ObjectLoader.getImage("ChipSet", name);
50 73
51 return null; 74 chipSets.put(name, temp);
52 } 75 }
53 76
54 private HashMap<Integer,ChipSetData> chipSetData = new HashMap<Integer,ChipSetData>(); //162 77 private static HashMap<String, ChipSet> chipSets = new HashMap<String, ChipSet>();
55 public HashMap<Integer,ChipSetData> getChipSetData() 78 public static ChipSet getChipSet(String name)
56 { 79 {
57 return chipSetData; 80 return chipSets.get(name);
58 } 81 }
59 82
60} 83}
84
85class ChipSetDefaultHandler extends DefaultHandler {
86
87 public ChipSetDefaultHandler() {
88 }
89
90 private String name;
91 private int trans;
92
93 private int curTile;
94 private HashMap<Integer, ChipSetData> lower = new HashMap<Integer,ChipSetData>();
95
96 private String terrain;
97 private Layer layer;
98
99 @Override
100 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
101 {
102 if (qName.equals("tileset"))
103 {
104 name = attributes.getValue("name");
105 } else if (qName.equals("image"))
106 {
107 trans = Integer.decode("0x" + attributes.getValue("trans"));
108 } else if (qName.equals("tile"))
109 {
110 curTile = Integer.decode(attributes.getValue("id"));
111 lower.put(curTile, new ChipSetData("Grass", Layer.Below));
112 } else if (qName.equals("property"))
113 {
114 if (attributes.getValue("name").equals("terrain"))
115 {
116 terrain = attributes.getValue("value");
117 } else if (attributes.getValue("name").equals("layer"))
118 {
119 layer = Layer.valueOf(attributes.getValue("value"));
120 }
121 }
122 }
123
124 @Override
125 public void endElement(String uri, String localName, String qName) throws SAXException
126 {
127 if (qName.equals("tile"))
128 {
129 lower.put(curTile, new ChipSetData(terrain, layer));
130 }
131 }
132
133 @Override
134 public void endDocument() throws SAXException
135 {
136 ChipSet.initalize(name, trans, lower);
137 }
138
139} \ No newline at end of file