Example character level animations.
This commit is contained in:
@ -15,12 +15,27 @@ namespace Dawn {
|
||||
|
||||
template<typename T>
|
||||
struct SimpleAnimation : public Animation {
|
||||
protected:
|
||||
/**
|
||||
* Function for subclasses to be "notified" when the value has been
|
||||
* modified.
|
||||
*/
|
||||
virtual void onValueModified() {
|
||||
|
||||
}
|
||||
|
||||
public:
|
||||
easefunction_t *easing = &easeLinear;
|
||||
T *modifies;
|
||||
std::vector<struct SimpleKeyframe<T>> keyframes;
|
||||
|
||||
/**
|
||||
* Constructs a new Simple Animation instance.
|
||||
*
|
||||
* @param modifies Pointer to the value that will be modified.
|
||||
*/
|
||||
SimpleAnimation(T *modifies) {
|
||||
assertNotNull(modifies);
|
||||
this->modifies = modifies;
|
||||
}
|
||||
|
||||
@ -31,6 +46,8 @@ namespace Dawn {
|
||||
* @param value Value at this given time.
|
||||
*/
|
||||
void addKeyframe(float_t time, T value) {
|
||||
assertTrue(time >= 0);
|
||||
|
||||
struct SimpleKeyframe<T> keyframe;
|
||||
keyframe.time = time;
|
||||
keyframe.value = value;
|
||||
@ -83,6 +100,17 @@ namespace Dawn {
|
||||
this->addSequentialKeyframes(0, frameTime, start, end, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Immediately sets the value, bypassing keyframes and ticks. Useful for
|
||||
* setting an initial value.
|
||||
*
|
||||
* @param value Value to set.
|
||||
*/
|
||||
void setValue(T value) {
|
||||
*modifies = value;
|
||||
this->onValueModified();
|
||||
}
|
||||
|
||||
void tick(float_t delta) override {
|
||||
if(this->finished) return;
|
||||
|
||||
@ -106,6 +134,7 @@ namespace Dawn {
|
||||
if(keyframeCurrent != nullptr && keyframeNext == nullptr) {
|
||||
// "End of animation"
|
||||
*this->modifies = keyframeCurrent->value;
|
||||
this->onValueModified();
|
||||
} else if(keyframeNext != nullptr) {
|
||||
T oldValue;
|
||||
float_t oldTime;
|
||||
@ -127,6 +156,13 @@ namespace Dawn {
|
||||
*this->modifies = oldValue + (
|
||||
(keyframeNext->value - oldValue) * keyframeDelta
|
||||
);
|
||||
this->onValueModified();
|
||||
}
|
||||
|
||||
// First possible frame? I think this can be done cleaner
|
||||
if(this->time == 0 && keyframeCurrent->time == 0) {
|
||||
*this->modifies = keyframeCurrent->value;
|
||||
this->onValueModified();
|
||||
}
|
||||
|
||||
// Update time.
|
||||
|
51
src/dawn/display/animation/SimpleCallbackAnimation.hpp
Normal file
51
src/dawn/display/animation/SimpleCallbackAnimation.hpp
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "SimpleAnimation.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
template<typename T, class I>
|
||||
struct SimpleCallbackAnimation : public SimpleAnimation<T> {
|
||||
protected:
|
||||
I *instance;
|
||||
void (I::*callback)(T arg);
|
||||
T value;
|
||||
|
||||
/**
|
||||
* Internally invoke the function that this animation is owning.
|
||||
*/
|
||||
void invoke() {
|
||||
assertNotNull(this->instance);
|
||||
((*this->instance).*(this->callback))(this->value);
|
||||
}
|
||||
|
||||
void onValueModified() override {
|
||||
SimpleAnimation::onValueModified();
|
||||
this->invoke();
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Construct a new Simple Function Animation object
|
||||
*/
|
||||
SimpleCallbackAnimation() :
|
||||
SimpleAnimation(&value)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the callback for the animation to use.
|
||||
*
|
||||
* @param instance Instance of the object that has the callback.
|
||||
* @param callback Callback method to be invoked.
|
||||
*/
|
||||
void setCallback(I *instance, void (I::*callback)(T arg)) {
|
||||
assertNotNull(instance);
|
||||
this->instance = instance;
|
||||
this->callback = callback;
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user