refactoring: Game::EnterScene

This commit is contained in:
Nomango 2018-09-10 15:28:19 +08:00
parent f7dba39243
commit 83d0b3f5cf
8 changed files with 67 additions and 78 deletions

View File

@ -339,14 +339,10 @@ namespace e2d
// 游戏是否暂停 // 游戏是否暂停
bool IsPaused(); bool IsPaused();
// 场景入栈 // 切换场景
void EnterScene( void EnterScene(
Scene * scene /* 下一个场景的指针 */ Scene * scene, /* 场景 */
); Transition * transition = nullptr /* 场景动画 */
// 场景入栈
void EnterScene(
Transition * transition /* 场景动画 */
); );
// 获取当前场景 // 获取当前场景

View File

@ -16,7 +16,6 @@ namespace e2d
public: public:
explicit Transition( explicit Transition(
Scene* scene,
float duration float duration
); );
@ -27,9 +26,9 @@ namespace e2d
protected: protected:
// 初始化场景过渡动画 // 初始化场景过渡动画
virtual bool Init( virtual void Init(
Game * game, Scene * prev,
Scene * prev Scene * next
); );
// 更新场景过渡动画 // 更新场景过渡动画
@ -64,7 +63,6 @@ namespace e2d
{ {
public: public:
explicit FadeTransition( explicit FadeTransition(
Scene* scene, /* 切换的场景 */
float duration /* 动画持续时长 */ float duration /* 动画持续时长 */
); );
@ -72,9 +70,9 @@ namespace e2d
// 更新动画 // 更新动画
virtual void Update() override; virtual void Update() override;
virtual bool Init( virtual void Init(
Game * game, Scene * prev,
Scene * prev Scene * next
) override; ) override;
}; };
@ -85,16 +83,15 @@ namespace e2d
{ {
public: public:
explicit EmergeTransition( explicit EmergeTransition(
Scene* scene, /* 切换的场景 */
float duration /* 浮现动画持续时长 */ float duration /* 浮现动画持续时长 */
); );
protected: protected:
virtual void Update() override; virtual void Update() override;
virtual bool Init( virtual void Init(
Game * game, Scene * prev,
Scene * prev Scene * next
) override; ) override;
}; };
@ -105,16 +102,15 @@ namespace e2d
{ {
public: public:
explicit BoxTransition( explicit BoxTransition(
Scene* scene, /* 切换的场景 */
float duration /* 动画持续时长 */ float duration /* 动画持续时长 */
); );
protected: protected:
virtual void Update() override; virtual void Update() override;
virtual bool Init( virtual void Init(
Game * game, Scene * prev,
Scene * prev Scene * next
) override; ) override;
}; };
@ -125,7 +121,6 @@ namespace e2d
{ {
public: public:
explicit MoveTransition( explicit MoveTransition(
Scene* scene, /* 切换的场景 */
float moveDuration, /* 场景移动动画持续时长 */ float moveDuration, /* 场景移动动画持续时长 */
Direction direction = Direction::Left /* 场景移动方向 */ Direction direction = Direction::Left /* 场景移动方向 */
); );
@ -133,9 +128,9 @@ namespace e2d
protected: protected:
virtual void Update() override; virtual void Update() override;
virtual bool Init( virtual void Init(
Game * game, Scene * prev,
Scene * prev Scene * next
) override; ) override;
virtual void Reset() override; virtual void Reset() override;

View File

@ -115,10 +115,19 @@ void e2d::Game::Quit()
quit_ = true; quit_ = true;
} }
void e2d::Game::EnterScene(Scene * scene) void e2d::Game::EnterScene(Scene * scene, Transition * transition)
{ {
if (!scene) if (scene == nullptr)
{
WARN("Next scene is null pointer!");
return; return;
}
if (next_scene_ != nullptr)
{
WARN("Scene is transitioning...");
return;
}
if (next_scene_) if (next_scene_)
{ {
@ -126,29 +135,18 @@ void e2d::Game::EnterScene(Scene * scene)
} }
next_scene_ = scene; next_scene_ = scene;
next_scene_->Retain(); next_scene_->Retain();
}
void e2d::Game::EnterScene(Transition * transition) if (transition)
{
if (!transition)
return;
EnterScene(transition->in_scene_);
if (transition_)
{ {
transition_->Stop(); if (transition_)
transition_->Release(); {
} transition_->Stop();
transition_ = transition; transition_->Release();
transition_->Retain(); }
transition_ = transition;
transition_->Retain();
// 初始化场景切换动画 transition_->Init(curr_scene_, next_scene_)
if (!transition_->Init(this, curr_scene_))
{
WARN("Transition initialize failed!");
transition_->Release();
transition_ = nullptr;
} }
} }

View File

