summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/ChipSet.java
blob: ddf7c940ea7dac2b1be30e559fedb4d2be5989e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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.io.InputStream;
import java.net.URISyntaxException;
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 class ChipSet {
    
    private ChipSet() {}

    private BufferedImage chipSetImage;
    public BufferedImage getImage(int offset)
    {
        int sx = (offset % 30) * 16;
        int sy = (offset / 30) * 16;
        
        return chipSetImage.getSubimage(sx, sy, 16, 16);
    }
    
    private HashMap<Integer,ChipSetData> chipSetData = new HashMap<Integer,ChipSetData>(); //162
    public HashMap<Integer,ChipSetData> getChipSetData()
    {
        return chipSetData;
    }

    public static void initalize(String name)
    {
        ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap();
        InputStream cs = null;
        
        if (rm.getClassLoader().getResource(rm.getResourcesDir() + "chipset/" + name + ".tsx") == null)
        {
            if (rm.getClassLoader().getResource("com/fourisland/fourpuzzle/resources/chipset/" + name + ".tsx") == null)
            {
                throw new ResourceNotFoundException("ChipSet", name);
            } else {
                cs = rm.getClassLoader().getResourceAsStream("com/fourisland/fourpuzzle/resources/chipset/" + name + ".tsx");
            }
        } else {
            cs = rm.getClassLoader().getResourceAsStream(rm.getResourcesDir() + "chipset/" + name + ".tsx");
        }
        
        try {
            SAXParserFactory.newInstance().newSAXParser().parse(cs, new ChipSetDefaultHandler());
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ChipSet.class.getName()).log(Level.SEVERE, null, ex);
        } 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);
        }
    }
    
    public static void initalize(String name, int trans, HashMap<Integer, ChipSetData> lower)
    {
        ChipSet temp = new ChipSet();
        temp.chipSetData = lower;
        temp.chipSetImage = ObjectLoader.getImage("ChipSet", name, trans);
        
        chipSets.put(name, temp);
    }
    
    private static HashMap<String, ChipSet> chipSets = new HashMap<String, ChipSet>();
    public static ChipSet getChipSet(String name)
    {
        if (!chipSets.containsKey(name))
        {
            initalize(name);
        }
        
        return chipSets.get(name);
    }

}

class ChipSetDefaultHandler extends DefaultHandler {

    public ChipSetDefaultHandler() {
    }
    
    private String name;
    private int trans;
    
    private int curTile;
    private HashMap<Integer, ChipSetData> lower = new HashMap<Integer,ChipSetData>();
    
    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);
    }

}