void Monster::FollowRun(CCNode* m_hero,CCNode* m_map)
{
//得到两点x的距离,记得怪物的坐标要加上地图的
float x = m_hero->getPositionX()-(this->getPositionX()+m_map->getPositionX());
//得到两点y的距离,记得怪物的坐标要加上地图的
float y = m_hero->getPositionY()-(this->getPositionY()+m_map->getPositionY());
//先计算怪物和英雄的距离
dis = sqrt(pow(x,2) + pow(y,2));
if(dis>=300)//当怪物与英雄距离超过300
return;
if(dis<=100)//在怪物攻击范围内,怪物停止移动
{
this->StopAnimation();//停止跑动
JudegeAttack();//以一定的概率判断是是否出动攻击
return;
}
if(x<-100)//判断怪物横坐标和英雄的距离
{
MonsterDirecton=true;
m_MonsterSprite->setFlipX(MonsterDirecton);//设置方向
if(IsAttack)
return;
this->setPosition(this->getPositionX()-1,this->getPositionY());//怪物向英雄移动
this->SetAnimationAdv("monster_run.plist","monster_run.png", "monster_run", 1, 6, MonsterDirecton);//播放动画
}
else if(x>100)
{
MonsterDirecton=false;
m_MonsterSprite->setFlipX(MonsterDirecton);//设置方向
if(IsAttack)
return;
this->setPosition(this->getPositionX()+1,this->getPositionY());
this->SetAnimationAdv("monster_run.plist", MonsterDirecton);//播放动画
}
else if(x<=100)//怪物橫坐標和英雄相差在100以内时,开始移动怪物纵坐标
{
if(m_hero->getPositionY()>this->getPositionY())
{
m_MonsterSprite->setFlipX(MonsterDirecton);//设置方向
if(IsAttack)
return;
this->setPosition(this->getPositionX(),this->getPositionY()+1);
this->SetAnimationAdv("monster_run.plist", MonsterDirecton);//播放动画
}
else if(m_hero->getPositionY()<this->getPositionY())
{
m_MonsterSprite->setFlipX(MonsterDirecton);//设置方向
if(IsAttack)
return;
this->setPosition(this->getPositionX(),this->getPositionY()-1);
this->SetAnimationAdv("monster_run.plist", MonsterDirecton);//播放动画
}
}
}
unsigned long long GetCurrentTime()
{
struct timeval tv;
gettimeofday(&tv,NULL);
unsigned long long sec = tv.tv_sec;
sec = sec * 1000;
unsigned long long usec = tv.tv_usec;
usec = usec / 1000;
unsigned long long cur = sec + usec;
return cur;
}
void Monster::JudegeAttack()
{
srand((uint)GetCurrentTime());
int x = rand()%100;
if(x>98)
{
this->AttackAnimation("monster_attack.plist", "monster_attack.png", "monster_attack", 5,MonsterDirecton);
}
}
void Monster::MonsterSeeRun()
{
if(dis<300)
return;
this->SetAnimation("monster_run",6,MonsterDirecton);//播放动画
CCMoveBy *moveby1;
if(MonsterDirecton==true)
moveby1=CCMoveBy::create(2,ccp(-100,0));
else
moveby1=CCMoveBy::create(2,ccp(100,0));
//创建回调动作,巡逻路线完后
CCCallFunc* callFunc=CCCallFunc::create(this,callfunc_selector(Monster::StopAnimation));
//创建连续动作
CCActionInterval* xunluo=CCSequence::create(moveby1,callFunc,NULL);
this->runAction(xunluo);
}
//启动监听
void Monster::StartListen(CCNode* m_hero,CCNode* m_map)
{
my_hero=m_hero;
my_map=m_map;
this->schedule(schedule_selector(Monster::updateMonster),3.0f);//每隔3秒计算距离
this->scheduleUpdate();//英雄一旦进入可视范围,怪物追着英雄打
}
//监听函数,每隔3秒检测下
void Monster::updateMonster(float delta)
{
//得到两点x的距离,记得怪物的坐标要加上地图的
float x = my_hero->getPositionX()-(this->getPositionX()+my_map->getPositionX());
//得到两点y的距离,记得怪物的坐标要加上地图的
float y = my_hero->getPositionY()-(this->getPositionY()+my_map->getPositionY());
//先计算怪物和英雄的距离
dis = sqrt(pow(x,2));
if(dis>=300)
{
if(!IsRunning)
MonsterSeeRun();
}
}
void Monster::update(float delta)
{
if(dis<300)///当英雄在它的可视范围内,不断追着英雄
FollowRun(my_hero,my_map);
}
|