diff options
Diffstat (limited to 'src/com/fourisland')
-rw-r--r-- | src/com/fourisland/fourpuzzle/PuzzleApplication.java | 2 | ||||
-rw-r--r-- | src/com/fourisland/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 @@ | |||
5 | package com.fourisland.fourpuzzle; | 5 | package com.fourisland.fourpuzzle; |
6 | 6 | ||
7 | import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState; | 7 | import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState; |
8 | import com.fourisland.fourpuzzle.gamestate.mapview.ChipSet; | ||
8 | import java.awt.GraphicsEnvironment; | 9 | import java.awt.GraphicsEnvironment; |
9 | import java.awt.event.KeyAdapter; | 10 | import java.awt.event.KeyAdapter; |
10 | import java.awt.event.KeyEvent; | 11 | import 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 | ||
6 | package com.fourisland.fourpuzzle.gamestate.mapview; | 6 | package com.fourisland.fourpuzzle.gamestate.mapview; |
7 | 7 | ||
8 | import com.fourisland.fourpuzzle.Layer; | ||
8 | import com.fourisland.fourpuzzle.PuzzleApplication; | 9 | import com.fourisland.fourpuzzle.PuzzleApplication; |
9 | import com.fourisland.fourpuzzle.util.ObjectLoader; | 10 | import com.fourisland.fourpuzzle.util.ObjectLoader; |
10 | import com.fourisland.fourpuzzle.util.ResourceNotFoundException; | ||
11 | import java.awt.image.BufferedImage; | 11 | import java.awt.image.BufferedImage; |
12 | import java.io.File; | ||
13 | import java.io.IOException; | ||
14 | import java.net.URL; | ||
12 | import java.util.HashMap; | 15 | import java.util.HashMap; |
13 | import java.util.logging.Level; | 16 | import java.util.logging.Level; |
14 | import java.util.logging.Logger; | 17 | import java.util.logging.Logger; |
18 | import javax.xml.parsers.ParserConfigurationException; | ||
19 | import javax.xml.parsers.SAXParserFactory; | ||
20 | import org.jdesktop.application.ResourceMap; | ||
21 | import org.xml.sax.Attributes; | ||
22 | import org.xml.sax.SAXException; | ||
23 | import org.xml.sax.helpers.DefaultHandler; | ||
15 | 24 | ||
16 | /** | 25 | /** |
17 | * | 26 | * |
18 | * @author hatkirby | 27 | * @author hatkirby |
19 | */ | 28 | */ |
20 | public abstract class ChipSet { | 29 | public 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 | |||
85 | class 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 | ||