From c1fc60c5a2a4b96b830afc29942648714944b9d7 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 26 Feb 2021 18:54:38 -0500 Subject: Added sprite bobbing (for Lucas underwater) --- res/scripts/common.lua | 15 +++++++++++++++ res/scripts/underwater.lua | 1 + res/scripts/underwater_start.lua | 1 + src/animation_system.cpp | 19 +++++++++++++++++++ src/animation_system.h | 1 + src/character_system.cpp | 3 +++ src/renderer.cpp | 3 +++ src/script_system.cpp | 3 ++- src/sprite.h | 8 +++++++- 9 files changed, 52 insertions(+), 2 deletions(-) diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 895ad7b..825d2e5 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua @@ -329,3 +329,18 @@ function AllowCrouching() local playerSprite = getSprite(playerId) playerSprite.cantCrouch = false end + +--- Makes a sprite start bobbing up and down (for underwater). +-- This only applies when the sprite is on a normal medium (so, not on ladders). +function StartBobbing(spriteName) + local spriteId = getSpriteByAlias(spriteName) + local sprite = getSprite(spriteId) + sprite.bobsWhenNormal = true +end + +--- Makes a sprite stop bobbing up and down. +function StopBobbing(spriteName) + local spriteId = getSpriteByAlias(spriteName) + local sprite = getSprite(spriteId) + sprite.bobsWhenNormal = false +end diff --git a/res/scripts/underwater.lua b/res/scripts/underwater.lua index 4c5d2bb..3500079 100644 --- a/res/scripts/underwater.lua +++ b/res/scripts/underwater.lua @@ -2,6 +2,7 @@ underwater = {} function underwater.leave() AllowCrouching() + StopBobbing("lucas") end function underwater.fish2() diff --git a/res/scripts/underwater_start.lua b/res/scripts/underwater_start.lua index 98a6e67..c14f4b8 100644 --- a/res/scripts/underwater_start.lua +++ b/res/scripts/underwater_start.lua @@ -2,6 +2,7 @@ underwater_start = {} function underwater_start.init() PreventCrouching() + StartBobbing("lucas") end function underwater_start.talk_to_fish1() diff --git a/src/animation_system.cpp b/src/animation_system.cpp index baf94a4..5892f64 100644 --- a/src/animation_system.cpp +++ b/src/animation_system.cpp @@ -134,6 +134,25 @@ void AnimationSystem::tick(double dt) { } } } + + bobbingTimer_.accumulate(dt); + while (bobbingTimer_.step()) { + for (Sprite& sprite : game_.getSprites() | game_.spriteView()) { + if (sprite.isAnimated && sprite.bobbing) { + if (sprite.bobbingDown) { + sprite.bobAmount--; + if (sprite.bobAmount == 0) { + sprite.bobbingDown = false; + } + } else { + sprite.bobAmount++; + if (sprite.bobAmount == 4) { + sprite.bobbingDown = true; + } + } + } + } + } } void AnimationSystem::setSpriteDirection(int spriteId, Direction dir) { diff --git a/src/animation_system.h b/src/animation_system.h index c64c2dc..42aa516 100644 --- a/src/animation_system.h +++ b/src/animation_system.h @@ -30,6 +30,7 @@ private: Game& game_; std::vector animTimers_ = {{1000/5}, {1000/60}};//30fps * 1000 t/s;; + Timer bobbingTimer_ {1000/7}; }; #endif /* end of include guard: ANIMATION_SYSTEM_H_CCCC7CB8 */ diff --git a/src/character_system.cpp b/src/character_system.cpp index 87407af..9b91716 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp @@ -332,12 +332,14 @@ void CharacterSystem::setAnimationFor(int spriteId, CharacterState state) { game_.getSystem().setSpriteAnimation(spriteId, animName); sprite.hasShadow = sprite.normallyHasShadow; + sprite.bobbing = sprite.bobsWhenNormal; break; } case CharacterMedium::Ladder: { game_.getSystem().setSpriteAnimation(spriteId, "climb"); sprite.hasShadow = false; + sprite.bobbing = false; if (state == CharacterState::Still || state == CharacterState::Crouching) { sprite.animPaused = true; @@ -355,6 +357,7 @@ void CharacterSystem::setAnimationFor(int spriteId, CharacterState state) { game_.getSystem().setSpriteAnimation(spriteId, animName); sprite.hasShadow = false; + sprite.bobbing = false; break; } diff --git a/src/renderer.cpp b/src/renderer.cpp index 1196e50..3007378 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -115,6 +115,9 @@ void Renderer::renderSprite(const Sprite& sprite) { const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).frameIndices.at(sprite.animationFrame)); const SDL_Rect& src = frame.srcRect; SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() }; + if (sprite.bobbing) { + dest.y -= sprite.bobAmount; + } SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest); } } diff --git a/src/script_system.cpp b/src/script_system.cpp index 92e6afe..e218969 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp @@ -40,7 +40,8 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { "persistent", &Sprite::persistent, "paused", &Sprite::paused, "clipping", &Sprite::clipping, - "cantCrouch", &Sprite::cantCrouch); + "cantCrouch", &Sprite::cantCrouch, + "bobsWhenNormal", &Sprite::bobsWhenNormal); engine_.new_usertype( "message", diff --git a/src/sprite.h b/src/sprite.h index 657a692..570a906 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -64,7 +64,7 @@ public: std::string walkthroughScript; std::string enclosureZone; - // Animation + // Animation (internals) bool isAnimated = false; std::string spritesheet; Direction dir = Direction::down; @@ -76,8 +76,13 @@ public: std::map> nameDirToAnim; bool animFinished = false; bool hasShadow = false; + int bobAmount = 0; + bool bobbingDown = false; + + // Animation (controls) bool normallyHasShadow = false; bool animPaused = false; + bool bobbing = false; // Character bool orientable = false; @@ -92,6 +97,7 @@ public: int runningSfxChannel = -1; bool clipping = false; bool cantCrouch = false; // Use this to prevent running + bool bobsWhenNormal = false; // If enabled, sets the animation bobbing flag whenever medium is Normal // Input bool controllable = false; -- cgit 1.4.1