Skip to content

Commit

Permalink
Add star bouncing animation
Browse files Browse the repository at this point in the history
  • Loading branch information
newo-2001 committed Oct 14, 2024
1 parent b77630f commit 6defe2c
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(Impacto_Src
src/modelviewer.cpp
src/characterviewer.cpp
src/spriteanimation.cpp
src/pathanimation.cpp
src/background2d.cpp
src/mask2d.cpp
src/character2d.cpp
Expand Down Expand Up @@ -334,6 +335,7 @@ set(Impacto_Header
src/characterviewer.h
src/spritesheet.h
src/spriteanimation.h
src/pathanimation.h
src/font.h
src/background2d.h
src/mask2d.h
Expand Down
61 changes: 60 additions & 1 deletion profiles/chlcc/hud/titlemenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ root.TitleMenu = {
PressToStartAnimDurationOut = 0.5,
PressToStartSprite = "TitleMenuPressToStart",
IntroBackgroundSprite = "TitleMenuIntroBackground",
IntroBigStarSprite = "TitleMenuIntroBigStar",
BackgroundSprite = "TitleMenuBackground",
DelusionADVUnderSprite = "DelusionADVUnder", -- "DelusionADVUnderEnglish" with the TLed assets, "DelusionADVUnder" with the original ones
DelusionADVUnderX = 78, --74 with the TLed assets, 78 with the original ones
Expand Down Expand Up @@ -101,7 +102,8 @@ root.TitleMenu = {
MenuEntriesSprites = {},
MenuEntriesHighlightedSprites = {},
LineNum = 6,
LineEntriesSprites = {}
LineEntriesSprites = {},
IntroStarBounceAnimationSegmentCount = 7
};

for i = 0, 3 do
Expand Down Expand Up @@ -241,6 +243,11 @@ root.Sprites["TitleMenuIntroBackground"] = {
Bounds = { X = 0, Y = 0, Width = 1280, Height = 720 },
};

root.Sprites["TitleMenuIntroBigStar"] = {
Sheet = "Title",
Bounds = { X = 1156, Y = 345, Width = 178, Height = 170 }
};

root.Sprites["TitleMenuBackground"] = {
Sheet = "TitleBg2",
Bounds = { X = 0, Y = 0, Width = 1280, Height = 720 },
Expand Down Expand Up @@ -312,3 +319,55 @@ root.Sprites["TitleMenuSecondaryItemHighlight"] = {
Sheet = "Title",
Bounds = { X = 915, Y = 989, Width = 285, Height = 34 },
};

root.TitleMenu.IntroStarBounceAnimationPath = {
{
StartPosition = { X = 1344, Y = 144 },
EndPosition = { X = 1152, Y = 576 },
Duration = 0.72,
EasingX = EasingFunction.Linear,
EasingY = EasingFunction.QuadraticIn
},
{
StartPosition = { X = 1152, Y = 576 },
EndPosition = { X = 1050, Y = 504 },
Duration = 0.3,
EasingX = EasingFunction.Linear,
EasingY = EasingFunction.QuadraticOut
},
{
StartPosition = { X = 1050, Y = 504 },
EndPosition = { X = 960, Y = 576 },
Duration = 0.3,
EasingX = EasingFunction.Linear,
EasingY = EasingFunction.QuadraticIn
},
{
StartPosition = { X = 960, Y = 576 },
EndPosition = { X = 870, Y = 504 },
Duration = 0.3,
EasingX = EasingFunction.Linear,
EasingY = EasingFunction.QuadraticOut
},
{
StartPosition = { X = 870, Y = 504 },
EndPosition = { X = 780, Y = 576 },
Duration = 0.3,
EasingX = EasingFunction.Linear,
EasingY = EasingFunction.QuadraticIn
},
{
StartPosition = { X = 780, Y = 576 },
EndPosition = { X = 704, Y = 252 },
Duration = 0.66,
EasingX = EasingFunction.Linear,
EasingY = EasingFunction.QuadraticOut
},
{
StartPosition = { X = 704, Y = 252 },
EndPosition = { X = 640, Y = 360 },
Duration = 0.4,
EasingX = EasingFunction.Linear,
EasingY = EasingFunction.QuadraticIn
}
}
24 changes: 23 additions & 1 deletion src/games/chlcc/titlemenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../../profile/games/chlcc/titlemenu.h"
#include "../../profile/scriptvars.h"
#include "../../profile/game.h"
#include <vector>

namespace Impacto {
namespace UI {
Expand All @@ -20,6 +21,7 @@ namespace CHLCC {
using namespace Impacto::Profile::TitleMenu;
using namespace Impacto::Profile::CHLCC::TitleMenu;
using namespace Impacto::Profile::ScriptVars;
using namespace Impacto::Audio;

using namespace Impacto::UI::Widgets::CHLCC;

Expand Down Expand Up @@ -257,6 +259,7 @@ void TitleMenu::Hide() {

void TitleMenu::Update(float dt) {
UpdateInput();
IntroStarBounceAnimation.Update(dt);
PressToStartAnimation.Update(dt);
SpinningCircleAnimation.Update(dt);
PrimaryFadeAnimation.Update(dt);
Expand Down Expand Up @@ -378,6 +381,7 @@ void TitleMenu::Render() {
switch (ScrWork[SW_TITLEDISPCT]) {
case 0: { // Initial animation
Renderer->DrawSprite(IntroBackgroundSprite, glm::vec2(0.0f));
DrawIntroAnimation();
} break;
case 1: { // Press to start
DrawTitleMenuBackGraphics();
Expand Down Expand Up @@ -443,6 +447,24 @@ void TitleMenu::Render() {
}
}

inline void TitleMenu::DrawIntroAnimation() {
if (IntroStarBounceAnimation.State == AS_Stopped &&
IntroStarBounceAnimation.IsOut()) {
IntroStarBounceAnimation.StartOut(true);
} else if (IntroStarBounceAnimation.State == AS_Playing) {
if (IntroStarBounceAnimation.GetCurrentSegmentIndex() == 5 &&
Audio::Channels[Audio::AC_SE0]->State == ACS_Paused) {
Audio::Channels[Audio::AC_SE0]->Resume();
}

glm::vec2 starPosition = IntroStarBounceAnimation.GetPosition() -
glm::vec2(StarLogoSprite.Bounds.Width / 2,
StarLogoSprite.Bounds.Height / 2);

Renderer->DrawSprite(StarLogoSprite, starPosition);
}
}

inline void TitleMenu::DrawTitleMenuBackGraphics() {
Renderer->DrawSprite(BackgroundSprite, glm::vec2(0.0f));
Renderer->DrawSprite(SpinningCircleSprite,
Expand Down Expand Up @@ -472,4 +494,4 @@ inline void TitleMenu::DrawTitleMenuBackGraphics() {

} // namespace CHLCC
} // namespace UI
} // namespace Impacto
} // namespace Impacto
5 changes: 4 additions & 1 deletion src/games/chlcc/titlemenu.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../../animation.h"
#include "../../pathanimation.h"
#include "../../ui/menu.h"
#include "../../ui/widgets/group.h"
#include "../../ui/widgets/button.h"
Expand All @@ -25,10 +26,12 @@ class TitleMenu : public Menu {
Animation ItemsFadeInAnimation;
Animation SecondaryItemsFadeInAnimation;
Animation SpinningCircleAnimation;
PathAnimation IntroStarBounceAnimation;

void MenuButtonOnClick(Widgets::Button* target);
void SecondaryButtonOnClick(Widgets::Button* target);


void DrawIntroAnimation();
void DrawTitleMenuBackGraphics();

private:
Expand Down
81 changes: 81 additions & 0 deletions src/pathanimation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "pathanimation.h"
#include <numeric>

namespace Impacto {

float Ease(float progress, EasingFunction function) {
switch (function) {
case EasingFunction::Linear: {
return progress;
} break;
case EasingFunction::QuadraticIn: {
return progress * progress;
} break;
case EasingFunction::QuadraticOut: {
return 1 - (1 - progress) * (1 - progress);
} break;
case EasingFunction::CubicIn: {
return progress * progress * progress;
} break;
case EasingFunction::CubicOut: {
return 1 - std::powf(1 - progress, 3);
} break;
}
}

PathAnimation::PathAnimation(std::vector<PathSegment> path) : Path(path) {
float totalDuration = 0;
for (PathSegment& segment : path) {
totalDuration += segment.Duration;
}

this->DurationIn = totalDuration;
this->DurationOut = totalDuration;
}

float PathAnimation::GetCurrentSegmentProgress() const {
return CurrentSegmentProgress;
}

size_t PathAnimation::GetCurrentSegmentIndex() const {
return CurrentSegmentIndex;
}

glm::vec2 PathAnimation::GetPosition() const {
const PathSegment& segment = Path[CurrentSegmentIndex];
glm::vec2 direction = segment.EndPosition - segment.StartPosition;

float progress = GetCurrentSegmentProgress();
return segment.StartPosition + glm::vec2(Ease(progress, segment.EasingX),
Ease(progress, segment.EasingY)) *
direction;
}

void PathAnimation::UpdateImpl(float dt) {
Animation::UpdateImpl(dt);

float segmentEndProgress = Direction == 1 ? 0 : 1;
size_t segment = Direction == 1 ? 0 : Path.size() - 1;
size_t end = Direction == 1 ? Path.size() : -1;

while (segment != end) {
float segmentStartProgress = segmentEndProgress;
segmentEndProgress += Path[segment].Duration / this->DurationIn * Direction;

if ((Direction == 1 && this->Progress < segmentEndProgress) ||
(Direction == -1 && this->Progress > segmentEndProgress)) {
CurrentSegmentIndex = segment;

float segmentDuration = Path[segment].Duration;
CurrentSegmentProgress = (this->Progress - std::min(segmentStartProgress,
segmentEndProgress)) /
(segmentDuration / this->DurationIn);

break;
}

segment += Direction;
}
}

} // namespace Impacto
42 changes: 42 additions & 0 deletions src/pathanimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <vector>
#include <glm/vec2.hpp>
#include "enum.h"
#include "animation.h"

namespace Impacto {

BETTER_ENUM(EasingFunction, int, Linear, QuadraticIn, QuadraticOut, CubicIn,
CubicOut)

float Ease(float progress, EasingFunction function = EasingFunction::Linear);

struct PathSegment {
glm::vec2 StartPosition;
glm::vec2 EndPosition;
float Duration;
EasingFunction EasingX = EasingFunction::Linear;
EasingFunction EasingY = EasingX;
};

class PathAnimation : public Animation {
public:
PathAnimation() : PathAnimation(std::vector<PathSegment>()) {};
PathAnimation(std::vector<PathSegment> segments);

std::vector<PathSegment> Path;

glm::vec2 GetPosition() const;
size_t GetCurrentSegmentIndex() const;
float GetCurrentSegmentProgress() const;

protected:
void UpdateImpl(float dt) override;

private:
size_t CurrentSegmentIndex = 0;
float CurrentSegmentProgress = 0;
};

} // namespace Impacto
14 changes: 14 additions & 0 deletions src/profile/games/chlcc/titlemenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace TitleMenu {

void Configure() {
IntroBackgroundSprite = EnsureGetMemberSprite("IntroBackgroundSprite");
IntroBigStarSprite = EnsureGetMemberSprite("IntroBigStarSprite");
BackgroundSprite = EnsureGetMemberSprite("BackgroundSprite");
DelusionADVUnderSprite = EnsureGetMemberSprite("DelusionADVUnderSprite");
DelusionADVUnderX = EnsureGetMemberFloat("DelusionADVUnderX");
Expand Down Expand Up @@ -111,6 +112,14 @@ void Configure() {
EnsureGetMemberFloat("SecondaryMenuSystemConfigY");
SecondaryMenuSystemSaveY = EnsureGetMemberFloat("SecondaryMenuSystemSaveY");

IntroStarBounceAnimationSegmentCount =
EnsureGetMemberInt("IntroStarBounceAnimationSegmentCount");
IntroStarBounceAnimationPath =
new PathSegment[IntroStarBounceAnimationSegmentCount];
GetMemberPathSegmentArray(IntroStarBounceAnimationPath,
IntroStarBounceAnimationSegmentCount,
"IntroStarBounceAnimationPath");

UI::CHLCC::TitleMenu* menu = new UI::CHLCC::TitleMenu();
menu->PressToStartAnimation.DurationIn =
Profile::TitleMenu::PressToStartAnimDurationIn;
Expand All @@ -129,6 +138,11 @@ void Configure() {
menu->SpinningCircleAnimation.DurationIn = SpinningCircleAnimationDuration;
menu->SpinningCircleAnimation.DurationOut = SpinningCircleAnimationDuration;

menu->IntroStarBounceAnimation = PathAnimation(std::vector(
IntroStarBounceAnimationPath,
IntroStarBounceAnimationPath + IntroStarBounceAnimationSegmentCount
));

UI::TitleMenuPtr = menu;

auto drawType = Game::DrawComponentType::_from_integral_unchecked(
Expand Down
17 changes: 17 additions & 0 deletions src/profile/games/chlcc/titlemenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "../../../spritesheet.h"
#include "../../../games/chlcc/titlemenu.h"
#include "../../../pathanimation.h"

namespace Impacto {
namespace Profile {
Expand All @@ -13,6 +14,7 @@ void Configure();
int constexpr LineEntriesNumMax = 32;

inline Sprite IntroBackgroundSprite;
inline Sprite IntroBigStarSprite;
inline Sprite BackgroundSprite;
inline Sprite DelusionADVUnderSprite;
inline Sprite DelusionADVSprite;
Expand Down Expand Up @@ -102,6 +104,21 @@ inline float SecondaryMenuSystemSaveY;

inline int LineNum;

// TODO: Move to lua
/* inline std::array<std::pair<glm::vec3, EasingFunction>, 8>
IntroStarPath = {
std::pair(glm::vec3(1.05f, 0.2f, 0), EasingFunction::Linear),
std::pair(glm::vec3(0.9f, 0.8f, 0.24f), EasingFunction::QuadraticIn),
std::pair(glm::vec3(0.82f, 0.7f, 0.34f), EasingFunction::QuadraticOut),
std::pair(glm::vec3(0.75f, 0.8f, 0.44f), EasingFunction::QuadraticIn),
std::pair(glm::vec3(0.68f, 0.7f, 0.55f), EasingFunction::QuadraticOut),
std::pair(glm::vec3(0.61f, 0.8f, 0.65f), EasingFunction::QuadraticIn),
std::pair(glm::vec3(0.55f, 0.35f, 0.87f), EasingFunction::QuadraticOut),
std::pair(glm::vec3(0.5f, 0.5f, 1), EasingFunction::QuadraticIn)};*/

inline PathSegment* IntroStarBounceAnimationPath;
inline int IntroStarBounceAnimationSegmentCount;

} // namespace TitleMenu
} // namespace CHLCC
} // namespace Profile
Expand Down
1 change: 1 addition & 0 deletions src/profile/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ void MakeLuaProfile(std::string const& name) {
DefineEnumInt<FontType>(LuaState);
DefineEnumInt<LKMVersion>(LuaState);
DefineEnumInt<Dialogue::REVNameLocationType>(LuaState);
DefineEnumInt<EasingFunction>(LuaState);

ImpLog(LL_Info, LC_Profile, "Starting profile %s\n", name.c_str());

Expand Down
Loading

0 comments on commit 6defe2c

Please sign in to comment.