-/* $Id: objects.js,v 1.3 2005/07/30 09:04:42 mmondor Exp $ */
+/* $Id: objects.js,v 1.4 2005/07/30 23:17:01 mmondor Exp $ */
/*
* Copyright (c) 2005, Matthew Mondor
* ALL RIGHTS RESERVED.
*/
+/*
+ * Primarily written in JavaScript for easy and quick prototyping/testing.
+ * May eventually be written in C afterwards if need be, but the game might
+ * actually be implemented in JavaScript too depending on future decisions.
+ */
+
/*
}
Item.prototype = {
- volume: function()
+ isContainer: function()
+ {
+ return false;
+ },
+
+ getVolume: function()
{
return this.volume;
},
- weight: function()
+ getWeight: function()
{
return this.weight;
}
* And an object to handle container Items, inheriting the Item object.
*/
+const E_OK = 0;
const E_TOOLARGE = -1;
const E_TOOHEAVY = -2;
const E_BADTYPE = -3;
}
ContainerItem.prototype = {
+ isContainer: function()
+ {
+ return true;
+ },
+
open: function()
{
this.con_open = true;
this.con_open = false;
},
- volume: function()
+ getVolume: function()
{
/*
* Objects which expand, such as bags, need to report proper
this.volume;
},
- weight: function()
+ getWeight: function()
{
return this.weight + this.con_weight;
},
/* Can only add items to open container */
if (!this.con_open)
return E_CLOSED;
+
/* We can't add ourself in :) */
- if (item.index == this.item.index)
+ if (item.index == this.index)
return E_SELF;
+
/* Nor items which are already inside */
if (this.con_items[item.index] != undefined)
return E_ALREADY;
+
/* We only accept items of proper type */
if ((this.con_ctype & item.ctype) == 0)
return E_BADTYPE;
+
/* We can only hold objects which are small enough */
- if (this.con_neck < item.volume())
+ if (this.con_neck < item.getVolume())
return E_TOOLARGE;
- if (this.con_volume_cur + item.volume() > this.con_volume_max)
+ if (this.con_volume_cur + item.getVolume() >
+ this.con_volume_max)
return E_TOOLARGE;
+
/* We can only hold objects which are light enough */
- if (this.con_weight_cur + item.weight() > this.con_weight_max)
+ if (this.con_weight_cur + item.getWeight() >
+ this.con_weight_max)
return E_TOOHEAVY;
/*
* expandable, whenever objects are added/removed, and would
* also verify their parent's volume/weight limits anytime.
* We need to do this for weights anyway.
+ * So if a container is inside a container, it must have a
+ * parent. That parent must go up iteratively until the
+ * master container is found, stopping and reverting anything
+ * when reaching the limit of any parent.
*
* Also implement a recursive runner which maps all objects,
* for instance for inventory purposes, but only recursing
* through containers which are in the open state.
*/
+ /*
+ * The weight has to propagate to parent containers.
+ * The volume also has, in the case where containers
+ * are expandable.
+ * We need to observe the maximum allowed volume and weight
+ * of all parent containers as well.
+ * We need to climb through the parents list to do this.
+ * Moreover, we need to actually reverse any changes made
+ * to the parents in case of any volume/weight exceeded.
+ *
+ * XXX Hmm perhaps that I should use another method
+ * alltogether, always evaluating the volume and weight of an
+ * object with all its children? The type of game is probably
+ * not performance critical anyways.
+ */
+ for (var o = this.con_parent; o != undefined;
+ o = o.con_parent) {
+ /* XXX */
+ // if (o.weight + this.getWeight() > o.con_weight_max)
+ }
+
this.con_items[item.index] = item;
- this.con_volume_cur += item.volume();
- this.con_weight_cur += item.weight();
+ this.con_volume_cur += item.getVolume();
+ this.con_weight_cur += item.getWeight();
this.con_items_cnt++;
- return 0;
+ return E_OK;
},
remove: function(item)
/* Can only remove items from open container */
if (!this.con_open)
return E_CLOSED;
+
/* We can only remove items which are really inside */
if (this.con_items[item.index] == undefined)
return E_NONE;
- this.con_weight_cur -= item.weight();
- this.con_volume_cur -= item.volume();
+ this.con_weight_cur -= item.getWeight();
+ this.con_volume_cur -= item.getVolume();
delete this.con_items[item.index];
this.con_items_cnt--;
+
+ return E_OK;
}
};
+
+
+
+/*
+ * Some testing in order
+ */
+
+/* Create an item */
+var i = new Item('a ring', 'the one ring', 5, 5, CT_SOLID);
+
+/* Create a container item */
+var c = new ContainerItem('a bag', 'a soft cloth bag', 5, 5, CT_SOLID,
+ 10, 15, 20, CT_SOLID | CT_EXPAND);
+
+/*
+ * Open container, add item in it and then close it, reopening it again to
+ * remove back the item from it and to close it once and for all.
+ */
+c.open();
+print(c.add(i));
+c.close();
+c.open();
+print(c.remove(i));
+c.close();