Acho que consegui corrigi a ponta das armas
[Projeto-PCG.git] / game.cpp
blob9fb40b772813a14e7cc68f5f4d7ba0728c7845b8
1 #include "game.h"
2 #include "geometry.h"
3 #include "luaenv.h"
4 #include "timer.h"
5 #include "controleteclado.h"
6 #include "controlewii.h"
7 #include "shotmanager.h"
9 void Game::loadMap(std::string mapname) {
10 if (mapa != NULL)
11 delete mapa;
12 mapa = new Mapa(mapname,this);
15 void Game::addPlatform(Platform* plat) {
16 gravityManager->addPlatform(plat);
19 void Game::removePlatforms() {
20 gravityManager->removePlatforms();
23 bool init_GL() {
24 //Set clear color
25 glClearColor( 1, 1, 1, 0 );
27 //Initialize modelview matrix
28 glMatrixMode( GL_MODELVIEW );
29 glLoadIdentity();
31 //Linhas
32 glLineWidth(2.5);
33 glEnable(GL_LINE_SMOOTH);
34 glEnable(GL_BLEND);
35 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
36 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
38 //If there was any errors
39 if( glGetError() != GL_NO_ERROR )
41 return false;
44 //If everything initialized
45 return true;
48 Game::Game(ConfigManager *cfg) {
49 gravityManager = new GravityManager;
50 mapa = NULL;
51 player = NULL;
52 config = cfg;
53 //Initialize SDL
54 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
56 //erro
59 //Create Window
60 if( SDL_SetVideoMode( config->screen["width"], config->screen["height"], config->screen["bpp"], SDL_OPENGL ) == NULL )
62 //erro
65 //Initialize OpenGL
66 if( !init_GL() )
68 //erro
71 //Set caption
72 SDL_WM_SetCaption( "Prototipo Jogo", NULL );
75 void Game::desenhaMira(Ponto aim) {
76 glPushMatrix();
77 glTranslatef(camera.x+aim.x,camera.y+aim.y,0);
78 drawCircle(10,10);
79 glPopMatrix();
82 void Game::show() {
83 glClear( GL_COLOR_BUFFER_BIT );
84 double x, y;
85 int width = config->screen["width"];
86 int height = config->screen["height"];
88 if (player->getX() <= width/2)
89 x = 0;
90 else if (player->getX() >= mapa->xmax() - width/2)
91 x = mapa->xmax() - width;
92 else
93 x = player->getX() - width/2;
95 if (player->getY() <= 2*height/3)
96 y = 0;
97 else if (player->getY() >= mapa->ymax() - height/3)
98 y = mapa->ymax() - height;
99 else
100 y = player->getY() - 2*height/3;
101 camera.x = x;
102 camera.y = y;
103 player->desenha();
104 mapa->desenha();
105 shotManager->desenha();
107 glMatrixMode( GL_PROJECTION );
108 glLoadIdentity();
109 glOrtho( camera.x, camera.x+width, camera.y+height, camera.y, -1, 1 );
110 SDL_GL_SwapBuffers();
113 void Game::reloadLua() {
114 config->load();
115 loadMap(config->maps.front());
116 weaponManager->loadWeapons();
117 shotManager->clearShots();
118 player->equip(weaponManager->getWeapon("Shotgun"));
121 void Game::setSpawn(Ponto spawn) {
122 this->spawn = spawn;
125 void Game::mainLoop() {
126 Timer fps;
128 loadMap(config->maps.front());
130 player = new Player(this, spawn, config->player["speed"]);
131 Controle *c;
132 if (true)
133 c = new ControleTeclado(*player);
134 else
135 c = new ControleWii(*player);
137 shotManager = new ShotManager;
138 weaponManager = new WeaponManager(this);
139 collisionManager = new CollisionManager;
140 weaponManager->loadWeapons();
141 player->equip(weaponManager->getWeapon("Shotgun"));
143 collisionManager->subscribe(player);
144 bool quit = false;
145 while (!quit) {
146 fps.start();
147 //player events
148 c->handleEvents();
150 //collision, gravity
151 gravityManager->update();
152 collisionManager->update();
154 //movements
155 player->move();
156 shotManager->move();
157 quit = c->getQuit();
158 show();
159 if (fps.get_ticks() < 1000 / config->screen["fps"] ) {
160 SDL_Delay( ( 1000 / config->screen["fps"] ) - fps.get_ticks() );
163 SDL_Quit();