Re: Confused about mutually dependant classes
From: H. S. Lahman (h.lahman_at_verizon.net)
Date: 05/20/04
- Next message: Neo: "Re: Nearest Common Ancestor Report (XDb1's $1000 Challenge)"
- Previous message: Matthias Hofmann: "Re: Aggregation vs composition"
- In reply to: Peter Ruderman: "Confused about mutually dependant classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 20 May 2004 21:05:04 GMT
Responding to Ruderman...
> I'm writing a game (though it isn't a serious project). The game contains
> creatures, each of whom has a number of hit points. Hit points are lost by
> taking damage, and creatures may have various ways of modifying the amount
> of damage they receive. In addition, damage may have other effects
> unrelated to the creature itself. For example, fire damage might destroy a
> creature's flammable possessions.
>
> I'm confused about how to design the classes to support this system. It
> seems appropriate to have a damage class that abstracts the various things
> damage can do, but how do I divide the work of dealing damage between the
> creature class and the damage class? Should damage be aware of the various
> ways it can be resisted? Should creatures be aware of all the things damage
> can do (besides reducing hit points)? Should damage deal itself to
> creatures, or should creatures receive damage? How do I design a mutual
> interface such that only these two classes can use it?
A Damage class is possible, but I am a bit skeptical. A lot more
context information about the game would be needed to determine that.
The reason I am skeptical is that damage would be a result of some
activity that caused the damage. The owner of that activity probably
doesn't care about the resulting damage; it just needs to announce that
the activity took place.
Whoever or whatever gets damaged have an interest in knowing about that
activity. They would understand how to react to that activity by
assessing damage.
So the normal thing would be for whoever executes the activity to send a
message identifying the activity to everyone who might be affected by
the activity, including damage assessment. So we might have something like:
+------------------+
| |
| zaps |
| 1 |
[Creature] -------------+
| 1 1
|
|
| carries
| *
[Stuff]
So Creature A zaps Creature B with a flame-thrower. That the zap
mechanism was a flame-thrower could be part of the message data packet
for a generic "zap" message or it could be a unique message, such as
"zapWithFlame".
Handling the [Stuff] carried by Creature B is a matter of context. One
choice is for Creature A to also send the same message to each of
Creature B's Stuff objects. Alternatively, Creature B could decide
whether it should notify any of its Stuff about the flame-thrower in the
course of its own damage assessment.
FWIW, without more context I would be inclined to do the second option
for a couple of reasons. The main one is that Creature A probably
doesn't need to know Creature B is carrying anything; that seems like a
personal issue between B and B's Stuff. A related reason is that it is
likely that Creature B is a better judge of what to do about damage to
itself and its stuff. For example, if Creature B has invested in a
flame-retardant backpack that is likely to affect the damage to Stuff
and Creature A probably isn't in a position to know that.
*************
There is nothing wrong with me that could
not be cured by a capful of Drano.
H. S. Lahman
hsl@pathfindermda.com
Pathfinder Solutions -- Put MDA to Work
http://www.pathfindermda.com
(888)-OOA-PATH
- Next message: Neo: "Re: Nearest Common Ancestor Report (XDb1's $1000 Challenge)"
- Previous message: Matthias Hofmann: "Re: Aggregation vs composition"
- In reply to: Peter Ruderman: "Confused about mutually dependant classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|