StatePlay.cpp
上传用户:jalin138
上传日期:2022-02-12
资源大小:5720k
文件大小:3k
- #include "StatePlay.h"
- #include "StateMenu.h"
- #include "PlayCommand.h"
- #include <stdlib.h>
- #include <time.h>
- #define _CRTDBG_MAP_ALLOC
- #include <crtdbg.h>
- #ifdef _DEBUG
- #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
- #endif
- CStatePlay::CStatePlay(void)
- {
- int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
- tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
- _CrtSetDbgFlag( tmpFlag );
- m_pButton = new CbuttonFont( L"返回主菜单", 25 );
- m_pFont = new GfxFont("幼圆",20,true);
- m_pFont->SetColor(0xff30e97f);
- m_pPlay = new ST_GAME_PLAY;
- m_pPlay->SetZero();
- memset(m_vSprite,NULL,sizeof(m_vSprite));
- InitializeDate();
- m_pPlayManage = new CSceneManage( new CPlayCommand(m_vSprite,m_pPlay) );
- }
- CStatePlay::~CStatePlay(void)
- {
- SAFE_DELETE( m_pButton );
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i ++ )
- {
- SAFE_DELETE(m_vSprite[i]);
- }
- SAFE_DELETE(m_pPlay);
- SAFE_DELETE( m_pPlayManage );
- SAFE_DELETE( m_pFont );
- SAFE_DELETE( m_pButton );
- }
- // 逻辑实现
- bool CStatePlay::Logic(void)
- {
- if ( m_pButton->Logic(15,275 ) )
- return true;
- m_pPlay->score -= g_Hge->Timer_GetDelta()*2;
- if ( m_pPlay->score <= 0 )
- return true;
- return m_pPlayManage->Logic();
- }
- // 渲染实现
- void CStatePlay::Render(void)
- {
- RenderBack();
- DrawLine();
-
- m_pButton->Render();
- m_pFont->Print( 30,350,"积分:%d",(int)m_pPlay->score );
- float m_fsize = m_pPlay->width/72.0f - 0.15f;
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i ++)
- {
- m_vSprite[i]->Render(m_fsize);
- #ifdef _DEBUG
- g_Font->Print(m_vSprite[i]->m_fx-20,m_vSprite[i]->m_fy,"[%d][%d]",i,m_vSprite[i]->m_nType);
- #endif
- }
- m_pPlayManage->Render();
- }
- // 初始化游戏数据
- void CStatePlay::InitializeDate(void)
- {
- m_pPlay->check_sum =
- g_Hge->Ini_GetInt( "Play", "check sum", 3 );
- m_pPlay->spr_sum =
- g_Hge->Ini_GetInt( "Play", "sprite", 4 );
- m_pPlay->score =
- (float)g_Hge->Ini_GetInt( "Play", "score", 50 );
- m_pPlay->size =
- g_Hge->Ini_GetInt( "Play", "size", 8 );
- m_pPlay->width = 460.0f/m_pPlay->size;
- srand((unsigned)time(NULL));
- int mi_type = 0,mj_type = 0;
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i ++ )
- {
- mi_type = 0;
- mj_type = 0;
- if ( i % 8 >= m_pPlay->check_sum - 1 )
- {
- int mt_at = m_pPlay->check_sum - 1;
- mi_type = m_vSprite[i-mt_at]->m_nType;
- }
- if ( i+m_pPlay->size >= m_pPlay->size*m_pPlay->check_sum )
- {
- int mt_at = m_pPlay->size*(m_pPlay->check_sum-1);
- mj_type = m_vSprite[i-mt_at]->m_nType;
- }
- m_vSprite[i] = new CSprite(mi_type,mj_type,m_pPlay->spr_sum);
- m_vSprite[i]->m_fx = (i%m_pPlay->size) * m_pPlay->width
- + 170.0f + m_pPlay->width/2.0f;
- m_vSprite[i]->m_fy = (i/m_pPlay->size) * m_pPlay->width
- + 100.0f + m_pPlay->width/2.0f;
- }
- }
- // 绘制线框
- void CStatePlay::DrawLine(void)
- {
- DWORD color = 0xffd1abab;
- for (int i = 0;i <= m_pPlay->size;i ++ )
- {
- g_Hge->Gfx_RenderLine( 170.0f + i*m_pPlay->width,
- 100.0f,
- 170.0f + i*m_pPlay->width,
- 560.0f, color );
- g_Hge->Gfx_RenderLine( 170.0f,
- 100.0f + i*m_pPlay->width,
- 630.0f,
- 100.0f + i*m_pPlay->width, color );
- }
- }
- // 场景切换,返回true时退出游戏
- bool CStatePlay::Handle(CSceneManage *_SceneManage)
- {
- if ( NULL == _SceneManage )
- return false;
- _SceneManage->ChangeState( new CStateMenu() );
- return false;
- }