gravity corrected, mass changed to 5 for start balls
[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
PG
25#include "Entities/Ball.h"
26#include "graphics/colors.h"
27
10baa49b 28
617dcc71
PG
29/// ***** Private Variables *****
30
f72f4a59 31typedef std::queue<Ball*> queBall;
10baa49b
PG
32
33queBall startBalls;
34queBall mouseBalls;
35
36/// ***** Private Method Headers *****
37
38Ball* addBall(const Vector2& pos,
39 float radius,
40 const float* color,
41 queBall& que);
42
43void removeBall(queBall& que);
44void removeAllBalls(queBall& que);
a483ed75 45
617dcc71 46/// ***** Initializers/Cleaners *****
a483ed75 47
054d658f
PG
48void creator::init()
49{
6a40b627 50 Ball* ball;
e89e708a 51 float startMass = 5;
6a40b627
PG
52
53 ball = addBall(Vector2(50, 50), 20, cWhite, startBalls);
e89e708a 54 ball->mass = startMass;
6a40b627
PG
55
56 ball = addBall(Vector2(150, 50), 20, cGrey, startBalls);
e89e708a 57 ball->mass = startMass;
6a40b627
PG
58
59 ball = addBall(Vector2(50, 100), 20, cRed, startBalls);
e89e708a 60 ball->mass = startMass;
6a40b627
PG
61
62 ball = addBall(Vector2(100, 100), 20, cGreen, startBalls);
e89e708a 63 ball->mass = startMass;
6a40b627
PG
64
65 ball = addBall(Vector2(150, 100), 20, cBlue, startBalls);
e89e708a 66 ball->mass = startMass;
6a40b627
PG
67
68 ball = addBall(Vector2(50, 150), 20, cYellow, startBalls);
e89e708a 69 ball->mass = startMass;
6a40b627
PG
70
71 ball = addBall(Vector2(100, 150), 20, cMagenta, startBalls);
e89e708a 72 ball->mass = startMass;
6a40b627
PG
73
74 ball = addBall(Vector2(150, 150), 20, cCyan, startBalls);
e89e708a 75 ball->mass = startMass;
054d658f
PG
76}
77void creator::clean()
78{
10baa49b
PG
79 removeAllBalls(startBalls);
80 removeAllBalls(mouseBalls);
054d658f
PG
81}
82
617dcc71
PG
83/// ***** Public Methods *****
84
88e62c4f 85void creator::addBall(const Vector2& pos, float radius, const float* color)
054d658f 86{
10baa49b
PG
87 addBall(pos, radius, color, mouseBalls);
88}
89void creator::removeBall()
90{
91 removeBall(mouseBalls);
92}
93void creator::removeAllBalls()
94{
95 removeAllBalls(mouseBalls);
96}
97
98void creator::handleInput()
99{
100 if(input::mouseLeft())
101 addBall(input::mousePosition(), 10, cCyan);
102
103 if(input::mouseRight() && ! mouseBalls.empty())
104 removeBall();
105}
106
107/// ***** Private Methods *****
108
109Ball* addBall(const Vector2& pos,
110 float radius,
111 const float* color,
112 queBall& que)
113{
88e62c4f
PG
114 Ball* ball = new Ball(pos, radius, color);
115
10baa49b 116 que.push(ball);
88e62c4f 117 manager::add(ball);
10baa49b
PG
118
119 return ball;
054d658f 120}
10baa49b
PG
121
122void removeBall(queBall& que)
054d658f 123{
10baa49b 124 Ball* ball = que.front();
88e62c4f 125
10baa49b
PG
126 manager::remove(ball);
127 que.pop();
88e62c4f
PG
128
129 delete ball;
054d658f 130}
10baa49b 131void removeAllBalls(queBall& que)
054d658f 132{
10baa49b 133 while(! que.empty() )
f72f4a59 134 {
10baa49b
PG
135 Ball* ball = que.front();
136 que.pop();
f72f4a59 137 manager::remove(ball);
054d658f 138
f72f4a59
PG
139 delete ball;
140 }
054d658f 141}