Basically it's a "computer" that handles the paddle and watches the ball to move where the ball is going.
When the ball reaches GetPosition().x >= 400, it will start to move to where it's going. It works going down, but when it's needed to go up, it doesn't move at all.
Paddle & gBall inherit sprite characteristics
dir is a variable used within gBall to find out which direction it is going.
2 = right down 3 = right up
class Computer
{
public:
Paddle * IControl;
gBall * IWatch;
float BPD;
void Set(Paddle * CPaddle, gBall * IWatchIt);
void SetBall(gBall * IWatchIt){ IWatch = IWatchIt;}
void SetPlayer(Paddle * CPaddle){ IControl = CPaddle;}
void play();
float crunch();
void move();
};
void Computer::play()
{
if ( IWatch->GetPosition().x > 400 && IWatch->dir == 3 )
{
BPD = crunch();
if ( BPD > 50)
move();
}
if ( IWatch->GetPosition().x > 400 && IWatch->dir == 3 )
{
BPD = crunch();
if ( BPD < -50)
move();
}
if (IWatch->GetPosition().x > 400 && IWatch->dir == 2 )
{
BPD = crunch();
if(BPD > 50)
move();
}
if (IWatch->GetPosition().x > 400 && IWatch->dir == 2 )
{
BPD = crunch();
if (BPD < -50)
move();
}
}
float Computer::crunch ()
{
float copy;
if (IWatch->dir == 3 && IControl->GetPosition().y <= 300.f)
copy = IWatch->GetPosition().y - IControl->GetPosition().y;
else if (IWatch->dir == 3 && IControl->GetPosition().y >= 300.f)
copy = IWatch->GetPosition().y - IControl->GetPosition().y;
else if (IWatch->dir == 2 && IControl->GetPosition().y <= 300.f)
copy = IWatch->GetPosition().y - IControl->GetPosition().y;
else if (IWatch->dir == 2 && IControl->GetPosition().y >= 300.f)
copy = IWatch->GetPosition().y - IControl->GetPosition().y;
std::cout << BPD << " " << IControl->GetPosition().y << " " << IWatch->GetPosition().y << " " << IWatch->dir << std::endl;
return copy;
}
void Computer::move ()
{
if(IWatch->dir == 3)
if (BPD >= (-50))
IControl->Move(0.f, -.5f); // doesn't work
if (BPD >= 50)
IControl->Move(0.f, .5f);
if(IWatch->dir == 2)
if (BPD >= (-50))
IControl->Move(0.f, -.5f); // doesn't work
if (BPD >= 50)
IControl->Move(0.f, .5f);
}