Collection class hierarchy
Overview
This document describes the overall class hierarchy for physical collections.
This includes liquids, stacks (such as a stack of arrows or coins), bags
containing other items, etc. The exact methods may change slightly
in the future as the design is put to test, but I have implemented most
of these classes and the overall design seems to work well.
-
Thing
-
Collection
-
AnonymousCollection
-
IndividualCollection
-
SingleTypeCollection
-
Container
-
Bag, Chest, Closet, etc...
Collection (extends Thing)
This is the abstract superclass for all Things that represent a collection
of other Things. The main method here is:
This is not to be confused with ContainerNode.countChildren(). The
difference is that getItemCount() will only count the items that are considered
to be inside this collection, i.e not Effects. countChildren()
will count all container hierarchy children, which includes Effects.
getWeight() and getBulk() (overidden from Thing) will return the weight
and bulk of the collection as a whole, i.e including the contained items.
AnonymousCollection (extends Collection)
This is a collection which does not individually keep track of each
item inside - for example a Liquid or CoinStack. In a CoinStack we
don't want each coin to be an independent object instance (imagine a stack
of 30000 coins...). The main methods are:
void addItems(int count);
void removeItems(int count);
void removeAllItems();
void transferItemsTo(AnonymousCollection target, int count);
void mergeWith(AnonymousCollection target);
So it is anonymous in the sense that you don't actually add individual
item instances - you just use integer values to specify how much to add
or remove. The integer value could represent single items (like for
coins or pebbles), mass (for liquids), doses, or whatever.
An AnonymousCollection will disappear when all items have been removed
- so you cannot have an AnonymousCollection with itemCount = 0.
CoinStack (extends AnonymousCollection)
This may just as well be called Money. It represents a set of 1 or
more coins. Later on I might refine this to include different types
of coins. If you have a CoinStack in your bag and insert another
CoinStack, they will be merged together -this is to simplify the handling
of coins.
Liquid (extends AnonymousCollection)
Liquids are special in that they can be "poured out". There will
be a special class or interface called LiquidContainer (or something like
that), and a Liquid must be placed in a LiquidContainer or it will disappear.
So if you pour out your water on the table, the water will be lost.
LiquidContainer will also include methods for determine possible leakage
and stuff like that.
I may add a subclass called MixedLiquid which is what you get when you
pour two Liquids into the same LiquidContainer. A MixedLiquid keeps
track of the proportions of each liquid included, or may in some cases
turn into a new Liquid. This is fun - it allows us to make a whole
alchemy system, where you can mix stuff together in the right proportions
to create special potions :o)
IndividualCollection (extends Collection)
This is a collection which tracks each contained item individually, and
doesn't just bunch everything together as one integer value. It's
main method is:
This method returns all children of type Thing or Creature. The normal
ContainerNode methods addChild(...) and removeChild(...) are used to add
and remove items to the collection.
SingleTypeCollection (extends IndividualCollection)
This is a collection of items of a specific type. The desired type
is specified in the constructor, for example orb.things.Arrow.class.
So this is essentially a "stack".
It does not add any new methods, but overrides a few methods to make
sure that only items of the given type can be added. A SingleTypeCollection
must have a size of at least 2, and will disappear if smaller.
Note the difference here between a stack of arrows (being a SingleTypeCollection)
and a stack of coins (being an AnonymousCollection). The arrows are
tracked seperately, and the SingleTypeCollection is like an invisible "shell"
around them that allows them to be manipulated together as a single item.
So if one arrow is magical and you add it into a bunch of other arrows,
it will still be magical when it comes (i.e not "mixed up" with the others).
The coin stack, however, is really not a collection - it is more like
an item in itself (it does not contain any other items, in reality).
If you have a coin stack of size 2, and split it up, then it will become
two CoinStacks of size 1 (although the icon will look like a single coin
of course). If you do the same with an arrow stack of size 2, however,
you will end up with two Arrows, and no collection at all.
Likewise, if you put two coins together into a stack then one item will
disappear (i.e one of the CoinStacks). If you put two arrows together
into a stack, one item will be created (the SingleTypeCollection)!
It is important to understand the difference between an AnonymousCollection
and an IndividualCollection.
Container (extends IndividualCollection)
This is the superclass for all physical container items such as bags, chests,
closets, etc. It adds physical constraints of the container itself,
i.e the bulk and weight of the container itself, and the maximum bulk and
weight that the container can hold. It's main methods are:
int getWeight();
int getBulk();
int getInsideWeight();
int getInsideBulk();
int getContainerWeight();
int getContainerBulk();
int getMaxWeight();
int getMaxBulk();
getWeight() is the total weight of the container plus contents. getInsideWeight()
is the weight of the contents of this container. getContainerWeight()
is the weight of the container itself. getMaxWeight() is the maximum
inside weight allowed.
Henrik
Kniberg
Last updated: