【cocos2d-x 手游研发----怪物智能AI】

时间:2023-03-10 01:18:13
【cocos2d-x 手游研发----怪物智能AI】

原创文章,转载请注明出处:http://www.cnblogs.com/zisou/p/cocos2d-xARPG4.html

谈到怪物AI,我觉得就比较话多了,首先理解一下(Artificial Intelligence人工智能),人工智能是计算机科学的一个分支,人工智能是计算机科学技术的前沿科技领域。人工智能与计算机软件有密切的关系。各种人工智能应用系统都要用计算机软件去实现,许多聪明的计算机软件也应用了人工智能的理论方法和技术。

总而言之,言而总之,我理解的AI就是:用人类自己的思考模式去赋予游戏中角色判断的能力,来进行某些特定的行为!

下面具体图文介绍一下,我在游戏中一些实用的AI制作思路;

【cocos2d-x 手游研发----怪物智能AI】

这是这个思路就可以让怪物捕获玩家,并且追踪玩家攻击的一个图示!这个思路是基本,最终决策怪物是否去执行这个行为的操作算法又是一个AI智能,所以咱们又可以分为:

决策AI:可以根据怪物等级,怪物的类型(BOSS),怪物技能类型等很多决策和根本属性相连所起作用的操作,都可以做成决策AI;

行为AI:追踪玩家,攻击玩家,给玩家加血(如同伴),逃跑等这些行为动作操作;

OK,下面大家应该了解AI的基本思路了,那我们直接贴代码了;

我直接把AI写在了地图层里面了,因为之前我框架的设计,是有一个地图base,而所有的怪物,主角色,NPC都属于地图层,因为要做很多遍历操作,所以在地图base中写上智能AI算法,然后再继承拓展下来。

首先先制造一群怪物!并且让怪物在设置的Rect内随机移动!

/********************************************
注释:
makenum 最大怪物数量
monsteridtags 怪物起始标签
randomrange 配置怪圈大小(只能填正整数)
monster_model 怪物数据
yinzihigth 影子的距离
fristpoint 怪物初始设定的怪圈点
*********************************************/
void Maps_Diyu::makemonster(int makenum,int monsteridtags,CCPoint randomrange,MainRoledata monster_model,int yinzihigth,CCPoint fristpoint)
{
for (int i = ; i < makenum; i++)
{
int add_x = (int)(CCRANDOM_MINUS1_1()*randomrange.x);
int add_y =(int)(CCRANDOM_MINUS1_1()*randomrange.y);
monster_model.tags=monsteridtags+i;
monster_model.nowpoint= CCPointMake(nowmap->getContentSize().width/+fristpoint.x+add_x,nowmap->getContentSize().height/+fristpoint.y+add_y);
SpiritsMonster* newmonster = new SpiritsMonster(monster_model,,yinzihigth);
nowmap->addChild(newmonster->monster, ,monsteridtags+i);
}
}

然后处理怪物的行为AI:

