Ajuste leve na velocidade permitindo movimentos mais sutis
[Projeto-PCG.git] / game.cpp
blob1bcee94214c0bd9e098a256c0725842e513b5d11
1 #include "game.h"
2 #include "geometry.h"
3 #include "luaenv.h"
4 #include "timer.h"
5 #include "controlewii.h"
6 #include "shotmanager.h"
7 #include "weaponitem.h"
8 #include "enemy.h"
10 void Game::loadMap(std::string mapname) {
11 if (mapa != NULL)
12 delete mapa;
13 mapa = new Mapa(mapname,this);
16 void Game::addPlatform(Platform* plat) {
17 gravityManager->addPlatform(plat);
20 void Game::removePlatforms() {
21 gravityManager->removePlatforms();
24 bool init_GL() {
25 //Set clear color
26 glClearColor( 1, 1, 1, 0 );
28 //Initialize modelview matrix
29 glMatrixMode( GL_MODELVIEW );
30 glLoadIdentity();
32 //Z-buffer
33 glEnable(GL_DEPTH_TEST);
34 glDepthFunc(GL_LEQUAL);
36 //Linhas
37 glLineWidth(2.5);
38 glEnable(GL_LINE_SMOOTH);
39 glEnable(GL_BLEND);
40 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
41 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
43 //If there was any errors
44 if( glGetError() != GL_NO_ERROR )
46 return false;
49 //If everything initialized
50 return true;
53 Game::Game(ConfigManager *cfg) {
54 gravityManager = new GravityManager;
55 mapa = NULL;
56 player = NULL;
57 config = cfg;
58 //Initialize SDL
59 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
61 //erro
64 //Create Window
65 if( SDL_SetVideoMode( config->integer["width"], config->integer["height"], config->integer["bpp"], SDL_OPENGL | SDL_RESIZABLE ) == NULL )
67 //erro
70 //Initialize OpenGL
71 if( !init_GL() )
73 //erro
76 //Set caption
77 SDL_WM_SetCaption( "Big Stick", NULL );
80 void Game::desenhaMira(Ponto aim) {
81 glPushMatrix();
82 glTranslatef(aim.x,aim.y,0);
83 drawCircle(10,10);
84 glPopMatrix();
87 void Game::show() {
88 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
89 double x, y;
90 int width = config->integer["width"];
91 int height = config->integer["height"];
93 if (player->getX() <= width/2)
94 x = 0;
95 else if (player->getX() >= mapa->xmax() - width/2)
96 x = mapa->xmax() - width;
97 else
98 x = player->getX() - width/2;
100 if (player->getY() <= 2*height/3)
101 y = 0;
102 else if (player->getY() >= mapa->ymax() - height/3)
103 y = mapa->ymax() - height;
104 else
105 y = player->getY() - 2*height/3;
106 camera.x = x;
107 camera.y = y;
108 player->desenha();
109 mapa->desenha();
110 shotManager->desenha();
111 enemyManager->desenha();
113 double vida = std::max(0.0,player->hp/double(config->integer["hp"]));
115 glPushMatrix();
116 glTranslatef(camera.x,camera.y,0);
117 glColor3f(0,0,0);
118 glBegin(GL_LINE_LOOP);
119 glVertex3f(10,10,0);
120 glVertex3f(50,10,0);
121 glVertex3f(50,500,0);
122 glVertex3f(10,500,0);
123 glEnd();
124 glColor3f(1,0,0);
125 glBegin(GL_POLYGON);
126 glVertex3f(10+1,10+1,0);
127 glVertex3f(50-1,10+1,0);
128 glVertex3f(50-1,std::max(vida*(500-1)-1,10.0),0);
129 glVertex3f(10+1,std::max(vida*(500-1)-1,10.0),0);
130 glEnd();
131 glColor3f(0,0,0);
132 glPopMatrix();
134 glMatrixMode( GL_PROJECTION );
135 glLoadIdentity();
136 glOrtho( camera.x, camera.x+width, camera.y+height, camera.y, -2, 2 );
137 SDL_GL_SwapBuffers();
140 void Game::reloadLua() {
141 config->load();
142 resize(config->integer["width"], config->integer["height"]);
143 loadMap(config->currentMap());
144 weaponManager->loadWeapons();
145 shotManager->clearShots();
146 player->equip(weaponManager->getWeapon("Shotgun"));
149 void Game::setSpawn(Ponto spawn) {
150 this->spawn = spawn;
153 void Game::resize(GLsizei x, GLsizei y) {
154 config->integer["width"] = x;
155 config->integer["height"] = y;
156 SDL_SetVideoMode(x, y, config->integer["bpp"], SDL_OPENGL | SDL_RESIZABLE);
157 glViewport(0, 0, x, y);
160 WeaponItem* Game::dropWeapon(std::string name) {
161 return weaponManager->getItem(name);
164 void Game::spawnEnemy(std::string name, Ponto position) {
165 Enemy* enemy = enemyManager->createEnemy(name);
166 enemy->setPosition(position.x,position.y);
169 void Game::previousMap() {
170 enemyManager->loadEnemies();
171 shotManager->clearShots();
172 loadMap(config->previousMap());
173 player->setPosition(spawn.x, spawn.y);
176 void Game::reloadMap(bool respawn) {
177 enemyManager->loadEnemies();
178 shotManager->clearShots();
179 loadMap(config->currentMap());
180 if (respawn)
181 player->setPosition(spawn.x, spawn.y);
184 void Game::nextMap() {
185 enemyManager->loadEnemies();
186 shotManager->clearShots();
187 loadMap(config->nextMap());
188 player->setPosition(spawn.x, spawn.y);
191 void Game::mainLoop() {
192 Timer fps;
193 shotManager = new ShotManager;
194 weaponManager = new WeaponManager(this);
195 collisionManager = new CollisionManager;
196 weaponManager->loadWeapons();
197 enemyManager = new EnemyManager(this);
198 enemyManager->loadEnemies();
199 loadMap(config->currentMap());
201 player = new Player(this, spawn, config->ponto["speed"]);
202 Controle *c;
203 c = new ControleWii(*player);
204 player->equip(weaponManager->getWeapon("Shotgun"));
206 collisionManager->subscribe(player);
207 std::list<WeaponItem*>::iterator itW;
208 for (itW = mapa->items.begin(); itW != mapa->items.end(); itW++) {
209 collisionManager->subscribe(*itW);
210 gravityManager->subscribe(*itW);
213 bool quit = false;
214 rate = 1.0;
215 while (!quit) {
216 int ifps = config->integer["fps"];
217 fps.start();
219 if (!enemyManager->hasEnemies())
220 nextMap();
222 //player events
223 c->handleEvents();
224 enemyManager->think();
226 //collision, gravity
227 collisionManager->update();
228 gravityManager->update();
230 player->animate();
231 enemyManager->animate();
233 //movements
234 player->move();
235 shotManager->move();
236 enemyManager->move();
237 mapa->move();
238 quit = c->getQuit();
239 show();
240 rate = ((double)fps.get_ticks())/ifps;
241 if (rate < 1.0)
242 rate = 1.0;
243 if (fps.get_ticks() < 1000 / ifps ) {
244 SDL_Delay( ( 1000 / ifps ) - fps.get_ticks() );
247 SDL_Quit();