| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | #include "..\e2daction.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e2d::Animate::Animate()  | 
					
						
							|  |  |  | 	: _frameIndex(0) | 
					
						
							|  |  |  | 	, _animation(nullptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e2d::Animate::Animate(Animation * animation) | 
					
						
							|  |  |  | 	: _frameIndex(0) | 
					
						
							|  |  |  | 	, _animation(nullptr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	this->setAnimation(animation); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e2d::Animate::~Animate() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e2d::Animation * e2d::Animate::getAnimation() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return _animation; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Animate::setAnimation(Animation * animation) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (animation && animation != _animation) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		SafeRelease(&_animation); | 
					
						
							|  |  |  | 		_animation = animation; | 
					
						
							|  |  |  | 		_animation->retain(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Animate::_init() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Action::_init(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	auto target = dynamic_cast<Sprite*>(_target); | 
					
						
							|  |  |  | 	if (target && _animation) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		target->open(_animation->getFrames()[_frameIndex]); | 
					
						
							|  |  |  | 		++_frameIndex; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Animate::_update() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Action::_update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!_animation) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		this->stop(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while ((Time::getTotalTime() - _last) >= _animation->getInterval()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		auto& frames = _animation->getFrames(); | 
					
						
							|  |  |  | 		auto target = dynamic_cast<Sprite*>(_target); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (target) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			target->open(frames[_frameIndex]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		_frameIndex++; | 
					
						
							| 
									
										
										
										
											2018-05-10 18:18:02 +08:00
										 |  |  | 		_last += _animation->getInterval(); | 
					
						
							| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		if (_frameIndex == frames.size()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			this->stop(); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 18:18:02 +08:00
										 |  |  | void e2d::Animate::_resetTime() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Action::_resetTime(); | 
					
						
							|  |  |  | 	_last = Time::getTotalTime(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | void e2d::Animate::reset() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Action::reset(); | 
					
						
							|  |  |  | 	_frameIndex = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void e2d::Animate::onDestroy() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Action::onDestroy(); | 
					
						
							|  |  |  | 	SafeRelease(&_animation); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e2d::Animate * e2d::Animate::clone() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-10 14:03:54 +08:00
										 |  |  | 	if (_animation) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return new (std::nothrow) Animate(_animation); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return nullptr; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e2d::Animate * e2d::Animate::reverse() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-10 14:03:54 +08:00
										 |  |  | 	if (_animation) | 
					
						
							| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-05-10 14:03:54 +08:00
										 |  |  | 		auto& oldFrames = _animation->getFrames(); | 
					
						
							|  |  |  | 		std::vector<Image*> frames(oldFrames.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!oldFrames.empty()) | 
					
						
							| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-05-10 14:03:54 +08:00
										 |  |  | 			for (auto iter = oldFrames.crbegin(), iterCrend = oldFrames.crend(); iter != iterCrend; ++iter) | 
					
						
							| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-05-10 14:03:54 +08:00
										 |  |  | 				Image* frame = *iter; | 
					
						
							|  |  |  | 				if (frame) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					frames.push_back(frame); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 14:03:54 +08:00
										 |  |  | 		auto animation = new (std::nothrow) Animation(_animation->getInterval(), frames); | 
					
						
							|  |  |  | 		return new (std::nothrow) Animate(animation); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return nullptr; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-05-10 00:58:43 +08:00
										 |  |  | } |