Re: Double Dispatch Problem: Mobile Creatures and Projectiles in a Game World
- From: Ed Wegner <ed.wegner@xxxxxxxxxx>
- Date: Thu, 29 Mar 2007 17:00:54 +1200
Aaron J. M. wrote:
I'm designing a game, and I've come across a problem that's similar to
the one
listed here (Google Groups):
http://groups.google.ca/group/comp.object/browse_thread/thread/4a5c70e3560388c8
I have a World, and in this world are Creatures and Projectiles. Both
of these
have a location, are able to move around somehow, and can collide with
each
other. Creatures also create Projectiles which get added to the
World.
For that reason I thought about having both of them be subclasses of a
class
Mobile. In addition to sharing behavior, Mobile is convenient because
the
World only has to keep one big list of Mobiles instead of a list of
Creatures
and a list of Projectiles.
1 N 1
World --------> Mobile <----\
has A 1| | collides with
| \------/
|
/----------^----------\
| |
| 1 N |
Creature -----------> Projectile
shoots
(Did Google Groups take away the fixed width font option?)
The problem is when Mobiles try to collide with each other. Two
Projectiles
colliding doesn't do anything (they pass over each other) and neither
does two
Creatures colliding (they don't move). It's only when a Creature and
a
Projectile collide that something interesting happens (the Creature is
hit).
I couldn't really do that with Mobile without either double dispatch
or the
Visitor pattern.
To avoid that problem I can just have World hold two lists: one of
Creatures and
one of Projectile. But...then I'd need two lists. That might not be
a bad
thing, but it is something extra the World has to take into account
and I'm also
lazy.
What do people here think about this?
I think your model above matches your description. In particular, Creatures and Projectiles being generalisations of Mobile. This appears to be a complete subset - that is you can view the "list" of Mobiles as being composed of exactly the "list" of Creatures plus the "list" of Projectiles. So, you have one list, but each member of the list must indicate whether it's a Creature or a Projectile (each must be exactly one or the other).
To address the collision logic you describe, I'd add an Association class off the "collides with" reflexive relationship between instances of Mobiles. Let's call it "Collision". When a Collision occurs, the first thing it checks is whether the colliding Mobiles are one of each. If so, execute the "Projectile hits Creature" logic. It could also be the locus of future logic that supported Creature-to-Creature collision interactions and Projectile-to-Projectile collision interactions....
Good luck with the game.
Thanks,
Aaron J. M.
Ed Wegner
.
- References:
- Prev by Date: Re: Long Life Objects
- Next by Date: Re: Separation of concerns
- Previous by thread: Double Dispatch Problem: Mobile Creatures and Projectiles in a Game World
- Next by thread: Re: Double Dispatch Problem: Mobile Creatures and Projectiles in a Game World
- Index(es):
Relevant Pages
|