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

147 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>
2020-05-24 11:26:21 +08:00
#include <kiwano/utils/Logger.h>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
2020-02-12 22:34:40 +08:00
2020-07-20 21:35:33 +08:00
ActionGroup::ActionGroup(const Vector<ActionEntityPtr>& actions, bool parallel)
2020-02-12 22:34:40 +08:00
{
2020-07-22 21:08:48 +08:00
SetEntity(MakePtr<ActionGroupEntity>(actions, parallel));
2020-02-12 22:34:40 +08:00
}
2020-01-21 10:09:55 +08:00
2020-07-20 21:35:33 +08:00
ActionGroupEntity::ActionGroupEntity()
2020-06-28 16:22:46 +08:00
: parallel_(false)
2020-01-21 10:09:55 +08:00
{
}
2020-07-22 21:08:48 +08:00
ActionGroupEntity::ActionGroupEntity(const Vector<ActionEntityPtr>& actions, bool parallel)
2020-06-28 16:22:46 +08:00
: parallel_(parallel)
2020-01-21 10:09:55 +08:00
{
2020-07-22 21:08:48 +08:00
AddActions(actions);
2020-01-21 10:09:55 +08:00
}
2020-07-20 21:35:33 +08:00
ActionGroupEntity::~ActionGroupEntity() {}
2020-01-21 10:09:55 +08:00
2020-07-20 21:35:33 +08:00
void ActionGroupEntity::Init(Actor* target)
2020-01-21 10:09:55 +08:00
{
2020-02-15 17:32:32 +08:00
if (actions_.IsEmpty())
2020-01-21 10:09:55 +08:00
{
Done();
return;
}
2020-07-20 21:35:33 +08:00
// reset all actions
for (current_ = actions_.GetFirst(); current_; current_ = current_->GetNext())
2020-01-21 10:09:55 +08:00
{
2020-07-20 21:35:33 +08:00
current_->Reset();
2020-01-21 10:09:55 +08:00
}
2020-07-20 21:35:33 +08:00
if (!parallel_)
2020-02-15 17:32:32 +08:00
{
current_ = actions_.GetFirst();
}
2020-01-21 10:09:55 +08:00
}
2020-07-20 21:35:33 +08:00
void ActionGroupEntity::Update(Actor* target, Duration dt)
2020-01-21 10:09:55 +08:00
{
2020-06-28 16:22:46 +08:00
if (!parallel_)
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
2020-07-20 21:35:33 +08:00
if (!current_)
2020-01-21 10:09:55 +08:00
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-07-20 21:35:33 +08:00
void ActionGroupEntity::AddAction(ActionEntityPtr 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-07-20 21:35:33 +08:00
void ActionGroupEntity::AddActions(const Vector<ActionEntityPtr>& 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
}
2020-07-22 21:08:48 +08:00
ActionGroupEntity* ActionGroupEntity::Clone() const
2020-01-21 10:09:55 +08:00
{
2020-07-20 21:35:33 +08:00
Vector<ActionEntityPtr> actions;
2020-02-15 17:32:32 +08:00
if (!actions_.IsEmpty())
2020-01-21 10:09:55 +08:00
{
2020-07-20 21:35:33 +08:00
for (auto action = actions_.GetFirst(); action; action = action->GetNext())
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-07-23 17:21:44 +08:00
ActionGroupEntity* ptr = new ActionGroupEntity(actions, parallel_);
2020-07-22 21:08:48 +08:00
DoClone(ptr);
return ptr;
2020-01-21 10:09:55 +08:00
}
2020-07-22 21:08:48 +08:00
ActionGroupEntity* ActionGroupEntity::Reverse() const
2020-01-21 10:09:55 +08:00
{
2020-07-20 21:35:33 +08:00
Vector<ActionEntityPtr> 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-07-23 17:21:44 +08:00
ActionGroupEntity* ptr = new ActionGroupEntity(actions, parallel_);
2020-07-22 21:08:48 +08:00
DoClone(ptr);
return ptr;
2020-01-21 10:09:55 +08:00
}
} // namespace kiwano