Adicionado campo velocidade ao tiro
[Projeto-PCG.git] / game.cpp
blobc59d6933a61fe271791120d9312aaf8cf6d771c7
1 #include "game.h"
2 #include "geometry.h"
3 #include "luaenv.h"
4 #include "timer.h"
5 #include "controleteclado.h"
6 #include "shotmanager.h"
8 void Game::loadMap(std::string mapname) {
9 if (mapa != NULL)
10 delete mapa;
11 mapa = new Mapa(mapname,this);
14 void Game::addPlatform(Platform* plat) {
15 gravityManager->addPlatform(plat);
18 void Game::removePlatforms() {
19 gravityManager->removePlatforms();
22 bool init_GL() {
23 //Set clear color
24 glClearColor( 1, 1, 1, 0 );
26 //Initialize modelview matrix
27 glMatrixMode( GL_MODELVIEW );
28 glLoadIdentity();
30 //Linhas
31 glLineWidth(2.5);
32 glEnable(GL_LINE_SMOOTH);
33 glEnable(GL_BLEND);
34 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
35 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
37 //If there was any errors
38 if( glGetError() != GL_NO_ERROR )
40 return false;
43 //If everything initialized
44 return true;
47 Game::Game(ConfigManager *cfg) {
48 gravityManager = new GravityManager;
49 mapa = NULL;
50 player = NULL;
51 config = cfg;
52 //Initialize SDL
53 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
55 //erro
58 //Create Window
59 if( SDL_SetVideoMode( config->screen["width"], config->screen["height"], config->screen["bpp"], SDL_OPENGL ) == NULL )
61 //erro
64 //Initialize OpenGL
65 if( !init_GL() )
67 //erro
70 //Set caption
71 SDL_WM_SetCaption( "Prototipo Jogo", NULL );
74 void Game::desenhaMira(Ponto aim) {
75 glPushMatrix();
76 glTranslatef(camera.x+aim.x,camera.y+aim.y,0);
77 drawCircle(10,10);
78 glPopMatrix();
81 void Game::show() {
82 glClear( GL_COLOR_BUFFER_BIT );
83 double x, y;
84 int width = config->screen["width"];
85 int height = config->screen["height"];
87 if (player->getX() <= width/2)
88 x = 0;
89 else if (player->getX() >= mapa->xmax() - width/2)
90 x = mapa->xmax() - width;
91 else
92 x = player->getX() - width/2;
94 if (player->getY() <= 2*height/3)
95 y = 0;
96 else if (player->getY() >= mapa->ymax() - height/3)
97 y = mapa->ymax() - height;
98 else
99 y = player->getY() - 2*height/3;
100 camera.x = x;
101 camera.y = y;
102 player->desenha();
103 mapa->desenha();
104 shotManager->desenha();
106 glMatrixMode( GL_PROJECTION );
107 glLoadIdentity();
108 glOrtho( camera.x, camera.x+width, camera.y+height, camera.y, -1, 1 );
109 SDL_GL_SwapBuffers();
112 void Game::reloadLua() {
113 config->load();
114 loadMap(config->maps.front());
115 weaponManager->loadWeapons();
116 player->equip(weaponManager->getWeapon("Shotgun"));
119 void Game::setSpawn(Ponto spawn) {
120 this->spawn = spawn;
123 void Game::mainLoop() {
124 Timer fps;
126 loadMap(config->maps.front());
128 player = new Player(this, spawn, config->player["speed"]);
129 ControleTeclado c(*player);
131 shotManager = new ShotManager;
132 weaponManager = new WeaponManager(this);
133 weaponManager->loadWeapons();
134 player->equip(weaponManager->getWeapon("Shotgun"));
136 bool quit = false;
137 while (!quit) {
138 fps.start();
139 //player events
140 c.handleEvents();
142 //colision, gravity
143 gravityManager->update();
145 //movements
146 player->move();
147 shotManager->move();
148 quit = c.getQuit();
149 show();
150 if (fps.get_ticks() < 1000 / config->screen["fps"] ) {
151 SDL_Delay( ( 1000 / config->screen["fps"] ) - fps.get_ticks() );
154 SDL_Quit();