1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/*
===========================================================================
blockattack - Block Attack - Rise of the Blocks
Copyright (C) 2005-2017 Poul Sander
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/
Source information and contacts persons can be found at
http://www.blockattack.net
===========================================================================
*/
#ifndef BALLMANAGER_HPP
#define BALLMANAGER_HPP
#include <array>
extern GlobalData globalData;
//other ball constants:
const double gravity = 200.8; //acceleration
const double startVelocityY = 50.0;
const double VelocityX = 50.0;
const int ballSize = 16;
const double minVelocity = 200.0;
//The small things that are faaling when you clear something
class ABall {
private:
double x = 0.0;
double y = 0.0;
double velocityY = 0.0;
double velocityX = 0.0;
int color = 0;
unsigned long int lastTime = 0;
public:
bool inUse = false;
ABall() {
}
//constructor:
ABall(int X, int Y, bool right, int coulor) {
double tal = 1.0+((double)rand()/((double)RAND_MAX));
velocityY = -tal*startVelocityY;
lastTime = SDL_GetTicks();
x = (double)X;
y = (double)Y;
color = coulor;
if (right) {
velocityX = tal*VelocityX;
}
else {
velocityX = -tal*VelocityX;
}
} //constructor
void update() {
double timePassed = (((double)(SDL_GetTicks()-lastTime))/1000.0); //time passed in seconds
x = x+timePassed*velocityX;
y = y+timePassed*velocityY;
velocityY = velocityY + gravity*timePassed;
if (y<1.0) {
velocityY=10.0;
}
if ((velocityY>minVelocity) && (y>(globalData.ysize-ballSize)) && (y<globalData.ysize)) {
velocityY = -0.70*velocityY;
y = globalData.ysize-ballSize;
}
lastTime = SDL_GetTicks();
}
int getX() {
return (int)x;
}
int getY() {
return (int)y;
}
int getColor() {
return color;
}
}; //aBall
class BallManager {
static const int maxNumberOfBalls = 6*12*2*2;
public:
std::array<ABall, maxNumberOfBalls> ballArray;
BallManager() {
}
//Adds a ball to the screen at given coordiantes, traveling right or not with color
int addBall(int x, int y,bool right,int color) {
size_t ballNumber = 0;
//Find a free ball
while (ballNumber<ballArray.size() && ballArray[ballNumber].inUse) {
ballNumber++;
}
//Could not find a free ball, return -1
if (ballNumber==ballArray.size()) {
return -1;
}
ballArray[ballNumber] = ABall(x,y,right,color);
ballArray[ballNumber].inUse = true;
return 1;
} //addBall
void update() {
for (size_t i = 0; i<ballArray.size(); i++) {
if (ballArray[i].inUse) {
ballArray[i].update();
if (ballArray[i].getY()>globalData.ysize+100 || ballArray[i].getX()>globalData.xsize || ballArray[i].getX()<-ballSize) {
ballArray[i].inUse = false;
}
}
}
} //update
}; //theBallManager
#endif /* BALLMANAGER_HPP */
|