Sistema de tiro agora atira, bastante trabalho para fazer ainda.
[Projeto-PCG.git] / game.cpp
blob8773324b54e9de59c53384ac515e3fe398ecd974
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 const int SCREEN_WIDTH = 640;
9 const int SCREEN_HEIGHT = 480;
10 const int SCREEN_BPP = 32;
11 const int FRAMES_PER_SECOND = 60;
13 void Game::loadMap(std::string mapname) {
14 if (mapa != NULL)
15 delete mapa;
16 const double alturaChao = 400;
17 mapa = new Mapa(mapname,this);
20 void Game::addPlatform(Platform* plat) {
21 gravityManager->addPlatform(plat);
24 void Game::removePlatforms() {
25 gravityManager->removePlatforms();
28 bool init_GL()
30 //Set clear color
31 glClearColor( 1, 1, 1, 0 );
33 //Initialize modelview matrix
34 glMatrixMode( GL_MODELVIEW );
35 glLoadIdentity();
37 //Linhas
38 glLineWidth(2.5);
39 glEnable(GL_LINE_SMOOTH);
40 glEnable(GL_BLEND);
41 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
42 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
44 //If there was any errors
45 if( glGetError() != GL_NO_ERROR )
47 return false;
50 //If everything initialized
51 return true;
54 Game::Game()
56 gravityManager = new GravityManager;
57 mapa = NULL;
58 player = NULL;
59 //Initialize SDL
60 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
62 //erro
65 //Create Window
66 if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
68 //erro
71 //Initialize OpenGL
72 if( !init_GL() )
74 //erro
77 //Set caption
78 SDL_WM_SetCaption( "Prototipo Jogo", NULL );
81 void Game::desenhaMira(Ponto aim) {
82 glPushMatrix();
83 glTranslatef(camera.x+aim.x,camera.y+aim.y,0);
84 drawCircle(10,10);
85 glPopMatrix();
88 void Game::show() {
89 glClear( GL_COLOR_BUFFER_BIT );
90 double x, y;
92 if (player->getX() <= SCREEN_WIDTH/2)
93 x = 0;
94 else if (player->getX() >= mapa->xmax() - SCREEN_WIDTH/2)
95 x = mapa->xmax() - SCREEN_WIDTH;
96 else
97 x = player->getX() - SCREEN_WIDTH/2;
99 if (player->getY() <= 2*SCREEN_HEIGHT/3)
100 y = 0;
101 else if (player->getY() >= mapa->ymax() - SCREEN_HEIGHT/3)
102 y = mapa->ymax() - SCREEN_HEIGHT;
103 else
104 y = player->getY() - 2*SCREEN_HEIGHT/3;
105 camera.x = x;
106 camera.y = y;
107 player->desenha();
108 mapa->desenha();
109 shotManager->desenha();
111 glMatrixMode( GL_PROJECTION );
112 glLoadIdentity();
113 glOrtho( camera.x, camera.x+SCREEN_WIDTH, camera.y+SCREEN_HEIGHT, camera.y, -1, 1 );
114 SDL_GL_SwapBuffers();
117 void Game::reloadLua() {
118 loadMap(currentMap);
119 weaponManager->loadWeapons();
120 player->equip(weaponManager->getWeapon("Shotgun"));
123 void Game::setSpawn(Ponto spawn) {
124 this->spawn = spawn;
127 void Game::mainLoop() {
128 luaRun luaEnv;
129 luaEnv.registerScripts();
130 luaEnv.loadScripts();
131 Timer fps;
133 player = new Player(this);
134 currentMap = "map1.lua";
135 loadMap(currentMap);
136 player->setPosition(spawn.x,spawn.y);
137 ControleTeclado c(*player);
138 bool quit = false;
139 shotManager = new ShotManager;
140 weaponManager = new WeaponManager;
141 weaponManager->loadWeapons();
142 player->equip(weaponManager->getWeapon("Shotgun"));
143 while (!quit) {
144 fps.start();
145 //player events
146 c.handleEvents();
148 //colision, gravity
149 gravityManager->update();
151 //movements
152 player->move();
153 shotManager->move();
154 quit = c.getQuit();
155 show();
156 if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) {
157 SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
160 SDL_Quit();