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