effectmaster.cpp 3.92 KB
Newer Older
魔大农's avatar
魔大农 committed
1
/* Quatter
魔大农's avatar
魔大农 committed
2
// Copyright (C) 2017 LucKey Productions (luckeyproductions.nl)
魔大农's avatar
魔大农 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

19 20 21 22
#include "effectmaster.h"

using namespace Urho3D;

23
EffectMaster::EffectMaster(Context* context) : Master(context)
24 25 26
{
}

27
void EffectMaster::FadeTo(Material* material, Color color, float duration, float delay)
28
{
魔大农's avatar
魔大农 committed
29
    Color startColor{ material->GetShaderParameter("MatDiffColor").GetColor() };
30
    ValueAnimation* fade{ new ValueAnimation(context_) };
31

32
    fade->SetKeyFrame(0.0f, startColor);
33 34 35 36
    if (delay)
        fade->SetKeyFrame(delay, startColor);
    fade->SetKeyFrame(delay + duration, color);

37 38 39 40 41
    material->SetShaderParameterAnimation("MatDiffColor", fade, WM_ONCE);
}

void EffectMaster::FadeTo(Light* light, float brightness, float duration)
{
42
    ValueAnimation* fade{ new ValueAnimation(context_) };
43 44 45 46 47
    fade->SetKeyFrame(0.0f, light->GetBrightness());
    fade->SetKeyFrame(duration, brightness);
    light->SetAttributeAnimation("Brightness Multiplier", fade, WM_ONCE);
}

魔大农's avatar
魔大农 committed
48 49
void EffectMaster::FadeTo(SoundSource* soundSource, float gain, float duration)
{
魔大农's avatar
魔大农 committed
50
    ValueAnimation* fade{ new ValueAnimation(context_) };
魔大农's avatar
魔大农 committed
51 52 53 54 55 56 57
    fade->SetKeyFrame(0.0f, soundSource->GetGain());
    fade->SetKeyFrame(0.42f * duration, Lerp(soundSource->GetGain(), gain, 0.5f));
    fade->SetKeyFrame(duration, gain);
    soundSource->SetAttributeAnimation("Gain", fade, WM_ONCE);
}
void EffectMaster::FadeOut(SoundSource* soundSource, float duration)
{
58
    float lastGain{ soundSource->GetGain() };
魔大农's avatar
魔大农 committed
59

魔大农's avatar
魔大农 committed
60
    ValueAnimation* fade{ new ValueAnimation(context_) };
魔大农's avatar
魔大农 committed
61 62 63 64 65 66 67
    fade->SetKeyFrame(0.0f, lastGain);
    fade->SetKeyFrame(0.2f * duration, 0.5f * lastGain);
    fade->SetKeyFrame(0.46f * duration, 0.1f * lastGain);
    fade->SetKeyFrame(duration, 0.0f);
    soundSource->SetAttributeAnimation("Gain", fade, WM_ONCE);
}

魔大农's avatar
魔大农 committed
68 69
void EffectMaster::TransformTo(Node* node, Vector3 pos, Quaternion rot, float duration)
{
70
    ValueAnimation* posAnim{ new ValueAnimation(context_) };
魔大农's avatar
魔大农 committed
71

魔大农's avatar
魔大农 committed
72 73 74 75
    posAnim->SetKeyFrame(0.0f, node->GetPosition());
    posAnim->SetKeyFrame(duration, pos);
    node->SetAttributeAnimation("Position", posAnim, WM_ONCE);

76
    ValueAnimation* rotAnim{ new ValueAnimation(context_) };
魔大农's avatar
魔大农 committed
77

魔大农's avatar
魔大农 committed
78 79 80 81 82
    rotAnim->SetKeyFrame(0.0f, node->GetRotation());
    rotAnim->SetKeyFrame(duration, rot);
    node->SetAttributeAnimation("Rotation", rotAnim, WM_ONCE);
}

魔大农's avatar
魔大农 committed
83
void EffectMaster::ArchTo(Node* node, Vector3 pos, Quaternion rot, float archHeight, float duration, float delay)
魔大农's avatar
魔大农 committed
84
{
魔大农's avatar
魔大农 committed
85
    ValueAnimation* posAnim{ new ValueAnimation(context_) };
魔大农's avatar
魔大农 committed
86
    posAnim->SetKeyFrame(0.0f, node->GetPosition());
87

88 89
    for (int i{0}; i < WAYPOINTS - 1; ++i) {

魔大农's avatar
魔大农 committed
90 91
        float t{ static_cast<float>(i) / WAYPOINTS };
        float t2{ 0.5f * (t + (0.5f + 0.5f * pow(2.0f * (t - 0.5f), 3.0f))) };
魔大农's avatar
魔大农 committed
92
        posAnim->SetKeyFrame(delay + (duration * t2),
魔大农's avatar
魔大农 committed
93 94 95 96
                             node->GetPosition().Lerp(pos, t) + archHeight * Vector3::UP * Arch(t));
    }
    posAnim->SetKeyFrame(delay + duration, pos);
    node->SetAttributeAnimation("Position", posAnim, WM_ONCE);
97

98
    ValueAnimation* rotAnim{ new ValueAnimation(context_) };
魔大农's avatar
魔大农 committed
99

魔大农's avatar
魔大农 committed
100
    rotAnim->SetKeyFrame(0.0f, node->GetRotation());
魔大农's avatar
魔大农 committed
101
    if (delay != 0.0f)
魔大农's avatar
魔大农 committed
102 103 104 105
        rotAnim->SetKeyFrame(delay, node->GetRotation());
    rotAnim->SetKeyFrame(duration, rot);
    node->SetAttributeAnimation("Rotation", rotAnim, WM_ONCE);
}