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 | 82 | |
aa2791cf | 83 | // HACK |
4a76d2e2 PG |
84 | // add a polygon into the mix (currently not cleaned up) |
85 | vector<Vector2> points; | |
aa2791cf PG |
86 | points.push_back(Vector2(500,500)); |
87 | points.push_back(Vector2(300,500)); | |
88 | points.push_back(Vector2(500,300)); | |
4a76d2e2 | 89 | |
30a93c39 | 90 | manager::add(new Polygon(points, cRed)); |
054d658f PG |
91 | } |
92 | void creator::clean() | |
93 | { | |
10baa49b PG |
94 | removeAllBalls(startBalls); |
95 | removeAllBalls(mouseBalls); | |
054d658f PG |
96 | } |
97 | ||
617dcc71 PG |
98 | /// ***** Public Methods ***** |
99 | ||
88e62c4f | 100 | void creator::addBall(const Vector2& pos, float radius, const float* color) |
054d658f | 101 | { |
10baa49b PG |
102 | addBall(pos, radius, color, mouseBalls); |
103 | } | |
104 | void creator::removeBall() | |
105 | { | |
106 | removeBall(mouseBalls); | |
107 | } | |
108 | void creator::removeAllBalls() | |
109 | { | |
110 | removeAllBalls(mouseBalls); | |
111 | } | |
112 | ||
113 | void creator::handleInput() | |
114 | { | |
115 | if(input::mouseLeft()) | |
116 | addBall(input::mousePosition(), 10, cCyan); | |
117 | ||
118 | if(input::mouseRight() && ! mouseBalls.empty()) | |
119 | removeBall(); | |
120 | } | |
121 | ||
122 | /// ***** Private Methods ***** | |
123 | ||
124 | Ball* addBall(const Vector2& pos, | |
125 | float radius, | |
126 | const float* color, | |
127 | queBall& que) | |
128 | { | |
88e62c4f PG |
129 | Ball* ball = new Ball(pos, radius, color); |
130 | ||
10baa49b | 131 | que.push(ball); |
88e62c4f | 132 | manager::add(ball); |
10baa49b PG |
133 | |
134 | return ball; | |
054d658f | 135 | } |
10baa49b PG |
136 | |
137 | void removeBall(queBall& que) | |
054d658f | 138 | { |
10baa49b | 139 | Ball* ball = que.front(); |
88e62c4f | 140 | |
10baa49b PG |
141 | manager::remove(ball); |
142 | que.pop(); | |
88e62c4f PG |
143 | |
144 | delete ball; | |
054d658f | 145 | } |
10baa49b | 146 | void removeAllBalls(queBall& que) |
054d658f | 147 | { |
10baa49b | 148 | while(! que.empty() ) |
f72f4a59 | 149 | { |
10baa49b PG |
150 | Ball* ball = que.front(); |
151 | que.pop(); | |
f72f4a59 | 152 | manager::remove(ball); |
054d658f | 153 | |
f72f4a59 PG |
154 | delete ball; |
155 | } | |
054d658f | 156 | } |