MoveManager管理类

本文介绍了一个游戏中的移动管理系统实现方式,通过MoveManager类管理玩家和NPC的移动,并使用临时位置列表来更新附近玩家的位置信息。文章详细展示了如何通过遍历移动对象列表来更新玩家的移动状态,并最终将移动信息发送给相关玩家。
MoveManager:移动管理类

struct MoveOpt
{
    int cur_seq;
    ObjecInfo* obj;
};
std::map<ObjID, MoveOpt> m_move_objs;//主要容器
//加入移动标记
void MoveManager::add_mask(ObjecInfo* obj)
{
    auto& objId = obj->objId;
    MoveOpt mov_opt = {m_move_seq++, obj};
    m_move_objs[objId] = mov_opt;

}

主要函数
//每个玩家都有std::vector<ObjTgtPos> tmpPos;临时位置
//用于保存附近玩家的移动信息,所有当一个玩家或者NPC移动时addMask()这样其就加入到m_move_objs
void MoveManager::run()
{
    time_t tb = GetTickCount();

    if(m_move_objs.empty())
        return ;

    //需要通知的player
    std::map<int, PlayerInfo*> tmpPlayers; 
    for(auto it = m_move_objs.begin(); it != m_move_objs.end(); ++it)//遍历移动玩家列表
    {
        auto objex = (ObjecInfoEx*)it->second.obj;
        ObjTgtPos pos;
        pos.seq = it->second.cur_seq; //顺序变量
        pos.speed = objex->speed;
        pos.objId = objex->objId;
        pos.player.tgtPos = objex->tgtPos;
        pos.player.curPos = objex->curPos;
        pos.mov_face = objex->mov_face;
        pos.dir = objex->dir;

        if(it->first.type == ObjID_Player)
        {
            auto player = (PlayerInfo*)objex;
            auto& near_pls = player->nearby_players; 
            for(auto p : near_pls)
            {
                //将玩家的移动位置和顺序变量加入到其附近玩家的tmpPos
                p.second->tmpPos.push_back(pos);//临时位置,我在想为什么要搞个向量保存,每次只保存一个还要向量?
                tmpPlayers[p.first] = p.second;
            }
        }
        else
        {
            //NPC也是将其位置加入到附近玩家的tmpPos
            auto monster = (I_NPC*)objex;
            auto& near_players = monster->map_nearby_player;
            for(auto p : near_players)
            {
                p.second->tmpPos.push_back(pos);
                tmpPlayers[p.first] = p.second;
            }
        }
    }
    //然后向附近玩家发送位置信息,然后客户端表现就行了
    for(auto it = tmpPlayers.begin(); it != tmpPlayers.end(); it++)
    {    
        it->second->send_mul_pos(it->second->tmpPos);//std::vector<playerTgtPos> tmpPos
        it->second->tmpPos.clear();
    }

    this->clear();//清除本次移动记录将m_move_objs.clear()和m_move_seq这个m_move_seq到底什么用?
    time_t te = GetTickCount() - tb;
}

 玩家或者NPC移动,加入到m_mapMoveObjs中,然后map定时50ms遍历这map,将位置信息加入到附近玩家的临时位置中,最后通知这些玩家,感觉有点蛋疼啊

转载于:https://www.cnblogs.com/zzyoucan/p/3947603.html

internal override void ClearUnProcessed(bool reschedule) { try { MoveManagerClear newstate = new MoveManagerClear(); // lilx need liuzh confirm 2014-7-9 撤销注释 MoveManager.GetIns().SetState(newstate); // lilx need liuzh confirm 2014-7-9 撤销注释 Scheduler.GetInstance().StopSchedule(); TaskManager.GetIns().ClearReadyMoves(); MoveManagerIdle idlestate = new MoveManagerIdle(); MoveManager.GetIns().SetState(idlestate); ActionContainerManager.GetIns().Halt(); if (reschedule) { MyLogger.GetLogData().Debug("ClearUnProcessed 1 matlist.count=" + MoveManager.GetIns().MatList.Count); List<Material> temp = MoveManager.GetIns().GetMatlistCopy(); List<Material> input = MoveManager.GetIns().SortMatList(temp);//不sort的话,顺序是完全乱的 //List<Material> input2 = new List<Material>(); #region 20130729 设置mat的priority放到job开始前,此处只是按下发顺序整理输入 //foreach (Task task in TaskManager.GetIns().TaskList.Values) //{ // if (task.GetType() == typeof(ClusterTask)) // { // ClusterTask ctask = (ClusterTask)task; // foreach (ProcessJob pjob in ctask.PjobList) // { // Dictionary<string, Route> defineRoute = pjob.DefineRoute; // foreach (string slotid in defineRoute.Keys) // { // int slot = int.Parse(slotid); // for (int i = 0; i < temp.Count; i++) // { // Material mat = temp[i]; // int matSlot = ((TransferMaterial)mat).SrcCassetteSlot; // if (((TransferMaterial)mat).SrcCassetteModule == ctask.SrcCassette.Name && matSlot == slot) // { // input2.Add(mat); // break; // } // } // } // } // } //} #endregion List<Material> input2 = MoveManager.GetIns().GetMatListByHostSeq(temp); foreach (Material mat in input) //把不参与job的mat也添加到scheduler,sheduler会为这些mats分配空间。 { if (!input2.Contains(mat)) { input2.Add(mat); } } List<Condition> conds = TaskManager.GetIns().GetExecutingClusterTaskConds(); MyLogger.GetLogData().Debug(" AddCondition in ClearUnProcessed"); foreach (Condition cond in conds) { MyLogger.GetLogData().Debug("Condition: " + cond.Triggered); } Scheduler.GetInstance().AddCondition(conds); MyLogger.GetLogData().Debug(" AddMaterialsForSchedule in ClearUnProcessed"); MoveManager.GetIns().InitStationForSch(input); //abort tool后不进行move计算 if (TaskManager.GetIns().TState != ToolState.Aborting && TaskManager.GetIns().TState != ToolState.Aborted) { Scheduler.GetInstance().AddMaterialsForSchedule(input); Scheduler.GetInstance().StartSchedule(); } } else { MoveManager.GetIns().Notifier.PostEvent((int)Movemanager_events.movemanagerhalted, new BaseEventArgs(null)); } MyLogger.GetLogData().Debug("ClearUnProcessed 2 matlist.count=" + MoveManager.GetIns().MatList.Count); } catch (Exception ex) { MyLogger.GetLogIns().Error("MoveManagerIdle-->ClearUnProcessed exception:" + ex.ToString()); } } 总结一下这个方法
08-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值