Window类增加弹窗功能;修复Action重置时间处的Bug

This commit is contained in:
Nomango 2018-05-10 18:18:02 +08:00
parent 77d064a0c4
commit b5e6650566
11 changed files with 144 additions and 43 deletions

View File

@ -15,18 +15,6 @@ e2d::Action::~Action()
{ {
} }
bool e2d::Action::_isDone()
{
return _done;
}
void e2d::Action::_startWithTarget(Node* target)
{
_target = target;
_running = true;
this->reset();
}
bool e2d::Action::isRunning() bool e2d::Action::isRunning()
{ {
return _running; return _running;
@ -74,10 +62,28 @@ void e2d::Action::onDestroy()
ActionManager::__remove(this); ActionManager::__remove(this);
} }
void e2d::Action::reset()
{
_initialized = false;
_done = false;
_last = Time::getTotalTime();
}
bool e2d::Action::_isDone()
{
return _done;
}
void e2d::Action::_startWithTarget(Node* target)
{
_target = target;
_running = true;
this->reset();
}
void e2d::Action::_init() void e2d::Action::_init()
{ {
_initialized = true; _initialized = true;
// 记录当前时间
_last = Time::getTotalTime(); _last = Time::getTotalTime();
} }
@ -89,14 +95,6 @@ void e2d::Action::_update()
} }
} }
void e2d::Action::reset()
{
_initialized = false;
_done = false;
_last = Time::getTotalTime();
}
void e2d::Action::_resetTime() void e2d::Action::_resetTime()
{ {
_last = Time::getTotalTime();
} }

View File

@ -2,8 +2,14 @@
e2d::ActionGradual::ActionGradual(double duration) e2d::ActionGradual::ActionGradual(double duration)
: _delta(0) : _delta(0)
, _duration(max(duration, 0))
{ {
_duration = max(duration, 0); }
void e2d::ActionGradual::reset()
{
Action::reset();
_delta = 0;
} }
void e2d::ActionGradual::_init() void e2d::ActionGradual::_init()
@ -19,13 +25,20 @@ void e2d::ActionGradual::_update()
{ {
_delta = 1; _delta = 1;
this->stop(); this->stop();
return;
} }
else
_delta = min((Time::getTotalTime() - _last) / _duration, 1);
if (_delta >= 1)
{ {
this->stop(); _delta = min((Time::getTotalTime() - _last) / _duration, 1);
if (_delta >= 1)
{
this->stop();
}
} }
} }
void e2d::ActionGradual::_resetTime()
{
Action::_resetTime();
_last = Time::getTotalTime() - _delta * _duration;
}

View File

@ -64,8 +64,8 @@ void e2d::Animate::_update()
target->open(frames[_frameIndex]); target->open(frames[_frameIndex]);
} }
_last += _animation->getInterval();
_frameIndex++; _frameIndex++;
_last += _animation->getInterval();
if (_frameIndex == frames.size()) if (_frameIndex == frames.size())
{ {
@ -75,6 +75,12 @@ void e2d::Animate::_update()
} }
} }
void e2d::Animate::_resetTime()
{
Action::_resetTime();
_last = Time::getTotalTime();
}
void e2d::Animate::reset() void e2d::Animate::reset()
{ {
Action::reset(); Action::reset();

View File

@ -1,8 +1,9 @@
#include "..\e2daction.h" #include "..\e2daction.h"
e2d::Delay::Delay(double duration) e2d::Delay::Delay(double duration)
: _delta(0)
, _delay(max(duration, 0))
{ {
_delay = max(duration, 0);
} }
e2d::Delay * e2d::Delay::clone() const e2d::Delay * e2d::Delay::clone() const
@ -10,6 +11,12 @@ e2d::Delay * e2d::Delay::clone() const
return new (std::nothrow) Delay(_delay); return new (std::nothrow) Delay(_delay);
} }
void e2d::Delay::reset()
{
Action::reset();
_delta = 0;
}
void e2d::Delay::_init() void e2d::Delay::_init()
{ {
Action::_init(); Action::_init();
@ -19,8 +26,16 @@ void e2d::Delay::_update()
{ {
Action::_update(); Action::_update();
if ((Time::getTotalTime() - _last) >= _delay) _delta = Time::getTotalTime() - _last;
if (_delta >= _delay)
{ {
this->stop(); this->stop();
} }
} }
void e2d::Delay::_resetTime()
{
Action::_resetTime();
_last = Time::getTotalTime() - _delta;
}

View File

@ -149,17 +149,22 @@ void e2d::Game::pause()
void e2d::Game::resume() void e2d::Game::resume()
{ {
if (isPaused()) if (s_bInitialized && s_bPaused)
{ {
s_bPaused = false; s_bPaused = false;
// 刷新当前时间 Game::reset();
Time::__updateLast();
// 重置动作和定时器
ActionManager::__resetAll();
Timer::__resetAll();
} }
} }
void e2d::Game::reset()
{
// 刷新当前时间
Time::__reset();
// 重置动作和定时器
ActionManager::__resetAll();
Timer::__resetAll();
}
bool e2d::Game::isPaused() bool e2d::Game::isPaused()
{ {
return s_bPaused; return s_bPaused;

View File

@ -55,8 +55,7 @@ bool Input::__init()
} }
else else
{ {
MessageBox(nullptr, L"Keyboard not found!", L"Error", MB_ICONERROR | MB_OK); Window::error(L"Keyboard not found!");
Game::quit();
return false; return false;
} }
} }
@ -75,7 +74,7 @@ bool Input::__init()
} }
else else
{ {
MessageBox(nullptr, L"Mouse not found!", L"Error", MB_ICONERROR | MB_OK); Window::error(L"Mouse not found!");
return false; return false;
} }
} }

