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



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. 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.

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.

.



Relevant Pages