0

以下是我正在使用的头文件和 cpp 文件

标题

#include "2d/Vector2D.h"
#include <list>
#include "../../AbstTargetingSystem.h"


class AbstRaven_Bot;




class Fletcher_getClosestBotStrategy
{

public:
    Fletcher_getClosestBotStrategy()
    {}
    Fletcher_getClosestBotStrategy(AbstRaven_Bot* owner);

    //each time this method is called the opponents in the owner's sensory 
    //memory are examined and the closest  is assigned to m_pCurrentTarget.
    //if there are no opponents that have had their memory records updated
    //within the memory span of the owner then the current target is set
    //to null

    void       pickTarget();
};
class Fletcher_TargetingSystem : public AbstTargetingSystem
{

public:
    Fletcher_TargetingSystem(AbstRaven_Bot* owner);

    //each time this method is called the opponents in the owner's sensory 
    //memory are examined and the closest  is assigned to m_pCurrentTarget.
    //if there are no opponents that have had their memory records updated
    //within the memory span of the owner then the current target is set
    //to null
    void       Update();
    void         closestBotStrategy();

    void      setCurrentTarget(AbstRaven_Bot* t);
    AbstRaven_Bot* getCurrentTarget();

    void      setCurrentOwner(AbstRaven_Bot* t);
    AbstRaven_Bot* getCurrentOwner();
};

#endif

cpp

#include "Fletcher_TargetingSystem.h"
#include "../../AbstRaven_Bot.h"
#include "../../Raven_SensoryMemory.h"
#include "../../Debug/DebugConsole.h"


//-------------------------------- ctor ---------------------------------------
//-----------------------------------------------------------------------------
Fletcher_TargetingSystem::Fletcher_TargetingSystem(AbstRaven_Bot* owner):
AbstTargetingSystem(owner){

}

//-------------------------------- ctor ---------------------------------------
//-----------------------------------------------------------------------------
Fletcher_getClosestBotStrategy::Fletcher_getClosestBotStrategy(AbstRaven_Bot* owner){

}

//----------------------------- Update ----------------------------------------

//-----------------------------------------------------------------------------

//#define script AbstTargetingSystem::Instance()
//class AbstTargetingSystem;
//----------------------------- closestPlayer ----------------------------------------

//-----------------------------------------------------------------------------
void Fletcher_getClosestBotStrategy::pickTarget()
{

    double ClosestDistSoFar = MaxDouble;


    for (curBot; curBot != SensedBots.end(); ++curBot)
    {
        //make sure the bot is alive and that it is not the owner
        if ((*curBot)->isAlive() && (*curBot != getCurrentOwner()) )
        {
            double dist = Vec2DDistanceSq((*curBot)->Pos(), getCurrentOwner()->Pos());
            if (dist < ClosestDistSoFar)
            {
                ClosestDistSoFar = dist;
                setCurrentTarget(*curBot);// = *curBot;
            }

        }
    }

}
void Fletcher_TargetingSystem::Update()
{
    // currentStrategy = targetClosestBotStrategy;
    // target = currentStrategy.pickTarget();
    std::list<AbstRaven_Bot*> SensedBots;
    SensedBots = getCurrentOwner()->GetSensoryMem()->GetListOfRecentlySensedOpponents();

    std::list<AbstRaven_Bot*>::const_iterator curBot = SensedBots.begin();
    setCurrentTarget(0);//       = 0;

    Fletcher_getClosestBotStrategy* fGCBS = new Fletcher_getClosestBotStrategy(this->getCurrentOwner());
    fGCBS->pickTarget();
}

AbstRaven_Bot* Fletcher_TargetingSystem::getCurrentOwner(){
    return m_pOwner;
}
AbstRaven_Bot* Fletcher_TargetingSystem::getCurrentTarget(){
    return m_pCurrentTarget;
}

void Fletcher_TargetingSystem::setCurrentTarget(AbstRaven_Bot* t){
    m_pCurrentTarget = t;
}
void Fletcher_TargetingSystem::setCurrentOwner(AbstRaven_Bot* t){
    m_pOwner = m_pOwner;
}

void Fletcher_getClosestBotStrategy::pickTarget()我试图访问更新中的 curBot 和 SensedBot 方法void Fletcher_TargetingSystem::Update()。我也希望能够访问void Fletcher_TargetingSystem::setCurrentOwner(AbstRaven_Bot* t)和其他 3 类似于 dat

任何帮助表示赞赏。

4

1 回答 1

1

从您的方法Fletcher_getClosestBotStrategy::pickTarget()中,您无法访问Fletcher_TargetingSystem::Update()变量curBotSensedBot因为它们是局部变量。

因此,如果您确实需要访问它们,请将它们转换为Fletcher_TargetingSystem类属性,创建 setter 和 getter,并尝试避免friendclases,重新定义您的类依赖项:将Fletcher_TargetingSystemconst 引用传递给您的Fletcher_getClosestBotStrategy方法:

例如:

class Fletcher_getClosestBotStrategy
{
    void pickTarget(const Fletcher_TargetingSystem & targeting);
    ....
};

....

class Fletcher_TargetingSystem 
{
    protected:
        std::list<AbstRaven_Bot*> SensedBots;
    public:
        std::list<AbstRaven_Bot*> & getSensedBots() const { return SensedBots; }
};

void Fletcher_getClosestBotStrategy::pickTarget(const Fletcher_TargetingSystem & targeting)
{
    double ClosestDistSoFar = MaxDouble;
    std::list<AbstRaven_Bot*> & myLocalSensedBots = targeting.getSensedBots();
    ...
}
于 2013-04-11T12:59:50.660 回答