@ -2,14 +2,14 @@
#include "..\e2dobject.h" #include "..\e2dobject.h"
#include "..\e2dmodule.h" #include "..\e2dmodule.h"
e2d::BoxTransition::BoxTransition(Scene* scene, float duration) e2d::BoxTransition::BoxTransition(float duration)
: Transition(scene, duration) : Transition(duration)
{ {
} }
bool e2d::BoxTransition::Init(Game * game, Scene * prev) bool e2d::BoxTransition::Init(Scene * prev, Scene * next)
{ {
if (Transition::Init(game, prev)) if (Transition::Init(prev, next))
{ {
in_layer_param_.opacity = 0; in_layer_param_.opacity = 0;
return true; return true;

View File

@ -1,14 +1,14 @@
#include "..\e2dtransition.h" #include "..\e2dtransition.h"
#include "..\e2dobject.h" #include "..\e2dobject.h"
e2d::EmergeTransition::EmergeTransition(Scene* scene, float duration) e2d::EmergeTransition::EmergeTransition(float duration)
: Transition(scene, duration) : Transition(duration)
{ {
} }
bool e2d::EmergeTransition::Init(Game * game, Scene * prev) bool e2d::EmergeTransition::Init(Scene * prev, Scene * next)
{ {
if (Transition::Init(game, prev)) if (Transition::Init(prev, next))
{ {
out_layer_param_.opacity = 1; out_layer_param_.opacity = 1;
in_layer_param_.opacity = 0; in_layer_param_.opacity = 0;

View File

@ -1,14 +1,14 @@
#include "..\e2dtransition.h" #include "..\e2dtransition.h"
#include "..\e2dobject.h" #include "..\e2dobject.h"
e2d::FadeTransition::FadeTransition(Scene* scene, float duration) e2d::FadeTransition::FadeTransition(float duration)
: Transition(scene, duration) : Transition(duration)
{ {
} }
bool e2d::FadeTransition::Init(Game * game, Scene * prev) bool e2d::FadeTransition::Init(Scene * prev, Scene * next)
{ {
if (Transition::Init(game, prev)) if (Transition::Init(prev, next))
{ {
out_layer_param_.opacity = 1; out_layer_param_.opacity = 1;
in_layer_param_.opacity = 0; in_layer_param_.opacity = 0;

View File

@ -2,15 +2,15 @@
#include "..\e2dobject.h" #include "..\e2dobject.h"
#include "..\e2dmodule.h" #include "..\e2dmodule.h"
e2d::MoveTransition::MoveTransition(Scene* scene, float duration, Direction direction) e2d::MoveTransition::MoveTransition(float duration, Direction direction)
: Transition(scene, duration) : Transition(duration)
, direction_(direction) , direction_(direction)
{ {
} }
bool e2d::MoveTransition::Init(Game * game, Scene * prev) bool e2d::MoveTransition::Init(Scene * prev, Scene * next)
{ {
if (Transition::Init(game, prev)) if (Transition::Init(prev, next))
{ {
auto size = Window::GetInstance()->GetSize(); auto size = Window::GetInstance()->GetSize();
if (direction_ == Direction::Up) if (direction_ == Direction::Up)

View File

@ -2,7 +2,7 @@
#include "..\e2dtransition.h" #include "..\e2dtransition.h"
#include "..\e2dobject.h" #include "..\e2dobject.h"
e2d::Transition::Transition(Scene* scene, float duration) e2d::Transition::Transition(float duration)
: done_(false) : done_(false)
, started_() , started_()
, delta_(0) , delta_(0)
@ -14,8 +14,6 @@ e2d::Transition::Transition(Scene* scene, float duration)
, in_layer_param_() , in_layer_param_()
{ {
duration_ = std::max(duration, 0.f); duration_ = std::max(duration, 0.f);
if (in_scene_)
in_scene_->Retain();
} }
e2d::Transition::~Transition() e2d::Transition::~Transition()
@ -31,29 +29,31 @@ bool e2d::Transition::IsDone()
return done_; return done_;
} }
bool e2d::Transition::Init(Game * game, Scene * prev) void e2d::Transition::Init(Scene * prev, Scene * next)
{ {
started_ = Time::Now(); started_ = Time::Now();
out_scene_ = prev; out_scene_ = prev;
in_scene_ = next;
if (out_scene_) if (out_scene_)
out_scene_->Retain(); out_scene_->Retain();
HRESULT hr = S_OK; if (in_scene_)
in_scene_->Retain();
auto renderer = Renderer::GetInstance(); auto renderer = Renderer::GetInstance();
if (in_scene_) if (in_scene_)
{ {
hr = renderer->GetRenderTarget()->CreateLayer(&in_layer_); ThrowIfFailed(
renderer->GetRenderTarget()->CreateLayer(&in_layer_)
);
} }
if (SUCCEEDED(hr) && out_scene_) if (out_scene_)
{ {
hr = renderer->GetRenderTarget()->CreateLayer(&out_layer_); ThrowIfFailed(
} renderer->GetRenderTarget()->CreateLayer(&out_layer_)
);
if (FAILED(hr))
{
return false;
} }
out_layer_param_ = in_layer_param_ = D2D1::LayerParameters( out_layer_param_ = in_layer_param_ = D2D1::LayerParameters(