Thread: Level Editor in C++ / OOP Design

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User nathandelane's Avatar
    Join Date
    Dec 2010
    Posts
    9

    Thumbs up

    Quote Originally Posted by \007 View Post
    I wasn't thinking of renaming Sprites as movable-- just in my head I thought an Object and Sprite are essentially the same thing. The only difference is a Sprite can be moved so needs to handle input. On the other hand a Tile is the same as an Object but the difference is a Tile doesn't need to be moved so doesn't need to have input handled.
    Sorry that I wasn't very clear. I was not thinking that you should rename your Sprites to Movable. I was just thinking that you should create a Movable interface and have your Sprite class inherit that interface. Not knowing exactly what methods a "Movable" object would have, I made up my own:
    Code:
    class IMovable
    {
    public:
    	void MoveTo(struct Coordinates position) = 0;
    	struct Coordinates GetPosition() = 0;
    };
    That's probably over-simplifying. Maybe you would really have MoveLeft, MoveUp, MoveRight, and MoveDown methods. At any rate this is just an interface. You then need to implement it in your Sprite class:
    Code:
    class Sprite : public IMovable
    {
    private:
            struct Coordinates position;
    public:
            Sprite(struct Coordinates startPosition);
            ~Sprite();
        /* IMovable implementation */
            void MoveTo(struct Coordinates position);
            struct Coordinates GetPosition();
    };
    
    Sprite::Sprite(struct Coordinates startPosition)
    {
            this->position = startPosition;
    }
    
    Sprite::~Sprite()
    {
    }
    
    void Sprite::MoveTo(struct Coordinates position)
    {
            this->position = position;
    }
    
    struct Coordinates Sprite::GetPosition()
    {
            return this->position;
    }
    Then when you collect a bunch of objects that are movable, you check for their inheritance of the IMovable interface:
    Code:
    ...
    	std::vector<IMovable *> sprites;
    ...
    and you'll always know that the MoveTo and GetPosition methods will be part of those objects. This is how you program against an interface.

    Quote Originally Posted by \007 View Post
    I guess I was over complicating my designs?
    I don't think that you're over-complicating your designs. Just know that a simpler interface on the front usually requires more complex thinking and design patterns on the back.

    Quote Originally Posted by \007 View Post
    How do you help manage your designs while expanding your application? I pretty much hit the end of my initial design plans and now I am expanding things, in other words I am winging it now.
    Most of the time when I am expanding my application, if I need new things, maybe that I didn't think of before, I have need of refactoring quite a bit. Sometimes things have gotten too messy in the past, and I must virtually start over. It is always best to sit and think about designs and plan them out as much as possible before you touch a line of code. Also if you haven't studied design patterns, then I would suggest you do. There are several good books, and Wikipedia has a series on them. Not all of them are applicable everywhere, obviously. And try not to misuse them - some people misuse certain patterns especially. But overall, design patterns result in cleaner, more understandable, and more correct, performant code.
    Last edited by nathandelane; 04-05-2011 at 10:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Side-scroller Level Design
    By jmd15 in forum Game Programming
    Replies: 5
    Last Post: 04-25-2007, 02:48 PM
  2. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  3. OOP design question
    By codec in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2004, 02:48 PM
  4. OOP Design
    By filler_bunny in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2003, 05:37 AM
  5. design a game level
    By SuperNewbie in forum Windows Programming
    Replies: 3
    Last Post: 06-06-2002, 03:42 PM