View File

@ -73,6 +73,13 @@ void e2d::Time::__updateLast()
s_nTotalTime = static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tStart).count()); s_nTotalTime = static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tStart).count());
} }
void e2d::Time::__reset()
{
s_tLastUpdate = s_tFixedUpdate = s_tNow = steady_clock::now();
s_nInterval = 0;
s_nTotalTime = static_cast<unsigned int>(duration_cast<milliseconds>(s_tNow - s_tStart).count());
}
void e2d::Time::__sleep() void e2d::Time::__sleep()
{ {
// 计算挂起时长 // 计算挂起时长

View File

@ -80,7 +80,7 @@ bool e2d::Window::__init()
if (FAILED(hr)) if (FAILED(hr))
{ {
::MessageBox(nullptr, L"Create Window Failed!", L"Error", MB_OK); Window::error(L"Create Window Failed!");
} }
return SUCCEEDED(hr); return SUCCEEDED(hr);
@ -257,7 +257,7 @@ void e2d::Window::showConsole(bool show)
} }
else else
{ {
MessageBox(nullptr, L"Alloc Console Failed!", L"Error", MB_OK); Window::error(L"Alloc Console Failed!");
} }
} }
} }
@ -291,6 +291,24 @@ void e2d::Window::setTypewritingEnable(bool enable)
} }
} }
void e2d::Window::prompt(const String & text, const String & title)
{
::MessageBox(s_HWnd, text, title, MB_ICONINFORMATION | MB_OK);
Game::reset();
}
void e2d::Window::warning(const String& title, const String& text)
{
::MessageBox(s_HWnd, text, title, MB_ICONWARNING | MB_OK);
Game::reset();
}
void e2d::Window::error(const String & text, const String & title)
{
::MessageBox(s_HWnd, text, title, MB_ICONERROR | MB_OK);
Game::reset();
}
LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) LRESULT e2d::Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {

View File

@ -98,6 +98,9 @@ public:
double duration double duration
); );
// 重置动作
virtual void reset() override;
protected: protected:
// 初始化动作 // 初始化动作
virtual void _init() override; virtual void _init() override;
@ -105,6 +108,9 @@ protected:
// 更新动作 // 更新动作
virtual void _update() override; virtual void _update() override;
// 重置动作时间
virtual void _resetTime() override;
protected: protected:
double _duration; double _duration;
double _delta; double _delta;
@ -443,6 +449,9 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual Delay * clone() const override; virtual Delay * clone() const override;
// 重置动作
virtual void reset() override;
protected: protected:
// 初始化动作 // 初始化动作
virtual void _init() override; virtual void _init() override;
@ -450,8 +459,12 @@ protected:
// 更新动作 // 更新动作
virtual void _update() override; virtual void _update() override;
// 重置动作时间
virtual void _resetTime() override;
protected: protected:
double _delay; double _delay;
double _delta;
}; };
@ -666,6 +679,9 @@ protected:
// 更新动作 // 更新动作
virtual void _update() override; virtual void _update() override;
// 重置动作时间
virtual void _resetTime() override;
protected: protected:
UINT _frameIndex; UINT _frameIndex;
Animation * _animation; Animation * _animation;

View File

@ -34,6 +34,9 @@ public:
// 回收游戏资源 // 回收游戏资源
static void destroy(); static void destroy();
// 重置游戏内部计时
static void reset();
// 游戏是否暂停 // 游戏是否暂停
static bool isPaused(); static bool isPaused();
@ -100,6 +103,24 @@ public:
bool enable bool enable
); );
// 弹出提示窗口
static void prompt(
const String& text, /* 内容 */
const String& title = L"Prompt" /* 窗口标题 */
);
// 弹出警告窗口
static void warning(
const String& text, /* 内容 */
const String& title = L"Warning" /* 窗口标题 */
);
// 弹出错误窗口
static void error(
const String& text, /* 内容 */
const String& title = L"Error" /* 窗口标题 */
);
private: private:
// 初始化窗口 // 初始化窗口
static bool __init(); static bool __init();
@ -149,6 +170,9 @@ private:
// 更新时间信息 // 更新时间信息
static void __updateLast(); static void __updateLast();
// 重置时间信息
static void __reset();
// 挂起线程 // 挂起线程
static void __sleep(); static void __sleep();
}; };

View File

@ -101,7 +101,7 @@
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>false</SDLCheck> <SDLCheck>false</SDLCheck>
<DebugInformationFormat>None</DebugInformationFormat> <DebugInformationFormat>EditAndContinue</DebugInformationFormat>
<MinimalRebuild>false</MinimalRebuild> <MinimalRebuild>false</MinimalRebuild>
<TreatWarningAsError>true</TreatWarningAsError> <TreatWarningAsError>true</TreatWarningAsError>
</ClCompile> </ClCompile>