From d983ef6b8d66d916ed7502cf6d35b573c5366257 Mon Sep 17 00:00:00 2001
From: Star Rauchenberger <fefferburbia@gmail.com>
Date: Mon, 5 Jul 2021 10:39:06 -0400
Subject: Added fading in/out sprites

---
 res/scripts/common.lua   | 14 ++++++++++++++
 src/animation_system.cpp | 19 +++++++++++++++++++
 src/animation_system.h   |  1 +
 src/script_system.cpp    |  3 ++-
 src/sprite.h             |  3 ++-
 5 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/res/scripts/common.lua b/res/scripts/common.lua
index e355a32..2e95f26 100644
--- a/res/scripts/common.lua
+++ b/res/scripts/common.lua
@@ -423,6 +423,20 @@ function SetOpacity(spriteName, amount)
   sprite.opacity = amount
 end
 
+--- Fades out the given sprite.
+function FadeOutSprite(spriteName)
+  local spriteId = getSpriteByAlias(spriteName)
+  local sprite = getSprite(spriteId)
+  sprite.shouldBeFadedIn = false
+end
+
+--- Fades out the given sprite.
+function FadeInSprite(spriteName)
+  local spriteId = getSpriteByAlias(spriteName)
+  local sprite = getSprite(spriteId)
+  sprite.shouldBeFadedIn = true
+end
+
 --- Sets the enclosure zone for a sprite.
 -- The sprite will be prevented from exiting the area defined by that zone.
 function AddEnclosureZone(spriteName, zone)
diff --git a/src/animation_system.cpp b/src/animation_system.cpp
index ce5cc02..73d30d5 100644
--- a/src/animation_system.cpp
+++ b/src/animation_system.cpp
@@ -166,6 +166,25 @@ void AnimationSystem::tick(double dt) {
       }
     }
   }
+
+  fadingTimer_.accumulate(dt);
+  while (fadingTimer_.step()) {
+    for (Sprite& sprite : game_.getSprites() | game_.spriteView()) {
+      if (sprite.isAnimated) {
+        if (sprite.shouldBeFadedIn && sprite.opacity < 1.0) {
+          sprite.opacity += 0.05;
+          if (sprite.opacity > 1.0) {
+            sprite.opacity = 1.0;
+          }
+        } else if (!sprite.shouldBeFadedIn && sprite.opacity > 0.0) {
+          sprite.opacity -= 0.05;
+          if (sprite.opacity < 0.0) {
+            sprite.opacity = 0.0;
+          }
+        }
+      }
+    }
+  }
 }
 
 void AnimationSystem::setSpriteDirection(int spriteId, Direction dir) {
diff --git a/src/animation_system.h b/src/animation_system.h
index 42aa516..7474c54 100644
--- a/src/animation_system.h
+++ b/src/animation_system.h
@@ -31,6 +31,7 @@ private:
   Game& game_;
   std::vector<Timer> animTimers_ = {{1000/5}, {1000/60}};//30fps * 1000 t/s;;
   Timer bobbingTimer_ {1000/7};
+  Timer fadingTimer_ {1000/60};
 };
 
 #endif /* end of include guard: ANIMATION_SYSTEM_H_CCCC7CB8 */
diff --git a/src/script_system.cpp b/src/script_system.cpp
index 56a6012..b4a7b9b 100644
--- a/src/script_system.cpp
+++ b/src/script_system.cpp
@@ -56,7 +56,8 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) {
     "behaviourType", &Sprite::behaviourType,
     "followSpriteId", &Sprite::followSpriteId,
     "interactionScript", &Sprite::interactionScript,
-    "opacity", &Sprite::opacity);
+    "opacity", &Sprite::opacity,
+    "shouldBeFadedIn", &Sprite::shouldBeFadedIn);
 
   engine_.new_usertype<MessageSystem>(
     "message",
diff --git a/src/sprite.h b/src/sprite.h
index 782ae14..e7cb55b 100644
--- a/src/sprite.h
+++ b/src/sprite.h
@@ -100,13 +100,14 @@ public:
   int bobAmount = 0;
   bool bobbingDown = false;
   int animSlowdownProgress = 0;
-  double opacity = 1.0;
 
   // Animation (controls)
   bool normallyHasShadow = false;
   bool animPaused = false;
   bool bobbing = false;
   int animSlowdown = 1; // Animation will only advance every X frames (so, 1 means it's disabled)
+  double opacity = 1.0;
+  bool shouldBeFadedIn = true;
 
   // Character
   bool orientable = false;
-- 
cgit 1.4.1