- Home /
Optional actions based on Interface Implementations
I am unsure how to tackle an issue with developing the base system for my game. Try to think of the game scribblenauts, I will give a quick description of what I mean. When you click on an object, that object will have a list of actions. I.e. I click a chair and I can sit on it, or remove it. If I clicked on a bed I can jump on it, sleep in it, or remove it.
The first test is with a chair. At the moment I have a base class called RoomObjects.cs that handles objects in the room, RoomItem.cs that inherits from RoomObjects and implements an interface called IClickable that handles mouse click events on all RoomItems, Chair.cs that inherits from RoomItem.cs and Implements ISittable. This is fairly basic at the moment so when I call the mouse event from RoomItem.cs I can just override it in Chair.cs and add Sit() from ISittable in. However this seems rather tedious.
I was curious whether there was some method that allowed me to view all implemented interfaces of a class, then call each method based on the implemented interface. However this method also seems like it would be quite buggy.
Another example is Bed.cs, It inherits RoomItem.cs and Implements, IJumpable, ISittable, ISleepable, I Removable. When clicking on the bed it will display each method ( JumpOn, SitOn, SleepOn, Remove). I was wondering if there was an easier way for this to be done.
Thanks in advance.
The current system I have implemented does this. On the main character is a DisplayOptions.cs that handles displaying all possible options. RoomItem.cs has a reference to DisplayOptions.cs. The chair overrides the clicked events and calls DisplayOptions.Display(RoomItem _item) which is passed from chair with (this) as the parameter. This then checks whether the _item is ISittable and displays the option if it is,
This seems to work alright for now.