Package com.aqpproject.worldmodel.data

Source Code of com.aqpproject.worldmodel.data.ItemFactory

/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.worldmodel.data;

import com.aqpproject.tools.Vector2D;
import com.aqpproject.worldmodel.ItemType;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
*
* @author admlatterner
*/
public class ItemFactory {

    public ItemFactory(String path) throws ParserConfigurationException, SAXException, IOException {
        //Initialization
        m_itemsSpriteName = new HashMap<>();
        m_itemsPosition = new HashMap<>();
        m_itemsNumFrames = new HashMap<>();

        //XML reading
        loadItemList(path);
    }

    private void loadItemList(String path) throws ParserConfigurationException, SAXException, IOException {

        if (path != null) {
            File file = new File(path);
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(file);
            doc.getDocumentElement().normalize();

            Element root = doc.getDocumentElement();
            NodeList children = root.getChildNodes();

            for (int i = 0; i < children.getLength(); i++) {
                Node n = children.item(i);
                if (n.getNodeType() == Node.ELEMENT_NODE) {
                    Element e = (Element) n;
                    m_itemsSpriteName.put(e.getAttribute("name"), e.getAttribute("spriteName"));
                    m_itemsPosition.put(e.getAttribute("name"), new Vector2D(Integer.parseInt(e.getAttribute("width")), Integer.parseInt(e.getAttribute("height"))));
                    m_itemsNumFrames.put(e.getAttribute("name"), Integer.parseInt(e.getAttribute("frame")));
                }
            }
        }
    }

    public static Item getItem(String s, int cpt) {

        if (m_itemsSpriteName.containsKey(s) && m_itemsPosition.containsKey(s) && m_itemsNumFrames.containsKey(s)) {
            switch (s) {
                case "Missile":
                    return new Missile("Item" + cpt, m_itemsSpriteName.get(s), m_itemsPosition.get(s), 0, m_itemsNumFrames.get(s), ItemType.MISSILE);
                case "Bomb":
                    return new Bomb("Item" + cpt, m_itemsSpriteName.get(s), m_itemsPosition.get(s), 0, m_itemsNumFrames.get(s), ItemType.BOMB);
                case "Mine":
                    return new Mine("Item" + cpt, m_itemsSpriteName.get(s), m_itemsPosition.get(s), 0, m_itemsNumFrames.get(s), ItemType.MINE);
                case "PowerDown":
                    return new PowerDown("Item" + cpt, m_itemsSpriteName.get(s), m_itemsPosition.get(s), 0, m_itemsNumFrames.get(s), ItemType.POWERDOWN);
                case "Boost":
                    return new Boost("Item" + cpt, m_itemsSpriteName.get(s), m_itemsPosition.get(s), 0, m_itemsNumFrames.get(s), ItemType.BOOST);
                case "Troll":
                    return new Troll("Item" + cpt, m_itemsSpriteName.get(s), m_itemsPosition.get(s), 0, m_itemsNumFrames.get(s), ItemType.TROLL);
                default:
                    return null;
            }
        } else {
            return null;
        }
    }
    static HashMap<String, String> m_itemsSpriteName;
    static HashMap<String, Vector2D> m_itemsPosition;
    static HashMap<String, Integer> m_itemsNumFrames;
}
TOP

Related Classes of com.aqpproject.worldmodel.data.ItemFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.