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