mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-10 02:23:23 +08:00
Add progress ball object code.
This commit is contained in:
@@ -17,12 +17,12 @@ BallObject::BallObject(glm::vec2 pos, float radius, glm::vec2 velocity, Texture2
|
|||||||
|
|
||||||
glm::vec2 BallObject::Move(float dt, unsigned int window_width)
|
glm::vec2 BallObject::Move(float dt, unsigned int window_width)
|
||||||
{
|
{
|
||||||
// If not stuck to player board
|
// if not stuck to player board
|
||||||
if (!this->Stuck)
|
if (!this->Stuck)
|
||||||
{
|
{
|
||||||
// Move the ball
|
// move the ball
|
||||||
this->Position += this->Velocity * dt;
|
this->Position += this->Velocity * dt;
|
||||||
// Then check if outside window bounds and if so, reverse velocity and restore at correct position
|
// then check if outside window bounds and if so, reverse velocity and restore at correct position
|
||||||
if (this->Position.x <= 0.0f)
|
if (this->Position.x <= 0.0f)
|
||||||
{
|
{
|
||||||
this->Velocity.x = -this->Velocity.x;
|
this->Velocity.x = -this->Velocity.x;
|
||||||
@@ -42,7 +42,7 @@ glm::vec2 BallObject::Move(float dt, unsigned int window_width)
|
|||||||
return this->Position;
|
return this->Position;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resets the ball to initial Stuck Position (if ball is outside window bounds)
|
// resets the ball to initial Stuck Position (if ball is outside window bounds)
|
||||||
void BallObject::Reset(glm::vec2 position, glm::vec2 velocity)
|
void BallObject::Reset(glm::vec2 position, glm::vec2 velocity)
|
||||||
{
|
{
|
||||||
this->Position = position;
|
this->Position = position;
|
||||||
|
|||||||
@@ -12,8 +12,8 @@
|
|||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "game_object.h"
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
#include "power_up.h"
|
|
||||||
|
|
||||||
|
|
||||||
// BallObject holds the state of the Ball object inheriting
|
// BallObject holds the state of the Ball object inheriting
|
||||||
@@ -23,16 +23,16 @@
|
|||||||
class BallObject : public GameObject
|
class BallObject : public GameObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// Ball state
|
// ball state
|
||||||
float Radius;
|
float Radius;
|
||||||
bool Stuck;
|
bool Stuck;
|
||||||
bool Sticky, PassThrough;
|
bool Sticky, PassThrough;
|
||||||
// Constructor(s)
|
// constructor(s)
|
||||||
BallObject();
|
BallObject();
|
||||||
BallObject(glm::vec2 pos, float radius, glm::vec2 velocity, Texture2D sprite);
|
BallObject(glm::vec2 pos, float radius, glm::vec2 velocity, Texture2D sprite);
|
||||||
// Moves the ball, keeping it constrained within the window bounds (except bottom edge); returns new position
|
// moves the ball, keeping it constrained within the window bounds (except bottom edge); returns new position
|
||||||
glm::vec2 Move(float dt, unsigned int window_width);
|
glm::vec2 Move(float dt, unsigned int window_width);
|
||||||
// Resets the ball to original state with given position and velocity
|
// resets the ball to original state with given position and velocity
|
||||||
void Reset(glm::vec2 position, glm::vec2 velocity);
|
void Reset(glm::vec2 position, glm::vec2 velocity);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
/******************************************************************
|
||||||
|
** 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) { }
|
||||||
|
|
||||||
|
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) { }
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
/*******************************************************************
|
||||||
|
** 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.
|
||||||
|
******************************************************************/
|
||||||
|
#ifndef BALLOBJECT_H
|
||||||
|
#define BALLOBJECT_H
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "game_object.h"
|
||||||
|
#include "texture.h"
|
||||||
|
|
||||||
|
|
||||||
|
// BallObject holds the state of the Ball object inheriting
|
||||||
|
// relevant state data from GameObject. Contains some extra
|
||||||
|
// functionality specific to Breakout's ball object that
|
||||||
|
// were too specific for within GameObject alone.
|
||||||
|
class BallObject : public GameObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// ball state
|
||||||
|
float Radius;
|
||||||
|
bool Stuck;
|
||||||
|
// constructor(s)
|
||||||
|
BallObject();
|
||||||
|
BallObject(glm::vec2 pos, float radius, glm::vec2 velocity, Texture2D sprite);
|
||||||
|
// moves the ball, keeping it constrained within the window bounds (except bottom edge); returns new position
|
||||||
|
glm::vec2 Move(float dt, unsigned int window_width);
|
||||||
|
// resets the ball to original state with given position and velocity
|
||||||
|
void Reset(glm::vec2 position, glm::vec2 velocity);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user