Collisions ball
This commit is contained in:
55
src/game.cpp
55
src/game.cpp
@@ -3,25 +3,33 @@
|
||||
|
||||
SpriteRenderer* Renderer;
|
||||
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
delete Renderer;
|
||||
}
|
||||
|
||||
Game::Game(unsigned int width, unsigned int height)
|
||||
: State(GAME_ACTIVE), Keys(), Width(width), Height(height)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Initial size of the player paddle
|
||||
const glm::vec2 PLAYER_SIZE (100.0f, 20.0f);
|
||||
// Initial velocity of the player paddle
|
||||
const float PLAYER_VELOCITY(500.0f);
|
||||
GameObject* player;
|
||||
|
||||
// Initial velocity of the Ball
|
||||
const glm::vec2 INITIAL_BALL_VELOCITY(100.0f, -350.0f);
|
||||
// Radius of the ball object
|
||||
const float BALL_RADIUS = 12.5f;
|
||||
|
||||
BallObject* ball;
|
||||
|
||||
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
delete Renderer;
|
||||
delete player;
|
||||
delete ball;
|
||||
}
|
||||
|
||||
Game::Game(unsigned int width, unsigned int height)
|
||||
: State(GAME_ACTIVE), Keys(), Width(width), Height(height)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Game::Init()
|
||||
{
|
||||
@@ -54,6 +62,10 @@ void Game::Init()
|
||||
this->Height - PLAYER_SIZE.y);
|
||||
player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle"));
|
||||
|
||||
glm::vec2 ballPos = playerPos + glm::vec2(PLAYER_SIZE.x / 2.0 - BALL_RADIUS, -BALL_RADIUS * 2.0f);
|
||||
|
||||
ball = new BallObject(ballPos, BALL_RADIUS, INITIAL_BALL_VELOCITY, ResourceManager::GetTexture("face"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -65,20 +77,36 @@ void Game::ProcessInput(float dt)
|
||||
if( this->Keys[GLFW_KEY_A])
|
||||
{
|
||||
if(player->Position.x >= 0.0f)
|
||||
{
|
||||
player->Position.x -= velocity;
|
||||
if(ball->Stuck)
|
||||
{
|
||||
ball->Position.x -= velocity;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if( this->Keys[GLFW_KEY_D])
|
||||
{
|
||||
if(player->Position.x <= this->Width - player->Size.x)
|
||||
{
|
||||
player->Position.x += velocity;
|
||||
if(ball->Stuck)
|
||||
{
|
||||
ball->Position.x += velocity;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (this->Keys[GLFW_KEY_SPACE])
|
||||
ball->Stuck = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Game::Update(float dt)
|
||||
{
|
||||
|
||||
ball->Move(dt, this->Width);
|
||||
}
|
||||
|
||||
void Game::Render()
|
||||
@@ -93,5 +121,6 @@ void Game::Render()
|
||||
this->Levels[this->Level].Draw(*Renderer);
|
||||
|
||||
player->Draw(*Renderer);
|
||||
ball->Draw(*Renderer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user