📄 role.cpp
字号:
//#include "CRole.h" //角色Role
#include "../GameH.h"
#include "../GameLib/CIni.h"
//=========================================================================
// 实现部分 Flysky
//=========================================================================
//构造/析构 函数
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)); //主角一的坐标与地图的偏移
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)); //主角一的坐标与地图的偏移
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() //释放函数
{
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) //如果小于两个
{
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); //删除这个
}
}
if (tp.x==tp.y)
{
RlFind.insert(RlFind.begin()+tp.x,sf); //插入数据
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) //如果小于两个
{
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)//返回一个容器
{
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) //从接口结构转换为内部结构
{
stRole sr;
strcpy(sr.Name,tmpc.Name); //名字
sr.EScrFName=tmpc.EScrFName; //初始脚本
sr.Exp=tmpc.Exp; //经验值
sr.MaxExp=tmpc.MaxExp; //M经验值
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.move=tmpc.move; //移动
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);//打开一个INI文件
stRoleC nrole; //ROLE结构
nrole.Name=inifile.ReadText(RoleIDent,"Name"); //Role的名字
nrole.IsNPC=false; //是角色
nrole.face=inifile.ReadInt(RoleIDent,"Face"); //Role的站向
nrole.Level=inifile.ReadInt(RoleIDent,"Level"); //Role的等级
nrole.HP=inifile.ReadInt(RoleIDent,"Hp"); //Role的等级
nrole.MP=inifile.ReadInt(RoleIDent,"Hp"); //Role的等级
nrole.MaxHP=nrole.HP;nrole.MaxExp=nrole.MP; //满血一样
nrole.Exp=inifile.ReadInt(RoleIDent,"Exp"); //第一级的经验导入
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; //说不定有问题
if (actor.empty())
{
tact.x=0;
tact.y=0;//刷新坐标
}
else
{
tact.x=rl[actor[0]].x+int(actor.size());
tact.y=rl[actor[0]].y+int(actor.size());//刷新坐标
}
tact.facej=0; //刷新步数为站好
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; //纯正偏移数据
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; //等于这个ID
sf.value=tact.x+tact.y; //将X+y进入
if (RlFind.empty()) RlFind.push_back(sf); //OK
else TableAdd(sf); //搜索插入索引表
NPCZDS(int(rl.size())-1); //遮挡计算
}
}
return true;
}
bool CRole::AddNPC(char *FileName) //从列表添加NPC
{
char *tc; //临时
bool ret=false;
stRoleC nrole; //ROLE结构
CIni inifile(FileName);//打开一个INI文件
int MAX_NPC=inifile.ReadInt("NPC","Max_NPC"); //一共的NPC数
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对话
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; //说不定有问题
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; //刷新步数为站好
//图片偏移自判断 技术
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; //纯正偏移数据
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; //等于这个ID
sf.value=tnpc.x+tnpc.y; //将X+y进入
if (RlFind.empty()) RlFind.push_back(sf); //OK
else TableAdd(sf); //搜索插入索引表
NPCZDS(int(rl.size())-1); //遮挡计算
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]); //先删除这个
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]); //先删除这个
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;} //获取主角数目
int CRole::GetNpcNum(){if (!npc.empty()) return (int)npc.size(); return 0;} //获取NPC数目
int CRole::NPCNameToID(char *Name) //NPC名字转ID
{
//最笨的方法,其实这里不需要快
int ret=-1;
if (!npc.empty()) //如果不是空的
{
for (int i=0;i<int(npc.size());i++) //循环
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) //主角名字转ID
{
//最笨的方法,其实这里不需要快
int ret=-1;
if (!actor.empty()) //如果不是空的
{
for (int i=0;i<int(actor.size());i++) //循环
if (stricmp(rl[actor[i]].Name,Name)==0) ret=i;
}
return ret;
}
int CRole::NameToID(char *Name) //名字转ID,注意,此函数返回最后一个
{
//最笨的方法,其实这里不需要快
int ret=-1;
if (!rl.empty()) //如果不是空的
{
for (int i=0;i<int(rl.size());i++) //循环
if (stricmp(rl[i].Name,Name)==0) ret=i;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -