Package com.aqpproject.worldmodel.data

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

/*
* 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.game.Singleton;
import com.aqpproject.tools.Vector2D;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
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 ItemController {

    public ItemController(String path, int qty, int delay) throws ParserConfigurationException, SAXException, IOException {
        m_itemList = new ArrayList<>();
        m_itemsOccur = new HashMap<>();
        m_itemQty = qty;
        m_delay = delay;
        m_xmlPath = path;
        if ("SERVER".equals(Singleton.getOptionsController().getRole())) {
            //Lecture du fichier xml
            generateInfos();
            //Populate du fichier
            generateItems();
        }
    }

    private void generateInfos() throws ParserConfigurationException, SAXException, IOException {
        if (m_xmlPath != null) {
            int rarety;
            int min = 0;
            int max = 0;
            File file = new File(m_xmlPath);
            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;
                    rarety = Integer.parseInt(e.getAttribute("rarety"));
                    if (min == 0) {
                        max = min + rarety;
                    } else {
                        max = min + rarety - 1;
                    }
                    m_itemsOccur.put(e.getAttribute("name"), new Vector2D(min, max));
                    min = max + 1;
                }
            }
            m_maxRarety = max;
        }
    }

    private void generateItems() throws ParserConfigurationException, SAXException, IOException {
        // m_itemList.add(new Item("tata", "Box", new Vector2D(0,0), 0, 1));

        //Génération des items du niveau
        Random rand = new Random();
        ItemFactory itFactory = new ItemFactory(m_xmlPath);
        int r;
        for (int i = 0; i < m_itemQty; i++) {
            r = rand.nextInt(m_maxRarety);
            for (Entry e : m_itemsOccur.entrySet()) {
                if (((Vector2D) e.getValue()).x <= r && ((Vector2D) e.getValue()).y >= r) {
//                  System.out.println(e.getKey().toString());
                    m_itemList.add(itFactory.getItem(e.getKey().toString(), i));
                }
            }
        }
    }

    public Item peek() {
        if (m_itemList.size() > 0) {
            return m_itemList.get(0);
        } else {
            return null;
        }
    }

    public int getDelay() {
        return m_delay;
    }

    public Item getItem() throws ParserConfigurationException, SAXException, IOException {
        Item item = peek();
        if (m_itemList.size() > 0) {
            m_itemList.remove(0);
        }
        if (m_itemList.isEmpty() && "SERVER".equals(Singleton.getOptionsController().getRole())) {
            generateItems();
        }
        return item;
    }
    private HashMap<String, Vector2D> m_itemsOccur;
    private ArrayList<Item> m_itemList;
    private int m_itemQty;
    private int m_delay;
    private String m_xmlPath;
    private int m_maxRarety;
}
TOP

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

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.