Thursday, April 14, 2016

c++ - Communication between objects in games?


I want to make a simple game where I have some characters fighting on a plane (level). I find the trickiest part as figuring out who should do what here.


I want my terrain to have friction (so you could play on ice).


I also want fairly customizable characters.


At first I was thinking of a mediator pattern where the game gathers information about the current player state and modifies them based on that, but I do not think this offers enough flexibility.


I was thinking more along the lines of each player / character does have some logic.


The Input from the game gives it the command the player pressed, then the character would load the correct animation for this.


As for friction and whatnot, I was thinking that the world would somehow need to multiply the player's velocity vector by its coefficient of friction. Because I don't think the player needs to know about the level (and preferably the level does not know about characters)


It's in these little design aspects that I get a bit confused.


Is this a good design? What could be improved / changed / added given what I want to do?



Thanks



Answer



Well, it's not how I would do it. Since the velocity of the character is a property of the character, I would attach that information to the character. In fact, I would have the character know both its position and velocity, Heisenberg be damned. If that means the character needs to "know about" the level, at least insofar as being able to ask what the coefficient of friction is at its position so as to be able to update its velocity, then so be it. Radical decoupling is a virtue only toward a particular purpose, not in and of itself.


I think you were on the right track to some extent with your mediator pattern, though. Pushing the control input directly to the character object and making it solely responsible for animation makes it awkward to, for example, have an action interrupted by external interference. I would make the character object responsible for modeling what it's attempting to do, while being open to being told that something else is actually happening by the mediator, and for providing animation information to a view which would do the actual management of the animation (rather than the character model doing so itself).


This isn't to say that I think all logic should live in the mediator. As implied above, I would make the character model responsible for logic that's about it. The mediator should be responsible for resolving things in a broader scope.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...