Question: Is having a master class bad practice? I am making a simple pong game, but I'm writing the code as if I were making for something that might be for something more complicated I want to create in the future. Also, am I not doing it in a good manner?
What I am doing: I have a class, in this case "engine", which basically holds everything and runs all of its referenced objects logic inside of its own functions. All of the other objects are then linked to each other by holding the data of the "world or engine" object and use it as a linker to other objects that it can interact with.
NOTE: The sfml media library was used with this project.
note: if you need the ball definitions i'll post them
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
#include <stdlib.h>
class paddle;
class ball;
class enemy;
class player;
class engine{
ball *gameBall;
enemy *enemyPaddle;
player *playerPaddle;
Clock gameClock;
RenderWindow &refWindow;
int gameSpeed;
Event windowEvent;
void eventDraw();
void eventUpdate();
void handleWindow();
public:
engine( RenderWindow &rendy);
~engine();
bool start();
Vector2f getWindowSize();
void drawPart(Drawable &art);
Time getGameClock(){ return gameClock.getElapsedTime(); }
};
class ball{
private:
CircleShape myShape;
engine& world;
int radius;
Color color;
Vector2f position, velocity;
short signed int dirX, dirY;
short signed int getDirection();
void wallBounce();
public:
ball( engine& gameEngine);
void draw();
CircleShape getCircle(){ return myShape; }
void pop();
void update();
};
int main()
{
RenderWindow window(VideoMode(640, 480), "Pong");
engine gameEngine( window );
return (int)gameEngine.start();
}
// Class Prototyes
// Engine
engine::engine(RenderWindow &rendy):refWindow(rendy){
gameBall = new ball(*this);
enemyPaddle = new enemy;
playerPaddle = new player;
gameClock = Clock();
gameSpeed = (1000/60.0);
windowEvent = Event();
}
engine::~engine(){
delete enemyPaddle;
delete playerPaddle;
delete gameBall;
}
void engine::handleWindow(){
if ( refWindow.pollEvent( windowEvent ) )
{
if ( windowEvent.type == Event::Closed )
refWindow.close();
}
}
bool engine::start()
{
while( refWindow.isOpen() )
{
while( gameClock.getElapsedTime().asMilliseconds() > gameSpeed )
{
eventUpdate();
eventDraw();
// Restart Clock
gameClock.restart();
}
handleWindow();
}
return true;
}
void engine::eventUpdate(){
gameBall->update();
}
void engine::eventDraw(){
refWindow.clear( Color::Black );
gameBall->draw();
refWindow.display();
}
Vector2f engine::getWindowSize(){
Vector2f tempVec;
tempVec.x = refWindow.getSize().x;
tempVec.y = refWindow.getSize().y;
return tempVec;
}
void engine::drawPart(Drawable &art){
refWindow.draw( art );
}