前些天接手了一个同事的2d格斗类项目,这个游戏中的人物动画实现方式大致是这样的:
1,按照人物的头、手、脚等部分切成碎片;
2,用相应的碎片组合成一个需要的人物形象:帧;
3,在一个动作中选取需要的帧组合成一个动画
4,在动画中的帧上可以画些矩形框,矩形框可以有各种属性,比如atk表示攻击框,body表示被攻击框等等。
原先各个动画只有body、atk两个属性框,现在需要增加击退、击飞等其他属性框,需要改造升级。在看到代码中的攻击判断处理时不禁心悸,何方妖孽:
void XEntityManager::CheckCollision()
{
bool isHeroAttack = false;
int offset = m_pBattleMagMgr->GetMapOffset();
//主角
if(m_pHero)
{
int hero_action_id = m_pHero->GetSkillId();
CCRect defRect = m_pHero->GetDefenceRect();
CCPoint hero_pos = m_pHero->GetPosition();
if(hero_action_id == ACTION_ATTACK ||
(hero_action_id >= ACTION_SKILL_1 && hero_action_id <= ACTION_SKILL_6))
{
//主角-->敌人
CCRect atkRect = m_pHero->GetAttackRect();
float hero_range = m_pHero->GetRangeY();
if(!atkRect.size.equals(CCSizeZero))
{
//攻击框不出屏幕
if(atkRect.origin.x - offset < m_pBattleMagMgr->GetWinSize().width && atkRect.origin.x + atkRect.size.width > 0)
{
CCObject* pChild = NULL;
CCARRAY_FOREACH(m_pMonsterArray,pChild)
{
XMonstor* pObj = (XMonstor*)pChild;
if(!pObj->IsActive())
continue;
if(pObj->IsDeath())
continue;
if(pObj->IsGod())
continue;
//防御框为0
CCRect montor_def = pObj->GetDefenceRect();
if(montor_def.equals(CCRectZero))
continue;
//Y轴超出范围
CCPoint monstor_pos = pObj->GetPositionAbs();
if(abs(hero_pos.y - monstor_pos.y) > hero_range)
continue;
//碰撞框检测
if(atkRect.intersectsRect(montor_def))
{
XCollisionMsg * msg = new XCollisionMsg();
msg->fromID = m_pHero->GetID();
msg->toID = pObj->GetID();
msg->flashID = m_pHero->GetSkillId();
msg->frameID = m_pHero->GetRoleAction()->GetCurFrameId();
m_pMsgDispatcher->DispacherMessage(SEND_MSG_IMMEDIATELY,m_pHero,pObj,MSG_HERO_ATK,msg);
isHeroAttack = true;
}
}
}
}
}
//敌人-->主角
if(!m_pHero->IsGod() && hero_action_id < ACTION_LIEDOWN && !defRect.equals(CCRectZero))
{
//主角死亡就不再检测了
//if(!m_pHero->IsDeath())
{
//击飞过程中不再攻击主角
if(hero_action_id != ACTION_LIEDOWN && hero_action_id != ACTION_STAND_UP)
{
CCObject* pChild = NULL;
CCARRAY_FOREACH(m_pMonsterArray,pChild)
{
XMonstor* pObj = (XMonstor*)pChild;
if(!pObj->IsActive())
continue;
if(pObj->IsDeath())
continue;
int action_id = pObj->GetSkillId();
if(action_id < ACTION_ATTACK)
continue;
//攻击框为0
CCRect montor_atk = pObj->GetAttackRect();
if(montor_atk.size.equals(CCSizeZero))
continue;
//Y轴超出范围
CCPoint monstor_pos = pObj->GetPositionAbs();
if(abs(hero_pos.y - monstor_pos.y) > pObj->GetRangeY())
continue;
//碰撞框检测
if(defRect.intersectsRect(montor_atk))
{
XCollisionMsg * msg = new XCollisionMsg();
msg->fromID = pObj->GetID();
msg->toID = m_pHero->GetID();
msg->flashID = pObj->GetRoleAction()->GetCurFlashId();
msg->frameID = pObj->GetRoleAction()->GetCurFrameId();
m_pMsgDispatcher->DispacherMessage(SEND_MSG_IMMEDIATELY,pObj,m_pHero,MSG_MONSTOR_ATK,msg);
}
}
}
}
}
//通知 连击
if(isHeroAttack)
{
XLuaHelper::ExecuteScriptEvent(m_nScriptComboHandler);
}
}
}
这段代码阅读起来很让人受伤:
1,一个函数有123行
2,if语句嵌套了6层
3,if语句中的条件判断语句太长
4,一个函数处理了两种逻辑:主角攻击敌人;敌人攻击主角;
5,重复代码
6,变量声明定义的地方和使用的地方间隔太长:defRect的定义和使用间隔了58行;
第1点和第4点在这里可以说是同一个问题:可以通过拆分子函数来解决,将代码中的处理拆分为两个函数:
.1,主角攻击:HeroAttack();
.2,敌人攻击:MonstorAttack();
这样CheckCollsion可以修改为:
void XEntityManager::CheckCollision()
{
HeroAttack();
MonstorAttack();
}
再将对应的处理分别放到HeroAttack、MonstorAttack中。
第2点的重构,可以将判断成立的条件修改为不成立的条件,在监测到条件不成立时即刻中断执行,跳出函数。如下,修改if(m_pHero):
void XEntityManager::CheckCollision()
{
if(!m_pHero){
return;
}
HeroAttack();
MonstorAttack();
}
这样 HeroAttack和MonstorAttack中就少了一层if判断。
第3、5点可以用相同的方法来处理:封装函数,如:
if(hero_action_id == ACTION_ATTACK || (hero_action_id >= ACTION_SKILL_1 && hero_action_id <= ACTION_SKILL_6))
语句可以封装一个函数来判断当前的动作是否是处于攻击动作id范围内:
bool IsAttackAction(int nActionId)
{
return nActionId==ACTION_ATTACK || (nActionId>=ACTION_SKILL_1 && nActionId<=ACTION_SKILL_6);
}
然后这个判断条件就可以这么写了:
if (!IsAttackAction(hero_action_id)) {
return;
}
冗余代码:是否在攻击范围、碰撞框的监测等。其中碰撞框的监测只有msg->flashID和DispacherMessage函数中的取值方式不一样,其他的都一致。这样的话可以统一hero、monstor对msg->flashID的取值方式(他们两个是同一个基类),DispacherMessage需要的参数可以通过函数接口传递,修改为如下函数(其中获取属性框的方法和原来的不一样了):
/*
攻击监测
attacker : 发起攻击者
defender : 被攻击者
attackAttriRc : 攻击属性框
defenceAttriRc : 防御属性框
msgType : 向lua传递的消息
*/
bool XEntityManager::AttackChecker(XRoleBase *attacker, XRoleBase *defender, CCArray &attackAttriRc, CCArray &defenceAttriRc, MSG_TYPE msgType)
{
if (!attacker || !defender) {
CC_ASSERT(false && "Got an bad PTR!!!");
return false;
}
if(attackAttriRc.count()<=0 || defenceAttriRc.count()<=0){
return false;
}
bool isHitOnTarget = false;
CCObject *pAttackObj = NULL;
CCARRAY_FOREACH(&attackAttriRc, pAttackObj)
{
XAttriRect *pAttackRcAttri = static_cast<XAttriRect*>(pAttackObj);
//依次和防御框监测是否碰撞了
CCObject *pBodyObj = NULL;
CCARRAY_FOREACH(&defenceAttriRc, pBodyObj)
{
XAttriRect *pBodyAttriRc = static_cast<XAttriRect*>(pBodyObj);
if (!pAttackRcAttri->IsInsertRect(pBodyAttriRc)) {
continue;
}
//碰撞了
isHitOnTarget = true;
XCollisionMsg * msg = new XCollisionMsg();
msg->fromID = attacker->GetID();
msg->toID = defender->GetID();
msg->flashID = attacker->GetSkillId();
msg->frameID = attacker->GetRoleAction()->GetCurFrameId();
m_pMsgDispatcher->DispacherMessage(SEND_MSG_IMMEDIATELY,attacker,defender,msgType,msg);
}
}
return isHitOnTarget;
}
修改后的heroAttack函数代码如下:
void XEntityManager::HeroAttack()
{
int hero_action_id = m_pHero->GetSkillId();
if (!IsAttackAction(hero_action_id)) {
return;
}
//主角-->敌人
//攻击框
CCArray heroAttackRc;
m_pHero->GetAttackAttriRc(heroAttackRc);
if (heroAttackRc.count() <= 0) {
return;
}
bool isHitOnMonstor = false;
CCObject* pChild = NULL;
CCARRAY_FOREACH(m_pMonsterArray,pChild)
{
XMonstor* pObj = (XMonstor*)pChild;
if(!pObj->CanBeAttack()){
continue;
}
if(!m_pHero->IsInAttackRange(pObj)){
//pobj不在hero的攻击范围
continue;
}
CCArray monstorBeAttackRc;
pObj->GetBeAttackAttriRc(monstorBeAttackRc);
if (monstorBeAttackRc.count() <= 0) {
continue;
}
isHitOnMonstor = AttackChecker(m_pHero, pObj, heroAttackRc, monstorBeAttackRc, MSG_HERO_ATK);
}
//通知 连击
if(isHitOnMonstor)
{
XLuaHelper::ExecuteScriptEvent(m_nScriptComboHandler);
}
}
monstorAttack函数如下:
//敌人攻击
void XEntityManager::MonstorAttack()
{
if (!m_pHero->CanBeAttack()) {
return;
}
//英雄得防御框
CCArray heroBeAttackAttriRc;
m_pHero->GetBeAttackAttriRc(heroBeAttackAttriRc);
if (heroBeAttackAttriRc.count() <= 0) {
return;
}
//敌人-->主角
CCObject* pChild = NULL;
CCARRAY_FOREACH(m_pMonsterArray,pChild)
{
XMonstor* pObj = (XMonstor*)pChild;
if(!pObj->CanAttack()){
continue;
}
if (!IsAttackAction(pObj->GetSkillId())) {
continue;
}
if(!pObj->IsInAttackRange(m_pHero)){
//m_pHero不在pobj的攻击范围
continue;
}
CCArray monstorAttackAttriRc;
pObj->GetAttackAttriRc(monstorAttackAttriRc);
if (monstorAttackAttriRc.count() <= 0) {
continue;
}
//攻击监测
AttackChecker(pObj, m_pHero, monstorAttackAttriRc, heroBeAttackAttriRc, MSG_MONSTOR_ATK);
}
}
在拆分、封装函数的过程中把变量的定义放到距使用这个变量最近的地方。
本次重构主要使用的方法总结如下:
1,if语句判断不成立的条件,满足后,返回 --- 减少if的嵌套;
2,长判断语句封装为函数 ---- 增强阅读性
3,公用代码封装为函数 --- 增强阅读性、减少代码行数
4,就近声明、定义变量 ---增强阅读性、提升代码效率