From d1235174157bd498d0c148325d7c8066e3ab6ac7 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Mon, 15 Feb 2021 12:05:18 -0500 Subject: Added camera shake to lightning event --- res/scripts/common.lua | 8 ++++++++ res/scripts/map2.lua | 11 +++++++++++ src/effect_system.cpp | 28 ++++++++++++++++++++++++++++ src/effect_system.h | 17 +++++++++++++++++ src/renderer.cpp | 6 ++++-- src/script_system.cpp | 4 +++- 6 files changed, 71 insertions(+), 3 deletions(-) diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 2197417..422d157 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua @@ -131,6 +131,14 @@ function WaitForMapFade() end end +function ShakeCamera(period) + effect():shakeCamera(period) +end + +function StopShakingCamera() + effect():stopShakingCamera() +end + function SetPartyDirection(spriteId, direction) animation():setSpriteDirection(spriteId, direction) diff --git a/res/scripts/map2.lua b/res/scripts/map2.lua index 12a5df0..cdab0f5 100644 --- a/res/scripts/map2.lua +++ b/res/scripts/map2.lua @@ -26,6 +26,15 @@ end function map2.mailbox_lightning() StartCutscene() + DisplayMessage("* The mailbox lid is open...\n* Peek inside?", "", SpeakerType.NONE) + ShowChoice("Yes", "No") + WaitForEndOfMessage() + + if GetChoiceSelection() == 1 then + HideCutsceneBars() + return + end + FadeMap(500, 0.5) WaitForMapFade() @@ -38,11 +47,13 @@ function map2.mailbox_lightning() local lucasPos = GetPosition("lucas") CreateAnimatedSpriteAtPosition("lightning_from_mailbox", "lightning", lucasPos:x() + 56, lucasPos:y() - 72, "strike", Direction.DOWN_LEFT, SpriteLayer.ABOVE) + ShakeCamera(66) PlaySound("lightning_explosion.wav") SetDirection("lucas", Direction.DOWN) SetAnimation("lucas", "lightning_electrocute!") WaitForAnimation("lucas") + StopShakingCamera() DestroyNamedSprite("lightning_from_mailbox") SetAnimation("lucas", "lightning_collapse!") WaitForAnimation("lucas") diff --git a/src/effect_system.cpp b/src/effect_system.cpp index 3a0afe4..74fc8f3 100644 --- a/src/effect_system.cpp +++ b/src/effect_system.cpp @@ -18,6 +18,22 @@ void EffectSystem::tick(double dt) { mapFade_ = (mapFadeDest_ - mapFadeStart_) / mapFadeLength_ * mapFadeThus_ + mapFadeStart_; } + + if (cameraShaking_) { + cameraShakeTimer_.accumulate(dt); + + while (cameraShakeTimer_.step()) { + cameraShakeOn_ = !cameraShakeOn_; + } + + if (cameraShakeOn_) { + cameraShakeOffset_.x() = 8; + cameraShakeOffset_.y() = 8; + } else { + cameraShakeOffset_.x() = 0; + cameraShakeOffset_.y() = 0; + } + } } void EffectSystem::fadeScreen(int length, double amount) { @@ -33,3 +49,15 @@ void EffectSystem::fadeMap(int length, double amount) { mapFadeLength_ = length; mapFadeThus_ = 0; } + +void EffectSystem::shakeCamera(int period) { + cameraShaking_ = true; + cameraShakeOffset_ = { 0, 0 }; + cameraShakeOn_ = false; + cameraShakeTimer_ = Timer(period); +} + +void EffectSystem::stopShakingCamera() { + cameraShaking_ = false; + cameraShakeOffset_ = { 0, 0 }; +} diff --git a/src/effect_system.h b/src/effect_system.h index 2cdcb10..12ec7ca 100644 --- a/src/effect_system.h +++ b/src/effect_system.h @@ -2,6 +2,8 @@ #define EFFECT_SYSTEM_H_0B497B39 #include "system.h" +#include "timer.h" +#include "vector.h" class Game; @@ -26,6 +28,12 @@ public: // - amount is [0,1] void fadeMap(int length, double amount); + // Shakes the viewport by 1 tile in both directions. + // - period is in milliseconds, it's actually a half period + void shakeCamera(int period); + + void stopShakingCamera(); + /* Information */ bool isScreenFaded() const { return screenFade_ > 0.0; } @@ -40,6 +48,10 @@ public: bool isMapFadeComplete() const { return mapFade_ == mapFadeDest_; } + const vec2i& getCameraShakeOffset() const { return cameraShakeOffset_; } + + bool isCameraShaking() const { return cameraShaking_; } + private: Game& game_; @@ -55,6 +67,11 @@ private: double mapFadeStart_ = 0.0; double mapFadeLength_ = 0.0; double mapFadeThus_ = 0.0; + + bool cameraShaking_ = false; + vec2i cameraShakeOffset_ { 0, 0 }; + bool cameraShakeOn_ = false; + Timer cameraShakeTimer_ { 0 }; }; #endif /* end of include guard: EFFECT_SYSTEM_H_0B497B39 */ diff --git a/src/renderer.cpp b/src/renderer.cpp index c80b0d3..5aeb232 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -194,9 +194,11 @@ void Renderer::render(Game& game) { renderSprite(sprite); } + vec2i viewport = game.getSystem().getCameraPosition() + effects.getCameraShakeOffset(); + SDL_Rect cameraField { - game.getSystem().getCameraPosition().x(), - game.getSystem().getCameraPosition().y(), + viewport.x(), + viewport.y(), game.getSystem().getFieldOfView().w(), game.getSystem().getFieldOfView().h() }; diff --git a/src/script_system.cpp b/src/script_system.cpp index 7109d98..cbbd473 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp @@ -59,7 +59,9 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { "fadeScreen", &EffectSystem::fadeScreen, "isScreenFadeComplete", &EffectSystem::isScreenFadeComplete, "fadeMap", &EffectSystem::fadeMap, - "isMapFadeComplete", &EffectSystem::isMapFadeComplete); + "isMapFadeComplete", &EffectSystem::isMapFadeComplete, + "shakeCamera", &EffectSystem::shakeCamera, + "stopShakingCamera", &EffectSystem::stopShakingCamera); engine_.new_usertype( "mixer", -- cgit 1.4.1