5 #include <algorithm> |
5 #include <algorithm> |
6 #include <functional> |
6 #include <functional> |
7 #include <cmath> |
7 #include <cmath> |
8 |
8 |
9 PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions) |
9 PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions) |
10 : tick_timer(PHYSICS_TICK_MS), gravity(gravity), dimensions(dimensions), terrain(dimensions.x, std::vector<TerrainType>(dimensions.y, EMPTY)) { |
10 : tick_timer(PHYSICS_TICK_MS), gravity(gravity), dimensions(dimensions), terrain(dimensions.x, std::vector<TerrainType>(dimensions.y, DIRT)) { |
11 |
11 |
12 generateTerrain(1337); |
12 generateTerrain(1337); |
13 |
13 |
14 slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick); |
14 slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick); |
15 tick_timer.enable(); |
15 tick_timer.enable(); |
194 srand(seed); |
194 srand(seed); |
195 |
195 |
196 // some constants to control random generation |
196 // some constants to control random generation |
197 const int min_range = 10; |
197 const int min_range = 10; |
198 const int max_range = 40; |
198 const int max_range = 40; |
199 const int num = 1; |
199 const int num = 30; |
200 const int rock_rarity = 4; // 1 / rock_rarity will be rock circle |
200 const int rock_rarity = 4; // 1 / rock_rarity will be rock circle |
201 |
201 |
202 // loops for amount of circles |
202 // loops for amount of circles |
203 for(int i = 0; i < num; i++) { |
203 for(int i = 0; i < num; i++) { |
204 // information of new circle |
204 // information of new circle |
207 int range = rand()%(max_range-min_range)+min_range; |
207 int range = rand()%(max_range-min_range)+min_range; |
208 |
208 |
209 // put first circle in the middle of the cave |
209 // put first circle in the middle of the cave |
210 // so that we have some area we can certainly spawn into |
210 // so that we have some area we can certainly spawn into |
211 if(i == 0) { |
211 if(i == 0) { |
212 midx = 60; |
212 midx = dimensions.x/2; |
213 midy = 60; |
213 midy = dimensions.y/2; |
214 range = 50; |
214 range = 150; |
215 } |
215 } |
216 |
216 |
217 TerrainType type = DIRT; |
217 TerrainType type = EMPTY; |
218 if(rand()%rock_rarity == 0) { |
218 if(rand()%rock_rarity == 0) { |
219 type = ROCK; |
219 type = ROCK; |
220 } |
220 } |
|
221 |
|
222 Engine::log(DEBUG, "PhysicsObject.generta") << "Dims: " << dimensions.x << " " << dimensions.y; |
221 // loops for every pixel of circle |
223 // loops for every pixel of circle |
222 for(int x = std::max(0, midx-range); x < std::min((int)dimensions.x, midx+range); x++) { |
224 for(int x = std::max(0, midx-range); x < std::min((int)dimensions.x, midx+range); x++) { |
223 for(int y = std::max(0, midy-range); y < std::min((int)dimensions.y, midy+range); y++) { |
225 for(int y = std::max(0, midy-range); y < std::min((int)dimensions.y, midy+range); y++) { |
224 if(x*x+y*y < range*range) { |
226 if((x-midx) * (x-midx) + (y-midy) * (y-midy) < range*range) { |
225 // and sets it to type |
227 // and sets it to type |
226 terrain[x][y] = type; |
228 terrain[x][y] = type; |
227 } |
229 } |
228 } |
230 } |
229 } |
231 } |