Magic_Game/core/actions/Sequence.cpp

139 lines
2.9 KiB
C++
Raw Normal View History

2018-10-03 22:02:46 +08:00
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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.
#include "..\e2daction.h"
2018-10-16 14:13:15 +08:00
easy2d::Sequence::Sequence()
2018-09-04 22:42:34 +08:00
: action_index_(0)
{
}
2018-10-16 14:13:15 +08:00
easy2d::Sequence::Sequence(const Actions& actions)
2018-09-04 22:42:34 +08:00
: action_index_(0)
2018-05-10 00:58:43 +08:00
{
2018-09-04 22:42:34 +08:00
this->Add(actions);
}
2018-10-16 14:13:15 +08:00
easy2d::Sequence::~Sequence()
{
2018-09-07 00:28:54 +08:00
for (auto action : actions_)
2018-07-07 01:43:41 +08:00
{
2018-09-07 00:28:54 +08:00
SafeRelease(action);
2018-07-07 01:43:41 +08:00
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Sequence::Init()
{
2018-09-04 22:42:34 +08:00
Action::Init();
// <20><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
if (target_)
{
2018-09-04 22:42:34 +08:00
for (const auto& action : actions_)
{
2018-09-04 22:42:34 +08:00
action->target_ = target_;
}
}
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2018-09-04 22:42:34 +08:00
actions_[0]->Init();
}
2018-10-16 14:13:15 +08:00
void easy2d::Sequence::Update()
{
2018-09-04 22:42:34 +08:00
Action::Update();
2018-09-04 22:42:34 +08:00
auto &action = actions_[action_index_];
action->Update();
2018-09-04 22:42:34 +08:00
if (action->IsDone())
{
2018-09-04 22:42:34 +08:00
++action_index_;
if (action_index_ == actions_.size())
{
2018-09-04 22:42:34 +08:00
this->Stop();
}
else
{
2018-09-04 22:42:34 +08:00
actions_[action_index_]->Init();
}
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Sequence::Reset()
{
2018-09-04 22:42:34 +08:00
Action::Reset();
for (const auto& action : actions_)
{
2018-09-04 22:42:34 +08:00
action->Reset();
}
2018-09-04 22:42:34 +08:00
action_index_ = 0;
}
2018-10-16 14:13:15 +08:00
void easy2d::Sequence::ResetTime()
{
2018-09-04 22:42:34 +08:00
for (const auto& action : actions_)
{
2018-09-04 22:42:34 +08:00
action->ResetTime();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Sequence::Add(Action * action)
{
if (action)
{
2018-09-04 22:42:34 +08:00
actions_.push_back(action);
action->Retain();
}
}
2018-10-16 14:13:15 +08:00
void easy2d::Sequence::Add(const Actions& actions)
2018-05-10 00:58:43 +08:00
{
for (const auto &action : actions)
{
2018-09-04 22:42:34 +08:00
this->Add(action);
}
}
2018-10-16 14:13:15 +08:00
easy2d::Sequence * easy2d::Sequence::Clone() const
{
2018-09-07 00:28:54 +08:00
auto sequence = new Sequence();
2018-09-04 22:42:34 +08:00
for (const auto& action : actions_)
{
2018-05-10 14:03:54 +08:00
if (action)
{
2018-09-04 22:42:34 +08:00
sequence->Add(action->Clone());
2018-05-10 14:03:54 +08:00
}
}
2018-05-10 14:03:54 +08:00
return sequence;
}
2018-10-16 14:13:15 +08:00
easy2d::Sequence * easy2d::Sequence::Reverse() const
{
2018-09-07 00:28:54 +08:00
auto sequence = new Sequence();
2018-09-04 22:42:34 +08:00
if (sequence && !actions_.empty())
{
2018-09-04 22:42:34 +08:00
std::vector<Action*> newActions(actions_.size());
for (auto iter = actions_.crbegin(), iterCrend = actions_.crend(); iter != iterCrend; ++iter)
2018-05-10 14:03:54 +08:00
{
2018-09-04 22:42:34 +08:00
newActions.push_back((*iter)->Reverse());
2018-05-10 14:03:54 +08:00
}
2018-09-04 22:42:34 +08:00
sequence->Add(newActions);
}
2018-05-10 14:03:54 +08:00
return sequence;
}