Magic_Game/core/actions/Spawn.cpp

137 lines
2.6 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"
e2d::Spawn::Spawn()
{
}
2018-09-04 22:42:34 +08:00
e2d::Spawn::Spawn(const Actions& actions)
2018-05-10 00:58:43 +08:00
{
2018-09-04 22:42:34 +08:00
this->Add(actions);
}
e2d::Spawn::~Spawn()
{
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-09-04 22:42:34 +08:00
void e2d::Spawn::Init()
{
2018-09-04 22:42:34 +08:00
Action::Init();
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_;
action->Init();
}
}
}
2018-09-04 22:42:34 +08:00
void e2d::Spawn::Update()
{
2018-09-04 22:42:34 +08:00
Action::Update();
size_t doneNum = 0;
2018-09-04 22:42:34 +08:00
for (const auto& action : actions_)
{
2018-09-04 22:42:34 +08:00
if (action->IsDone())
{
2018-05-14 22:51:40 +08:00
++doneNum;
}
else
{
2018-09-04 22:42:34 +08:00
action->Update();
}
}
2018-09-04 22:42:34 +08:00
if (doneNum == actions_.size())
{
2018-09-04 22:42:34 +08:00
this->Stop();
}
}
2018-09-04 22:42:34 +08:00
void e2d::Spawn::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
void e2d::Spawn::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-09-04 22:42:34 +08:00
void e2d::Spawn::Add(Action * action)
{
if (action)
{
2018-09-04 22:42:34 +08:00
actions_.push_back(action);
action->Retain();
}
}
2018-09-04 22:42:34 +08:00
void e2d::Spawn::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-09-04 22:42:34 +08:00
e2d::Spawn * e2d::Spawn::Clone() const
{
2018-09-07 00:28:54 +08:00
auto spawn = new Spawn();
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
spawn->Add(action->Clone());
2018-05-10 14:03:54 +08:00
}
}
2018-05-10 14:03:54 +08:00
return spawn;
}
2018-09-04 22:42:34 +08:00
e2d::Spawn * e2d::Spawn::Reverse() const
{
2018-09-07 00:28:54 +08:00
auto spawn = new Spawn();
2018-09-04 22:42:34 +08:00
if (spawn && !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
spawn->Add(newActions);
}
2018-05-10 14:03:54 +08:00
return spawn;
}