| 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 EXPLOSIONMANAGER_HPP |
| 25 | #define EXPLOSIONMANAGER_HPP |
| 26 | |
| 27 | #include <array> |
| 28 | |
| 29 | //a explosions, non moving |
| 30 | class AnExplosion { |
| 31 | private: |
| 32 | int x = 0; |
| 33 | int y = 0; |
| 34 | Uint8 frameNumber = 0; |
| 35 | #define frameLength 80 |
| 36 | //How long an image in an animation should be showed |
| 37 | #define maxFrame 4 |
| 38 | //How many images there are in the animation |
| 39 | unsigned long int placeTime = 0; //Then the explosion occored |
| 40 | public: |
| 41 | bool inUse = false; |
| 42 | |
| 43 | AnExplosion() { |
| 44 | } |
| 45 | |
| 46 | //constructor: |
| 47 | AnExplosion(int X, int Y) { |
| 48 | placeTime = SDL_GetTicks(); |
| 49 | x = X; |
| 50 | y = Y; |
| 51 | frameNumber=0; |
| 52 | } //constructor |
| 53 | |
| 54 | //true if animation has played and object should be removed from the screen |
| 55 | bool removeMe() { |
| 56 | frameNumber = (SDL_GetTicks()-placeTime)/frameLength; |
| 57 | return (!(frameNumber<maxFrame)); |
| 58 | } |
| 59 | |
| 60 | int getX() { |
| 61 | return (int)x; |
| 62 | } |
| 63 | |
| 64 | int getY() { |
| 65 | return (int)y; |
| 66 | } |
| 67 | |
| 68 | int getFrame() { |
| 69 | return frameNumber; |
| 70 | } |
| 71 | }; //nExplosion |
| 72 | |
| 73 | class ExplosionManager { |
| 74 | static const int maxNumberOfExplosions = 6*12*2*2; |
| 75 | public: |
| 76 | std::array<AnExplosion, maxNumberOfExplosions> explosionArray; |
| 77 | |
| 78 | ExplosionManager() { |
| 79 | } |
| 80 | |
| 81 | int addExplosion(int x, int y) { |
| 82 | size_t explosionNumber = 0; |
| 83 | while ( explosionNumber<explosionArray.size() && explosionArray[explosionNumber].inUse) { |
| 84 | explosionNumber++; |
| 85 | } |
| 86 | if (explosionNumber==explosionArray.size()) { |
| 87 | return -1; |
| 88 | } |
| 89 | explosionArray[explosionNumber] = AnExplosion(x,y); |
| 90 | explosionArray[explosionNumber].inUse = true; |
| 91 | return 1; |
| 92 | } //addBall |
| 93 | |
| 94 | void update() { |
| 95 | for (size_t i = 0; i<explosionArray.size(); i++) { |
| 96 | |
| 97 | if (explosionArray[i].inUse) { |
| 98 | if (explosionArray[i].removeMe()) { |
| 99 | explosionArray[i].inUse = false; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } //update |
| 104 | |
| 105 | |
| 106 | }; //explosionManager |
| 107 | |
| 108 | #endif /* EXPLOSIONMANAGER_HPP */ |
| 109 |