cleaned how balls are added
[physics.git] / src / entityCreator.cpp
1 /*
2  *  Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "entityCreator.h"
19
20 #include <queue>
21
22 #include "entityManager.h"
23 #include "input/inputManager.h"
24
25 #include "Entities/Ball.h"
26 #include "graphics/colors.h"
27
28
29 /// ***** Private Variables *****
30
31 typedef std::queue<Ball*> queBall;
32
33 queBall startBalls;
34 queBall mouseBalls;
35
36 /// ***** Private Method Headers *****
37
38 Ball* addBall(const Vector2& pos,
39               float radius,
40               const float* color,
41               queBall& que);
42
43 void removeBall(queBall& que);
44 void removeAllBalls(queBall& que);
45
46 /// ***** Initializers/Cleaners *****
47
48 void creator::init()
49 {
50     addBall(Vector2(50, 50), 20, cWhite, startBalls);
51     addBall(Vector2(150, 50), 20, cGrey, startBalls);
52     addBall(Vector2(50, 100), 20, cRed, startBalls);
53     addBall(Vector2(100, 100), 20, cGreen, startBalls);
54     addBall(Vector2(150, 100), 20, cBlue, startBalls);
55     addBall(Vector2(50, 150), 20, cYellow, startBalls);
56     addBall(Vector2(100, 150), 20, cMagenta, startBalls);
57     addBall(Vector2(150, 150), 20, cCyan, startBalls);
58 }
59 void creator::clean()
60 {
61     removeAllBalls(startBalls);
62     removeAllBalls(mouseBalls);
63 }
64
65 /// ***** Public Methods *****
66
67 void creator::addBall(const Vector2& pos, float radius, const float* color)
68 {
69     addBall(pos, radius, color, mouseBalls);
70 }
71 void creator::removeBall()
72 {
73     removeBall(mouseBalls);
74 }
75 void creator::removeAllBalls()
76 {
77     removeAllBalls(mouseBalls);
78 }
79
80 void creator::handleInput()
81 {
82     if(input::mouseLeft())
83         addBall(input::mousePosition(), 10, cCyan);
84
85     if(input::mouseRight() && ! mouseBalls.empty())
86         removeBall();
87 }
88
89 /// ***** Private Methods *****
90
91 Ball* addBall(const Vector2& pos,
92                        float radius,
93                        const float* color,
94                        queBall& que)
95 {
96     Ball* ball = new Ball(pos, radius, color);
97
98     que.push(ball);
99     manager::add(ball);
100
101     return ball;
102 }
103
104 void removeBall(queBall& que)
105 {
106     Ball* ball = que.front();
107
108     manager::remove(ball);
109     que.pop();
110
111     delete ball;
112 }
113 void removeAllBalls(queBall& que)
114 {
115     while(! que.empty() )
116     {
117         Ball* ball = que.front();
118         que.pop();
119         manager::remove(ball);
120
121         delete ball;
122     }
123 }