OOP Design help

Hi. I'm trying to rewrite a game i'm working on to be more object oriented in design. I'm wondering if i should write all the code in functions in the class, and also a bigger class function to run all smaller class functions depending if certain if cases match etc or should i put those smaller if statements outside the class and not use a bigger function if i'm making any sense.

Edit: Basically i'm wondering if all code should go into class functions or should i write some logic outside of the class function.

For example if i make a function in class called fireBullet();
Should i write some logic outside of the class for example:
1
2
3
if (mouseClick){
   fireBullet();
}


or should i also write that in a class function?
Last edited on
Definitely put that logic outside the class. That way you can also do
1
2
3
if (user pressed 'f' key) {
    fireBullet();
}


Don't write methods that do the work. Write methods that make doing the work easy.

If you think of class design this way you'll create classes that are more general and easier to use.
I would prefer following structure:
[InputHandler class recieves mouse clicked event][InputHandler tells Player to fire bullet <somewhere>][Player creates Bullet in specific position moving in specific direction with specific speed][Bullet handles moving and collision by itself]

In this example input handler does not know much about player class. It knows that he can fire a bullet somewhere and that is all. Player class does not know how fast bullet is flying, its damage and other inner workings. He knows that he can shoot bullets of this specifeic type. Bullet does not know who fired it, and does not care about it. It flies forward seeking obstacle it can damage. Everything is decoupled and is easy to modify. For example to make Player fire rockets, you can just switch pointer to Bullet with pointer to Rocket (assuming they all derived from Projectile class)
To make AI controller monsters, you can just switch InputHandler to AI module, etc.

Also it is good to have classes and methods follow the rule of single responsibility. For example method to insert new value into queue should not also print current content of queue. Method which solves quadratic equation should return pair of results instead of outputting them right away. That way your methods could be easily reused in different situations.
Last edited on
Thanks guys, that made it clearer for me. I am working a bit on it now and will get back to you with a code example and see if it's correct.
Well i was going to post what i came up with so far. It was more work than i thought because of complications when changing alot of code, but i can't post it because of posting restrictions (Max 8192 length) :/. Thanks for your help anyway, i think i made some improvements on my code.
Topic archived. No new replies allowed.