I’m porting a 2D platformer and I need a good way of getting some extensibility out of my level tiles. I’m not sure if Decorator is correct here, but nothing else comes to mind. Maybe I need something unique.
Anyway, say I have a basic Tile that has things like it’s image, and whether the player can pass through it (background or foreground). Now I want to add properties to them like making some tiles act as ladders, or making some hurt or kill the player on contact. Maybe some can disappear. Some properties can be combined.
This sounds like decorator, but I really don’t like how that requires my base class to implement dummy versions of everything, like isLadder(), isLethal(), etc. Is there a better way? I’m not going to be the guy who changes my design just so it looks like something out of the GoF book.
Sorry if this has been asked a million times before, I didn’t quite find it in the related questions.
How you design your class methods can make a huge difference in how difficult Decorator is to implement – for instance, instead of
isLadder()andisLethal()and so on, why not have methods based on the ways in which the player could interact with a tile (enter(from_dir),exit(to_dir)), etc? A lethal tile could override theenter()method to signal that the player should be killed; a ladder tile could override eitherenter()orexit()depending on the desired functionality.