Package com.aqpproject.worldmodel.data

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

/*
* 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 com.aqpproject.worldmodel.game.entity.WEBox;
import com.aqpproject.worldmodel.game.entity.WECar;
import com.aqpproject.worldmodel.game.entity.WEItemMini;
import com.aqpproject.worldmodel.game.state.RaceGameState;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
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 Inventory {

    public Inventory(int _max, String path, RaceGameState state, WECar car) throws ParserConfigurationException, SAXException, IOException {
        m_maxItem = _max;
        m_itemList = new ArrayList<Item>();
        m_miniatures = new HashMap<String, String>();
        m_miniaturesList = new ArrayList<WEItemMini>();
        m_state = state;
        m_car = car;
        m_itemFac = new ItemFactory(path);
        initializeMiniatures(path);
    }

    public boolean add(WEBox box) {
        if (m_itemList.size() < m_maxItem) {
            if (Singleton.getOptionsController().getRole().equals("SERVER")) {
                Item it = box.getItem();
                if (it != null) {
                    if (m_itemList.add(it)) {
                        updateMiniatures();
                        if (m_car.isMyCar()) {
                            try {
                                Singleton.getAudioController().playSound("clic", false);
                            } catch (ParserConfigurationException | SAXException | IOException ex) {
                                Logger.getLogger(Inventory.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        return true;


                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                if (m_nextItemType != null) {
                    box.getItem();
                    add(m_nextItemType, m_nextItemCpt);
                    m_nextItemType = null;
                    m_nextItemCpt = 0;
                    updateMiniatures();
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }

    public void add(String type, int cpt) {
        m_itemList.add(m_itemFac.getItem(type, cpt));
    }

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

    public void deleteAll() {
        m_itemList.removeAll(m_itemList);
    }

    public boolean use() {
        if (m_itemList.size() > 0) {
            m_itemList.get(0).action(m_state, m_car);
            m_itemList.remove(0);
            updateMiniatures();
            return true;
        } else {
            return false;
        }
    }

    private void initializeMiniatures(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_miniatures.put(e.getAttribute("name").toUpperCase(), e.getAttribute("minSpriteName"));
                }
            }
        }
    }

    public void updateMiniatures() {
        if (m_car.getName().equals(m_state.getPlayerName())) {
            for (WEItemMini itm : m_miniaturesList) {
                itm.destroy();
            }
            m_miniaturesList.removeAll(m_miniaturesList);
            int x = 128;
            int y = (int) Singleton.getVisualisation().getResolution().y - 20 - 64;
            for (Item it : m_itemList) {
                m_miniaturesList.add(new WEItemMini("mini_" + m_miniaturesList.size(), m_miniatures.get(it.getType().toString()), new Vector2D(x, y), 0, 1));
                x += 64;
            }
        }
    }

    public void removeMiniatures() {
        for (WEItemMini itm : m_miniaturesList) {
            itm.destroy();
        }
        m_miniaturesList.removeAll(m_miniaturesList);
    }

    private ArrayList<Item> getItemList() {
        return m_itemList;
    }

    public void setNextItem(String type, int cpt) {
        m_nextItemType = type;
        m_nextItemCpt = cpt;
    }

    public boolean hasItem() {
        return m_itemList.size() > 0;
    }
    private String m_nextItemType;
    private int m_nextItemCpt;
    private int m_maxItem;
    private ArrayList<Item> m_itemList;
    private HashMap<String, String> m_miniatures;
    private ArrayList<WEItemMini> m_miniaturesList;
    private RaceGameState m_state;
    private WECar m_car;
    private ItemFactory m_itemFac;
}
TOP

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

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.