让机器人朝靠近英雄的地方走

来源:互联网 发布:中国烟民为什么多 知乎 编辑:程序博客网 时间:2024/04/29 18:21
void GameLayer::updateRobots(float dt){int alive=0; //当前活着的机器人的数量int distanceSQ;int randomChoice=0;CCObject* pObject=NULL;CCARRAY_FOREACH(this->_robots,pObject){Robot* robot=(Robot*)pObject;if(robot->_actionState != kActionDead){alive++;if(::GetCurTime()>robot->_nextDecisionTime){distanceSQ=ccpDistanceSQ(robot->getPosition(),this->_hero->getPosition());if(distanceSQ<=50*50){robot->_nextDecisionTime=::GetCurTime()+frandom_range(0.1,0.5)*1000;randomChoice=random_range(0,1);if(randomChoice==0){   //机器人执行攻击动作if(this->_hero->getPositionX()>robot->getPositionX()){robot->setScaleX(1.0f);}else{robot->setScaleX(-1.0f);}robot->_nextDecisionTime=robot->_nextDecisionTime+frandom_range(0.1,0.5)*2000;robot->attack();//检测机器人是否攻击到英雄if(fabsf(this->_hero->getPositionY()-robot->getPositionY())<10){if(_hero->_hitBox.actual.intersectsRect(robot->_attackBox.actual)){_hero->hurtWithDamage(robot->_damage);if(_hero->_actionState==kActionDead && (((GameScene*)(this->getParent()))->_hudLayer->getActionByTag(537)==NULL)){this->endGame();}}}}else{   //执行空闲动作robot->idle();}}else if(distanceSQ <= CCDirector::sharedDirector()->getWinSize().width*CCDirector::sharedDirector()->getWinSize().width){robot->_nextDecisionTime=::GetCurTime()+frandom_range(0.5,1.0)*1000;randomChoice=random_range(0,2);if(randomChoice==0){  //机器人执行行走动作CCPoint moveDirection=ccpNormalize(ccpSub(_hero->getPosition(),robot->getPosition()));  //求标准化向量robot->walkWithDirection(moveDirection);robot->updateDesiredPosition(dt*20);//robot->setPosition(robot->_desiredPosition);}else{  //执行空闲动作robot->idle();}}}}}if(alive==0 && ((GameScene*)(this->getParent()))->_hudLayer->getChildByTag(537)==NULL){this->endGame();}}

0 0