#include "Engine.h"
//Screen Resolution properties for proper values among positioning for renders.
sf::Vector2f Screen = sf::Vector2f(800,600);
//************************Constructor****************************
Engine::Engine()
{
//
}
//************************Deconstructor****************************
Engine::~Engine()
{
//
}
//************************What is rendered upon initialization****************************
bool Engine::init()
{
//Point to the window and create one for our use.
window = new sf::RenderWindow(sf::VideoMode(Screen.x,Screen.y,32), "Pong");
//Define the player Shapes
defineShapes();
//if the window fails return a negative response
if(!window)
return false;
//else continue
return true;
}
//************************Determine what keys perform Events****************************
void Engine::ProcessEvents()
{
sf::Event event;
//perform Events while the window is open.
while(window->pollEvent(event))
{
switch(event.type)
{
//Upon the selection of the close buttons on a window Close the window
case sf::Event::Closed:
window->close();
break;
//upon a Key that is pressed close the window
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Escape)
window->close();
break;
}
}
//--------------------------Call upon player Functions---------------------------
//movement settings for the first rectangle on the Left
movePlayerOne();
//movement settings for the Second rectangle on the Right
movePlayerTwo();
//Determine Collison factor and move the ball
movCollision();
}
//************************Draw to the Screen [Update]****************************
void Engine::Update()
{
//clear the window
window->clear(sf::Color(40,40,40));
//draw the first rectangle on the Left
window->draw(*playerOne);
//draw the Second rectangle on the Right
window->draw(*playerTwo);
//draw the Ball in the Center
window->draw(*Ball);
}
//************************Window Display Settings****************************
void Engine::RenderFrame()
{
window->display();
}
//************************Determine the actions after Start****************************
void Engine::Main()
{
while(window->isOpen())
{
ProcessEvents();
Update();
RenderFrame();
}
}
//************************Start the Engine****************************
void Engine::Go()
{
//if it doesn't initialize send an error message
if(!init())
throw "Could not initialize game loop";
//continue to main loop
Main();
}
//************************Movement for Player One****************************
void Engine::movePlayerOne()
{
//set the speed of the player
int speed = 10000;
//Create a clock
sf::Clock clock;
//Restart the clock
clock.restart();
//if W is pressed on the keyboard move Up
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
playerOne->move(0,clock.getElapsedTime().asSeconds() * -speed);
}
//if S is pressed on the Keyboard move Down
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
playerOne->move(0,clock.getElapsedTime().asSeconds() * speed);
}
}
//************************Movement for Player Two****************************
void Engine::movePlayerTwo()
{
//set the speed of the player 2
int speed = 10000;
//Create another clock
sf::Clock clock;
//Restart the clock
clock.restart();
//if Up is pressed on the Keyboard Move Up
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
playerTwo->move(0,clock.getElapsedTime().asSeconds() * -speed);
}
//if Down is pressed on the Keyboard Move Down
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
playerTwo->move(0,clock.getElapsedTime().asSeconds() * speed);
}
}
//************************Begin Defining Shapes****************************
void Engine::defineShapes()
{
//Define Player One Rectangle
playerOne = new sf::RectangleShape();
//Set the Player One Rectangle Size
playerOne->setSize(sf::Vector2f(16,64));
//Set the Player One Rectangle Color
playerOne->setFillColor(sf::Color(255,255,255,255));
//Set the Player One Rectangle Position
playerOne->setPosition(sf::Vector2f(40,20));
//Define Player Two Rectangle
playerTwo = new sf::RectangleShape();
//Set the Player Two Rectangle Size
playerTwo->setSize(sf::Vector2f(16,64));
//Set the Player Two Rectangle Color
playerTwo->setFillColor(sf::Color(255,255,255,255));
//Set the Player Two Rectangle Position
playerTwo->setPosition(sf::Vector2f(740,520));
//Define Ball
Ball = new sf::RectangleShape();
//Set the Ball Size
Ball->setSize(sf::Vector2f(16,16));
//Set the Ball Color
Ball->setFillColor(sf::Color(0,255,0,255));
//Set the Ball Position
Ball->setPosition(740, 520);
}
//************************Begin Defining Collision for the Ball and Players****************************
void Engine::movCollision()
{
//Store Player Positions into Vectors
sf::Vector2f playerOnePosition = sf::Vector2f(playerOne->getPosition());
sf::Vector2f playerTwoPosition = sf::Vector2f(playerTwo->getPosition());
sf::Vector2f BallPosition = sf::Vector2f(Ball->getPosition());
//Define Rectangle around the players
sf::IntRect playerOneRect(playerOnePosition.x,playerOnePosition.y, playerOne->getSize().x, playerOne->getSize().y);
sf::IntRect playerTwoRect(playerTwoPosition.x,playerTwoPosition.y, playerTwo->getSize().x, playerTwo->getSize().y);
sf::IntRect BallRect(BallPosition.x,BallPosition.y, Ball->getSize().x, Ball->getSize().y);
//set the clock for speed
sf::Clock clock;
//Restart the clock
clock.restart();
//determine position and velocity.
sf::Vector2f pos;
sf::Vector2f velocity;
velocity.x = clock.getElapsedTime().asSeconds() * -12000;
velocity.y = clock.getElapsedTime().asSeconds() * -600;
//Detect Collison
bool collisionPlayerTwo = BallRect.intersects(playerTwoRect);
bool collisionPlayerOne = BallRect.intersects(playerOneRect);
//If Collision
if(collisionPlayerTwo && !collisionPlayerOne)
velocity.x = velocity.x;
if(collisionPlayerOne && !collisionPlayerTwo)
velocity.x = -velocity.x;
pos += velocity;
Ball->move(pos);
}