?? role.cpp
字號(hào):
//#include "CRole.h" //角色Role
#include "../GameH.h"
#include "../GameLib/CIni.h"
//=========================================================================
// 實(shí)現(xiàn)部分 Flysky
//=========================================================================
//構(gòu)造/析構(gòu) 函數(shù)
POINT CRole::getPY() //獲取偏移
{
POINT tp;
tp.x=PX;
tp.y=PY;
return tp;
}
CRole::CRole(CMap &cm) //made
{
Map=(CMap *)&cm; //指針
PX=int(0.5*(ScreenHeight-(TileHeight>>1)+(ScreenWidth>>1)-(TileWidth>>1))/(TileWidth>>1)); //主角一的坐標(biāo)與地圖的偏移
PY=int(0.5*(ScreenHeight-(ScreenWidth>>1))/(TileWidth>>1));
Map->MapStartX=-PX;Map->MapStartY=-PY;
PRole=0;
if (!rl.empty()){rl.clear();}
if (!actor.empty()){actor.clear();}
if (!npc.empty()){npc.clear();}
}
CRole::CRole(CMap &cm,char *FileName)
{
Map=(CMap *)&cm; //指針
PX=int(0.5*(ScreenHeight-(TileHeight>>1)+(ScreenWidth>>1)-(TileWidth>>1))/(TileWidth>>1)); //主角一的坐標(biāo)與地圖的偏移
PY=int(0.5*(ScreenHeight-(ScreenWidth>>1))/(TileWidth>>1));
Map->MapStartX=-PX;Map->MapStartY=-PY;
PRole=0;
LoadRoleIni(FileName);
if (!rl.empty()){rl.clear();}
if (!actor.empty()){actor.clear();}
if (!npc.empty()){npc.clear();}
}
CRole::~CRole() //釋放函數(shù)
{
Map=NULL; //空指針
if (!rl.empty())
{
for(int i=0;i<int(rl.size());i++)
{
if (rl[i].lpDDS_ROLEL!=NULL) {rl[i].lpDDS_ROLEL->Release();rl[i].lpDDS_ROLEL=NULL;}
if (rl[i].lpDDS_ROLEP!=NULL) {rl[i].lpDDS_ROLEP->Release();rl[i].lpDDS_ROLEP=NULL;}
}
rl.clear();
}
if (!actor.empty()){actor.clear();}
if (!npc.empty()){npc.clear();} //釋放
}
void CRole::LoadRoleIni(char *FileName){if (FileName!=NULL) {strcpy(IniFileName,FileName);}} //讀取INI
//索引表部分
int CRole::TableAdd(stFindTable sf) //插入索引表
{
//二分找表法
//ByFlysky
if (!RlFind.empty())
{
//12
//_2_____1___________
//|13|15|17|19|21|23|
//|__|__|__|__|__|__|
int i=int(RlFind.size());
POINT tp={0,i}; //X是從 Y是到
while(tp.x<tp.y) //如果小于兩個(gè)
{
i=int((tp.x+tp.y)>>1); //除以二
if (sf.value<RlFind[i].value) tp.y=i-1; //減1
else if (sf.value>RlFind[i].value) tp.x=i+1; //加一
else if (sf.value==RlFind[i].value)
{
if (RlFind[i].ID!=sf.ID)
{
tp.x=i; //X等
tp.y=i; //Y等
}
else RlFind.erase(RlFind.begin()+i); //刪除這個(gè)
}
}
if (tp.x==tp.y)
{
RlFind.insert(RlFind.begin()+tp.x,sf); //插入數(shù)據(jù)
return tp.x;
}
return -1;
}
RlFind.push_back(sf); //OK
return int(RlFind.size());
}
int CRole::FindDataT(int Value)
{
int Ret=-1;
int i=int(RlFind.size());
POINT tp={0,i}; //X是從 Y是到
while(tp.x<=tp.y) //如果小于兩個(gè)
{
i=int((tp.x+tp.y)>>1); //除以二
if (Value<RlFind[i].value) tp.y=i-1; //減1
else if (Value>RlFind[i].value) tp.x=i+1; //加一
else if (Value==RlFind[i].value)
{
Ret=RlFind[i].ID;
}
}
return Ret;
}
vector<int> CRole::FindDataTV(int Value)//返回一個(gè)容器
{
vector<int> tmp;
int i=int(RlFind.size());
POINT tp={0,i}; //X是從 Y是到
for (int i=0;i<int(RlFind.size());i++)
{
if (RlFind[i].value<Value) continue;
else if (RlFind[i].value==Value)
{
tmp.push_back(RlFind[i].ID);
}
else if (RlFind[i].value>Value) break;
}
return tmp;
}
int CRole::TableAdd(int ID,int value) //插入
{
stFindTable tmp; //建立索引
tmp.ID=ID; tmp.value=value;
return TableAdd(tmp); //傳入
}
bool CRole::TablePX() //索引表排序
{
stFindTable tmp;
for (int i=0;i<int(RlFind.size()-1);i++)
{
tmp=RlFind[i+1];
int j=i;
while (j>=0&&tmp.value<RlFind[j].value)
{
RlFind[j+1]=RlFind[j];
j--;
}
RlFind[j+1]=tmp;
}
return true;
}
//添加刪除部分
stRole CRole::StCToSt(stRoleC tmpc) //從接口結(jié)構(gòu)轉(zhuǎn)換為內(nèi)部結(jié)構(gòu)
{
stRole sr;
strcpy(sr.Name,tmpc.Name); //名字
sr.EScrFName=tmpc.EScrFName; //初始腳本
sr.Exp=tmpc.Exp; //經(jīng)驗(yàn)值
sr.MaxExp=tmpc.MaxExp; //M經(jīng)驗(yàn)值
sr.face=tmpc.face; //面向
sr.facej=0;
sr.IsNPC=tmpc.IsNPC; //以后改
strcpy(sr.facePic,tmpc.facePic); //PIC
sr.HP=tmpc.HP;
sr.MP=tmpc.MP;
sr.MaxHP=tmpc.MaxHP;
sr.MaxMP=tmpc.MaxMP;
sr.Level=tmpc.Level;
sr.lpDDS_ROLEL=NULL;
sr.lpDDS_ROLEP=NULL;
sr.movex=tmpc.movex;
sr.movey=tmpc.movey;
sr.oldx=0;
sr.oldy=0;
sr.PathSteps=0;
sr.PathCSteps=0;
sr.roler=tmpc.roler;
sr.WalkLoop=false;
sr.facejd=false;
sr.VS=tmpc.VS;
sr.x=tmpc.x;
sr.y=tmpc.y;
return sr; //返回
}
bool CRole::AddActor(char *RoleIDent)
{
CIni inifile(IniFileName);//打開(kāi)一個(gè)INI文件
stRoleC nrole; //ROLE結(jié)構(gòu)
nrole.Name=inifile.ReadText(RoleIDent,"Name"); //Role的名字
nrole.IsNPC=false; //是角色
nrole.face=inifile.ReadInt(RoleIDent,"Face"); //Role的站向
nrole.Level=inifile.ReadInt(RoleIDent,"Level"); //Role的等級(jí)
nrole.HP=inifile.ReadInt(RoleIDent,"Hp"); //Role的等級(jí)
nrole.MP=inifile.ReadInt(RoleIDent,"Hp"); //Role的等級(jí)
nrole.Exp=inifile.ReadInt(RoleIDent,"Exp"); //第一級(jí)的經(jīng)驗(yàn)導(dǎo)入
nrole.VS=IntTobool(inifile.ReadInt(RoleIDent,"VS")); //是否顯示
//nrole.RLSEx
nrole.facePic=inifile.ReadText(RoleIDent,"PicName"); //Role的人物圖片
nrole.roler.left=inifile.ReadInt(RoleIDent,"PicLeft"); //Pic的左邊偏移
nrole.roler.top=inifile.ReadInt(RoleIDent,"PicTop"); //Pic的上邊偏移
nrole.roler.right=inifile.ReadInt(RoleIDent,"PicWidth"); //Pic的寬度
nrole.roler.bottom=inifile.ReadInt(RoleIDent,"PicHeight"); //Pic的高度
bool ret=AddActor(nrole);
if (nrole.Name!=NULL) {delete[]nrole.Name;nrole.Name=NULL;}
if (nrole.facePic!=NULL) {delete[]nrole.facePic;nrole.facePic=NULL;}
return ret;
}
bool CRole::AddActor(stRoleC tactc)
{
char tmpcx[255];
if (tactc.IsNPC==false) //是NPC
{
stRole tact=StCToSt(tactc); //建立真正的
strcpy(tmpcx,NPCPath);
strcat(tmpcx,tact.facePic);
if(CreateSurface(tact.lpDDS_ROLEP,tmpcx,0,0)!=true) return false; //說(shuō)不定有問(wèn)題
if (actor.empty())
{
tact.x=0;
tact.y=0;//刷新坐標(biāo)
}
else
{
tact.x=rl[actor[0]].x+int(actor.size());
tact.y=rl[actor[0]].y+int(actor.size());//刷新坐標(biāo)
}
tact.facej=0; //刷新步數(shù)為站好
if (tact.roler.left==-9999999&&tact.roler.top==-9999999)
{
POINT tp=FindRoleP(tact.lpDDS_ROLEP);
tact.PicS.x=tp.x;tact.PicS.y=tp.y; //純正偏移數(shù)據(jù)
tact.roler.left=tact.roler.right-TileWidth-tp.x;tact.roler.top=tact.roler.bottom-TileHeight-tp.y; //刷新偏移
}
rl.push_back(tact); //放入
if (!rl.empty())
{
actor.push_back(int(rl.size())-1); //將角色的ID放入主角表
stFindTable sf;
sf.ID=int(rl.size())-1; //等于這個(gè)ID
sf.value=tact.x+tact.y; //將X+y進(jìn)入
if (RlFind.empty()) RlFind.push_back(sf); //OK
else TableAdd(sf); //搜索插入索引表
NPCZDS(int(rl.size())-1); //遮擋計(jì)算
}
}
return true;
}
bool CRole::AddNPC(char *FileName) //從列表添加NPC
{
char *tc; //臨時(shí)
bool ret=false;
stRoleC nrole; //ROLE結(jié)構(gòu)
CIni inifile(FileName);//打開(kāi)一個(gè)INI文件
int MAX_NPC=inifile.ReadInt("NPC","Max_NPC"); //一共的NPC數(shù)
for (int i=0;i<MAX_NPC;i++)
{
tc=inifile.ReadText("NPC",i); //讀取名字
nrole.Name=inifile.ReadText(tc,"Name"); //Role的名字
nrole.IsNPC=true; //是NPC
nrole.VS=IntTobool(inifile.ReadInt(tc,"VS")); //是否顯示
nrole.face=inifile.ReadInt(tc,"Face"); //Role的站向
nrole.x=inifile.ReadInt(tc,"x"); //RoleX
nrole.y=inifile.ReadInt(tc,"y"); //RoleY
nrole.movex=inifile.ReadInt(tc,"Move_x"); //RoleX
nrole.movey=inifile.ReadInt(tc,"Move_y"); //RoleY
nrole.EScrFName=inifile.ReadText(tc,"Script"); //Role對(duì)話
nrole.facePic=inifile.ReadText(tc,"PicName"); //Role的人物圖片
nrole.roler.left=inifile.ReadInt(tc,"PicLeft"); //Pic的左邊偏移
nrole.roler.top=inifile.ReadInt(tc,"PicTop"); //Pic的上邊偏移
nrole.roler.right=inifile.ReadInt(tc,"PicWidth"); //Pic的寬度
nrole.roler.bottom=inifile.ReadInt(tc,"PicHeight"); //Pic的高度
ret=AddNPC(nrole);
if (nrole.Name!=NULL) {delete[]nrole.Name;nrole.Name=NULL;}
if (nrole.facePic!=NULL) {delete[]nrole.facePic;nrole.facePic=NULL;}
if (ret==false) return false;
}
if(tc!=NULL) delete []tc;tc=NULL;
return ret;
}
bool CRole::AddNPC(stRoleC tnpcc)
{
char tmpcx[255];
if (tnpcc.IsNPC==true) //是NPC
{
stRole tnpc=StCToSt(tnpcc); //建立真正的
sprintf(tmpcx,"%s%s",NPCPath,tnpc.facePic); //路徑OK
if(CreateSurface(tnpc.lpDDS_ROLEP,tmpcx,0,0)!=true) return false; //說(shuō)不定有問(wèn)題
if (tnpc.x<0) tnpc.x=0; if (tnpc.y<0)tnpc.y=0;
if (tnpc.x>=Map->Width) tnpc.x=Map->Width-1; if (tnpc.y>=Map->Height)tnpc.y=Map->Height-1;
tnpc.facej=0; //刷新步數(shù)為站好
//圖片偏移自判斷 技術(shù)
if (tnpc.roler.left==-9999999&&tnpc.roler.top==-9999999)
{
POINT tp=FindRoleP(tnpc.lpDDS_ROLEP); //尋找偏移
tnpc.PicS.x=tp.x;tnpc.PicS.y=tp.y; //純正偏移數(shù)據(jù)
tnpc.roler.left=tnpc.roler.right-TileWidth-tp.x;tnpc.roler.top=tnpc.roler.bottom-TileHeight-tp.y; //刷新偏移
}
rl.push_back(tnpc); //放入
if (!rl.empty())
{
npc.push_back(int(rl.size())-1); //將角色的ID放入主角表
//角色索引表
stFindTable sf;
sf.ID=int(rl.size())-1; //等于這個(gè)ID
sf.value=tnpc.x+tnpc.y; //將X+y進(jìn)入
if (RlFind.empty()) RlFind.push_back(sf); //OK
else TableAdd(sf); //搜索插入索引表
NPCZDS(int(rl.size())-1); //遮擋計(jì)算
Map->CBlockOn(true,rl[rl.size()-1].x,rl[rl.size()-1].y); //加上障礙
return true;
}
return false;
}
return false;
}
void CRole::DeleteActor(int ID) //刪除主角
{
if (!rl.empty()) rl.erase(rl.begin()+actor[ID]); //先刪除這個(gè)
if (!RlFind.empty()) for (int i=0;i<int(RlFind.size());i++) if (RlFind[i].ID==actor[ID]) RlFind.erase(RlFind.begin()+i);
}
void CRole::DeleteNPC(int ID) //刪除NPC
{
if (!rl.empty()) rl.erase(rl.begin()+npc[ID]); //先刪除這個(gè)
if (!RlFind.empty()) for (int i=0;i<int(RlFind.size());i++) if (RlFind[i].ID==npc[ID]) RlFind.erase(RlFind.begin()+i);
}
int CRole::GetActorNum(){if (!actor.empty()) return (int)actor.size(); return 0;} //獲取主角數(shù)目
int CRole::GetNpcNum(){if (!npc.empty()) return (int)npc.size(); return 0;} //獲取NPC數(shù)目
int CRole::NPCNameToID(char *Name) //NPC名字轉(zhuǎn)ID
{
//最笨的方法,其實(shí)這里不需要快
int ret=-1;
if (!npc.empty()) //如果不是空的
{
for (int i=0;i<int(npc.size());i++) //循環(huán)
if (stricmp(rl[npc[i]].Name,Name)==0) ret=i;
//if (*rl[npc[i]].Name==*Name) return i; //返回ID
}
return ret;
}
int CRole::ActorNameToID(char *Name) //主角名字轉(zhuǎn)ID
{
//最笨的方法,其實(shí)這里不需要快
int ret=-1;
if (!actor.empty()) //如果不是空的
{
for (int i=0;i<int(actor.size());i++) //循環(huán)
if (stricmp(rl[actor[i]].Name,Name)==0) ret=i;
}
return ret;
}
int CRole::NameToID(char *Name) //名字轉(zhuǎn)ID,注意,此函數(shù)返回最后一個(gè)
{
//最笨的方法,其實(shí)這里不需要快
int ret=-1;
if (!rl.empty()) //如果不是空的
{
for (int i=0;i<int(rl.size());i++) //循環(huán)
if (stricmp(rl[i].Name,Name)==0) ret=i;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -