added usages of trim to lists
[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
a41bba6f 20#include <bear/Queue.h>
f72f4a59 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
a41bba6f
PG
32typedef bear::Queue<Ball*> queBall;
33typedef bear::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
a41bba6f
PG
64 s_startBalls.init();
65 s_mouseBalls.init();
66
67 s_startPolys.init();
68
423931a3
PG
69 pBall = addBall(Vector2(50, 50), 20, cWhite, s_startBalls);
70 pBall->mass = fStartMass;
6a40b627 71
423931a3
PG
72 pBall = addBall(Vector2(150, 50), 20, cGrey, s_startBalls);
73 pBall->mass = fStartMass;
6a40b627 74
423931a3
PG
75 pBall = addBall(Vector2(50, 100), 20, cRed, s_startBalls);
76 pBall->mass = fStartMass;
6a40b627 77
423931a3
PG
78 pBall = addBall(Vector2(100, 100), 20, cGreen, s_startBalls);
79 pBall->mass = fStartMass;
6a40b627 80
423931a3
PG
81 pBall = addBall(Vector2(150, 100), 20, cBlue, s_startBalls);
82 pBall->mass = fStartMass;
6a40b627 83
423931a3
PG
84 pBall = addBall(Vector2(50, 150), 20, cYellow, s_startBalls);
85 pBall->mass = fStartMass;
6a40b627 86
423931a3
PG
87 pBall = addBall(Vector2(100, 150), 20, cMagenta, s_startBalls);
88 pBall->mass = fStartMass;
6a40b627 89
423931a3
PG
90 pBall = addBall(Vector2(150, 150), 20, cCyan, s_startBalls);
91 pBall->mass = fStartMass;
6a40b627 92
598fa0d5 93
423931a3 94 for( int i = 0; i < 50; i++)
598fa0d5 95 {
423931a3 96 addBall(Vector2(200 + i * 2, 200 + i * 2), 10, cCyan);
598fa0d5 97 }
4a76d2e2 98
423931a3 99
4a76d2e2 100 vector<Vector2> points;
aa2791cf
PG
101 points.push_back(Vector2(500,500));
102 points.push_back(Vector2(300,500));
103 points.push_back(Vector2(500,300));
4a76d2e2 104
423931a3 105 addPoly(points, cRed, s_startPolys);
d227d586
PG
106
107
108 s_startPolys.trim();
109 s_startBalls.trim();
054d658f
PG
110}
111void creator::clean()
112{
a41bba6f
PG
113 removeAllPolys(s_startPolys);
114 s_startPolys.fini();
115
423931a3 116 removeAllBalls(s_mouseBalls);
a41bba6f 117 s_mouseBalls.fini();
423931a3 118
a41bba6f
PG
119 removeAllBalls(s_startBalls);
120 s_startBalls.fini();
054d658f
PG
121}
122
617dcc71
PG
123/// ***** Public Methods *****
124
88e62c4f 125void creator::addBall(const Vector2& pos, float radius, const float* color)
054d658f 126{
423931a3 127 addBall(pos, radius, color, s_mouseBalls);
10baa49b
PG
128}
129void creator::removeBall()
130{
423931a3 131 removeBall(s_mouseBalls);
10baa49b
PG
132}
133void creator::removeAllBalls()
134{
423931a3 135 removeAllBalls(s_mouseBalls);
10baa49b
PG
136}
137
138void creator::handleInput()
139{
140 if(input::mouseLeft())
141 addBall(input::mousePosition(), 10, cCyan);
142
a41bba6f 143 if(input::mouseRight() && ! s_mouseBalls.isEmpty())
10baa49b
PG
144 removeBall();
145}
146
147/// ***** Private Methods *****
148
423931a3
PG
149Ball* addBall(const Vector2& vecPos,
150 float fRadius,
151 const float* color,
152 queBall& que)
153{
154 Ball* pBall = new Ball(vecPos, fRadius, color);
155
a41bba6f 156 que.pushBack(pBall);
423931a3
PG
157 manager::add(pBall);
158
159 return pBall;
160}
161Polygon* addPoly(const vector<Vector2>& vecPoints,
162 const float* color,
163 quePoly& que)
10baa49b 164{
423931a3 165 Polygon* pPoly = new Polygon(vecPoints, color);
88e62c4f 166
a41bba6f 167 que.pushBack(pPoly);
423931a3 168 manager::add(pPoly);
10baa49b 169
423931a3 170 return pPoly;
054d658f 171}
10baa49b
PG
172
173void removeBall(queBall& que)
054d658f 174{
a41bba6f 175 Ball* pBall = que.getFront();
88e62c4f 176
423931a3 177 manager::remove(pBall);
a41bba6f 178 que.popFront();
88e62c4f 179
423931a3 180 delete pBall;
054d658f 181}
10baa49b 182void removeAllBalls(queBall& que)
054d658f 183{
a41bba6f 184 while(! que.isEmpty() )
f72f4a59 185 {
423931a3
PG
186 removeBall(que);
187 }
188}
054d658f 189
423931a3
PG
190static void removePoly(quePoly& que)
191{
a41bba6f 192 Polygon* pPoly = que.getFront();
423931a3
PG
193
194 manager::remove(pPoly);
a41bba6f 195 que.popFront();
423931a3
PG
196
197 delete pPoly;
198}
199static void removeAllPolys(quePoly& que)
200{
a41bba6f 201 while(! que.isEmpty() )
423931a3
PG
202 {
203 removePoly(que);
f72f4a59 204 }
054d658f 205}