⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 charactar.cpp

📁 diablo图形引擎例子
💻 CPP
字号:
// Charactar.cpp: implementation of the Charactar class.
//
//////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <assert.h>
#include "ImageList.h"
#include "IniFile.h"
#include "Charactar.h"
#include "AstarPathfinder.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Charactar::Charactar()
{
	m_iStandTimeCount=0;
	m_iLookAroundTimeCount=0;
	m_iWalkTimeCount=0;
	m_iCurState=0;
	m_ixOffset=0;
	m_iyOffset=0;
}

Charactar::~Charactar()
{

}

bool Charactar::Load(char *lpszFilename,bool bColorMode)
{
	IniFile inifile;
	inifile.Load(lpszFilename);
	m_ix=inifile.GetValue("[Charactar]","StartX")/32*32;
	m_iy=inifile.GetValue("[Charactar]","StartY")/32*32;
	//
	AdjustPosition(m_ix,m_iy);

	m_iDirect=inifile.GetValue("[Charactar]","Direct");

	m_iWalkSpeed=inifile.GetValue("[Charactar]","WalkSpeed");
	
	///////////////////////////////////////////
	for(int i=0;i<3;i++)
		m_lpImageList[i]=new ImageList;

	char strtmp[255];
	inifile.GetString("[Charactar]","lepstand",strtmp,255);
	m_lpImageList[0]->Create(strtmp,bColorMode,4);
	inifile.GetString("[Charactar]","leplookaround",strtmp,255);
	m_lpImageList[1]->Create(strtmp,bColorMode,4);
	inifile.GetString("[Charactar]","lepwalk",strtmp,255);
	m_lpImageList[2]->Create(strtmp,bColorMode,4);

	m_lpAstarPathfinder=new AstarPathfinder;
	m_lpAstarPathfinder->InitAstarTileMap(m_lpMBlock,m_iMapWidth,m_iMapHeight);
	return true;
}

void Charactar::Stand()
{
	m_iStandTimeCount++;
	if (m_iStandTimeCount>10) 
	{
		m_iCurState=1;
		m_iStandTimeCount=0;
	}
}

void Charactar::Loop()
{
	switch(m_iCurState)
	{
	case 0:
			Stand();
			break;
	case 1:
			LookAround();
			break;
	case 2:
			Walk();
			break;
	}

}

void Charactar::LookAround()
{
	m_iLookAroundTimeCount++;
	if (m_iLookAroundTimeCount>=m_lpImageList[1]->m_iTotalFrame/8)
	{
		m_iLookAroundTimeCount=0;
		m_iCurState=0;
	}
}

void Charactar::Walk()
{

	int i,j,tx,ty,xo,yo,xa,ya;
	if ( !m_lpAstarPathfinder->ReachedGoal() && (m_ix==m_iDestinationPositionX*32)&&(m_iy==m_iDestinationPositionY*32) )
         {
			m_lpAstarPathfinder->PathNextNode();   // point to next node
            m_iDestinationPositionX = m_lpAstarPathfinder->NodeGetX(); // get the positions from current node
      		m_iDestinationPositionY = m_lpAstarPathfinder->NodeGetY();
         }
	xo = m_ix % 32;
	yo = m_iy % 32;
	xa   = xo - yo;
	ya   = xo/2 + yo/2;

	tx=m_ix/32;ty=m_iy/32;
	i=m_ix-m_iDestinationPositionX*32;
	j=m_iy-m_iDestinationPositionY*32;

	if (i==0 && j==0) 
	{
		m_iCurState=0;
	}
	int dx=-1,dy=-1,dd=-1;
	if (j<0)  dy=1;
	if (j>0)  dy=5;
	if (i>0)  dx=3;
	if (i<0)  dx=7;
	if (dy==1 && dx==7)  dd=0;
	if (dy==1 && dx==3)  dd=2;
	if (dy==5 && dx==7)  dd=6;
	if (dy==5 && dx==3)  dd=4;
	if (dd==-1) dd=dx;
	if (dd==-1) dd=dy;
	MovePosition(dd);
	
	m_iWalkTimeCount++;
	if (m_iWalkTimeCount>=m_lpImageList[2]->m_iTotalFrame/8)
	{
		m_iWalkTimeCount=0;
	}
	

}

void Charactar::Draw(int screenx, int screeny, int tx, int ty, WORD *lpBitmap,long lPitch)
{
  int mapx,mapy,xo,yo,xa,ya,index;
  unsigned int x,y;

  x=m_ix;y=m_iy;
  mapx = x / 32;
  xo   = x % 32;
  mapy = y / 32;
  yo   = y % 32;
  xa   = xo - yo;
  ya   = xo/2 + yo/2;

  
  m_DumpXo=xo;
  m_DumpYo=yo;
  m_DumpXa=xa;
  m_DumpYa=ya;
  
  if ( tx==mapx && ty==mapy)
  {
	  switch(m_iCurState)
	  {
	  case 0:      
			index=m_iDirect;
			if (index>=0 && index<m_lpImageList[0]->m_iTotalFrame) 
			{
				m_lpImageList[0]->DrawCenter(index,screenx+xa,screeny+ya+32,lpBitmap,lPitch);//,RGB15(160,160,200),RGB15(0,0,0));
			}
			break;
	  case 1 :
		  	index=m_iLookAroundTimeCount;
			if (index>=0 && index<m_lpImageList[1]->m_iTotalFrame/8) 
			{			 
				index=m_iDirect*m_lpImageList[1]->m_iTotalFrame/8+index;
				m_lpImageList[1]->DrawCenter(index,screenx+xa,screeny+ya+32,lpBitmap,lPitch);//,RGB15(160,160,200),RGB15(0,0,0));
			}  
			break;
	  case 2 :
		  	index=m_iWalkTimeCount;
			if (index>=0 && index<m_lpImageList[2]->m_iTotalFrame/8) 
			{			 
				index=m_iDirect*m_lpImageList[2]->m_iTotalFrame/8+index;
				m_lpImageList[2]->DrawCenter(index,screenx+xa,screeny+ya+32,lpBitmap,lPitch);//,RGB15(160,160,200),RGB15(0,0,0));
			}  
			break;


	  } 
  } 
}
void Charactar::AdjustPosition(int x, int y)
{
	int tx,ty,i;

	tx=x-0;ty=y-0;
	
	for(i=0;i<tx/2;i++)
	{
		m_ixOffset+=2;
		m_iyOffset++;
	}
	for(i=0;i<ty/2;i++)
	{
		m_ixOffset-=2;
		m_iyOffset++;
	}
}



