mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-11 02:53:23 +08:00
Add full source code of the finished (and revised code) Breakout example. Not set up for cross-platform compilation as it has irrKlang and FreeType dependency, but at least source code is available online (and can be referenced from Breakout chapters).
This commit is contained in:
54
src/7.in_practice/3.2d_game/0.full_source/ball_object.cpp
Normal file
54
src/7.in_practice/3.2d_game/0.full_source/ball_object.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/******************************************************************
|
||||
** This code is part of Breakout.
|
||||
**
|
||||
** Breakout is free software: you can redistribute it and/or modify
|
||||
** it under the terms of the CC BY 4.0 license as published by
|
||||
** Creative Commons, either version 4 of the License, or (at your
|
||||
** option) any later version.
|
||||
******************************************************************/
|
||||
#include "ball_object.h"
|
||||
|
||||
|
||||
BallObject::BallObject()
|
||||
: GameObject(), Radius(12.5f), Stuck(true), Sticky(false), PassThrough(false) { }
|
||||
|
||||
BallObject::BallObject(glm::vec2 pos, float radius, glm::vec2 velocity, Texture2D sprite)
|
||||
: GameObject(pos, glm::vec2(radius * 2.0f, radius * 2.0f), sprite, glm::vec3(1.0f), velocity), Radius(radius), Stuck(true), Sticky(false), PassThrough(false) { }
|
||||
|
||||
glm::vec2 BallObject::Move(float dt, unsigned int window_width)
|
||||
{
|
||||
// If not stuck to player board
|
||||
if (!this->Stuck)
|
||||
{
|
||||
// Move the ball
|
||||
this->Position += this->Velocity * dt;
|
||||
// Then check if outside window bounds and if so, reverse velocity and restore at correct position
|
||||
if (this->Position.x <= 0.0f)
|
||||
{
|
||||
this->Velocity.x = -this->Velocity.x;
|
||||
this->Position.x = 0.0f;
|
||||
}
|
||||
else if (this->Position.x + this->Size.x >= window_width)
|
||||
{
|
||||
this->Velocity.x = -this->Velocity.x;
|
||||
this->Position.x = window_width - this->Size.x;
|
||||
}
|
||||
if (this->Position.y <= 0.0f)
|
||||
{
|
||||
this->Velocity.y = -this->Velocity.y;
|
||||
this->Position.y = 0.0f;
|
||||
}
|
||||
}
|
||||
return this->Position;
|
||||
}
|
||||
|
||||
// Resets the ball to initial Stuck Position (if ball is outside window bounds)
|
||||
void BallObject::Reset(glm::vec2 position, glm::vec2 velocity)
|
||||
{
|
||||
this->Position = position;
|
||||
this->Velocity = velocity;
|
||||
this->Stuck = true;
|
||||
this->Sticky = false;
|
||||
this->PassThrough = false;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user