I need some advice on how to design the Entity module in my game, how to apply the MVC pattern and generally how the Entity should interact with its controller and its representation.
First some details about the game: it's a 2D action-platformer, it's data-driven (or rather, I'd like it to be data-driven — it isn't yet) and the Entities should be scriptable (I'll be using Python).
I'm still at the very beginning of the development and I have some general ideas about how the Entity module should work, but I get stuck on the details. Here's roughly what I have up until now:
class Entity
{
private:
// The representation holds the actual animation and sounds that
// are used to render the entity. The representation can read the
// Entity's state but it cannot alter it.
EntityRepresentation* representation_;
// The controller handles events and changes the Entity's state
// based on these events.
EntityController* controller_;
// Here's some of Entity's attributes
struct Vector{ float x, y; } position_;
float directionRadians_;
float speed_;
public:
void handleEvent(const Event& e) {
controller_->handleEvent(e, this); // It's obvious what it does :)
// The representation needs to handle events so it can change the
// current animation, queue up a sound for playing, etc. In my
// current setup it doesn't yet need to take 'this' as a parameter.
representation_->handleEvent(e);
}
void update(unsigned long timePassed) {
position_.x += speed_ * timePassed * std::cos(directionRadians_);
position_.y += speed_ * timePassed * std::sin(directionRadians_);
// .. And any other updates I might need to do...
}
void render(DrawingContext* context, unsigned long timePassed) {
representation_->render(context, timePassed, position_.x, position_.y);
}
};
There are a few things I think could be problematic in this setup. For one, if you look at the Entity::handleEvent()
function you will see that the Entity forwards the event to the controller as well as to the representation. The controller uses the event to alter the state of the Entity; the representation however uses it to alter its own state (to use some other animation, to decide whether it should play a sound, etc.).
This means the representation has a state of its own which is characteristic to this Entity only. Do you think it's bad that each representation can only be used for one Entity? (Note: this does not mean that in case there are two entities using the same sprite, that sprite is loaded twice in memory: the sprite itself is a common resource.)
Another thing I'm having problems with the deciding how to organize the actual entity data on disk. Where should the bounding box info be read from: the animation or somewhere separate? should there be a single animation file for each character? should scripts be separate from the data or should everything related to one type of entity be stored in a single folder (scripts and animation and sounds)?
tl;dr Can you give some some general advice on how the Entity should interact with its controller and its representation (i.e. sprites and sounds). Also, do you have any advice on how the data itself should be organized on the hard-drive?