基于cocos2dx的RPG简单实用算法之3 - 多角色跟随阵型移动
来源:互联网 发布:安卓简单捕鱼源码 编辑:程序博客网 时间:2024/04/30 09:03
1. 确定到一个阵型中心对象。
也许是一个英雄,也可以是一个隐藏的对象。也就是下文种的 GridCenter
2. 预先计算号每个阵型“槽” 相对中心对象的 向量。
void GameControlManager::startGridMode()
{
if(m_MainScene->heroList.empty())
return;
m_IsStartGridMode = true;
Point GridCenter = findGridCenter();
if(memberNumber == 1)
return;
else if(memberNumber == 2)
{
// 0
// 1
originRelativeVec[0] = Vec2(0,Grid_Slot_Radius);
originRelativeVec[1] = Vec2(0,-Grid_Slot_Radius);
}
else if(memberNumber == 3)
{
Point firstPos = GridCenter + Vec2(0,Grid_Slot_Radius);
Point secondPos = firstPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-120));
Point thirdPos = secondPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-120));
// 0
//
// 1 2
originRelativeVec[0] = firstPos - GridCenter;
originRelativeVec[1] = secondPos - GridCenter;
originRelativeVec[2] = thirdPos - GridCenter;
}
else if(memberNumber == 4)
{
Point firstPos = GridCenter + Vec2(0,Grid_Slot_Radius);
Point secondPos = firstPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-90));
Point thirdPos = secondPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-90));
Point fourthPos = thirdPos.rotateByAngle(GridCenter, CC_DEGREES_TO_RADIANS(-90));
// 0 战士
// 1 2 猎人 法师
// 3 牧师
originRelativeVec[0] = firstPos - GridCenter;
originRelativeVec[1] = fourthPos - GridCenter;
originRelativeVec[2] = secondPos - GridCenter;
originRelativeVec[3] = thirdPos - GridCenter;
}
//认领slot位置
int slotIndex = 0;
int minSpeed = 999;
for(auto hero : m_MainScene->heroList) //已经排序
{
if(!hero->getIsAlly() && !hero->getIsDead())
{
hero->setSlotIndex(slotIndex);
slotIndex ++;
auto actorInfo = GameData::getActorInfoFromMap(hero->getUnitID());
if(actorInfo->speed < minSpeed)
minSpeed = actorInfo->speed;
}
}
3. 根据一号英雄相对中心对象的方向来确定阵型初始朝向
Point firstmanPos = m_MainScene->heroList.front()->getCenterPoint();
// crossover_point(firstmanPos, GridCenter, )
Vec2 heroVec = firstmanPos - GridCenter;
heroVec.normalize();
m_GridAngle = getDirectionByChief(heroVec);
for(int index = 0; index < memberNumber; index++)
{
Vec2 cur = originRelativeVec[index];
Point curPoint = cur + GridCenter;
curPoint = curPoint.rotateByAngle(GridCenter, m_GridAngle);
cur = curPoint - GridCenter;
slotRelativeVec[index] = cur;
}
4. 所有英雄各就各位
for(auto hero : m_MainScene->heroList)
{
if(!hero->getIsAlly() && !hero->getIsDead())
{
Vec2 curVec = slotRelativeVec[hero->getSlotIndex()];
Point des = GridCenter + curVec;
hero->setDestinationPoint(des);
}
}
....
}
5. 当阵型移动,根据 ”中心对象“相对目的地位置 targetPos来更新阵型朝向角度GridAngle
再根据GridAngle刷新 每个槽的相对向量 cur
void GameControlManager::setGridDirection(Point targetPos)
{
//更新阵型朝向
auto GridCenter = getGridCenter();
Vec2 chiefVec = targetPos - GridCenter;
chiefVec.normalize();
m_GridAngle = getDirectionByChief(chiefVec);
for(int index = 0; index < memberNumber; index++)
{
Vec2 cur = originRelativeVec[index];
Point curPoint = cur + m_gridObject->getPosition();
curPoint = curPoint.rotateByAngle(m_gridObject->getPosition(),m_GridAngle); //在原基础上旋转
cur = curPoint - m_gridObject->getPosition();
slotRelativeVec[index] = cur;
}
m_gridObject->setRotation(CC_RADIANS_TO_DEGREES(-m_GridAngle));
}
6. 每一帧让角色移动到自己对应的槽
void GameControlManager::updateGridDirection()
{
Point slot = getSlotPosByIndex(hero->getSlotIndex());
if(hero->getCenterPoint().distance(slot) > getElasticRange())
{
hero->moveToward(slot);
}
}
- 基于cocos2dx的RPG简单实用算法之3 - 多角色跟随阵型移动
- 基于cocos2dx的RPG简单实用算法之2 - 角色跟随移动
- 基于cocos2dx的RPG简单实用算法之1 - 角色的移动
- RPG黑暗之光(3)EasyTouch插件的使用、角色控制、相机跟随、状态记录
- [Unity&RPG]实现简单的鼠标点击移动 角色
- 基于安卓的RPG角色生成器
- RPG简单角色加载
- SURVIVAL SHOOTER TUTORIAL之Camera跟随角色移动 [4]
- Unity3d相机跟随角色移动
- Unity摄像机跟随角色移动
- cocos2dx CCNotificationCenter的简单实用
- Lgame之RPG之角色
- Unity3D AI角色移动和自动跟随
- unity 如何控制摄像机跟随角色移动
- 转载他人:角色移动和相机跟随
- Unity3d摄像机跟随角色移动脚本
- UE4之跟随移动
- RPG角色游戏的创建
- 用perl,ruby和python各写了一段相同的程序弯沉功能
- 翻译Wenguang's Introduction to Universal Disk Format (UDF)--完成
- Linux备份与恢复
- hdu 4614(区间更新)
- IO-00 ---- IO-09
- 基于cocos2dx的RPG简单实用算法之3 - 多角色跟随阵型移动
- gridview无法点击之谜
- Amino lockFreelist jar包下载
- (function(){})()与!function(){}()
- JPA 缓存
- POJ 1556 The Doors(判断线段相交 && 最短路)
- 本地编译MTK6582的步骤4
- ubuntu12.04下安装Docker
- fifo实现本地简单聊天程序