Magic_Game/src/kiwano/2d/action/ActionGroup.cpp

153 lines
3.7 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.
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Actor.h>
2020-01-21 10:09:55 +08:00
#include <kiwano/2d/action/ActionGroup.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/Logger.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-02-12 22:34:40 +08:00
2020-02-19 12:09:50 +08:00
ActionGroupPtr ActionGroup::Create(const Vector<ActionPtr>& actions, bool sync)
2020-02-12 22:34:40 +08:00
{
2020-02-20 22:27:09 +08:00
ActionGroupPtr ptr = memory::New<ActionGroup>();
2020-02-12 22:34:40 +08:00
if (ptr)
{
2020-02-20 22:27:09 +08:00
ptr->sync_ = sync;
2020-02-12 22:34:40 +08:00
ptr->AddActions(actions);
}
return ptr;
}
2020-01-21 10:09:55 +08:00
ActionGroup::ActionGroup()
2020-02-05 19:56:22 +08:00
: sync_(false)
2020-01-21 10:09:55 +08:00
{
}
2020-02-12 22:34:40 +08:00
ActionGroup::ActionGroup(bool sync)
2020-02-05 19:56:22 +08:00
: sync_(sync)
2020-01-21 10:09:55 +08:00
{
}
ActionGroup::~ActionGroup() {}
void ActionGroup::Init(Actor* target)
{
2020-02-15 17:32:32 +08:00
if (actions_.IsEmpty())
2020-01-21 10:09:55 +08:00
{
Done();
return;
}
2020-02-05 19:56:22 +08:00
if (sync_)
2020-01-21 10:09:55 +08:00
{
// init all actions
2020-02-15 17:32:32 +08:00
for (current_ = actions_.GetFirst(); current_; current_ = current_->GetNext())
2020-01-21 10:09:55 +08:00
{
current_->Restart(target);
}
}
2020-02-15 17:32:32 +08:00
else
{
current_ = actions_.GetFirst();
current_->Restart(target); // init first action
}
2020-01-21 10:09:55 +08:00
}
void ActionGroup::Update(Actor* target, Duration dt)
{
2020-02-05 19:56:22 +08:00
if (!sync_)
2020-01-21 10:09:55 +08:00
{
if (current_)
{
current_->UpdateStep(target, dt);
if (current_->IsDone())
{
2020-02-15 17:32:32 +08:00
current_ = current_->GetNext();
2020-01-21 10:09:55 +08:00
if (current_)
current_->Restart(target); // init next action
else
Complete(target);
}
}
}
else
{
bool done = true;
2020-02-15 17:32:32 +08:00
for (current_ = actions_.GetFirst(); current_; current_ = current_->GetNext())
2020-01-21 10:09:55 +08:00
{
if (!current_->IsDone())
{
done = false;
current_->UpdateStep(target, dt);
}
}
if (done)
{
Complete(target);
}
}
}
2020-02-12 22:34:40 +08:00
void ActionGroup::AddAction(ActionPtr action)
2020-01-21 10:09:55 +08:00
{
if (action)
{
2020-02-15 17:32:32 +08:00
actions_.PushBack(action);
2020-01-21 10:09:55 +08:00
}
}
2020-01-21 10:09:55 +08:00
2020-02-19 12:09:50 +08:00
void ActionGroup::AddActions(const Vector<ActionPtr>& actions)
2020-01-21 10:09:55 +08:00
{
for (const auto& action : actions)
2020-02-12 22:34:40 +08:00
AddAction(action);
2020-01-21 10:09:55 +08:00
}
ActionPtr ActionGroup::Clone() const
{
2020-02-05 19:56:22 +08:00
Vector<ActionPtr> actions;
2020-02-15 17:32:32 +08:00
if (!actions_.IsEmpty())
2020-01-21 10:09:55 +08:00
{
2020-02-15 17:32:32 +08:00
for (auto action = actions_.GetLast(); action; action = action->GetPrev())
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
actions.push_back(action->Clone());
2020-01-21 10:09:55 +08:00
}
}
2020-02-12 23:37:05 +08:00
return InnerClone(ActionGroup::Create(actions, sync_));
2020-01-21 10:09:55 +08:00
}
ActionPtr ActionGroup::Reverse() const
{
2020-02-05 19:56:22 +08:00
Vector<ActionPtr> actions;
2020-02-15 17:32:32 +08:00
if (!actions_.IsEmpty())
2020-01-21 10:09:55 +08:00
{
2020-02-15 17:32:32 +08:00
for (auto action = actions_.GetLast(); action; action = action->GetPrev())
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
actions.push_back(action->Reverse());
2020-01-21 10:09:55 +08:00
}
}
2020-02-12 23:37:05 +08:00
return InnerClone(ActionGroup::Create(actions, sync_));
2020-01-21 10:09:55 +08:00
}
} // namespace kiwano