C # structure for objects in a 2D game

First, let me apologize for asking a question that may seem a bit vague (or poorly formalized), but I don't have enough experience to ask something more specific.

I create an engine for playing 2D adventure games (sort-by-you-on-object-and-something-happens) in C #, and I am considering the best structure for it. As you can imagine, different things can happen when you interact with an object: if it is a door, you expect to enter another room, if it is a person, you expect to start a conversation with them, etc. My idea is to achieve this behavior with delegates, for example:

public abstract class GameObject {
    public delegate void onAction();
    public onAction Click;
}

public class Door : GameObject {
    public Door() {
        Click = new onAction(ChangeRoom);
    }
    private void ChangeRoom() {
        //code to change room here
    }
}

public class Person : GameObject {
    public Person() {
        Click = new onAction(StartTalking);
    }
    private void StartTalking() {
        //code to display dialogue here
    }
}

, , , , :

specialObject.Click += new onAction(SpecialMethod);

. , , , SpecialMethod - , . (de), , , . ?

P.S.: , , , , , .

+5
5

, , , , . , , ... , , : D

, http://www.adventuregamestudio.co.uk/ . .

, , . " ". , , ..NET Framework , # . , #, .

, XML, , (), , , -. "", , . XML , - , .., .

+1

.

, script - (.. ), , .

# .

.NET. (, python) script . , script , . script ( ), (, ).

, , " GPU 6" ( 4). , , , . , , XML (, - question XML); , , , .

+3

SpecialMethod , , :

var medievalDoor = new Door();
medievalDoor.Click += OpenDoorNormally;

:

var starTrekDoor = new Door();
starTrekDoor.Click += SlideDoorUpward;

Click, . , DLL.

+1

, "" - , . , , , , , , , .

ISpecialBehaviorProvider ​​ ( , , ).

GameObject.Click , . , specialBehaviorProvider null, GameObject.Click 'in class', ISpecialBehaviorProvider.SpecialMethod.

, .

0

, , , , , - , .

, . BinaryFormatter , .

If possible, I would suggest simply setting up subtasks for your standard subsets and using polymorphism. If it has to be determined solely through data, you will essentially implement a scripting mechanism. Many of them are available in .NET. IronPython can be useful, although for games lua is pretty common (you should be able to use Lua.NET easily). Then you can simply add the optional script associated with the action and call the script (defined in the data).

0
source

All Articles