From 239c5dd8120dadcf57087169116c8c6cbe265363 Mon Sep 17 00:00:00 2001 From: AndreFerreira Date: Sat, 14 Nov 2009 19:27:52 -0200 Subject: [PATCH] Sistema de tiro agora atira, bastante trabalho para fazer ainda. --- Makefile | 1 + game.cpp | 4 ++++ game.h | 2 ++ shooter.cpp | 38 +++++++++++++++++++++++++++++++++++++- shot.cpp | 16 ++++++++++++++++ shot.h | 10 +++++++++- shotmanager.cpp | 30 ++++++++++++++++++++++++++++++ shotmanager.h | 18 ++++++++++++++++++ thing.cpp | 8 ++++---- weapon.h | 4 ++++ weaponmanager.cpp | 6 +++++- weaponmanager.lua | 3 ++- weapons.lua | 1 + 13 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 shotmanager.cpp create mode 100644 shotmanager.h diff --git a/Makefile b/Makefile index 8f0bd2f..22f11ee 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ OBJS = \ maploader.o \ weaponmanager.o \ shot.o \ + shotmanager.o \ SRCS = $(OBJS,.o=.cpp) diff --git a/game.cpp b/game.cpp index 1ced9ad..8773324 100644 --- a/game.cpp +++ b/game.cpp @@ -3,6 +3,7 @@ #include "luaenv.h" #include "timer.h" #include "controleteclado.h" +#include "shotmanager.h" const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; @@ -105,6 +106,7 @@ void Game::show() { camera.y = y; player->desenha(); mapa->desenha(); + shotManager->desenha(); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); @@ -134,6 +136,7 @@ void Game::mainLoop() { player->setPosition(spawn.x,spawn.y); ControleTeclado c(*player); bool quit = false; + shotManager = new ShotManager; weaponManager = new WeaponManager; weaponManager->loadWeapons(); player->equip(weaponManager->getWeapon("Shotgun")); @@ -147,6 +150,7 @@ void Game::mainLoop() { //movements player->move(); + shotManager->move(); quit = c.getQuit(); show(); if (fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) { diff --git a/game.h b/game.h index 8e2ca5f..ffa036d 100644 --- a/game.h +++ b/game.h @@ -8,6 +8,7 @@ class Player; class Mapa; +class ShotManager; class Game { private: @@ -17,6 +18,7 @@ class Game { WeaponManager* weaponManager; Ponto spawn; public: + ShotManager* shotManager; Ponto camera; Game(); void mainLoop(); diff --git a/shooter.cpp b/shooter.cpp index 436f745..78ec8b1 100644 --- a/shooter.cpp +++ b/shooter.cpp @@ -1,6 +1,8 @@ #include #include "shooter.h" #include "gravity.h" +#include "shot.h" +#include "shotmanager.h" Shooter::Shooter(Game* agame, Ponto pos, Ponto speed) { weapon = NULL; @@ -22,6 +24,12 @@ void allowFireFunc(void* param) { ((Shooter*)param)->allowFire(); } +void deleteShotFunc(void* param) { + Shot* shot = (Shot*) param; + shot->shotManager->deleteShot(shot); + delete shot; +} + Uint32 allowFireCallback(Uint32 interval, void *param) { SDL_Event event; SDL_UserEvent userevent; @@ -38,15 +46,43 @@ Uint32 allowFireCallback(Uint32 interval, void *param) { return 0; } + +Uint32 deleteShotCallback(Uint32 interval, void *param) { + SDL_Event event; + SDL_UserEvent userevent; + userevent.type = SDL_USEREVENT; + userevent.code = 0; + userevent.data1 = (void*)deleteShotFunc; + userevent.data2 = (void*)param; + + event.type = SDL_USEREVENT; + event.user = userevent; + + SDL_PushEvent(&event); + + return 0; +} + void Shooter::allowFire() { canfire = true; } void Shooter::fire() { - if (!canfire) + if (!canfire || weapon == NULL) return; SDL_AddTimer(weapon->fireRate,allowFireCallback,this); canfire = false; + Ponto tiplinha = weapon->getTip(); + double angle = getAngle(); + double cosAngle = cos(angle); + double sinAngle = sin(angle); + Ponto tip; + tip.x = (cosAngle*tiplinha.x-sinAngle*tiplinha.y); + tip.y = -(sinAngle*tiplinha.x+cosAngle*tiplinha.y); + tip = tip + pescoco(); + Shot* newshot = new Shot(tip.x+getX(),tip.y+getY(),angle,weapon,game->shotManager); + game->shotManager->addShot(newshot); + SDL_AddTimer(weapon->fireRate*2,deleteShotCallback,newshot); } diff --git a/shot.cpp b/shot.cpp index fd62166..ca1a34c 100644 --- a/shot.cpp +++ b/shot.cpp @@ -1 +1,17 @@ #include "shot.h" +#include "weapon.h" +#include +#include +Shot::Shot(double x, double y, double angle, Weapon* w, ShotManager* s) { + weapon = w; + shotManager = s; + setPosition(x,y); + setSpeed(-cos(angle)*weapon->shotSpeed,sin(angle)*weapon->shotSpeed); +} + +void Shot::desenha() { + glPushMatrix(); + glTranslatef(getX(),getY(),0); + drawCircle(1,30); + glPopMatrix(); +} diff --git a/shot.h b/shot.h index e8abe93..ce367b0 100644 --- a/shot.h +++ b/shot.h @@ -3,8 +3,16 @@ #include "thing.h" +class Weapon; +class ShotManager; + class Shot: public Thing { - //public: + private: + Weapon* weapon; + public: + ShotManager* shotManager; + void desenha(); + Shot(double x, double y, double angle, Weapon* w, ShotManager* s); }; #endif diff --git a/shotmanager.cpp b/shotmanager.cpp new file mode 100644 index 0000000..32e4920 --- /dev/null +++ b/shotmanager.cpp @@ -0,0 +1,30 @@ +#include "shotmanager.h" +#include "shot.h" + +void ShotManager::addShot(Shot* shot) { + shots.push_back(shot); +} + +void ShotManager::desenha() { + std::vector::iterator it; + for (it = shots.begin(); it != shots.end(); it++) { + (*it)->desenha(); + } +} + +void ShotManager::move() { + std::vector::iterator it; + for (it = shots.begin(); it != shots.end(); it++) { + (*it)->move(); + } +} + +void ShotManager::deleteShot(Shot* shot) { + std::vector::iterator it; + for (it = shots.begin(); it != shots.end(); it++) { + if (*it == shot) { + shots.erase(it); + return; + } + } +} diff --git a/shotmanager.h b/shotmanager.h new file mode 100644 index 0000000..6eb831c --- /dev/null +++ b/shotmanager.h @@ -0,0 +1,18 @@ +#ifndef SHOTMANAGER_H +#define SHOTMANAGER_H + +#include + +class Shot; + +class ShotManager { + private: + std::vector shots; + public: + void addShot(Shot* shot); + void desenha(); + void move(); + void deleteShot(Shot* shot); +}; + +#endif diff --git a/thing.cpp b/thing.cpp index f5ffe2b..67a5659 100644 --- a/thing.cpp +++ b/thing.cpp @@ -11,8 +11,8 @@ void Thing::addSpeed(double xspeed, double yspeed) { } void Thing::setSpeed(double xspeed, double yspeed) { - velocidade.x = xspeed; - velocidade.y = yspeed; + velocidade.x = std::max(std::min(xspeed,maxspeed.x),-maxspeed.x); + velocidade.y = std::max(std::min(yspeed,maxspeed.y),-maxspeed.y); } void Thing::setPosition(double x, double y) { @@ -21,8 +21,8 @@ void Thing::setPosition(double x, double y) { } void Thing::move() { - posicao.x += std::max(std::min(velocidade.x,maxspeed.x),-maxspeed.x); - posicao.y += std::max(std::min(velocidade.y,maxspeed.y),-maxspeed.y); + posicao.x += velocidade.x; + posicao.y += velocidade.y; } Linha Thing::getBaseLine() { diff --git a/weapon.h b/weapon.h index d8b34eb..7cb63c4 100644 --- a/weapon.h +++ b/weapon.h @@ -10,7 +10,9 @@ class Weapon: public Thing { private: Ponto leftHand; Ponto rightHand; + Ponto tip; public: + double shotSpeed; Polygon sprite; int fireRate; std::string name; @@ -20,6 +22,8 @@ class Weapon: public Thing { Ponto getLeftHand() {return leftHand;} Ponto getRightHand(){return rightHand;} void desenha(); + void setTip(Ponto t) {tip = t;} + Ponto getTip() {return tip;} }; diff --git a/weaponmanager.cpp b/weaponmanager.cpp index d0fbb96..6598172 100644 --- a/weaponmanager.cpp +++ b/weaponmanager.cpp @@ -9,7 +9,7 @@ WeaponManager::WeaponManager() { std::list *_weapons = NULL; static int regweapon (lua_State *L) { - Ponto r,l; + Ponto r,l,tip; r.x = lua_tonumber(L, 1); r.y = lua_tonumber(L, 2); l.x = lua_tonumber(L, 3); @@ -17,8 +17,12 @@ static int regweapon (lua_State *L) { Weapon* newweapon = new Weapon(); newweapon->name = lua_tostring (L, 5); newweapon->fireRate = lua_tonumber(L, 6); + tip.x = lua_tonumber(L, 7); + tip.y = lua_tonumber(L, 8); + newweapon->shotSpeed = 10; newweapon->setRightHand(r); newweapon->setLeftHand(l); + newweapon->setTip(tip); lua_pushlightuserdata(L, newweapon); _weapons->push_front(newweapon); return 1; diff --git a/weaponmanager.lua b/weaponmanager.lua index 69efd9a..214c29a 100644 --- a/weaponmanager.lua +++ b/weaponmanager.lua @@ -1,7 +1,8 @@ function Weapon(t) local w = regweapon(t.righthand[1],t.righthand[2], t.lefthand[1],t.lefthand[2], - t.name,t.firerate) + t.name,t.firerate, + t.tip[1],t.tip[2]) for i = 1, #t.sprite do regspriteline(w,t.sprite[i][1],t.sprite[i][2],t.sprite[i][3],t.sprite[i][4]) end diff --git a/weapons.lua b/weapons.lua index b7f7dae..5345bdb 100644 --- a/weapons.lua +++ b/weapons.lua @@ -7,6 +7,7 @@ shotgun = Weapon{ name = "Shotgun", righthand = {-15,-15}, lefthand = {-30,-15+10}, + tip = {-45,-15+10}, firerate = 1000, sprite = { Line(cabo), -- 2.11.4.GIT