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

150 lines
3.6 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
ActionGroupPtr ActionGroup::Create(Vector<ActionPtr> const& actions, bool sync)
{
ActionGroupPtr ptr = new (std::nothrow) ActionGroup(sync);
if (ptr)
{
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)
{
if (actions_.empty())
{
Done();
return;
}
current_ = actions_.first_item();
current_->Restart(target); // init first action
2020-02-05 19:56:22 +08:00
if (sync_)
2020-01-21 10:09:55 +08:00
{
// init all actions
for (; current_; current_ = current_->next_item())
{
current_->Restart(target);
}
}
}
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())
{
current_ = current_->next_item();
if (current_)
current_->Restart(target); // init next action
else
Complete(target);
}
}
}
else
{
bool done = true;
for (current_ = actions_.first_item(); current_; current_ = current_->next_item())
{
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)
{
actions_.push_back(action);
}
}
2020-01-21 10:09:55 +08:00
2020-02-12 22:34:40 +08:00
void ActionGroup::AddActions(Vector<ActionPtr> const& 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;
if (!actions_.empty())
2020-01-21 10:09:55 +08:00
{
2020-02-05 19:56:22 +08:00
for (auto action = actions_.last_item(); action; action = action->prev_item())
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;
if (!actions_.empty())
2020-01-21 10:09:55 +08:00
{
for (auto action = actions_.last_item(); action; action = action->prev_item())
{
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