4 #include "../wall/wall.h" 5 #include "../block/block.h" 6 #include "../../math/vector2D.h" 17 Ball::Ball() : notMoving(true), random(
Random::getInstance()) {}
19 Ball::Ball(
double x,
double y,
double newSpeed, pair<double, double>
size) : velocity(newSpeed, -newSpeed), origin(x,y), speed(newSpeed), notMoving(true), spedUp(false), random(
Random::getInstance()),
Entity(x, y, size) {}
35 invisDuration = period;
42 vector<int> collisions;
43 for(
int e = 0; e < entities.size(); e++) {
45 collisions.push_back(e);
49 if(!collisions.empty()) {
50 if(collisions.size() == 2) {
55 if(entities[collisions[0]]->
getPosition().
x == entities[collisions[1]]->getPosition().x) {
58 }
else if(entities[collisions[0]]->
getPosition().
y == entities[collisions[1]]->getPosition().y) {
65 }
else if(collisions.size() == 1) {
69 const double gapRight = abs((
position.
x +
size.first) - entities[collisions[0]]->getPosition().x);
70 const double gapLeft = abs(
position.
x - (entities[collisions[0]]->getPosition().x + entities[collisions[0]]->getSize().first));
71 const double gapTop = abs(
position.
y - (entities[collisions[0]]->getPosition().y + entities[collisions[0]]->getSize().second));
72 const double gapBottom = abs((
position.
y +
size.second) - entities[collisions[0]]->getPosition().y);
75 const double minGapHor = min(gapRight, gapLeft);
76 const double minGapVer = min(gapTop, gapBottom);
79 if(minGapHor < minGapVer) {
96 template vector<int> Ball::bounceIfPossible<Block>(vector<unique_ptr<Block>>
const &entities);
97 template vector<int> Ball::bounceIfPossible<Wall>(vector<unique_ptr<Wall>>
const &entities);
103 const double maxPlayerPos = player->getPosition().x + player->getSize().first;
104 const double playerLength = maxPlayerPos - player->getPosition().x;
105 const double halfLength = playerLength / 2;
106 const double centerPlayer = maxPlayerPos - (playerLength / 2);
110 if(collisionPoint > maxPlayerPos) {
111 collisionPoint = maxPlayerPos;
113 collisionPoint = player->getPosition().x;
115 const double relativeCollision = collisionPoint - player->getPosition().x;
119 if(collisionPoint > centerPlayer) {
121 ratio = (relativeCollision - halfLength) / halfLength;
122 angle = 90.0 - (ratio * 70.0);
126 ratio = (halfLength - relativeCollision) / halfLength;
127 angle = 90.0 + (ratio * 70.0);
131 velocity.
x = speed * cos((angle + random->
randomDouble(-5, 5)) * (M_PI/180));
132 velocity.
y = -1 * speed * sin((angle + random->
randomDouble(-5, 5)) * (M_PI/180));
Vector2D position
The current position of the Entity (in the 9x7 grid).
bool collidesWith(const Entity &other) const
Vector2D getPosition() const
vector< int > bounceIfPossible(vector< unique_ptr< T >> const &entities)
double randomDouble(int from, int to)
void speedUp(double factor)
double y
The 'y' component of the Vector2D.
Singleton: generates "random" numbers.
double x
The 'x' component of the Vector2D.
All the game logic of the Arkanoid game.
An Entity represents an "object" (like Player, Ball, Block, ...) in the game World.
void setInvisible(int period)
pair< double, double > size
The size (width and height respectively) of the Entity.