Magic_Game/src/kiwano/2d/action/Action.h

294 lines
6.2 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2020-01-21 10:09:55 +08:00
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Common.h>
2019-12-23 18:05:08 +08:00
#include <kiwano/core/ObjectBase.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/core/SmartPtr.hpp>
#include <kiwano/core/Time.h>
2020-02-15 17:32:32 +08:00
#include <kiwano/core/IntrusiveList.h>
2020-02-06 16:54:47 +08:00
#include <kiwano/math/Math.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
class Actor;
class ActionManager;
KGE_DECLARE_SMART_PTR(Action);
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* \defgroup Actions
2020-01-21 10:09:55 +08:00
*/
/**
* \addtogroup Actions
* @{
*/
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 动画
2020-01-21 10:09:55 +08:00
class KGE_API Action
: public virtual ObjectBase
2020-02-15 17:32:32 +08:00
, protected IntrusiveListValue<ActionPtr>
2020-01-21 10:09:55 +08:00
{
friend class ActionManager;
friend class ActionGroup;
friend IntrusiveList<ActionPtr>;
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 动画结束时的回调函数
2020-01-21 10:09:55 +08:00
using DoneCallback = Function<void(Actor* /* target */)>;
Action();
virtual ~Action();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 继续动画
2020-01-21 10:09:55 +08:00
void Resume();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 暂停动画
2020-01-21 10:09:55 +08:00
void Pause();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 停止动画
2020-01-21 10:09:55 +08:00
void Stop();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置动画延时
2020-01-21 10:09:55 +08:00
void SetDelay(Duration delay);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置循环次数
/// @param loops 循环次数,-1 为永久循环
2020-01-21 10:09:55 +08:00
void SetLoops(int loops);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 动画结束时移除目标角色
2020-01-21 10:09:55 +08:00
void RemoveTargetWhenDone();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置动画结束时的回调函数
2020-01-21 10:09:55 +08:00
void SetDoneCallback(DoneCallback const& cb);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置动画循环结束时的回调函数
2020-01-21 10:09:55 +08:00
void SetLoopDoneCallback(DoneCallback const& cb);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取动画的拷贝
2020-01-21 10:09:55 +08:00
virtual ActionPtr Clone() const = 0;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取动画的倒转
2020-01-21 10:09:55 +08:00
virtual ActionPtr Reverse() const = 0;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取动画的运行状态
2020-01-21 10:09:55 +08:00
bool IsRunning() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取动画的循环次数
2020-01-21 10:09:55 +08:00
int GetLoops() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取动画的延时
2020-01-21 10:09:55 +08:00
Duration GetDelay() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取动画结束时的回调函数
2020-01-21 10:09:55 +08:00
DoneCallback GetDoneCallback() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取动画循环结束时的回调函数
2020-01-21 10:09:55 +08:00
DoneCallback GetLoopDoneCallback() const;
protected:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 初始化动画
2020-01-21 10:09:55 +08:00
virtual void Init(Actor* target);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 更新动画
2020-01-21 10:09:55 +08:00
virtual void Update(Actor* target, Duration dt);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 更新一个时间步
2020-01-21 10:09:55 +08:00
void UpdateStep(Actor* target, Duration dt);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 完成动画
2020-01-21 10:09:55 +08:00
void Complete(Actor* target);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 重新开始动画
2020-01-21 10:09:55 +08:00
void Restart(Actor* target);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 动画状态
2020-01-21 10:09:55 +08:00
enum class Status
{
2020-02-10 17:32:04 +08:00
NotStarted, ///< 未开始
Delayed, ///< 等待延时
Started, ///< 已开始
Done, ///< 已结束
Removeable ///< 可移除
2020-01-21 10:09:55 +08:00
};
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取动画状态
2020-01-21 10:09:55 +08:00
Status GetStatus() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取消逝时间
2020-01-21 10:09:55 +08:00
Duration GetElapsed() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取完成的循环次数
2020-01-21 10:09:55 +08:00
int GetLoopsDone() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 结束动画
2020-01-21 10:09:55 +08:00
void Done();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否已结束
2020-01-21 10:09:55 +08:00
bool IsDone() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否可移除
2020-01-21 10:09:55 +08:00
bool IsRemoveable() const;
2020-02-12 23:37:05 +08:00
protected:
ActionPtr InnerClone(ActionPtr to) const;
2020-01-21 10:09:55 +08:00
private:
Status status_;
bool running_;
bool detach_target_;
int loops_;
int loops_done_;
Duration delay_;
Duration elapsed_;
DoneCallback cb_done_;
DoneCallback cb_loop_done_;
};
/** @} */
inline void Action::Resume()
{
running_ = true;
}
inline void Action::Pause()
{
running_ = false;
}
inline void Action::Stop()
{
Done();
}
inline void Action::SetDelay(Duration delay)
{
delay_ = delay;
}
inline void Action::SetLoops(int loops)
{
loops_ = loops;
}
inline void Action::RemoveTargetWhenDone()
{
detach_target_ = true;
}
inline void Action::SetDoneCallback(DoneCallback const& cb)
{
cb_done_ = cb;
}
inline void Action::SetLoopDoneCallback(DoneCallback const& cb)
{
cb_loop_done_ = cb;
}
inline void Action::Done()
{
status_ = Status::Done;
}
inline Action::Status Action::GetStatus() const
{
return status_;
}
inline bool Action::IsRunning() const
{
return running_;
}
inline bool Action::IsDone() const
{
return status_ == Status::Done || status_ == Status::Removeable;
}
inline bool Action::IsRemoveable() const
{
return status_ == Status::Removeable;
}
inline int Action::GetLoops() const
{
return loops_;
}
inline Duration Action::GetDelay() const
{
return delay_;
}
inline Duration Action::GetElapsed() const
{
return elapsed_;
}
inline int Action::GetLoopsDone() const
{
return loops_done_;
}
inline Action::DoneCallback Action::GetDoneCallback() const
{
return cb_done_;
}
inline Action::DoneCallback Action::GetLoopDoneCallback() const
{
return cb_loop_done_;
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano