1 /*
2  *  CVS: $Id: Item.java,v 1.12 2004/07/25 19:07:20 marcus Exp $
3  * 
4  *  This file is part of JZuul.
5  *
6  *  JZuul is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10 *
11 *  JZuul is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with Zuul; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 * 
20 *  Copyrigth 2004 by marcus, leh
21 * 
22 */
23
24package org.jzuul.engine;
25
26import java.util.HashMap;
27import java.util.Map;
28
29import org.jdom.Element;
30
31/**
32 * Diese Klasse stellt statische GameObjects (Gegenstände) zur Verfügung
33 * 
34 * 
35 * @version $Revision: 1.12 $
36*/
37public class Item extends GameObject {
38    /**
39     * Enthällt mappings von GameObject namen auf EventHandler, um "use with" Befehle zu implementieren,
40     */
41    Map withObjects;
42
43    /**
44     * Erstellt ein neues GameObject.
45     * 
46     * @param name  der Name des GameObject
47     */
48    public Item(String name) {
49        super(name);
50        this.takeable = true;
51        this.useable = true;
52        withObjects = new HashMap();
53    }
54
55    /**
56     * Implementiert den Befehl "use with".
57     * Der Befehl "use with" Benutzt das Eventsystem um die daraus resultierenden
58     * Aktionen zu implementieren. Dafür wird dem GameObject nach dem "use" eine
59     * Event Map angelegt das mappings von Namen auf EventHandler hat. Diese EventHandler
60     * werden dann mit execute(this) aufgerufen,
61     * 
62     * @param item  das GameObject das nach dem "with" steht
63     * @return  true on success, false on failure
64     * @see         org.jzuul.engine.EventHandler
65     */
66    public boolean useWith(GameObject item) {
67        if (withObjects.containsKey(item.getName())) {
68            Engine.debug(getName() + " executing EventHandler for \"use with " + item.getName() + "\"",1); //$NON-NLS-1$ //$NON-NLS-2$
69            if (((EventHandler) withObjects.get(item.getName())).execute(item))  {
70                item.doEvent(Event.USE_SUCCESS);
71                return true;
72            } else {
73                item.doEvent(Event.USE_FAILURE);
74                return false;
75            }
76        } else {
77            Engine.player.say(Messages.getString("ITEM_COMBINATION_ERROR")); //$NON-NLS-1$
78            return false;
79        }
80    }
81
82    /**
83     * Verarbeitet ein Event.
84     * Der einzige Default handler der von Item Objekten implementiert wird
85     * ist für den Befehl "use". Falls das Item true für deleteOnUse hat wird
86     * es nach dem Event.USE gelösct.
87     * 
88     * @param eventId   die Id des Events
89     * @see org.jzuul.engine.Event
90     */
91    public void doEvent(int eventId) {
92        super.doEvent(eventId);
93        switch (eventId) {
94            case Event.USE :
95                if (deleteOnUse)
96                    Engine.player.findAndDeleteGameObject(this.name);
97                break;
98        }
99
00    }
01
02    /**
03     * Wandelt das Item Objekt in ein JDOM XML Element um.
04     *
05     *@return eine JDOM XML Element representations des Objekts
06     */
07    public Element toElement() {
08        Element itemE = new Element("item"); //$NON-NLS-1$
09        itemE.setAttribute("name", this.getName()); //$NON-NLS-1$
10        return itemE;
11    }
12
13    /**
14     * Fügt einen "withObject" EventHandler hinzu.
15     * Im Endeffekt das gleiche wie die normalen EventHandler, nur das die Namen
16     * der Events hier richtige GameObject Objektnamen sind 
17     * 
18     * @param objName   der Name des withObjects
19     * @param handler   der dazugehörige EventHandler
20     */
21    public void addWithObject(String objName, EventHandler handler) {
22        Engine.debug("Added use " + this.getName() + " with " + objName, 1); //$NON-NLS-1$ //$NON-NLS-2$
23        withObjects.put(objName, handler);
24    }
25
26    public GameObject copy() {
27        Item newItem = new Item(this.getName());
28        super.cloneInto(newItem);
29        newItem.withObjects = this.withObjects;
30            
31        return newItem;
32    }
33    
34    public boolean isItem() {
35        return true;
36    }
37
38}
39