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