Hero.cpp
上传用户:pfmy85
上传日期:2007-01-07
资源大小:22k
文件大小:4k
- // Hero.cpp: implementation of the CHero class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Isotest.h"
- #include "Hero.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- const char* CHero::m_szHeroAction[9] ={
- "ht","st","wl","fm","lm","qm","as","at","aw"};
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CHero::CHero()
- {
- for (int e=0; e<ACTION_CNT; e++)
- m_pAction[e] = NULL;
- m_eAction = ACTION_ST;
- m_eDirection = RIGHT_DOWN;
- m_nCurFrame = 0;
- // test code
- m_nPosX = 310;
- m_nPosY = 200;
- }
- CHero::~CHero()
- {
- for (int e=0; e<ACTION_CNT; e++)
- {
- if (m_pAction[e]!=NULL)
- {
- delete m_pAction[e];
- m_pAction[e] = NULL;
- }
- }
- }
- bool CHero::Create(CDDDevice* pDDDevice,
- LPCTSTR lpszName,
- POINT* pSize,
- CPackFileManager* pPackFileManager/*=NULL*/)
- {
- ASSERT(pDDDevice!=NULL);
- ASSERT(pSize!=NULL);
- CString strFile;
- memcpy(m_ptSpriteSize, pSize, ACTION_CNT*sizeof(POINT));
- for (int i=0; i< ACTION_CNT; i++)
- {
- strFile.Format("%s%s%s", lpszName, m_szHeroAction[i], ".bmp");
- m_pAction[i] = new CDXSprite;
- if (m_pAction[i] == NULL)
- return false;
- if (!m_pAction[i]->Create(pDDDevice,
- strFile,
- m_ptSpriteSize[i].x,
- m_ptSpriteSize[i].y,
- pPackFileManager))
- {
- delete m_pAction[i];
- m_pAction[i] = NULL;
- m_nFrames[i] = 0;
- }
- else
- {
- // 每个动作有8个方向
- // 最后空白帧数不能超过8帧,也就是说精灵原始图
- // 列数不能超过8
-
- m_nFrames[i] = m_pAction[i]->GetTotalFrames()/8;
- }
- }
- return true;
- }
- void CHero::SetViewRect(LPRECT lprcView)
- {
- ASSERT(lprcView!=NULL);
- m_rcView.top = lprcView->top;
- m_rcView.left = lprcView->left;
- m_rcView.bottom = lprcView->bottom;
- m_rcView.right = lprcView->right;
- }
- bool CHero::SetAction(CHero::HERO_ACTION eAction)
- {
- if (m_eAction != eAction)
- {
- m_eAction = eAction;
- m_nCurFrame = 0;
- return true;
- }
- return false;
- };
- bool CHero::SetDirection(DIRECTION eDir)
- {
- if (eDir != m_eDirection)
- {
- m_eDirection = eDir;
- m_nCurFrame = 0;
- return true;
- }
- return false;
- }
- void CHero::SetPos(int x, int y)
- {
- m_nPosX = x;
- m_nPosY = y;
- };
- void CHero::Move(CDXIsoMap* pMap, DIRECTION eDir)
- {
- int nx, ny, nTx, nTy;
- switch (eDir)
- {
- case DOWN:
- ny = m_nPosY +1;
- break;
- case LEFT_DOWN:
- nx = m_nPosX - 2;
- ny = m_nPosY + 1;
- break;
- case LEFT:
- nx = m_nPosX - 2;
- break;
- case LEFT_UP:
- nx = m_nPosX - 2;
- ny = m_nPosY - 1;
- break;
- case UP:
- ny = m_nPosY - 1;
- break;
- case RIGHT_UP:
- nx = m_nPosX + 2;
- ny = m_nPosY - 1;
- break;
- case RIGHT:
- nx = m_nPosX + 2;
- break;
- case RIGHT_DOWN:
- nx = m_nPosX + 2;
- ny = m_nPosY + 1;
- break;
- }
- pMap->AbsoluteCell(nx, ny, nTx, nTy, FALSE);
- CELL* pCell = pMap->GetCell(nTx, nTy);
- if (((pCell->base.flag & CDXIsoMap::m_CellRmrFlg) == 0)
- && ((pCell->base.flag & CDXIsoMap::m_CellRmrFlg) == 0))
- pMap->Scroll(eDir);
- }
- void CHero::Draw(CDDSurface* pDestSurface, LPRECT pRect/*=NULL*/)
- {
- if (m_pAction[m_eAction] == NULL)
- return;
- m_pAction[m_eAction]->SetFrame(m_nFrames[m_eAction] * m_eDirection
- + m_nCurFrame);
- m_pAction[m_eAction]->MoveTo(
- m_nPosX - (m_ptSpriteSize[m_eAction].x >> 1),
- m_nPosY - m_ptSpriteSize[m_eAction].y+16);
- if (pRect==NULL)
- m_pAction[m_eAction]->Draw(pDestSurface, &m_rcView);
- else
- m_pAction[m_eAction]->Draw(pDestSurface, pRect);
- }
- void CHero::ServeIt(__int64 nTime)
- {
- static __int64 nLastTime = 0;
- const __int64 nTimeSpan = 0x10000;
- if ((nTime - nLastTime) > nTimeSpan)
- {
- nLastTime = nTime;
- }
- else
- return;
- m_nCurFrame++;
- if (m_nCurFrame>= m_nFrames[m_eAction])
- {
- switch (m_eAction)
- {
- case ACTION_AT:
- case ACTION_FM:
- case ACTION_LM:
- case ACTION_QM:
- case ACTION_HT:
- m_eAction = ACTION_AS;
- break;
- default:
- break;
- }
- m_nCurFrame = 0;
- }
- }