Let’s say I have a Player class. It has a Move method that takes in a direction parameter, a ChangePosition method that takes in two ints, a Disappear void method, and others.
I want to allow the user (probably using listbox) to be able to add some of those methods to the Player object’s behavior. For example, they could choose to have it move right and then move down and then disappear. The methods that they choose would then be added to an event, so that when that event occurs, the methods that they chose would execute.
Has anyone done something like this before? What is the best way to do this?
(I’m 13 and I don’t know much of design patterns)
First of all, you should put all the game logic into an Engine project, with an Engine class, which is responsible for that alone (handling all the game’s logic and communicating with the UI and Framework [and lower layers, if there are any]).
Second, you can have an EngineAction class, which has an enum EngineActionType (Move, ChangePos, Disappear, …), and other properties. The general idea is this:
Have an enum to hold the types:
Create an abstract class to group all the engine action classes logically and add them with the common Type property:
Create a class for each engine action that holds all needed parameters as properties. It should derive from EngineAction and send the appropriate EngineActionType to the base constructor:
After all that, you can put those objects in a list, as the user orders, and the iterate over it, performing them one by one according on their types:
Further tip: The least logic you have in the UI, the better. Always.
Edit: Great idea by Charleh:
Adding to list example:
And iteration: