Re: Double Dispatch Problem: Mobile Creatures and Projectiles in a Game World



"Aaron J. M." <ajmacd@xxxxxxxxxxxxxxx> wrote:
On Mar 29, 9:50 am, "Daniel T." <danie...@xxxxxxxxxxxxx> wrote:

I don't think there is a "lazy" solution, you need two lists.
Here's a possible solution:

World<---Mobile

class World
+reportCreaturePostion( Point )
+reportProjectilePosition( Point )
+creatureAt( Point ): boolean
+projectileAt( Point ): boolean

class Mobile
+attachTo( World )

Mobiles tell the world where they are, and ask the world if there are
any creatures or projectiles at that location. If there are, the mobile
does what it knows it should do.

So, World keeps two lists (Creatures and Projectiles), and all
Mobiles are able to attach themselves to a World.

No. World keeps two lists of Points and something else attaches the
Mobiles to the World.

World would need the methods attachCreature(Creature, Point) and
attachProjectile(Projectile, Point) in that case. I'd also have to
change World whenever I wanted to add a new type of Mobile.

World would need the methods attachCreature(Point) and
attachProjectile(Point), but yes it would need to be changed if a new
Mobile that is not a Projectile or Creature exists.

It might get more complicated when I want to get timing involved.
With just one list in World I could give World the method update()
which tells all the Mobiles to move(). The two lists could be just
as easy since I could loop through both lists, but what if I want
every Mobile to have a "speed" which affects the order in which they
need to be updated regardless of their actual class? I would have
to introduce a third list that holds (references to) all of the
Mobiles and loop through that when I want them to move.

In the idea above, World doesn't have pointers to Mobiles, and doesn't
send any messages to them. Each Mobile would act independently.

This is only one option of many and may not even be the best option
depending on many factors.
.



Relevant Pages