commit d450da8b
Also started to move the BallManager and ExplosionManager outside
Changed files
| A | source/code/BallManager.hpp |
| A | source/code/ExplosionManager.hpp |
| M | source/code/global.hpp before |
| M | source/code/main.cpp before |
| M | source/code/mainVars.inc before |
diff --git a/source/code/BallManager.hpp b/source/code/BallManager.hpp
new file mode 100644
index 0000000..0a65675
--- /dev/null
+++ b/source/code/BallManager.hpp
@@ -0,0 +1,137 @@
+/*
+===========================================================================
+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
+
+
+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
+
+static const int maxNumberOfBalls = 6*12*2*2;
+
+class BallManager {
+public:
+ ABall ballArray[maxNumberOfBalls];
+
+ 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) {
+ int ballNumber = 0;
+ //Find a free ball
+ while (ballNumber<maxNumberOfBalls && ballArray[ballNumber].inUse) {
+ ballNumber++;
+ }
+ //Could not find a free ball, return -1
+ if (ballNumber==maxNumberOfBalls) {
+ return -1;
+ }
+ ballArray[ballNumber] = ABall(x,y,right,color);
+ ballArray[ballNumber].inUse = true;
+ return 1;
+ } //addBall
+
+ void update() {
+ for (int i = 0; i<maxNumberOfBalls; 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 */
+
diff --git a/source/code/ExplosionManager.hpp b/source/code/ExplosionManager.hpp
new file mode 100644
index 0000000..67a4590
--- /dev/null
+++ b/source/code/ExplosionManager.hpp
@@ -0,0 +1,107 @@
+/*
+===========================================================================
+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 EXPLOSIONMANAGER_HPP
+#define EXPLOSIONMANAGER_HPP
+
+//a explosions, non moving
+class AnExplosion {
+private:
+ int x = 0;
+ int y = 0;
+ Uint8 frameNumber = 0;
+#define frameLength 80
+ //How long an image in an animation should be showed
+#define maxFrame 4
+ //How many images there are in the animation
+ unsigned long int placeTime = 0; //Then the explosion occored
+public:
+ bool inUse = false;
+
+ AnExplosion() {
+ }
+
+ //constructor:
+ AnExplosion(int X, int Y) {
+ placeTime = SDL_GetTicks();
+ x = X;
+ y = Y;
+ frameNumber=0;
+ } //constructor
+
+ //true if animation has played and object should be removed from the screen
+ bool removeMe() {
+ frameNumber = (SDL_GetTicks()-placeTime)/frameLength;
+ return (!(frameNumber<maxFrame));
+ }
+
+ int getX() {
+ return (int)x;
+ }
+
+ int getY() {
+ return (int)y;
+ }
+
+ int getFrame() {
+ return frameNumber;
+ }
+}; //nExplosion
+
+class ExplosionManager {
+public:
+ static const int maxNumberOfExplosions = 6*12*2*2;
+ AnExplosion explosionArray[maxNumberOfExplosions];
+
+ ExplosionManager() {
+ }
+
+ int addExplosion(int x, int y) {
+ int explosionNumber = 0;
+ while ( explosionNumber<maxNumberOfExplosions && explosionArray[explosionNumber].inUse) {
+ explosionNumber++;
+ }
+ if (explosionNumber==maxNumberOfExplosions) {
+ return -1;
+ }
+ explosionArray[explosionNumber] = AnExplosion(x,y);
+ explosionArray[explosionNumber].inUse = true;
+ return 1;
+ } //addBall
+
+ void update() {
+ for (int i = 0; i<maxNumberOfExplosions; i++) {
+
+ if (explosionArray[i].inUse) {
+ if (explosionArray[i].removeMe()) {
+ explosionArray[i].inUse = false;
+ }
+ }
+ }
+ } //update
+
+
+}; //explosionManager
+
+#endif /* EXPLOSIONMANAGER_HPP */
+
diff --git a/source/code/global.hpp b/source/code/global.hpp
index 9b44be0..b7f47e4 100644
--- a/source/code/global.hpp
+++ b/source/code/global.hpp
@@ -30,6 +30,7 @@ http://www.blockattack.net
#include "highscore.h"
#include "sago/GameStateInterface.hpp"
#include "TextManager.hpp"
+#include "ExplosionManager.hpp"
void MainMenu();
void ResetFullscreen();
@@ -80,6 +81,8 @@ struct GlobalData {
int mousey = 0;
};
+#include "BallManager.hpp"
+
extern GlobalData globalData;
#endif /* _GLOBAL_HPP */
diff --git a/source/code/main.cpp b/source/code/main.cpp
index 5b5a67e..32f0cc3 100644
--- a/source/code/main.cpp
+++ b/source/code/main.cpp
@@ -302,189 +302,8 @@ void UpdateMouseCoordinates(const SDL_Event& event, int& mousex, int& mousey) {
}
}
-//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 = currentTime;
- x = (double)X;
- y = (double)Y;
- color = coulor;
- if (right) {
- velocityX = tal*VelocityX;
- }
- else {
- velocityX = -tal*VelocityX;
- }
- } //constructor
-
- void update() {
- double timePassed = (((double)(currentTime-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 = currentTime;
- }
-
- int getX() {
- return (int)x;
- }
-
- int getY() {
- return (int)y;
- }
-
- int getColor() {
- return color;
- }
-}; //aBall
-
-static const int maxNumberOfBalls = 6*12*2*2;
-
-class BallManager {
-public:
- ABall ballArray[maxNumberOfBalls];
-
- 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) {
- int ballNumber = 0;
- //Find a free ball
- while (ballNumber<maxNumberOfBalls && ballArray[ballNumber].inUse) {
- ballNumber++;
- }
- //Could not find a free ball, return -1
- if (ballNumber==maxNumberOfBalls) {
- return -1;
- }
- currentTime = SDL_GetTicks();
- ballArray[ballNumber] = ABall(x,y,right,color);
- ballArray[ballNumber].inUse = true;
- return 1;
- } //addBall
-
- void update() {
- currentTime = SDL_GetTicks();
- for (int i = 0; i<maxNumberOfBalls; 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
-
static BallManager theBallManager;
-//a explosions, non moving
-class AnExplosion {
-private:
- int x = 0;
- int y = 0;
- Uint8 frameNumber = 0;
-#define frameLength 80
- //How long an image in an animation should be showed
-#define maxFrame 4
- //How many images there are in the animation
- unsigned long int placeTime = 0; //Then the explosion occored
-public:
- bool inUse = false;
-
- AnExplosion() {
- }
-
- //constructor:
- AnExplosion(int X, int Y) {
- placeTime = currentTime;
- x = X;
- y = Y;
- frameNumber=0;
- } //constructor
-
- //true if animation has played and object should be removed from the screen
- bool removeMe() {
- frameNumber = (currentTime-placeTime)/frameLength;
- return (!(frameNumber<maxFrame));
- }
-
- int getX() {
- return (int)x;
- }
-
- int getY() {
- return (int)y;
- }
-
- int getFrame() {
- return frameNumber;
- }
-}; //nExplosion
-
-class ExplosionManager {
-public:
- AnExplosion explosionArray[maxNumberOfBalls];
-
- ExplosionManager() {
- }
-
- int addExplosion(int x, int y) {
- int explosionNumber = 0;
- while ( explosionNumber<maxNumberOfBalls && explosionArray[explosionNumber].inUse) {
- explosionNumber++;
- }
- if (explosionNumber==maxNumberOfBalls) {
- return -1;
- }
- currentTime = SDL_GetTicks();
- explosionArray[explosionNumber] = AnExplosion(x,y);
- explosionArray[explosionNumber].inUse = true;
- return 1;
- } //addBall
-
- void update() {
- currentTime = SDL_GetTicks();
- for (int i = 0; i<maxNumberOfBalls; i++) {
-
- if (explosionArray[i].inUse) {
- if (explosionArray[i].removeMe()) {
- explosionArray[i].inUse = false;
- }
- }
- }
- } //update
-
-
-}; //explosionManager
-
static ExplosionManager theExplosionManager;
diff --git a/source/code/mainVars.inc b/source/code/mainVars.inc
index 947745b..447cfd9 100644
--- a/source/code/mainVars.inc
+++ b/source/code/mainVars.inc
@@ -117,12 +117,7 @@ static bool drawBalls; //if true balls are drawed to the screen, thi
static bool editorMode = false;
static bool editorModeTest = false;
-//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;
+
//global settings (reset everytime the game starts)
static Uint8 player1Speed=0;