refactored entityCreator
[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 "Entities/Polygon.h"
27 #include "graphics/colors.h"
28
29
30 /// ***** Private Variables *****
31
32 typedef std::queue<Ball*> queBall;
33 typedef std::queue<Polygon*> quePoly;
34
35 static queBall s_startBalls;
36 static queBall s_mouseBalls;
37
38 static quePoly s_startPolys;
39
40 /// ***** Private Method Headers *****
41
42 static Ball* addBall(const Vector2& vecPos,
43                      float fRadius,
44                      const float* color,
45                      queBall& que);
46
47 static Polygon* addPoly(const vector<Vector2>& vecPoints,
48                         const float* color,
49                         quePoly& que);
50
51 static void removeBall(queBall& que);
52 static void removeAllBalls(queBall& que);
53
54 static void removePoly(quePoly& que);
55 static void removeAllPolys(quePoly& que);
56
57 /// ***** Initializers/Cleaners *****
58
59 void creator::init()
60 {
61     Ball* pBall;
62     float fStartMass = 5;
63
64     pBall = addBall(Vector2(50, 50), 20, cWhite, s_startBalls);
65     pBall->mass = fStartMass;
66
67     pBall = addBall(Vector2(150, 50), 20, cGrey, s_startBalls);
68     pBall->mass = fStartMass;
69
70     pBall = addBall(Vector2(50, 100), 20, cRed, s_startBalls);
71     pBall->mass = fStartMass;
72
73     pBall = addBall(Vector2(100, 100), 20, cGreen, s_startBalls);
74     pBall->mass = fStartMass;
75
76     pBall = addBall(Vector2(150, 100), 20, cBlue, s_startBalls);
77     pBall->mass = fStartMass;
78
79     pBall = addBall(Vector2(50, 150), 20, cYellow, s_startBalls);
80     pBall->mass = fStartMass;
81
82     pBall = addBall(Vector2(100, 150), 20, cMagenta, s_startBalls);
83     pBall->mass = fStartMass;
84
85     pBall = addBall(Vector2(150, 150), 20, cCyan, s_startBalls);
86     pBall->mass = fStartMass;
87
88
89     for( int i = 0; i < 50; i++)
90     {
91         addBall(Vector2(200 + i * 2, 200 + i * 2), 10, cCyan);
92     }
93
94
95     vector<Vector2> points;
96     points.push_back(Vector2(500,500));
97     points.push_back(Vector2(300,500));
98     points.push_back(Vector2(500,300));
99
100     addPoly(points, cRed, s_startPolys);
101 }
102 void creator::clean()
103 {
104     removeAllBalls(s_startBalls);
105     removeAllBalls(s_mouseBalls);
106
107     removeAllPolys(s_startPolys);
108 }
109
110 /// ***** Public Methods *****
111
112 void creator::addBall(const Vector2& pos, float radius, const float* color)
113 {
114     addBall(pos, radius, color, s_mouseBalls);
115 }
116 void creator::removeBall()
117 {
118     removeBall(s_mouseBalls);
119 }
120 void creator::removeAllBalls()
121 {
122     removeAllBalls(s_mouseBalls);
123 }
124
125 void creator::handleInput()
126 {
127     if(input::mouseLeft())
128         addBall(input::mousePosition(), 10, cCyan);
129
130     if(input::mouseRight() && ! s_mouseBalls.empty())
131         removeBall();
132 }
133
134 /// ***** Private Methods *****
135
136 Ball* addBall(const Vector2& vecPos,
137               float fRadius,
138               const float* color,
139               queBall& que)
140 {
141     Ball* pBall = new Ball(vecPos, fRadius, color);
142
143     que.push(pBall);
144     manager::add(pBall);
145
146     return pBall;
147 }
148 Polygon* addPoly(const vector<Vector2>& vecPoints,
149                  const float* color,
150                  quePoly& que)
151 {
152     Polygon* pPoly = new Polygon(vecPoints, color);
153
154     que.push(pPoly);
155     manager::add(pPoly);
156
157     return pPoly;
158 }
159
160 void removeBall(queBall& que)
161 {
162     Ball* pBall = que.front();
163
164     manager::remove(pBall);
165     que.pop();
166
167     delete pBall;
168 }
169 void removeAllBalls(queBall& que)
170 {
171     while(! que.empty() )
172     {
173         removeBall(que);
174     }
175 }
176
177 static void removePoly(quePoly& que)
178 {
179     Polygon* pPoly = que.front();
180
181     manager::remove(pPoly);
182     que.pop();
183
184     delete pPoly;
185 }
186 static void removeAllPolys(quePoly& que)
187 {
188     while(! que.empty() )
189     {
190         removePoly(que);
191     }
192 }