diff --git a/src/scriptengine/property_animator.cpp b/src/scriptengine/property_animator.cpp new file mode 100644 index 000000000..3ded5eff9 --- /dev/null +++ b/src/scriptengine/property_animator.cpp @@ -0,0 +1,106 @@ +#include "scriptengine/property_animator.hpp" +#include "tracks/track.hpp" +#include "tracks/track_object_presentation.hpp" +#include "utils/log.hpp" + +AnimatedProperty::AnimatedProperty(AnimatablePropery property, double from, double to, + double duration, void* data) +{ + m_property = property; + m_value_from = from; + m_value_to = to; + m_remaining_time = m_total_time = duration; + m_data = data; +} + +// ---------------------------------------------------------------------------- + +bool AnimatedProperty::update(double dt) +{ + bool done = false; + m_remaining_time -= dt; + if (m_remaining_time < 0) + { + m_remaining_time = 0; + done = true; + } + + double ratio = 1.0 - m_remaining_time / m_total_time; + double new_value = m_value_from + (m_value_to - m_value_from) * ratio; + + switch (m_property) + { + case AnimatablePropery::AP_LIGHT_ENERGY: + { + TrackObjectPresentationLight* light = (TrackObjectPresentationLight*)m_data; + light->setEnergy((float)new_value); + break; + } + + case AnimatablePropery::FOG_START: + { + Track* track = (Track*)m_data; + track->setFogStart((float)new_value); + break; + } + + case AnimatablePropery::FOG_END: + { + Track* track = (Track*)m_data; + track->setFogEnd((float)new_value); + break; + } + + case AnimatablePropery::FOG_MAX: + { + Track* track = (Track*)m_data; + track->setFogMax((float)new_value); + break; + } + + default: + Log::error("PropertyAnimator", "Unknown properry %i", (int)m_property); + break; + } + + return done; +} + +// ---------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- + +PropertyAnimator* PropertyAnimator::s_instance = NULL; + +PropertyAnimator* PropertyAnimator::get() +{ + if (s_instance == NULL) + s_instance = new PropertyAnimator(); + + return s_instance; +} + +// ---------------------------------------------------------------------------- + +void PropertyAnimator::add(AnimatedProperty* prop) +{ + m_properties.push_back(prop); +} + +// ---------------------------------------------------------------------------- + +void PropertyAnimator::update(double dt) +{ + for (int i = m_properties.size() - 1; i >= 0; i--) + { + bool done = m_properties[i].update(dt); + if (done) + m_properties.erase(i); + } +} + +// ---------------------------------------------------------------------------- + +void PropertyAnimator::clear() +{ + m_properties.clearAndDeleteAll(); +} diff --git a/src/scriptengine/property_animator.hpp b/src/scriptengine/property_animator.hpp new file mode 100644 index 000000000..21c6e7892 --- /dev/null +++ b/src/scriptengine/property_animator.hpp @@ -0,0 +1,61 @@ +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014-2015 SuperTuxKart Team +// +// 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 3 +// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef HEADER_PROPERTY_ANIMATOR_HPP +#define HEADER_PROPERTY_ANIMATOR_HPP + +#include "utils/ptr_vector.hpp" + +enum AnimatablePropery +{ + AP_LIGHT_ENERGY, + FOG_START, + FOG_END, + FOG_MAX +}; + +class AnimatedProperty +{ + AnimatablePropery m_property; + double m_value_from; + double m_value_to; + double m_total_time; + double m_remaining_time; + void* m_data; + +public: + AnimatedProperty(AnimatablePropery property, double from, double to, double duration, void* data); + + bool update(double dt); +}; + + +class PropertyAnimator +{ + PtrVector m_properties; + static PropertyAnimator* s_instance; +public: + + static PropertyAnimator* get(); + + void add(AnimatedProperty* prop); + void update(double dt); + void clear(); +}; + + +#endif diff --git a/src/scriptengine/script_track.cpp b/src/scriptengine/script_track.cpp index eb36a58a8..a450ae22d 100644 --- a/src/scriptengine/script_track.cpp +++ b/src/scriptengine/script_track.cpp @@ -134,6 +134,23 @@ namespace Scripting { new RacePausedDialog(0.8f, 0.6f); } + + void setFog(float maxDensity, float start, float end, int r, int g, int b, float duration) + { + PropertyAnimator* animator = PropertyAnimator::get(); + ::Track* track = World::getWorld()->getTrack(); + animator->add( + new AnimatedProperty(FOG_MAX, track->getFogMax(), maxDensity, duration, track) + ); + animator->add( + new AnimatedProperty(FOG_START, track->getFogStart(), start, duration, track) + ); + animator->add( + new AnimatedProperty(FOG_END, track->getFogEnd(), end, duration, track) + ); + + // TODO: animate fog color + } } /** \cond DOXYGEN_IGNORE */ @@ -331,6 +348,7 @@ namespace Scripting r = engine->RegisterGlobalFunction("TrackObject@ getTrackObject(const string &in, const string &in)", asFUNCTION(getTrackObject), asCALL_CDECL); assert(r >= 0); r = engine->RegisterGlobalFunction("void exitRace()", asFUNCTION(exitRace), asCALL_CDECL); assert(r >= 0); r = engine->RegisterGlobalFunction("void pauseRace()", asFUNCTION(pauseRace), asCALL_CDECL); assert(r >= 0); + r = engine->RegisterGlobalFunction("void setFog(float maxDensity, float start, float end, int r, int g, int b, float duration)", asFUNCTION(setFog), asCALL_CDECL); assert(r >= 0); // TrackObject r = engine->RegisterObjectMethod("TrackObject", "void setEnabled(bool status)", asMETHOD(::TrackObject, setEnabled), asCALL_THISCALL); assert(r >= 0); diff --git a/src/tracks/track.hpp b/src/tracks/track.hpp index 9f35487d0..74433fd20 100644 --- a/src/tracks/track.hpp +++ b/src/tracks/track.hpp @@ -550,14 +550,20 @@ public: // ------------------------------------------------------------------------ float getFogStart() const { return m_fog_start; } // ------------------------------------------------------------------------ + void setFogStart(float start) { m_fog_start = start; } + // ------------------------------------------------------------------------ float getFogEnd() const { return m_fog_end; } // ------------------------------------------------------------------------ + void setFogEnd(float end) { m_fog_end = end; } + // ------------------------------------------------------------------------ float getFogStartHeight() const { return m_fog_height_start; } // ------------------------------------------------------------------------ float getFogEndHeight() const { return m_fog_height_end; } // ------------------------------------------------------------------------ float getFogMax() const { return m_fog_max; } // ------------------------------------------------------------------------ + void setFogMax(float max) { m_fog_max = max; } + // ------------------------------------------------------------------------ video::SColor getFogColor() const { return m_fog_color; } // ------------------------------------------------------------------------ video::SColor getSunColor() const { return m_sun_diffuse_color; }