/********************************************
注释:
monsternum 最大怪物数量
monsteridtags 怪物起始标签
monster_model 怪物数据
reatR 怪物仇恨视角的范围值(通常200左右)
attck_reatR 怪物可攻击范围值(远程怪和物理怪物)
playerpoint 主角坐标
randomrange 怪物默认随机移动范围
fristpoint 怪物初始设定的怪圈点
*********************************************/
void Maps_Diyu::attact_todo(int monsternum,int monsteridtags ,MainRoledata monster_model,int reatR,int attck_reatR,CCPoint playerpoint,CCPoint randomrange,CCPoint fristpoint)
{
for (int i = ; i < monsternum; i++)
{
CCRect* attck_rect = new CCRectMake(this->getChildByTag(monsteridtags+i)->getPosition().x-attck_reatR/,this->getChildByTag(monsteridtags+i)->getPosition().y-attck_reatR/,attck_reatR,attck_reatR);
if(attck_rect->containsPoint(playerpoint)==true)
{
//执行怪物攻击主角
CCLog(FontChina::G2U("进入怪物可攻击范围"));
//攻击时改变方向
SpiritsMonster::attackTomainRole_dir(ccp((int)playerpoint.x,(int)playerpoint.y),(CCSprite*)(this->getChildByTag(monsteridtags+i)),monster_model);
//释放技能动画
int add_x = (int)(CCRANDOM_MINUS1_1()*);
int add_y =(int)(CCRANDOM_MINUS1_1()*);
SkillEffects* skill = new SkillEffects(ccp((int)playerpoint.x+add_x,(int)playerpoint.y+add_y),monster_model,(0.2f+CCRANDOM_0_1()),,,);
this->addChild(skill->effects_main,,);
}
else
{
CCRect* track_rect = new CCRectMake(this->getChildByTag(monsteridtags+i)->getPosition().x-reatR/,this->getChildByTag(monsteridtags+i)->getPosition().y-reatR/,reatR,reatR);
if(track_rect->containsPoint(playerpoint)==true)
{
//执行移动,不停追杀主角
CCLog(FontChina::G2U("进入怪物仇恨视野区域"));
//防止靠近主角,保持一定距离!主要为了好看和处理被攻击效果。
int add_x = (int)(CCRANDOM_MINUS1_1()*);
int add_y =(int)(CCRANDOM_MINUS1_1()*);
//移动时改变方向
SpiritsMonster::moveTomap_dir(ccp((int)playerpoint.x,(int)playerpoint.y),(CCSprite*)(this->getChildByTag(monsteridtags+i)),monster_model);
CCArray* callbackArray = CCArray::create();
int nowtags = monsteridtags+i;
char strs[];
sprintf(strs,"%d",nowtags);
CCString* ccnowtags = CCString::create(strs);
callbackArray->addObject(ccnowtags);
callbackArray->addObject(monster_model.spiritname);
char dir_x[];
sprintf(dir_x,"%d",(int)this->getChildByTag(nowtags)->getPositionX());
char dir_y[];
sprintf(dir_y,"%d",(int)this->getChildByTag(nowtags)->getPositionY());
CCString* d_x = CCString::create(dir_x);
CCString* d_y = CCString::create(dir_y);
callbackArray->addObject(d_x);
callbackArray->addObject(d_y);
CCFiniteTimeAction *actbackfun = CCCallFuncO::create(this, callfuncO_selector(Maps_Diyu::moveoverCallBack),callbackArray);
CCActionInterval* act_movexixue = CCMoveTo::create(+CCRANDOM_MINUS1_1(),ccp((int)playerpoint.x+add_x,(int)playerpoint.y+add_y));
this->getChildByTag(nowtags)->runAction(CCSequence::create(act_movexixue,actbackfun,NULL));
}
else
{
//脱离仇恨时,随机定点周围移动
int stopandrun = (int)(CCRANDOM_MINUS1_1()*);
if(stopandrun>=)
{
//脱离仇恨
int add_x = (int)(CCRANDOM_MINUS1_1()*randomrange.x);
int add_y =(int)(CCRANDOM_MINUS1_1()*randomrange.y);
CCPoint move_dir = ccp(this->getContentSize().width/+fristpoint.x+add_x,this->getContentSize().height/+fristpoint.y+add_y); SpiritsMonster::moveTomap_dir(ccp((int)move_dir.x,(int)move_dir.y),(CCSprite*)(this->getChildByTag(monsteridtags+i)),monster_model);
CCArray* callbackArray = CCArray::create();
int nowtags = monsteridtags+i;
char strs[];
sprintf(strs,"%d",nowtags);
CCString* ccnowtags = CCString::create(strs);
callbackArray->addObject(ccnowtags);
callbackArray->addObject(monster_model.spiritname);
char dir_x[];
sprintf(dir_x,"%d",(int)this->getChildByTag(nowtags)->getPositionX());
char dir_y[];
sprintf(dir_y,"%d",(int)this->getChildByTag(nowtags)->getPositionY());
CCString* d_x = CCString::create(dir_x);
CCString* d_y = CCString::create(dir_y);
callbackArray->addObject(d_x);
callbackArray->addObject(d_y);
CCFiniteTimeAction *actbackfun = CCCallFuncO::create(this, callfuncO_selector(Maps_Diyu::moveoverCallBack),callbackArray);
CCActionInterval* act_movexixue = CCMoveTo::create(+CCRANDOM_MINUS1_1(),ccp(move_dir.x,move_dir.y));
this->getChildByTag(nowtags)->runAction(CCSequence::create(act_movexixue,actbackfun,NULL)); }
}
}
}
}

这段逻辑包括了,如果玩家进入怪物的仇恨区域,那么怪物会先移动到玩家附近的点(此时玩家移动开,怪物会根据定时器去判断下一步如何操作:如1,玩家移动后还在仇恨区域,那么继续追踪玩家;2,如果玩家脱离仇恨,那么怪物执行回到初始坐标点的操作,然后继续原地巡逻!),如果此时玩家已经在怪物的攻击范围内了,那么怪物会直接选择攻击!这样怪物就会变得貌似有了思想一样,他会去考虑判断各种行为,那个区域我该做什么?不该做什么?

有了这个逻辑,我们还要赋予怪物循环思考的能力!

//启动所有怪物智能AI监听器
nowmap->schedule(schedule_selector(Maps_Diyu::makemonsterAttack), (3.0f));

我们根据定时器去每隔3秒内去监控行为AI!

整个行为AI已经完毕!就可以实现比较智能的怪物追杀主角了!这也就牵扯到了游戏难度怪物难度的问题了,往往好玩的游戏智能AI都占用了程序一半以上时间去处理;怪物并不是血多,防御高他就是一个BOSS,如果BOSS他会满地图追着玩家打,会自己加血,逃跑,那这样的BOSS我们作为玩家来说,打起来也比较刺激有意思,有挑战!

这篇就写这么多了,下回分解一下游戏界面UI交互的一些东西,充分运用定时器去管理一些玩家对屏幕基本操作的逻辑;

PS:COCOS2DQQ群 ,和大家一起分享cocos2dx开发经验【41131516】