|
|
 | | From: | steve mathers | | Subject: | cant find a pattern to fit neatly - how to make a large number of monsters? | | Date: | 23 Jan 2005 15:31:30 -0800 |
|
|
 | Hi , fairly new to OO design. Trying to solve a problem:
problem: I want to make a large number of different monsters. what makes monsters different from each other is their 'abilities'. These could such things as carrying a weapon or magic item that allows them to perform an action, or something like an intrinsic resistance to attack with fire, or a temporary ability ability granted by a spell cast on them.
* I want to be able to define different monster types and different ability types using as little memory as possible (mobile phone platform) - a hard coded table of abilities and then another table of monsters that lists which ability each monster has. Some sort of factory will have to create the monster given an index into this table. Thats not the hard part.
* I want to be able to add new monsters and new abilities to the table without changing the inteface of the monster and ability class. I think this is the hard part.
solution: At first thought, the decorator pattern seems right, but because abilities can come and go all the time, it seems to be unweildy.
I think what I want to happen is that every time the monster has to do something that involves its characteristics which may be modified by abilities (such as fire resistance), or it wants to perform an optional action that an ability may bestow it with (such as hit something with a weapon), it should interogate its entire list of current abilities to see if they impact on that charicteristic or action. But how to structure that process in my design?
Is there a particular pattern I should be looking at?
thanks,
Steve
|
|
 | | From: | thufir | | Subject: | Re: cant find a pattern to fit neatly - how to make a large number of monsters? | | Date: | 23 Jan 2005 23:04:26 -0800 |
|
|
 | stevenmathers@yahoo.com wrote: > performance not an issue in this application, but codebase size is. > and it would be nice to be able to add new types of abilities and > monsters to the system just by adding a few bytes to a table and a > function or two to handle them, and then have a factory creatre them at > runtime.
