From 68ccbe09754c3389f40083be109ce93ca518ba6d Mon Sep 17 00:00:00 2001 From: AndreFerreira Date: Sun, 15 Nov 2009 02:47:46 -0200 Subject: [PATCH] Tiros agora podem ter gravidade. --- controleteclado.cpp | 1 + gravity.cpp | 7 +++++-- gravity.h | 1 + shot.cpp | 15 +++++++++++++-- shot.h | 5 ++--- thing.cpp | 1 + thing.h | 1 + weaponmanager.cpp | 6 +++--- weaponmanager.lua | 7 +++++++ weapons.lua | 9 ++++++++- 10 files changed, 42 insertions(+), 11 deletions(-) diff --git a/controleteclado.cpp b/controleteclado.cpp index 70899b2..63110a3 100644 --- a/controleteclado.cpp +++ b/controleteclado.cpp @@ -6,6 +6,7 @@ ControleTeclado::ControleTeclado(Player &p) : Controle(p) { void ControleTeclado::handleOther() { Uint8 *keystates = SDL_GetKeyState( NULL ); + jogador.bypass = false; if (keystates[SDLK_DOWN]) { jogador.onGround = false; jogador.bypass = true; diff --git a/gravity.cpp b/gravity.cpp index c4915db..2fbbdb7 100644 --- a/gravity.cpp +++ b/gravity.cpp @@ -9,11 +9,15 @@ void GravityManager::addPlatform(Platform* linha) { platforms.insert(linha); } +void GravityManager::deleteThing(Thing* thing) { + things.erase(things.find(thing)); +} + void GravityManager::update() { std::set::iterator it; std::set::iterator plat; for (it = things.begin(); it != things.end(); it++) { - (*it)->addSpeed(0,0.3); + (*it)->addSpeed(0,(*it)->gravityRate); if ((*it)->getSpeedY() < 0.0) continue; bool colisao = false; for (plat = platforms.begin(); !colisao && plat != platforms.end(); plat++) { @@ -24,7 +28,6 @@ void GravityManager::update() { (*it)->setSpeed((*it)->getSpeedX(),0.0); } else { - (*it)->bypass = false; (*it)->onGround = false; } } diff --git a/gravity.h b/gravity.h index ba03faf..2bcf781 100644 --- a/gravity.h +++ b/gravity.h @@ -16,6 +16,7 @@ class GravityManager { void addPlatform(Platform* linha); void update(); void removePlatforms(); + void deleteThing(Thing* thing); }; #endif diff --git a/shot.cpp b/shot.cpp index 134aa25..1f9d8de 100644 --- a/shot.cpp +++ b/shot.cpp @@ -1,14 +1,25 @@ #include "shot.h" #include "weapon.h" +#include "game.h" +#include "shotmanager.h" #include -Shot::Shot(double x, double y, double angle, Weapon* w, ShotManager* s) { +Shot::Shot(double x, double y, double angle, double gravity, Weapon* w) { weapon = w; - shotManager = s; setPosition(x,y); + if (gravity != 0.0) + weapon->game->gravityManager->subscribe(this); + gravityRate = gravity; + bypass = true; setSpeed(-cos(angle)*weapon->shotSpeed,sin(angle)*weapon->shotSpeed); } +Shot::~Shot() { + weapon->game->shotManager->deleteShot(this); + if (gravityRate != 0.0) + weapon->game->gravityManager->deleteThing(this); +} + void Shot::desenha() { glPushMatrix(); glTranslatef(getX(),getY(),0); diff --git a/shot.h b/shot.h index ce367b0..deee626 100644 --- a/shot.h +++ b/shot.h @@ -4,15 +4,14 @@ #include "thing.h" class Weapon; -class ShotManager; class Shot: public Thing { private: Weapon* weapon; public: - ShotManager* shotManager; + Shot(double x, double y, double angle, double gravityRate, Weapon* w); + ~Shot(); void desenha(); - Shot(double x, double y, double angle, Weapon* w, ShotManager* s); }; #endif diff --git a/thing.cpp b/thing.cpp index 67a5659..2d0b941 100644 --- a/thing.cpp +++ b/thing.cpp @@ -4,6 +4,7 @@ Thing::Thing() { onGround = false; maxspeed.x = 99999; maxspeed.y = 99999; + gravityRate = 0.3; } void Thing::addSpeed(double xspeed, double yspeed) { diff --git a/thing.h b/thing.h index da2347e..08896ca 100644 --- a/thing.h +++ b/thing.h @@ -9,6 +9,7 @@ class Thing { Vetor velocidade; Vetor maxspeed; public: + double gravityRate; bool onGround; bool bypass; Thing(); diff --git a/weaponmanager.cpp b/weaponmanager.cpp index b5a098b..c057ed8 100644 --- a/weaponmanager.cpp +++ b/weaponmanager.cpp @@ -51,7 +51,6 @@ static int regspriteline (lua_State *L) { static void deleteShotFunc(void* param) { Shot* shot = (Shot*) param; - shot->shotManager->deleteShot(shot); delete shot; } @@ -73,15 +72,16 @@ static Uint32 deleteShotCallback(Uint32 interval, void *param) { static int createshot (lua_State *L) { - double x,y,angle,rate; + double x,y,angle,rate,gravity; Weapon* weapon; x = lua_tonumber(L, 1); y = lua_tonumber(L, 2); angle = lua_tonumber(L, 3); rate = lua_tonumber(L, 4); weapon = (Weapon*)lua_touserdata(L, 5); + gravity = lua_tonumber(L, 6); - Shot* newshot = new Shot(x,y,angle,weapon,weapon->game->shotManager); + Shot* newshot = new Shot(x,y,angle,gravity,weapon); weapon->game->shotManager->addShot(newshot); SDL_AddTimer(rate,deleteShotCallback,newshot); diff --git a/weaponmanager.lua b/weaponmanager.lua index b78547d..b752484 100644 --- a/weaponmanager.lua +++ b/weaponmanager.lua @@ -13,3 +13,10 @@ end function Line(t) return t end + +function Shot(t) + if t.gravity == nil then + t.gravity = 0 + end + createshot(t.x,t.y,t.angle,t.duration,t.weapon,t.gravity) +end diff --git a/weapons.lua b/weapons.lua index a02136f..42c82d4 100644 --- a/weapons.lua +++ b/weapons.lua @@ -22,7 +22,14 @@ shotgun = Weapon{ Line(cano2)}, whenfire = function (x,y,angle,w) for i = 1,5 do - createshot(x,y,angle+randomspread(0.2),1000,w) + Shot { + x = x, + y = y, + angle = angle+randomspread(0.2), + weapon = w, + duration = 1000 + } end end } + -- 2.11.4.GIT