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();
// 场景入栈
// 切换场景
void EnterScene(
Scene * scene /* 下一个场景的指针 */
);
// 场景入栈
void EnterScene(
Transition * transition /* 场景动画 */
Scene * scene, /* 场景 */
Transition * transition = nullptr /* 场景动画 */
);
// 获取当前场景

View File

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

View File

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

View File

@ -2,14 +2,14 @@
#include "..\e2dobject.h"
#include "..\e2dmodule.h"
e2d::BoxTransition::BoxTransition(Scene* scene, float duration)
: Transition(scene, duration)
e2d::BoxTransition::BoxTransition(float 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;
return true;

View File

@ -1,14 +1,14 @@
#include "..\e2dtransition.h"
#include "..\e2dobject.h"
e2d::EmergeTransition::EmergeTransition(Scene* scene, float duration)
: Transition(scene, duration)
e2d::EmergeTransition::EmergeTransition(float 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;
in_layer_param_.opacity = 0;

View File

@ -1,14 +1,14 @@
#include "..\e2dtransition.h"
#include "..\e2dobject.h"
e2d::FadeTransition::FadeTransition(Scene* scene, float duration)
: Transition(scene, duration)
e2d::FadeTransition::FadeTransition(float 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;
in_layer_param_.opacity = 0;

View File

@ -2,15 +2,15 @@
#include "..\e2dobject.h"
#include "..\e2dmodule.h"
e2d::MoveTransition::MoveTransition(Scene* scene, float duration, Direction direction)
: Transition(scene, duration)
e2d::MoveTransition::MoveTransition(float duration, Direction direction)
: Transition(duration)
, 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();
if (direction_ == Direction::Up)

View File

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