If each monsters attributes are composed of different objects, it should be very easy make a SuperSword class and have bigMonster.armWith("superSword") I'd think.
I don't know how to address your codebase size concern, sorry...
> example of the process I was thinking of: > > monster object: holds intrinsic characteristics and list of optional > abiltiies
instead of a list of abilities, try for objects :)
> ability object: enables monster to perform some function, and/or > modifies some of its abilities.
As in a sword object, or fireResistant object, of which a particular Monster type object would be composed. That is, each object would "have-a" object, or objects :)
> so there is a dependency there between monster and ability, but how to > make it as weak as possible. The vector of change is definately more > types of abilities, and more types of monsters.
Composition works very well at encapsulating data, which allows you to change dependant classes to a larger degree.
> use case 1: monster makes a melee attack: it interrogates each of its > abilities: are you my current melee weapon, and if so, what type and > degree of damage do you do?
public class BunchOfMonsters { public static void main (String args[]) { Creature blobMonster = new Monster("blob"); //"blob" is a monster type blobMonster.armWith("sword"); //sword is a type of weapon //have a list of monsters } }
public class Monster implements Creature { public void armWith(String wep){ if (wep == "sword") { Item aSword = new Sword(); } //give sword to monster instance //Sword implements Item }
public void attack() { // find best weapon // find target } }
> use case 2: monster needs to use its SPEED characetristic to determine > if it is its turn to act yet. it interogates all its current > abilities: here is my current SPEED - modify it if you will.
how about each instance of Monster has an instance of Melee? Using Timer() each melee object determines when it gets to move. This object would then determine whether to fight or flee?
> Perhaps there is a better system?
AFAIK this uses a "tick" idea, where there's a list of 'bots which gets iterated through once/sec; this is less complex than having a Melee class.
-- Thufir Hawat
|
|
 | | From: | thufir | | Subject: | Re: cant find a pattern to fit neatly - how to make a large number of monsters? | | Date: | 23 Jan 2005 15:50:44 -0800 |
|
|
 | steve mathers wrote: > Hi , fairly new to OO design. Trying to solve a problem: [..] > I think what I want to happen is that every time the monster has to do > something that involves its characteristics which may be modified by > abilities (such as fire resistance), or it wants to perform an > optional action that an ability may bestow it with (such as hit > something with a weapon), it should interogate its entire list of > current abilities to see if they impact on that charicteristic or > action. But how to structure that process in my design? [..]
That sounds very computationally intensive to me. Depending on the size of the table, mightn't that create more of a strain than keeping a mobs characteristics within a single object?
I've always heard that it's better to focus on design first and performance second. As an alternative, what's wrong with a very simple mob composed different "parts," like FireResistance.
-- Thufir Hawat
|
|
 | | From: | thufir | | Subject: | Re: cant find a pattern to fit neatly - how to make a large number of monsters? | | Date: | 23 Jan 2005 22:30:04 -0800 |
|
|
 | stevenmathers@yahoo.com wrote: > performance not an issue in this application, but codebase size is. > and it would be nice to be able to add new types of abilities and > monsters to the system just by adding a few bytes to a table and a > function or two to handle them, and then have a factory creatre them at > runtime.
If each monsters attributes are composed of different objects, it should be very easy make a SuperSword class and have bigMonster.armWith("superSword") I'd think.
I don't know how to address your codebase size concern, sorry...
> example of the process I was thinking of: > > monster object: holds intrinsic characteristics and list of optional > abiltiies
instead of a list of abilities, try for objects :)
> ability object: enables monster to perform some function, and/or > modifies some of its abilities.
As in a sword object, or fireResistant object, of which a particular Monster type object would be composed. That is, each object would "have-a" object, or objects :)
> so there is a dependency there between monster and ability, but how to > make it as weak as possible. The vector of change is definately more > types of abilities, and more types of monsters.
Composition works very well at encapsulating data, which allows you to change dependant classes to a larger degree.
> use case 1: monster makes a melee attack: it interrogates each of its > abilities: are you my current melee weapon, and if so, what type and > degree of damage do you do?
public class BunchOfMonsters { public static void main (String args[]) { Creature blobMonster = new Monster("blob"); //"blob" is a monster type blobMonster.armWith("sword"); //sword is a type of weapon //have a list of monsters } }
public class Monster implements Creature { public void armWith(String wep){ if (wep == "sword") { Item aSword = new Sword(); } //give sword to monster instance //Sword implements Item }
public void attack() { // find best weapon // find target } }
> use case 2: monster needs to use its SPEED characetristic to determine > if it is its turn to act yet. it interogates all its current > abilities: here is my current SPEED - modify it if you will.
how about each instance of Monster has an instance of Melee? Using Timer() each melee object determines when it gets to move. This object would then determine whether to fight or flee?
> Perhaps there is a better system?
AFAIK this uses a "tick" idea, where there's a list of 'bots which gets iterated through once/sec; this is less complex than having a Melee class.
-- Thufir Hawat
|
|
 | | From: | thufir.hawat at mail.com | | Subject: | Re: cant find a pattern to fit neatly - how to make a large number of monsters? | | Date: | 23 Jan 2005 23:07:57 -0800 |
|
|
 | stevenmathers@yahoo.com wrote: > performance not an issue in this application, but codebase size is. > and it would be nice to be able to add new types of abilities and > monsters to the system just by adding a few bytes to a table and a > function or two to handle them, and then have a factory creatre them at > runtime.
If each monsters attributes are composed of different objects, it should be very easy make a SuperSword class and have bigMonster.armWith("superSword") I'd think.
I don't know how to address your codebase size concern, sorry...
> example of the process I was thinking of: > > monster object: holds intrinsic characteristics and list of optional > abiltiies
instead of a list of abilities, try for objects :)
> ability object: enables monster to perform some function, and/or > modifies some of its abilities.
As in a sword object, or fireResistant object, of which a particular Monster type object would be composed. That is, each object would "have-a" object, or objects :)
> so there is a dependency there between monster and ability, but how to > make it as weak as possible. The vector of change is definately more > types of abilities, and more types of monsters.
Composition works very well at encapsulating data, which allows you to change dependant classes to a larger degree.
> use case 1: monster makes a melee attack: it interrogates each of its > abilities: are you my current melee weapon, and if so, what type and > degree of damage do you do?
public class BunchOfMonsters { public static void main (String args[]) { Creature blobMonster = new Monster("blob"); //"blob" is a monster type blobMonster.armWith("sword"); //sword is a type of weapon //have a list of monsters } }
public class Monster implements Creature { public void armWith(String wep){ if (wep == "sword") { Item aSword = new Sword(); } //give sword to monster instance //Sword implements Item }
public void attack() { // find best weapon // find target } }
> use case 2: monster needs to use its SPEED characetristic to determine > if it is its turn to act yet. it interogates all its current > abilities: here is my current SPEED - modify it if you will.
how about each instance of Monster has an instance of Melee? Using Timer() each melee object determines when it gets to move. This object would then determine whether to fight or flee?
> Perhaps there is a better system?
AFAIK this uses a "tick" idea, where there's a list of 'bots which gets iterated through once/sec; this is less complex than having a Melee class.
-- Thufir Hawat
|
|
 | | From: | stevenmathers at yahoo.com | | Subject: | Re: cant find a pattern to fit neatly - how to make a large number of monsters? | | Date: | 23 Jan 2005 16:09:30 -0800 |
|
|
 | performance not an issue in this application, but codebase size is. and it would be nice to be able to add new types of abilities and monsters to the system just by adding a few bytes to a table and a function or two to handle them, and then have a factory creatre them at runtime.
example of the process I was thinking of:
monster object: holds intrinsic characteristics and list of optional abiltiies
ability object: enables monster to perform some function, and/or modifies some of its abilities.
so there is a dependency there between monster and ability, but how to make it as weak as possible. The vector of change is definately more types of abilities, and more types of monsters.
use case 1: monster makes a melee attack: it interrogates each of its abilities: are you my current melee weapon, and if so, what type and degree of damage do you do?
use case 2: monster needs to use its SPEED characetristic to determine if it is its turn to act yet. it interogates all its current abilities: here is my current SPEED - modify it if you will.
Perhaps there is a better system?
thufir wrote: > steve mathers wrote: > > Hi , fairly new to OO design. Trying to solve a problem: > [..] > > I think what I want to happen is that every time the monster has to > do > > something that involves its characteristics which may be modified by > > abilities (such as fire resistance), or it wants to perform an > > optional action that an ability may bestow it with (such as hit > > something with a weapon), it should interogate its entire list of > > current abilities to see if they impact on that charicteristic or > > action. But how to structure that process in my design? > [..] > > That sounds very computationally intensive to me. Depending on the > size of the table, mightn't that create more of a strain than keeping a > mobs characteristics within a single object? > > I've always heard that it's better to focus on design first and > performance second. As an alternative, what's wrong with a very simple > mob composed different "parts," like FireResistance. > > -- > Thufir Hawat
|
|
|