| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +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
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +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
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +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>
 | 
					
						
							| 
									
										
										
										
											2019-12-23 18:05:08 +08:00
										 |  |  |  | #include <kiwano/math/math.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | class Actor; | 
					
						
							|  |  |  |  | class ActionManager; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | KGE_DECLARE_SMART_PTR(Action); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | /**
 | 
					
						
							|  |  |  |  |  * \~chinese | 
					
						
							|  |  |  |  |  * \defgroup Actions <EFBFBD><EFBFBD><EFBFBD><EFBFBD> | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | /**
 | 
					
						
							|  |  |  |  |  * \addtogroup Actions | 
					
						
							|  |  |  |  |  * @{ | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | /// \~chinese
 | 
					
						
							|  |  |  |  | /// @brief <20><><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | class KGE_API Action | 
					
						
							|  |  |  |  |     : public virtual ObjectBase | 
					
						
							|  |  |  |  |     , protected IntrusiveListItem<ActionPtr> | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     friend class ActionManager; | 
					
						
							|  |  |  |  |     friend class ActionGroup; | 
					
						
							|  |  |  |  |     friend IntrusiveList<ActionPtr>; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | public: | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     using DoneCallback = Function<void(Actor* /* target */)>; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     Action(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     virtual ~Action(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     void Resume(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     void Pause(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     void Stop(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD>ʱ
 | 
					
						
							|  |  |  |  |     void SetDelay(Duration delay); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     /// @param loops ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-1 Ϊ<><CEAA><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
 | 
					
						
							|  |  |  |  |     void SetLoops(int loops); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƴ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>ɫ
 | 
					
						
							|  |  |  |  |     void RemoveTargetWhenDone(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     void SetDoneCallback(DoneCallback const& cb); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     void SetLoopDoneCallback(DoneCallback const& cb); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>
 | 
					
						
							|  |  |  |  |     virtual ActionPtr Clone() const = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ת
 | 
					
						
							|  |  |  |  |     virtual ActionPtr Reverse() const = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
 | 
					
						
							|  |  |  |  |     bool IsRunning() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     int GetLoops() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
 | 
					
						
							|  |  |  |  |     Duration GetDelay() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     DoneCallback GetDoneCallback() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     DoneCallback GetLoopDoneCallback() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | protected: | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     virtual void Init(Actor* target); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD>¶<EFBFBD><C2B6><EFBFBD>
 | 
					
						
							|  |  |  |  |     virtual void Update(Actor* target, Duration dt); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʱ<EFBFBD>䲽
 | 
					
						
							|  |  |  |  |     void UpdateStep(Actor* target, Duration dt); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD>ɶ<EFBFBD><C9B6><EFBFBD>
 | 
					
						
							|  |  |  |  |     void Complete(Actor* target); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD>¿<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     void Restart(Actor* target); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD>״̬
 | 
					
						
							|  |  |  |  |     enum class Status | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         NotStarted,  ///< δ<><CEB4>ʼ
 | 
					
						
							|  |  |  |  |         Delayed,     ///< <20>ȴ<EFBFBD><C8B4><EFBFBD>ʱ
 | 
					
						
							|  |  |  |  |         Started,     ///< <20>ѿ<EFBFBD>ʼ
 | 
					
						
							|  |  |  |  |         Done,        ///< <20>ѽ<EFBFBD><D1BD><EFBFBD>
 | 
					
						
							|  |  |  |  |         Removeable   ///< <20><><EFBFBD>Ƴ<EFBFBD>
 | 
					
						
							|  |  |  |  |     }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>״̬
 | 
					
						
							|  |  |  |  |     Status GetStatus() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
 | 
					
						
							|  |  |  |  |     Duration GetElapsed() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD>ɵ<EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     int GetLoopsDone() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |     void Done(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20>Ƿ<EFBFBD><C7B7>ѽ<EFBFBD><D1BD><EFBFBD>
 | 
					
						
							|  |  |  |  |     bool IsDone() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     /// \~chinese
 | 
					
						
							|  |  |  |  |     /// @brief <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>Ƴ<EFBFBD>
 | 
					
						
							|  |  |  |  |     bool IsRemoveable() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 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_; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | }  // namespace kiwano
 |