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