This repository was archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWorld.cpp
More file actions
136 lines (111 loc) · 2.8 KB
/
Copy pathGameWorld.cpp
File metadata and controls
136 lines (111 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* GameWorld.cpp
*
* Created on: Dec 3, 2015
* Author: andres
*/
#include "GameWorld.h"
GameWorld::GameWorld(list<Entity*>* entities) {
srand(time(NULL));
this->entities = entities;
this->gameover = false;
this->gameStarted = false;
this->paused = false;
this->asteroidCount = 0;
this->maxAsteroids = 0;
this->screenW = VideoMode::getDesktopMode().width;
this->screenH = VideoMode::getDesktopMode().height;
}
GameWorld::~GameWorld() {
// TODO Auto-generated destructor stub
}
void GameWorld::pause()
{
if(gameStarted && !gameover)
{
paused = !paused;
Entity::paused = !Entity::paused;
}
}
void GameWorld::spawnAsteroids()
{
if(asteroidCount < maxAsteroids && !gameover)
{
/*Position*/
float posX, posY;
//If true posX:[0,screenW] and Y is either 0 or screenH
//If false posY:[0,screenY] and X is either 0 or screenW
if(rand()%2)
{
posX = rand()%((int)screenW);
posY = screenH*(rand()%2);
}
else
{
posY = rand()%((int)screenH);
posX = screenW*(rand()%2);
}
/*Scale size*/
float scale = (1.f/(rand()%4 + 2.f));
/*Asteroid image*/
float asteroidNum = rand()%4 + 1;
ostringstream textureKey;
textureKey << "Asteroid" << asteroidNum;
/*Force*/
float forceX = pow(-1, rand()%2)*(rand()%100 + 10);
float forceY = pow(-1, rand()%2)*(rand()%100 + 10);
//Makes sure that the force is always applied into the screen
if((posX >= screenW/2.0f && forceX >= 0) || (posX < screenW/2.0f && forceX < 0) )
forceX*=-1;
if((posY >= screenH/2.0f && forceY >= 0) || (posY < screenH/2.0f && forceY < 0))
forceY*=-1;
b2Vec2 force(forceX, forceY);
Asteroid* asteroid = new Asteroid(posX, posY, scale, textureKey.str());
asteroid->getBody()->ApplyForce(force, asteroid->getBody()->GetWorldCenter(), true);
entities->push_back(asteroid);
asteroidCount++;
}
}
void GameWorld::clean()
{
for(list<Entity*>::iterator i = entities->begin(); i != entities->end(); i++)
{
Entity* entity = *i;
if(entity->getType() == "Asteroid" || entity->getType() == "Bullet")
{
float posX = entity->getPosition().x;
float posY = entity->getPosition().y;
if(posX < 0)
posX*=-1;
if(posY < 0)
posY*=-1;
if(posX > screenW + 1000 || posY > screenH + 1000)
entity->deleteEntity();
}
if(entity->getType() == "Player")
{
Player* player = (Player*)entity;
if(player->hp == 0)
{
ofstream out("scores.orb", ostream::in);
if(!out.is_open())
out.open("scores.orb");
out.seekp(0, ios::end);
int saves = out.tellp()/4.0;
out.seekp(saves*4);
out.write((char*)&(player->points), 4);
gameover = true;
out.close();
player->deleteEntity();
gameover = true;
}
}
if(entity->deleteFlag)
{
if(entity->getType() == "Asteroid")
asteroidCount--;
entities->erase(i++);
delete entity;
}
}
}