Player atingido por lanca chamas nao fica mais girando feito doido
[Projeto-PCG.git] / game.cpp
bloba4bf722f5085c1845764466e5d39b76ce9b13efa
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->screen["width"], config->screen["height"], config->screen["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->screen["width"];
91 int height = config->screen["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 glMatrixMode( GL_PROJECTION );
114 glLoadIdentity();
115 glOrtho( camera.x, camera.x+width, camera.y+height, camera.y, -2, 2 );
116 SDL_GL_SwapBuffers();
119 void Game::reloadLua() {
120 config->load();
121 resize(config->screen["width"], config->screen["height"]);
122 loadMap(config->maps.front());
123 weaponManager->loadWeapons();
124 shotManager->clearShots();
125 player->equip(weaponManager->getWeapon("Shotgun"));
128 void Game::setSpawn(Ponto spawn) {
129 this->spawn = spawn;
132 void Game::resize(GLsizei x, GLsizei y) {
133 config->screen["width"] = x;
134 config->screen["height"] = y;
135 SDL_SetVideoMode(x, y, config->screen["bpp"], SDL_OPENGL | SDL_RESIZABLE);
136 glViewport(0, 0, x, y);
139 WeaponItem* Game::dropWeapon(std::string name) {
140 return weaponManager->getItem(name);
143 void Game::spawnEnemy(std::string name, Ponto position) {
144 Enemy* enemy = enemyManager->createEnemy(name);
145 enemy->setPosition(position.x,position.y);
148 void Game::mainLoop() {
149 Timer fps;
150 shotManager = new ShotManager;
151 weaponManager = new WeaponManager(this);
152 collisionManager = new CollisionManager;
153 weaponManager->loadWeapons();
154 enemyManager = new EnemyManager(this);
155 enemyManager->loadEnemies();
156 loadMap(config->maps.front());
158 player = new Player(this, spawn, config->player["speed"]);
159 Controle *c;
160 c = new ControleWii(*player);
161 player->equip(weaponManager->getWeapon("Shotgun"));
163 collisionManager->subscribe(player);
164 std::list<WeaponItem*>::iterator itW;
165 for (itW = mapa->items.begin(); itW != mapa->items.end(); itW++) {
166 collisionManager->subscribe(*itW);
167 gravityManager->subscribe(*itW);
170 bool quit = false;
171 rate = 1.0;
172 while (!quit) {
173 int ifps = config->screen["fps"];
174 fps.start();
175 //player events
176 c->handleEvents();
177 enemyManager->think();
179 //collision, gravity
180 collisionManager->update();
181 gravityManager->update();
183 player->animate();
184 enemyManager->animate();
186 //movements
187 player->move();
188 shotManager->move();
189 enemyManager->move();
190 mapa->move();
191 quit = c->getQuit();
192 show();
193 rate = ((double)fps.get_ticks())/ifps;
194 if (rate < 1.0)
195 rate = 1.0;
196 if (fps.get_ticks() < 1000 / ifps ) {
197 SDL_Delay( ( 1000 / ifps ) - fps.get_ticks() );
200 SDL_Quit();