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