Magic_Game/core/actions/Loop.cpp

117 lines
2.2 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::Loop::Loop(Action * action, int times /* = -1 */)
2018-09-04 22:42:34 +08:00
: action_(action)
, times_(0)
, total_times_(times)
{
2018-05-24 12:24:39 +08:00
WARN_IF(action == nullptr, "Loop NULL pointer exception!");
2018-05-10 14:03:54 +08:00
if (action)
{
2018-09-04 22:42:34 +08:00
action_ = action;
action_->Retain();
2018-05-10 14:03:54 +08:00
}
}
e2d::Loop::~Loop()
{
2018-09-07 00:28:54 +08:00
SafeRelease(action_);
}
2018-09-04 22:42:34 +08:00
e2d::Loop * e2d::Loop::Clone() const
{
2018-09-04 22:42:34 +08:00
if (action_)
2018-05-10 14:03:54 +08:00
{
2018-09-07 00:28:54 +08:00
return new Loop(action_->Clone());
2018-05-10 14:03:54 +08:00
}
else
{
return nullptr;
}
}
2018-09-04 22:42:34 +08:00
e2d::Loop * e2d::Loop::Reverse() const
2018-05-14 00:36:01 +08:00
{
2018-09-04 22:42:34 +08:00
if (action_)
2018-05-24 12:24:39 +08:00
{
2018-09-07 00:28:54 +08:00
return new Loop(action_->Clone());
2018-05-24 12:24:39 +08:00
}
else
{
return nullptr;
}
2018-05-14 00:36:01 +08:00
}
2018-09-04 22:42:34 +08:00
void e2d::Loop::Init()
{
2018-09-04 22:42:34 +08:00
Action::Init();
2018-05-10 14:03:54 +08:00
2018-09-04 22:42:34 +08:00
if (action_)
2018-05-10 14:03:54 +08:00
{
2018-09-04 22:42:34 +08:00
action_->target_ = target_;
action_->Init();
2018-05-10 14:03:54 +08:00
}
}
2018-09-04 22:42:34 +08:00
void e2d::Loop::Update()
{
2018-09-04 22:42:34 +08:00
Action::Update();
2018-09-04 22:42:34 +08:00
if (times_ == total_times_)
{
2018-09-04 22:42:34 +08:00
this->Stop();
return;
}
2018-09-04 22:42:34 +08:00
if (action_)
2018-05-10 14:03:54 +08:00
{
2018-09-04 22:42:34 +08:00
action_->Update();
2018-09-04 22:42:34 +08:00
if (action_->IsDone())
2018-05-10 14:03:54 +08:00
{
2018-09-04 22:42:34 +08:00
++times_;
2018-05-10 14:03:54 +08:00
2018-09-04 22:42:34 +08:00
Action::Reset();
action_->Reset();
2018-05-10 14:03:54 +08:00
}
}
else
{
2018-09-04 22:42:34 +08:00
this->Stop();
}
}
2018-09-04 22:42:34 +08:00
void e2d::Loop::Reset()
{
2018-09-04 22:42:34 +08:00
Action::Reset();
2018-09-04 22:42:34 +08:00
if (action_) action_->Reset();
times_ = 0;
}
2018-09-04 22:42:34 +08:00
void e2d::Loop::ResetTime()
{
2018-09-04 22:42:34 +08:00
if (action_) action_->ResetTime();
}