Killing In The Name

Published December 21, 2009
Advertisement
So, the xmas holidays have finally started for me [grin]. Unfortunately, I've got some assignments to do over these holidays. So in between doing those and relaxing after a busy term there won't be much time left for game development.

However, the next step I want to take is to rewrite Cops & Robbers from scratch as I was having a problem with playing animations, which I think is from not having a well structured system in place. I've seen a couple of things in various articles that I want to implement. (Some rather simple like having everything derive from an Entity class which will save me writing a lot of GetX(), GetY() GetImage() type functions [rolleyes]).

So, in the free time I do have I'm going to plan Cops & Robbers properly so that come the Easter holidays I can program it. I may post my plan as I go along to get any advice from your kind selves about obvious problems that I may not see.

Anyway, thanks for reading.
-AEdmonds
Previous Entry One Month Off
0 likes 2 comments

Comments

Aardvajk
Good to see you back in time for Christmas [smile]. Just a thought...

If you ever find yourself writing a lot of Get() methods, regardless of whether they are inherited from a base class, it is always a good time to consider whether you are doing the work in the right place.

Rather than:


class Entity
{
private:
    int x,y,image;

public:
    int GetX() const { return x; }
    int GetY() const { return y; }
    int GetImage() const { return image; }
};

void AnimateAndDraw(GraphicsDevice &g,Entity &e)
{
    int x=e.GetX();
    int y=e.GetY();

    int i=e.GetImage();

    i=UpdateAnimationSomehow(i); // or whatever

    g.Draw(x,y,i);
}



You might like to consider:


class Animation
{
public:
    void Update();
    int CurrentImage();
};

class Entity // pure virtual interface describing actions
{
public:
    virtual void Update()=0;
    virtual void Render(GraphicsDevice &g)=0;
};

class Robber : public Entity
{
private:
    float x,y;
    Animation a;

public:
    virtual void Update(){ a.Update(); }
    virtual void Render(GraphcisDevice &g){ g.Draw(x,y,a.CurrentImage()); }
};

typedef std::vector<Entity*> Entities;
Entities e;

void Init()
{
    e.push_back(new Robber());
}

void Update()
{
    for(Entities::iterator i=e.begin();i!=e.end();++i) i->Update();
}

void Render(GraphicsDevice &g)
{
    for(Entities::iterator i=e.begin();i!=e.end();++i) i->Render(g);
}



You should find this creates a lot more flexible system - for example concrete classes can choose how to render themselves - but using composition by giving a Robber and Animation allows you to still reuse the same animation code quite easily.

Whenever you find you need member functions describing properties, consider if they can be re-written to describe actions. Useful rule of thumb.
December 21, 2009 11:27 AM
AEdmonds
Thanks very much Aardvajk [smile]. That is a very useful piece of information that I will try to keep in mind.
December 21, 2009 12:52 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement