PlayCheckDel.cpp
上传用户:jalin138
上传日期:2022-02-12
资源大小:5720k
文件大小:9k
- #include "PlayCheckDel.h"
- #include "PlayChange.h"
- #include "PlayCommand.h"
- #include <time.h>
- CPlayCheckDel::CPlayCheckDel(CSprite* _Vector[],
- ST_GAME_PLAY *_play)
- {
- m_pPlay = _play;
- int i = 0;
- while ( i < m_pPlay->size*m_pPlay->size )
- {
- m_vSprite[i] = _Vector[i];
- i ++;
- }
- m_bHaveDel = false;
- Check_Del();
- m_nState = 1;
- m_sMatch = g_Res->GetEffect("Match");
- }
- CPlayCheckDel::~CPlayCheckDel(void)
- {
- m_pPlay = NULL;
- m_vMove.clear();
- memset(m_listDel,0,sizeof(m_listDel));
- std::list<hgeParticleSystem*>::iterator iter1;
- for (iter1 = m_listDelPar.begin();
- iter1 != m_listDelPar.end();
- iter1 ++)
- {
- SAFE_DELETE( *iter1 );
- }
- m_listDelPar.clear();
- memset(m_vSprite,NULL,sizeof(m_vSprite));
- m_listDelPar.clear();
- }
- // 状态逻辑接口
- bool CPlayCheckDel::Logic(void)
- {
- if ( m_bHaveDel )
- {
- UpdateDelPar();
- switch ( m_nState )
- {
- case 1:
- if ( SetColor() )
- m_nState = 2;
- break;
- case 2:
- if ( SetMove() )
- {
- m_nState = 3;
- }
- else
- {
- if ( Check_Del() )
- m_nState = 1;
- else
- return true;
- }
- break;
- case 3:
- if ( DropMove() )
- m_nState = 2;
- break;
- default:
- break;
- }
- return false;
- }
- return true;
- }
- // 状态渲染接口
- void CPlayCheckDel::Render(void)
- {
- std::list<hgeParticleSystem*>::iterator iter1;
- for ( iter1 = m_listDelPar.begin();
- iter1 != m_listDelPar.end();
- iter1 ++ )
- {
- (*iter1)->Render();
- }
- #ifdef _DEBUG
- g_Font->Print(10,30,"Play CheckDel");
- float mt_x = 0,mt_y = 0;
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i++)
- {
- if ( m_listDel[i] >= 0 )
- {
- g_Font->Print( 650.0f+mt_x, 260.0f+mt_y, "[%d]", m_listDel[i] );
- mt_x += 32;
- if (mt_x > 96)
- {
- mt_x = 0;
- mt_y += 20;
- }
- }
- }
- mt_x = 0;
- std::vector<int>::iterator iter3;
- for (iter3 = m_vMove.begin();
- iter3 != m_vMove.end();
- iter3++)
- {
- g_Font->Print( 650.0f+mt_x, 400.0f+mt_y, "[%d]", *iter3 );
- mt_x += 32;
- }
- #endif
- }
- // 检测消除
- bool CPlayCheckDel::Check_Del(void)
- {
- memset( m_listDel,-1,sizeof(m_listDel) );
- DeleteDelPar();
- m_vMove.clear();
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i ++)
- {
- Check_Across(i);
- Check_Erect(i);
- }
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i++)
- {
- if ( m_listDel[i] >= 0 )
- {
- m_bHaveDel = true;
- return true;
- }
- }
- return false;
- }
- // 横向检测
- bool CPlayCheckDel::Check_Across(const int _n)
- {
- if ( _n%m_pPlay->size > m_pPlay->size-m_pPlay->check_sum )
- return false;
- for (int i = 1;i < m_pPlay->check_sum;i ++)
- {
- if ( m_vSprite[_n+i]->m_nType != m_vSprite[_n]->m_nType )
- return false;
- }
- for (int i = 0;i < m_pPlay->check_sum;i ++)
- {
- RegisterDle(_n+i);
- }
- return true;
- }
- // 列向检测
- bool CPlayCheckDel::Check_Erect(const int _n)
- {
- if ( _n / m_pPlay->size > m_pPlay->size - m_pPlay->check_sum )
- return false;
- for (int i = 1;i < m_pPlay->check_sum;i ++)
- {
- if ( m_vSprite[_n+m_pPlay->size*i]->m_nType != m_vSprite[_n]->m_nType )
- return false;
- }
- for (int i = 0;i < m_pPlay->check_sum;i ++)
- {
- RegisterDle(_n+m_pPlay->size*i);
- }
- return true;
- }
- // 记录消除项
- void CPlayCheckDel::RegisterDle(int _n)
- {
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i++)
- {
- if ( m_listDel[i] == _n )
- return ;
- }
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i++ )
- {
- if ( m_listDel[i] < 0 )
- {
- m_listDel[i] = _n;
- SetDelPar(_n);
- return ;
- }
- }
- }
- void CPlayCheckDel::RemoveDle(int _n)
- {
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i++ )
- {
- if ( m_listDel[i] == _n )
- {
- m_listDel[i] = -1;
- return ;
- }
- }
- }
- // 消除效果
- bool CPlayCheckDel::UpdateDelPar(void)
- {
- std::list<hgeParticleSystem*>::iterator iter1;
- for ( iter1 = m_listDelPar.begin();
- iter1 != m_listDelPar.end();
- iter1 ++ )
- {
- if ((*iter1)->info.nEmission < 300 )
- (*iter1)->info.nEmission += 20;
- (*iter1)->Update(0.01f);
- }
- return false;
- }
- void CPlayCheckDel::SetDelPar(const int _n)
- {
- hgeSprite *sprPar = g_Res->GetSprite( "sprDel" );
- m_listDelPar.push_back( new hgeParticleSystem("particle/particle7.psi",sprPar) );
- float mt_x = (_n%m_pPlay->size)*m_pPlay->width + m_pPlay->width/2.0f+170.0f;
- float mt_y = (_n/m_pPlay->size)*m_pPlay->width + m_pPlay->width/2.0f+100.0f;
- m_listDelPar.back()->Fire();
- m_listDelPar.back()->MoveTo( mt_x, mt_y );
- m_listDelPar.back()->info.nEmission = 0;
- }
- void CPlayCheckDel::DeleteDelPar(void)
- {
- std::list<hgeParticleSystem*>::iterator iter1;
- for (iter1 = m_listDelPar.begin();
- iter1 != m_listDelPar.end();
- iter1 ++)
- {
- SAFE_DELETE( *iter1 );
- }
- m_listDelPar.clear();
- }
- // 减少对象透明度
- bool CPlayCheckDel::SetColor(void)
- {
- int mt_n = 0;
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i++)
- {
- if ( m_listDel[i] >= 0 )
- {
- m_vSprite[m_listDel[i]]->a -= 4;
- if ( m_vSprite[m_listDel[i]]->a < 50 )
- {
- m_pPlay->score += 2;//得分
- m_vSprite[m_listDel[i]]->a = 00;
- }
- mt_n = m_listDel[i];
- }
- }
-
- if ( m_vSprite[mt_n]->a < 1 )
- {
- g_Hge->Effect_Play(m_sMatch);
- std::list<hgeParticleSystem*>::iterator iter1;
- for ( iter1 = m_listDelPar.begin();
- iter1 != m_listDelPar.end();
- iter1 ++ )
- {
- (*iter1)->info.nEmission = 0;
- (*iter1)->Stop();
- }
- return true;
- }
- return false;
- }
- // 设置当前移动项
- bool CPlayCheckDel::SetMove(void)
- {
- // 检测是否为空
- int b_empty = true;
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i ++)
- {
- if ( m_listDel[i] >= 0 )
- {
- b_empty = false;
- break;
- }
- }
- if ( b_empty )
- return false;
- m_vMove.clear();
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i ++)
- {
- if ( m_listDel[i] >= 0 )
- {
- if ( m_listDel[i] < m_pPlay->size )
- {
- EddSprite( m_listDel[i] );
- RemoveDle( m_listDel[i] );
- continue;
- }
- SetMove(m_listDel[i]);
- }
- }
- return true;
- }
- bool CPlayCheckDel::SetMove(int _n)
- {
- std::vector<int>::iterator iter1;
- for (iter1 = m_vMove.begin();
- iter1 != m_vMove.end();
- iter1 ++ )
- {
- if (*iter1%m_pPlay->size == _n%m_pPlay->size )
- {
- return false;
- }
- }
- m_vMove.push_back( _n );
- return true;
- }
- // 落下
- bool CPlayCheckDel::DropMove(void)
- {
- static float mt_width = 0.0f;
- float mf_speed = 20.0f/m_pPlay->size;
- mt_width += mf_speed;
- for (int i = 0;i < m_pPlay->size*m_pPlay->size;i ++)
- {
- std::vector<int>::iterator iter1;
- for (iter1 = m_vMove.begin();
- iter1 != m_vMove.end();
- iter1 ++)
- {
- if ( i%m_pPlay->size == *iter1%m_pPlay->size
- && i/m_pPlay->size < *iter1/m_pPlay->size )
- {
- m_vSprite[i]->m_fy += mf_speed;
- }
- }
- }
-
- if ( mt_width > m_pPlay->width )
- {
- mt_width = 0.0f;
- std::vector<int>::iterator iter1;
- for (iter1 = m_vMove.begin();
- iter1 != m_vMove.end();
- iter1 ++)
- {
- Change( *iter1 );
- RemoveDle(*iter1);
- }
- m_vMove.clear();
- return true;
- }
- return false;
- }
- // 交换
- bool CPlayCheckDel::Change(const int _n)
- {
- if ( _n < m_pPlay->size )
- {
- EddSprite(_n);
- return true;
- }
- int n1 = _n;
- for (int i = 0;i < _n/m_pPlay->size;i ++)
- {
- int n2 = n1 - m_pPlay->size;
- int temp = 0;
- temp = m_vSprite[n1]->m_nType;
- m_vSprite[n1]->m_nType = m_vSprite[n2]->m_nType;
- m_vSprite[n2]->m_nType = temp;
- temp = m_vSprite[n1]->a;
- m_vSprite[n1]->a = m_vSprite[n2]->a;
- m_vSprite[n2]->a = temp;
- hgeSprite *spr = NULL;
- spr = m_vSprite[n1]->m_pSpr;
- m_vSprite[n1]->m_pSpr = m_vSprite[n2]->m_pSpr;
- m_vSprite[n2]->m_pSpr = spr;
- m_vSprite[n1]->m_fx =
- (n1%m_pPlay->size) * m_pPlay->width + 170.0f + m_pPlay->width/2.0f;
- m_vSprite[n1]->m_fy =
- (n1/m_pPlay->size) * m_pPlay->width + 100.0f + m_pPlay->width/2.0f;
- m_vSprite[n2]->m_fx =
- (n2%m_pPlay->size) * m_pPlay->width + 170.0f + m_pPlay->width/2.0f;
- m_vSprite[n2]->m_fy =
- (n2/m_pPlay->size) * m_pPlay->width + 100.0f + m_pPlay->width/2.0f;
- n1 -= m_pPlay->size;
- }
- EddSprite(_n);
- return true;
- }
- // 增加
- bool CPlayCheckDel::EddSprite(const int _n)
- {
- int n = _n%m_pPlay->size;
- srand((unsigned)time(NULL));
- int mi_type = 0,mj_type = 0,m_type = 0;
- for (int i = 0;i < m_pPlay->size;i ++ )
- {
- if ( n == i )
- {
- if ( i >= m_pPlay->check_sum - 1 )
- {
- int mt_at = m_pPlay->check_sum - 1;
- mi_type = m_vSprite[i-mt_at]->m_nType;
- }
- int mt_at = m_pPlay->size*(m_pPlay->check_sum-1);
- mj_type = m_vSprite[i+mt_at]->m_nType;
- m_type = rand()%m_pPlay->spr_sum+1;
- while ( m_type == mi_type || m_type == mj_type )
- {
- m_type = rand()%m_pPlay->spr_sum+1;
- }
- m_vSprite[i]->m_nType = m_type;
- char sprName[24] = {0};
- sprintf_s( sprName, sizeof(sprName), "sprSprite%d", m_type );
- m_vSprite[i]->m_pSpr = g_Res->GetSprite( sprName );
- m_vSprite[i]->m_fx = (i%m_pPlay->size) * m_pPlay->width
- + 170.0f + m_pPlay->width/2.0f;
- m_vSprite[i]->m_fy = 100.0f + m_pPlay->width/2.0f;
- m_vSprite[i]->a = 255;
- return true;
- }
- }
- return false;
- }
- // 传入场景管理对象,以改变状态
- bool CPlayCheckDel::Handle(CSceneManage *_Manage)
- {
- if ( NULL == _Manage )
- return false;
- if ( !m_bHaveDel )
- {
- int mt_n = 0;
- mt_n = m_pPlay->nChoice1;
- m_pPlay->nChoice1 = m_pPlay->nChoice2;
- m_pPlay->nChoice2 = mt_n;
- _Manage->ChangeState( new CPlayChange(m_vSprite,m_pPlay,PLAY_CHACK_DEL) );
- }
- else
- {
- _Manage->ChangeState( new CPlayCommand(m_vSprite,m_pPlay) );
- }
- return false;
- }