diff options
Diffstat (limited to 'src/gui/viewport.cpp')
-rw-r--r-- | src/gui/viewport.cpp | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/src/gui/viewport.cpp b/src/gui/viewport.cpp index 19bed735..f6166c8f 100644 --- a/src/gui/viewport.cpp +++ b/src/gui/viewport.cpp @@ -43,6 +43,8 @@ #include "utils/stringutils.h" +#include <cmath> + extern volatile int tick_time; Viewport::Viewport(): @@ -70,8 +72,8 @@ Viewport::Viewport(): setFocusable(true); - listen(CHANNEL_CONFIG); - listen(CHANNEL_ACTORSPRITE); + listen(Event::ConfigChannel); + listen(Event::ActorSpriteChannel); } Viewport::~Viewport() @@ -122,9 +124,9 @@ void Viewport::draw(gcn::Graphics *gcnGraphics) if (mScrollLaziness < 1) mScrollLaziness = 1; // Avoids division by zero - // Apply lazy scrolling while (lastTick < tick_time) { + // Apply lazy scrolling if (player_x > mPixelViewX + mScrollRadius) { mPixelViewX += (player_x - mPixelViewX - mScrollRadius) / @@ -145,6 +147,22 @@ void Viewport::draw(gcn::Graphics *gcnGraphics) mPixelViewY += (player_y - mPixelViewY + mScrollRadius) / mScrollLaziness; } + + // manage shake effect + for (ShakeEffects::iterator i = mShakeEffects.begin(); + i != mShakeEffects.end(); + i++) + { + // apply the effect to viewport + mPixelViewX += i->x *= -i->decay; + mPixelViewY += i->y *= -i->decay; + // check death conditions + if (abs(i->x) + abs(i->y) < 1.0f || + (i->duration > 0 && --i->duration == 0)) + { + i = mShakeEffects.erase(i); + } + } lastTick++; } @@ -223,6 +241,24 @@ void Viewport::draw(gcn::Graphics *gcnGraphics) WindowContainer::draw(gcnGraphics); } +void Viewport::shakeScreen(int intensity) +{ + float direction = rand()%628 / 100.0f; // random value between 0 and 2PI + float x = std::sin(direction) * intensity; + float y = std::cos(direction) * intensity; + shakeScreen(x, y); +} + +void Viewport::shakeScreen(float x, float y, float decay, unsigned duration) +{ + ShakeEffect effect; + effect.x = x; + effect.y = y; + effect.decay = decay; + effect.duration = duration; + mShakeEffects.push_back(effect); +} + void Viewport::logic() { WindowContainer::logic(); @@ -526,9 +562,10 @@ void Viewport::hideBeingPopup() mBeingPopup->setVisible(false); } -void Viewport::event(Channels channel, const Mana::Event &event) +void Viewport::event(Event::Channel channel, const Event &event) { - if (channel == CHANNEL_ACTORSPRITE && event.getName() == EVENT_DESTROYED) + if (channel == Event::ActorSpriteChannel + && event.getType() == Event::Destroyed) { ActorSprite *actor = event.getActor("source"); @@ -538,8 +575,8 @@ void Viewport::event(Channels channel, const Mana::Event &event) if (mHoverItem == actor) mHoverItem = 0; } - else if (channel == CHANNEL_CONFIG && - event.getName() == EVENT_CONFIGOPTIONCHANGED) + else if (channel == Event::ConfigChannel && + event.getType() == Event::ConfigOptionChanged) { const std::string option = event.getString("option"); if (option == "ScrollLaziness" || option == "ScrollRadius") |