Commit | Line | Data |
---|---|---|
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 | 32 | typedef std::queue<Ball*> queBall; |
10baa49b PG |
33 | |
34 | queBall startBalls; | |
35 | queBall mouseBalls; | |
36 | ||
37 | /// ***** Private Method Headers ***** | |
38 | ||
39 | Ball* addBall(const Vector2& pos, | |
40 | float radius, | |
41 | const float* color, | |
42 | queBall& que); | |
43 | ||
44 | void removeBall(queBall& que); | |
45 | void removeAllBalls(queBall& que); | |
a483ed75 | 46 | |
617dcc71 | 47 | /// ***** Initializers/Cleaners ***** |
a483ed75 | 48 | |
054d658f PG |
49 | void creator::init() |
50 | { | |
6a40b627 | 51 | Ball* ball; |
e89e708a | 52 | float startMass = 5; |
6a40b627 PG |
53 | |
54 | ball = addBall(Vector2(50, 50), 20, cWhite, startBalls); | |
e89e708a | 55 | ball->mass = startMass; |
6a40b627 PG |
56 | |
57 | ball = addBall(Vector2(150, 50), 20, cGrey, startBalls); | |
e89e708a | 58 | ball->mass = startMass; |
6a40b627 PG |
59 | |
60 | ball = addBall(Vector2(50, 100), 20, cRed, startBalls); | |
e89e708a | 61 | ball->mass = startMass; |
6a40b627 PG |
62 | |
63 | ball = addBall(Vector2(100, 100), 20, cGreen, startBalls); | |
e89e708a | 64 | ball->mass = startMass; |
6a40b627 PG |
65 | |
66 | ball = addBall(Vector2(150, 100), 20, cBlue, startBalls); | |
e89e708a | 67 | ball->mass = startMass; |
6a40b627 PG |
68 | |
69 | ball = addBall(Vector2(50, 150), 20, cYellow, startBalls); | |
e89e708a | 70 | ball->mass = startMass; |
6a40b627 PG |
71 | |
72 | ball = addBall(Vector2(100, 150), 20, cMagenta, startBalls); | |
e89e708a | 73 | ball->mass = startMass; |
6a40b627 PG |
74 | |
75 | ball = addBall(Vector2(150, 150), 20, cCyan, startBalls); | |
e89e708a | 76 | ball->mass = startMass; |
598fa0d5 | 77 | |
3bccd1d7 | 78 | for( int i = 0; i<50; i++) |
598fa0d5 PG |
79 | { |
80 | addBall(Vector2(200+i*2, 200+i*2), 10, cCyan); | |
81 | } | |
4a76d2e2 PG |
82 | |
83 | // add a polygon into the mix (currently not cleaned up) | |
84 | vector<Vector2> points; | |
85 | points.push_back(Vector2(50,50)); | |
86 | points.push_back(Vector2(50,100)); | |
87 | points.push_back(Vector2(100,50)); | |
88 | ||
89 | manager::add(new Polygon(points)); | |
054d658f PG |
90 | } |
91 | void creator::clean() | |
92 | { | |
10baa49b PG |
93 | removeAllBalls(startBalls); |
94 | removeAllBalls(mouseBalls); | |
054d658f PG |
95 | } |
96 | ||
617dcc71 PG |
97 | /// ***** Public Methods ***** |
98 | ||
88e62c4f | 99 | void creator::addBall(const Vector2& pos, float radius, const float* color) |
054d658f | 100 | { |
10baa49b PG |
101 | addBall(pos, radius, color, mouseBalls); |
102 | } | |
103 | void creator::removeBall() | |
104 | { | |
105 | removeBall(mouseBalls); | |
106 | } | |
107 | void creator::removeAllBalls() | |
108 | { | |
109 | removeAllBalls(mouseBalls); | |
110 | } | |
111 | ||
112 | void creator::handleInput() | |
113 | { | |
114 | if(input::mouseLeft()) | |
115 | addBall(input::mousePosition(), 10, cCyan); | |
116 | ||
117 | if(input::mouseRight() && ! mouseBalls.empty()) | |
118 | removeBall(); | |
119 | } | |
120 | ||
121 | /// ***** Private Methods ***** | |
122 | ||
123 | Ball* addBall(const Vector2& pos, | |
124 | float radius, | |
125 | const float* color, | |
126 | queBall& que) | |
127 | { | |
88e62c4f PG |
128 | Ball* ball = new Ball(pos, radius, color); |
129 | ||
10baa49b | 130 | que.push(ball); |
88e62c4f | 131 | manager::add(ball); |
10baa49b PG |
132 | |
133 | return ball; | |
054d658f | 134 | } |
10baa49b PG |
135 | |
136 | void removeBall(queBall& que) | |
054d658f | 137 | { |
10baa49b | 138 | Ball* ball = que.front(); |
88e62c4f | 139 | |
10baa49b PG |
140 | manager::remove(ball); |
141 | que.pop(); | |
88e62c4f PG |
142 | |
143 | delete ball; | |
054d658f | 144 | } |
10baa49b | 145 | void removeAllBalls(queBall& que) |
054d658f | 146 | { |
10baa49b | 147 | while(! que.empty() ) |
f72f4a59 | 148 | { |
10baa49b PG |
149 | Ball* ball = que.front(); |
150 | que.pop(); | |
f72f4a59 | 151 | manager::remove(ball); |
054d658f | 152 | |
f72f4a59 PG |
153 | delete ball; |
154 | } | |
054d658f | 155 | } |