I have been working on a Java game for a few months now and I've run into a problem.
My Player
walks around in an Environment
, which is comprised of tiles that all have their own CollisionBox
. Put simply: when the Player
's CollisionBox
collides with another CollisionBox
(like that of a box or something), then the Player can't move. That part is taken care of.
I'm trying to introduce InteractiveTile
s, in which the Player
will cause something to happen if it collides with a specific CollisionBox
, such as stairs or a door. Specifically, I want to be able to transition to a new Environment
if specific *InteractiveTile*s are hit.
I have the functionality to transition and redraw collision maps, but I don't know how to make that collision event tell the Environment (which is not conveniently in the hierarchy of the code to the collision event).
I am familiar with the Observer Pattern, in which the Environment
could "hear" this collision, but I am not really sure how it would be implemented here, or even if that's the right design pattern for this situation!
Even more simply...
When Player.IsCollidingWithDoorLeadingToNewEnvironment(newEnvironment)
is called, Environment.TransitionToNewEnvironment(newEnvironment)
should be called, but they are far apart from each other in the code structure.
More broadly: how can I send messages between two classes without layers upon layers of method/constructor injection?