void Charactar::Dump(char *buffer)
{
	assert(buffer);
	sprintf(buffer,"State=%.3d LookTC=%.3d StandTC=%.3d Direct=%.3d x=%.3d\n y=%.3d xOfs=%.3d yOfs=%.3d xo=%d yo=%d xa=%d ya=%d",
		m_iCurState,m_iLookAroundTimeCount,m_iStandTimeCount,m_iDirect,m_ix,m_iy,m_ixOffset,m_iyOffset
		,m_DumpXo,m_DumpYo,m_DumpXa,m_DumpYa);

}

void Charactar::WalkTo(int dx, int dy)
{
	if(dx<0 || dy<0 || dx>=m_iMapWidth || dy>=m_iMapHeight ) return;
	if (m_lpAstarPathfinder->NewPath(m_ix/32,m_iy/32,dx,dy))
         {
            m_lpAstarPathfinder->PathNextNode();   // point to next node = next tile on path
            m_iDestinationPositionX = m_lpAstarPathfinder->NodeGetX();  // get the positions from current node
      	  	m_iDestinationPositionY = m_lpAstarPathfinder->NodeGetY();  // and set the new destination
		    m_iCurState=2; 
		 }
}

void Charactar::SetupMapInfo(MAPBLOCK *lpMBlock, int w, int h)
{
	m_lpMBlock=lpMBlock;
	m_iMapWidth=w;
	m_iMapHeight=h;
}

void Charactar::MovePosition(int direct)
{
		switch(direct)
	{
		case 1: 
					m_iy+=m_iWalkSpeed;
					m_iDirect=1;
					m_ixOffset-=m_iWalkSpeed;
					m_iyOffset+=m_iWalkSpeed/2;
					break;
		case 5:
					m_iy-=m_iWalkSpeed;	
					m_iDirect=5;
					m_ixOffset+=m_iWalkSpeed;
					m_iyOffset-=m_iWalkSpeed/2;
					break;
		case 3:			
					m_ix-=m_iWalkSpeed;
					m_iDirect=3;
					m_ixOffset-=m_iWalkSpeed;
					m_iyOffset-=m_iWalkSpeed/2;
					break;
		case 7:
					m_ix+=m_iWalkSpeed;
					m_iDirect=7;
					m_ixOffset+=m_iWalkSpeed;
					m_iyOffset+=m_iWalkSpeed/2;
					break;
		case 4 :	m_ix-=m_iWalkSpeed;
					m_iy-=m_iWalkSpeed;
					m_iDirect=4;
					m_iyOffset-=m_iWalkSpeed;
					break;
		case 6 :	m_ix+=m_iWalkSpeed;
					m_iy-=m_iWalkSpeed;
					m_iDirect=6;
					m_ixOffset+=m_iWalkSpeed*2;
					break;
		case 0 :	m_ix+=m_iWalkSpeed;
					m_iy+=m_iWalkSpeed;
					m_iDirect=0;
					m_iyOffset+=m_iWalkSpeed;
					break;
		case 2 :	m_ix-=m_iWalkSpeed;
					m_iy+=m_iWalkSpeed;
					m_iDirect=2;
					m_ixOffset-=m_iWalkSpeed*2;
					break;
	}	

}


void Charactar::DrawiT(int screenx, int screeny, WORD *lpBitmap, long lPitch)
{
	int xa=0,ya=0,index;  
	switch(m_iCurState)
	  {
	  case 0:      
			index=m_iDirect;
			if (index>=0 && index<m_lpImageList[0]->m_iTotalFrame) 
			{
				m_lpImageList[0]->DrawCenter(index,32+screenx+xa,screeny+ya+32,lpBitmap,lPitch);//,RGB15(160,160,200),RGB15(0,0,0));
			}
			break;
	  case 1 :
		  	index=m_iLookAroundTimeCount;
			if (index>=0 && index<m_lpImageList[1]->m_iTotalFrame/8) 
			{			 
				index=m_iDirect*m_lpImageList[1]->m_iTotalFrame/8+index;
				m_lpImageList[1]->DrawCenter(index,32+screenx+xa,screeny+ya+32,lpBitmap,lPitch);//,RGB15(160,160,200),RGB15(0,0,0));
			}  
			break;
	  case 2 :
		  	index=m_iWalkTimeCount;
			if (index>=0 && index<m_lpImageList[2]->m_iTotalFrame/8) 
			{			 
				index=m_iDirect*m_lpImageList[2]->m_iTotalFrame/8+index;
				m_lpImageList[2]->DrawCenter(index,32+screenx+xa,screeny+ya+32,lpBitmap,lPitch);//,RGB15(160,160,200),RGB15(0,0,0));
			}  
			break;


	  } 
  } 



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -