Esempio n. 1
0
    public void Attack(BasicEntity entity)
    {
        VoxelBlocks map  = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> ();
        Vector3     ePos = entity.GetComponent <BlockInfoComponent> ().m_logicPosition;

        Vector3 tPos = target.GetComponent <BlockInfoComponent> ().m_logicPosition;

        if ((Mathf.Abs(ePos.x - tPos.x) <= 1.5f) && (Mathf.Abs(ePos.y - tPos.y) <= 1.5f))
        {
            AiToInput.Attack(entity, tPos);
        }
        else
        {
            target.GetComponent <BlockInfoComponent> ().m_blockType = BlockType.None;
            List <Vector3> path = FindPath.GetPath(ePos, tPos);
            target.GetComponent <BlockInfoComponent> ().m_blockType = BlockType.Player;
            if (path != null)
            {
                path.Remove(tPos);
                tPos = path [path.Count - 1];
                AiToInput.Move(entity, tPos);
            }
            else
            {
                Debug.Log("Fail to Attack");
            }
        }
    }
Esempio n. 2
0
 public override void Execute(List <BasicEntity> entities)
 {
     foreach (var entity in entities)
     {
         if (UICanvas == null)
         {
             UICanvas = new GameObject("UISystem");
             Canvas canvas = UICanvas.AddComponent <Canvas>();
             canvas.renderMode = RenderMode.WorldSpace;
         }
         GameObject  slider = entity.GetComponent <UIComponent>().m_hpSlider;
         VoxelBlocks map    = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks>();
         if (slider == null)
         {
             if (AddHpUI(entity) == false)
             {
                 return;
             }
             slider = entity.GetComponent <UIComponent>().m_hpSlider;
         }
         slider.GetComponentInChildren <Slider>().value = entity.GetComponent <DeadComponent>().hp;
         if (slider.GetComponentInChildren <Slider>().value <= 0)
         {
             GameObject.Destroy(slider);
         }
         slider.transform.position = entity.gameObject.transform.position + new Vector3(0, map.GetComponent <VoxelMap>().GetBlockSize().y *1.2f, 0);
         slider.transform.rotation = Camera.main.transform.rotation;
     }
 }
    //每帧都会调用对应的实体
    public override void Execute(List <BasicEntity> entities)
    {
        foreach (BasicEntity e in entities)
        {
            AttackComponent  attack = e.GetComponent <AttackComponent> ();
            VoxelBlocks      map    = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> ();
            AbilityComponent ab     = e.GetComponent <AbilityComponent> ();
            InputComponent   input  = e.GetComponent <InputComponent> ();
            StateComponent   ap     = e.GetComponent <StateComponent> ();

            int i = AiToInput.GetAbilityCount(e, M_LinkedType);
            //检测是否按下攻击键

            if (i >= ab.m_temporaryAbility.Count || i != input.currentKey)
            {
                continue;
            }

            //若无攻击对象则获取周围可攻击对象
            if (attack.enemy == null)
            {
                attack.enemy = GetEnemyAround(e);
                if (attack.enemy == null)
                {
                    return;
                }
            }
            List <Vector3> el = new List <Vector3> ();
            foreach (var enemy in attack.enemy)
            {
                el.Add(enemy.GetComponent <BlockInfoComponent> ().m_logicPosition);
            }
            UISimple ui = GameObject.Find("UI").GetComponent <UISimple> ();
            ui.ShowUI(el, 2);

            //左键攻击
            if (input.leftButtonDown)
            {
                BasicEntity enemy = map.GetBlockByLogicPos(input.currentPos).entity;

                //检测当前选中敌人是否处于攻击范围内
                List <BasicEntity> list = GetEnemyAround(e);
                if (list != null && !list.Contains(enemy))
                {
                    attack.enemy = list;
                    return;
                }
                //扣除敌人HP值
                DeadComponent  dead  = enemy.GetComponent <DeadComponent> ();
                StateComponent state = e.GetComponent <StateComponent> ();
                dead.hp             -= attack.STR;
                state.m_actionPoint -= 1;
                state.Invoke("AnimationEnd", 1);
                state.AnimationStart();
                //播放攻击动画
                //播放敌人受击动画
                //减少AP
            }
        }
    }
    List <BasicEntity> GetEnemyAround(BasicEntity entity)
    {
        VoxelBlocks        map  = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> ();
        List <BlockInfo>   list = map.GetNeibor(entity.GetComponent <BlockInfoComponent> ().m_logicPosition);
        List <BasicEntity> lb   = new List <BasicEntity> ();


        foreach (var b in list)
        {
            if (b.entity == null)
            {
                continue;
            }
            BlockType blockType = b.entity.GetComponent <BlockInfoComponent>().m_blockType;
            if (blockType != BlockType.Enemy && blockType != BlockType.Player)
            {
                continue;
            }
            if (b.entity != null && blockType != entity.GetComponent <BlockInfoComponent> ().m_blockType)
            {
                lb.Add(b.entity);
            }
        }
        if (lb.Count == 0)
        {
            return(null);
        }
        return(lb);
    }
Esempio n. 5
0
    //获取从s到e的路径,返回路径长度小于等于step
    static public Vector3 GetPathByStep(Vector3 s, Vector3 e, int step)
    {
        VoxelBlocks    map  = GameObject.Find("Voxel Map").GetComponentInChildren <VoxelBlocks>();
        List <Vector3> path = null;

        BlockInfo block = map.GetBlockByLogicPos(e);

        if (block != null && block.entity != null)
        {
            BlockType blockType = block.entity.GetComponent <BlockInfoComponent>().m_blockType;
            block.entity.GetComponent <BlockInfoComponent>().m_blockType = BlockType.None;
            path = GetPath(s, e);
            block.entity.GetComponent <BlockInfoComponent>().m_blockType = blockType;
            path.Remove(path[path.Count - 1]);
        }
        else
        {
            path = GetPath(s, e);
        }
        if (path != null && path.Count > step)
        {
            path.RemoveRange(step, path.Count - step);
        }

        if (path != null && path.Count > 0)
        {
            return(path[path.Count - 1]);
        }
        return(Vector3.down);
    }
Esempio n. 6
0
    public void ShowWornArea(BasicEntity entity)
    {
        List <Vector3> list = entity.GetComponent <MonitorComponent>().m_view;

        string      name = "Prefabs/UI/Simple/red";
        VoxelBlocks map  = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();

        List <GameObject> childList = new List <GameObject>();
        int childCount = entity.transform.childCount;

        for (int i = 0; i < childCount; i++)
        {
            GameObject child = entity.transform.GetChild(i).gameObject;
            childList.Add(child);
        }
        for (int i = 0; i < childCount; i++)
        {
            DestroyImmediate(childList[i]);
        }

        if (list == null)
        {
            return;
        }
        foreach (var pos in list)
        {
            GameObject obj = (GameObject)Resources.Load(name);
            obj = Instantiate(obj);
            obj.transform.parent   = entity.transform;
            obj.transform.position = map.LogToWorld(pos);
        }
    }
Esempio n. 7
0
    public void ShowUI(List <Vector3> list, int Type)
    {
        Type--;
        Transform   t    = transform.GetChild(Type);
        string      name = "Prefabs/UI/Simple/" + t.name;
        VoxelBlocks map  = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();

        List <GameObject> childList = new List <GameObject>();
        int childCount = t.transform.childCount;

        for (int i = 0; i < childCount; i++)
        {
            GameObject child = t.transform.GetChild(i).gameObject;
            childList.Add(child);
        }
        for (int i = 0; i < childCount; i++)
        {
            DestroyImmediate(childList[i]);
        }

        if (list == null)
        {
            return;
        }
        foreach (var pos in list)
        {
            GameObject obj = (GameObject)Resources.Load(name);
            obj = Instantiate(obj);
            obj.transform.parent   = t;
            obj.transform.position = map.LogToWorld(pos);
        }
    }
Esempio n. 8
0
    static private bool checkWall(BlockInfo blockInfo)
    {
        VoxelBlocks voxelBlocks = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();
        //获取下方方块的逻辑位置
        Vector3 logPos = blockInfo.logPos;

        logPos.z--;
        //若该位置没有东西就判断其下方的方块
        if (blockInfo.entity == null)
        {
            BasicEntity entity = voxelBlocks.GetBlockByLogicPos(logPos).entity;

            if (entity == null)
            {
                Debug.Log(logPos);
                return(true);
            }
            if (entity.GetComponent <BlockInfoComponent>().m_blockType != BlockType.Floor)
            {
                return(true);
            }
            return(false);
        }

        if (blockInfo.entity.GetComponent <BlockInfoComponent>().m_blockType != BlockType.None)
        {
            return(true);
        }
        return(false);
    }
Esempio n. 9
0
    static public List <Vector3> GetArea(Vector3 pos, int ridus)
    {
        List <Vector3> area = new List <Vector3>();
        VoxelBlocks    map  = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();
        Vector3        nPos = pos;

        for (int i = -ridus; i < ridus; i++)
        {
            nPos.x = pos.x + i;
            for (int j = -ridus; j < ridus; j++)
            {
                nPos.y = pos.y + j;
                if ((nPos - pos).magnitude >= ridus)
                {
                    continue;
                }
                if (map.GetBlockByLogicPos(nPos) != null)
                {
                    area.Add(nPos);
                }
            }
        }

        return(area);
    }
Esempio n. 10
0
    public void Escape(BasicEnemy enemy)
    {
        BasicEntity entity = enemy.m_entity;
        VoxelBlocks map    = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks> ();

        List <BasicEntity> enemyList = entity.GetComponent <MonitorComponent> ().m_enemy;

        if (AiToInput.CallFriend(entity, enemyList))
        {
            //呼叫成功
            StateComponent state = entity.GetComponent <StateComponent> ();

            state.AnimationStart();

            state.m_actionPoint -= 1;
            state.Invoke("AnimationEnd", 1);
            return;
        }

        Vector3 escapePos = FindPath.GetNearestFriend(entity.GetComponent <BlockInfoComponent> ().m_logicPosition);

        if (escapePos == Vector3.down)
        {
            //无路可走等死
            return;
        }
        else
        {
            AiToInput.Move(entity, escapePos);
        }
        return;
    }
Esempio n. 11
0
    private void SetPath(MoveComponent move)
    {
        float       time = 0;
        VoxelBlocks map  = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> ();
        float       oa   = move.transform.rotation.eulerAngles.y * 3.1415f / 180;

        for (int i = 1; i < move.pathList.Count; i++)
        {
            Vector3 pos;
            pos = map.LogToWorld(move.pathList [i]) - map.LogToWorld(move.pathList [i - 1]);
            int     n    = 1;
            Vector3 cPos = pos;
            while (i < move.pathList.Count - 1)
            {
                if (cPos != (map.LogToWorld(move.pathList[i + 1]) - map.LogToWorld(move.pathList[i])))
                {
                    break;
                }
                pos += cPos;
                i++;
                n++;
            }
            ;

            Hashtable args  = new Hashtable();
            float     angle = GetAngle(pos);
            if (Mathf.Abs(angle - oa) > 0.1)
            {
                iTween.RotateTo(move.gameObject, iTween.Hash("rotation", new Vector3(0, angle * 180 / 3.1415f, 0), "time", 0.25f, "easeType", "easeInOutBack", "loopType", "none", "delay", time));
                time += 0.25f;
                //获取转向后的前进坐标
                oa = angle;
            }
            pos = RotatePos(pos, -oa);
            args.Add("easeType", iTween.EaseType.linear);
            //移动的整体时间。如果与speed共存那么优先speed
            args.Add("time", move.moveSpeed * n);
            args.Add("loopType", "none");
            args.Add("delay", time);
            time += move.moveSpeed * n;
            // x y z 标示移动的位置。
            args.Add("x", pos.x);
            args.Add("z", pos.z);
            args.Add("onstart", "MoveStart");
            args.Add("onstartparams", move.pathList[i]);
            args.Add("onstarttarget", move.gameObject);

            if (move.pathList.Count - 1 == i)
            {
                int ap = (i - 1) / move.SPD + 1;
                move.GetComponent <StateComponent>().m_actionPoint -= ap;
                move.GetComponent <StateComponent>().Invoke("AnimationEnd", time);
                args.Add("oncomplete", "MoveEnd");

                args.Add("oncompleteparams", move.pathList[i]);
                args.Add("oncompletetarget", move.gameObject);
            }
            iTween.MoveBy(move.gameObject, args);
        }
    }
Esempio n. 12
0
    void Bomb(BasicEntity entity, float mr, float ridus, int demage)
    {
        InputComponent input = entity.GetComponent <InputComponent>();
        VoxelBlocks    map   = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();

        //获取鼠标位置与角色位置的距离
        float r = (input.currentPos - entity.GetComponent <BlockInfoComponent>().m_logicPosition).magnitude;

        //若距离大于预设值则退出
        if (r > mr)
        {
            return;
        }
        //获取炸弹投掷后可能影响的范围
        List <Vector3> ar = FindPath.GetArea(input.currentPos, Mathf.FloorToInt(ridus));
        //显示炸弹影响范围
        List <DeadComponent> mc = new List <DeadComponent>();

        UISimple ui = GameObject.Find("UI").GetComponent <UISimple>();

        ui.ShowUI(ar, 3);

        //获取该范围内所有的敌人的生命脚本
        foreach (var pos in ar)
        {
            BlockInfo block = map.GetBlockByLogicPos(pos);
            if (block != null && block.entity != null)
            {
                if (block.entity.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Enemy)
                {
                    mc.Add(block.entity.GetComponent <DeadComponent>());
                }
            }
        }
        //显示可被影响的敌人

        //若按下鼠标左键则扔出炸弹,影响周围敌人。
        if (input.leftButtonDown)
        {
            Debug.Log(mc.Count);
            foreach (var m in mc)
            {
                if (m != null)
                {
                    m.hp -= demage;
                }
            }
            ItemComponent itemComp = entity.GetComponent <ItemComponent>();
            //移除炸弹
            itemComp.item.Remove(ItemType.Bomb);
            entity.GetComponent <ItemComponent>().current = ItemType.Null;
            //使用一次消耗一点行动点
            StateComponent state = entity.GetComponent <StateComponent>();
            state.m_actionPoint -= 1;
            state.AnimationStart();

            state.Invoke("AnimationEnd", 1);
        }
    }
Esempio n. 13
0
    void RemoveDead(DeadComponent dead)
    {
        GameObject.Destroy(dead.gameObject, 1);
        //播放死亡动画
        VoxelBlocks map = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> ();

        map.RemoveBlock(dead.GetComponent <BlockInfoComponent> ().m_logicPosition);
    }
    public void MoveEnd(Vector3 logPos)

    {
        VoxelBlocks map       = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> ();
        AudioDemo   audioDemo = GameObject.Find("Audio").transform.GetComponent <AudioDemo>();

        audioDemo.pauseVoice(AudioType.Walk);
        map.ChangePos(GetComponent <BlockInfoComponent> ().m_logicPosition, logPos);
    }
Esempio n. 15
0
    public override void Execute(List <BasicEntity> entities)
    {
        foreach (var entity in entities)
        {
            KnockComponent   knock = entity.gameObject.GetComponent <KnockComponent>();
            AbilityComponent ab    = entity.GetComponent <AbilityComponent>();
            InputComponent   input = entity.GetComponent <InputComponent>();
            StateComponent   ap    = entity.GetComponent <StateComponent>();

            int i = AiToInput.GetAbilityCount(entity, M_LinkedType);
            if (i >= ab.m_temporaryAbility.Count || i != input.currentKey)
            {
                knock.m_area = null;
                continue;
            }
            //获取影响范围
            if (knock.m_area == null)
            {
                knock.m_area = FindPath.GetArea(knock.GetComponent <BlockInfoComponent>().m_logicPosition, knock.m_ridus);
            }
            UISimple ui = GameObject.Find("UI").GetComponent <UISimple>();
            ui.ShowUI(knock.m_area, 3);
            VoxelBlocks        map   = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();
            List <BasicEntity> enemy = new List <BasicEntity>();


            //获取影响范围内的敌人
            foreach (var pos in knock.m_area)
            {
                var e = map.GetBlockByLogicPos(pos).entity;
                if (e != null)
                {
                    if (e.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Enemy)
                    {
                        enemy.Add(e);
                    }
                }
            }
            //UI显示范围与敌人
            if (input.leftButtonDown)
            {
                foreach (var e in enemy)
                {
                    Debug.Log(e.name);
                    e.GetComponent <MonitorComponent>().m_voice.Add(entity.GetComponent <BlockInfoComponent>().m_logicPosition);
                }
                ui.ShowUI(null, 3);
                StateComponent state = entity.GetComponent <StateComponent>();
                state.m_actionPoint -= 1;
                state.AnimationStart();

                state.Invoke("AnimationEnd", 1);
            }
        }
    }
Esempio n. 16
0
    static List <Vector3> generatePath(NodeItem start)
    {
        List <Vector3> list        = new List <Vector3>();
        VoxelBlocks    voxelBlocks = GameObject.Find("Voxel Map").GetComponentInChildren <VoxelBlocks>();

        while (start.parent != null)
        {
            list.Add(start.m_logPos);
            start = start.parent;
        }
        list.Reverse();
        return(list);
    }
Esempio n. 17
0
    bool GetItem(BasicEntity entity, Vector3 mousePos)
    {
        VoxelBlocks map = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks>();
        //获取角色位置
        Vector3 pos = entity.GetComponent <BlockInfoComponent>().m_logicPosition;

        //判断鼠标位置是否在角色旁边
        if ((Mathf.Abs(pos.x - mousePos.x) <= 1.5f) && (Mathf.Abs(pos.y - mousePos.y) <= 1.5f))
        {
            //获取鼠标位置上的格子信息
            BlockInfo blockInfo = map.GetBlockByLogicPos(mousePos);
            if (blockInfo == null)
            {
                return(false);
            }
            BasicEntity itemEntity = blockInfo.entity;

            if (itemEntity != null)
            {
                ItemInfoComponent itemInfo = itemEntity.GetComponent <ItemInfoComponent>();
                //若该格子上不存在物品则退出
                if (itemInfo == null)
                {
                    return(false);
                }
                ItemComponent itemComp = entity.GetComponent <ItemComponent>();
                //判断物品是否超出持有上限
                if (itemInfo.num + itemComp.item.Count > itemComp.numLimit)
                {
                    //删除物品
                    itemComp.item.RemoveRange(0, itemInfo.num + itemComp.item.Count - itemComp.numLimit);
                }
                //添加物品
                for (int i = 0; i < itemInfo.num; i++)
                {
                    itemComp.item.Add(itemInfo.type);
                }
                map.RemoveBlock(mousePos);
                itemInfo.m_entity.RemoveAllComponent();
                GameObject.Destroy(itemInfo.gameObject);
            }
            return(true);
        }
        else
        {
            return(false);
        }
    }
Esempio n. 18
0
    static private NodeItem BItoNI(Vector3 pos)
    {
        VoxelBlocks voxelBlocks = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();
        NodeItem    node;
        BlockInfo   block = voxelBlocks.GetBlockByLogicPos(pos);

        if (block == null)
        {
            node = new NodeItem(false, pos);
        }
        else
        {
            node = new NodeItem(checkWall(block), block.logPos);
        }
        return(node);
    }
Esempio n. 19
0
    static public List <Vector3> FindPathInStep(Vector3 s, int n)
    {
        VoxelBlocks    voxelBlocks = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();
        List <Vector3> openSet     = new List <Vector3>();
        List <Vector3> nextSet     = new List <Vector3>();
        List <Vector3> closeSet    = new List <Vector3>();

        openSet.Add(s);
        closeSet.Add(openSet[0]);
        //步数限制
        for (int i = 0; i < n; i++)
        {
            //遍历第i步的所有格子
            for (int j = 0; j < openSet.Count; j++)
            {
                foreach (var blocks in voxelBlocks.GetNeibor(openSet[j]))
                {
                    if (blocks.entity != null || closeSet.Contains(blocks.logPos))
                    {
                        continue;
                    }
                    //将下一步能达到的格子加入到总表
                    closeSet.Add(blocks.logPos);
                    //将下一步能到达的格子单独保存
                    nextSet.Add(blocks.logPos);
                }
            }
            openSet.Clear();

            //取出下一步能达到的格子
            if (nextSet.Count > 0)
            {
                openSet.AddRange(nextSet);
            }
            nextSet.Clear();
        }
        if (closeSet.Count != 0)
        {
            closeSet.Remove(s);
            return(closeSet);
        }
        return(null);
    }
Esempio n. 20
0
    private Vector3 FindCover(Vector3 pos)
    {
        VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();


        List <BlockInfo> list = map.GetNeibor(pos);

        if (list == null)
        {
            return(Vector3.down);
        }
        foreach (var e in list)
        {
            if (e.entity != null && e.entity.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Wall)
            {
                return(e.logPos);
            }
        }

        return(Vector3.down);
    }
    //当这个物体变成可用时,进行绘制前的初始化
    void OnEnable()
    {
        //将选中的实例作为目标类型传递给变量
        voxelMap = target as VoxelMap;

        //如果没有用于保存所有准备绘制方块所需的父对象,则新建一个
        if (voxelMap.voxelLowerBlocks == null)
        {
            var go = new GameObject("voxelLowerBlocks");
            go.transform.SetParent(voxelMap.transform);
            go.transform.position = Vector3.zero;

            //在脚本中保存所有的方块数据
            VoxelBlocks voxelBlocks = go.AddComponent <VoxelBlocks> ();
            voxelBlocks.MapSize       = voxelMap.mapSize;
            voxelBlocks.BlockSize     = voxelMap.GetBlockSize();
            voxelMap.voxelLowerBlocks = go;
        }

        if (voxelMap.voxelUpperBlocks == null)
        {
            var go = new GameObject("voxelUpperBlocks");
            go.transform.SetParent(voxelMap.transform);
            go.transform.position = Vector3.zero;

            VoxelBlocks voxelBlocks = go.AddComponent <VoxelBlocks> ();
            voxelBlocks.MapSize       = voxelMap.mapSize;
            voxelBlocks.BlockSize     = voxelMap.GetBlockSize();
            voxelMap.voxelUpperBlocks = go;
        }

        //重新计算gizmo中方块的大小,并且新建一个笔刷
        if (voxelMap.basicBlock != null)
        {
            UpdateCalculations();
            NewBrush();
        }
    }
Esempio n. 22
0
    static public bool CallFriend(BasicEntity entity, List <BasicEntity> player)
    {
        if (player.Count == 0 || entity == null)
        {
            return(false);
        }
        MonitorComponent monitor = entity.GetComponent <MonitorComponent> ();
        List <Vector3>   view    = monitor.m_view;
        VoxelBlocks      map     = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks> ();
        bool             isFind  = false;


        foreach (Vector3 pos in view)
        {
            BasicEntity e = map.GetBlockByLogicPos(pos).entity;
            if (e != null && e.GetComponent <BlockInfoComponent> ().m_blockType == BlockType.Enemy)
            {
                isFind = true;
                e.GetComponent <MonitorComponent> ().m_enemy.AddRange(player);
            }
        }
        return(isFind);
    }
Esempio n. 23
0
    public override void Execute(List <BasicEntity> entities)
    {
        foreach (var e in entities)
        {
            MonitorComponent monitor = e.GetComponent <MonitorComponent>();
            if (monitor == null)
            {
                return;
            }
            //若监视范围为空或者实体进行移动,重置监控范围
            List <BasicEntity> oldEnemy = monitor.m_enemy;
            monitor.m_enemy = new List <BasicEntity>();
            UISimple ui = GameObject.Find("UI").GetComponent <UISimple>();

            VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks>();

            if (monitor.m_view == null || monitor.m_view.Contains(e.GetComponent <BlockInfoComponent>().m_logicPosition))
            {
                List <Vector3> list = FindPath.GetArea(e.GetComponent <BlockInfoComponent>().m_logicPosition, monitor.m_SightArea);
                //获取自身位置
                Vector3 s = e.GetComponent <BlockInfoComponent>().m_logicPosition;
                //初始化视野列表
                monitor.m_view = new List <Vector3>();
                //获取所有能看到的点
                foreach (var pos in list)
                {
                    if (InView(pos, s))
                    {
                        //添加视野点并判断是否为玩家
                        monitor.m_view.Add(pos);
                        BasicEntity entity = map.GetBlockByLogicPos(pos).entity;
                        //判断是否为玩家
                        if (entity != null)
                        {
                            if (entity.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Player && !monitor.m_enemy.Contains(entity))
                            {
                                //添加到敌人列表里
                                monitor.m_enemy.Add(entity);
                            }
                        }
                    }
                }
                //判断视野里是否出现新的敌人
                foreach (BasicEntity enemy in monitor.m_enemy)
                {
                    if (!oldEnemy.Contains(enemy))
                    {
                        monitor.GetComponent <StateComponent>().FindEnemy();
                        break;
                    }
                }
                //ui.ShowWornArea(e);
            }
            else
            {
                //查找所有在视野里的玩家
                foreach (var pos in monitor.m_view)
                {
                    BasicEntity entity = map.GetBlockByLogicPos(pos).entity;
                    //判断是否为玩家
                    if (entity != null)
                    {
                        if (entity.GetComponent <BlockInfoComponent>().m_blockType == BlockType.Player && !monitor.m_enemy.Contains(entity))
                        {
                            //添加到敌人列表里
                            monitor.m_enemy.Add(entity);
                        }
                    }
                }
            }
            foreach (var enemy in oldEnemy)
            {
                if (monitor.m_enemy.Contains(enemy))
                {
                    continue;
                }
                monitor.m_voice.Add(enemy.GetComponent <BlockInfoComponent>().m_logicPosition);
            }
        }
    }
Esempio n. 24
0
    static public List <Vector3> GetPath(Vector3 s, Vector3 e)
    {
        List <NodeItem> openSet     = new List <NodeItem>();
        List <NodeItem> closeSet    = new List <NodeItem>();
        VoxelBlocks     voxelBlocks = GameObject.Find("Voxel Map").GetComponentInChildren <VoxelBlocks>();

        NodeItem startNode = BItoNI(s);
        NodeItem endNode   = BItoNI(e);

        openSet.Add(startNode);
        while (openSet.Count > 0)
        {
            NodeItem curNode = openSet[0];

            //获取openSet内权重最低的点。
            for (int i = 0, max = openSet.Count; i < max; i++)
            {
                if (openSet[i].fCost <= curNode.fCost &&
                    openSet[i].hCost < curNode.hCost)
                {
                    curNode = openSet[i];
                }
            }
            openSet.Remove(curNode);
            closeSet.Add(curNode);

            // 找到的目标节点
            if (curNode.m_logPos == e)
            {
                return(generatePath(curNode));
            }

            // 判断周围节点,选择一个最优的节点
            foreach (var block in voxelBlocks.GetNeibor(curNode.m_logPos))
            {
                NodeItem item = BItoNI(block.logPos);
                // 如果是墙或者已经在关闭列表中

                if (item.isWall || ContainsNode(closeSet, item))
                {
                    continue;
                }
                // 计算当前相领节点现开始节点距离
                int newCost = curNode.gCost + getDistanceNodes(curNode, item);
                if (block.logPos.x != curNode.m_logPos.x && block.logPos.y != curNode.m_logPos.y)
                {
                    newCost += (int)(5 * voxelBlocks.m_blockSize.x);
                }

                // 如果距离更小,或者原来不在开始列表中
                if (newCost < item.gCost || !ContainsNode(openSet, item))
                {
                    if (!ContainsNode(openSet, item))
                    {
                        // 更新与开始节点的距离
                        item.gCost = newCost;
                        // 更新与终点的距离
                        item.hCost = getDistanceNodes(item, endNode);
                        // 更新父节点为当前选定的节点
                        item.parent = curNode;

                        openSet.Add(item);
                    }
                    else
                    {
                        foreach (var ni in openSet)
                        {
                            if (ni.m_logPos == item.m_logPos)
                            {
                                ni.gCost  = newCost;
                                ni.hCost  = getDistanceNodes(item, endNode);
                                ni.parent = curNode;
                            }
                        }
                    }
                }
            }
        }

        return(null);
    }
Esempio n. 25
0
    //检查是否能看到该位置
    private bool InView(Vector3 s, Vector3 e)
    {
        VoxelBlocks map = GameObject.Find("Voxel Map").GetComponent <VoxelBlocks> ();
        Vector3     next = s;
        int         deltCol = (int)(e.x - s.x);
        int         deltRow = (int)(e.y - s.y);
        int         stepCol, stepRow;
        int         fraction;

        //初始化路径数组

        stepCol = (int)Mathf.Sign(deltCol);              //获取x轴上的前进方向
        stepRow = (int)Mathf.Sign(deltRow);              //获取z轴上的前进方向
        deltRow = Mathf.Abs(deltRow * 2);
        deltCol = Mathf.Abs(deltCol * 2);

        if (deltCol > deltRow)
        {
            fraction = deltRow * 2 - deltCol;
            while (next.x != e.x)
            {
                if (fraction >= 0)
                {
                    next.y  += stepRow;
                    fraction = fraction - 2 * deltCol;
                }
                next.x  += stepCol;
                fraction = fraction + deltRow;
                BlockInfo bi = map.GetBlockByLogicPos(next);
                if (bi == null)
                {
                    return(false);
                }
                BasicEntity entity = bi.entity;
                if (entity != null && entity.GetComponent <BlockInfoComponent> ().m_blockType == BlockType.Wall)
                {
                    return(false);
                }
            }
        }
        else
        {
            fraction = deltCol * 2 - deltRow;
            while (next.y != e.y)
            {
                if (fraction >= 0)
                {
                    next.x  += stepCol;
                    fraction = fraction - 2 * deltRow;
                }
                next.y  += stepRow;
                fraction = fraction + deltCol;
                BlockInfo bi = map.GetBlockByLogicPos(next);
                if (bi == null)
                {
                    return(false);
                }
                BasicEntity entity = bi.entity;
                if (entity != null && entity.GetComponent <BlockInfoComponent> ().m_blockType == BlockType.Wall)
                {
                    return(false);
                }
            }
        }
        return(true);
    }
Esempio n. 26
0
    public override void Execute(List <BasicEntity> entities)
    {
        foreach (var entity in entities)
        {
            MoveComponent    move      = entity.gameObject.GetComponent <MoveComponent> ();
            AbilityComponent ab        = entity.GetComponent <AbilityComponent> ();
            InputComponent   input     = entity.GetComponent <InputComponent> ();
            StateComponent   ap        = entity.GetComponent <StateComponent> ();
            BlockType        blockType = entity.GetComponent <BlockInfoComponent>().m_blockType;

            int i = AiToInput.GetAbilityCount(entity, M_LinkedType);
            if (i >= ab.m_temporaryAbility.Count || i != input.currentKey)
            {
                continue;
            }
            UISimple ui = GameObject.Find("UI").GetComponent <UISimple>();

            VoxelBlocks map = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks> ();

            if (move.pathList == null)
            {
                move.pathList = new List <Vector3> ();
                move.pathList.Add(entity.GetComponent <BlockInfoComponent> ().m_logicPosition);
            }
            if (move.path == null)
            {
                move.path = FindPath.FindPathInStep(move.pathList [move.pathList.Count - 1], ap.m_actionPoint * move.SPD - move.pathList.Count + 1);
            }
            List <Vector3> a = move.path;
            if (blockType != BlockType.Enemy)
            {
                ui.ShowUI(a, 3);
            }
            if (input.currentPos != null)
            {
                if (a.Count == 0 && input.leftButtonDown)
                {
                    SetPath(move);
                    move.pathList        = null;
                    move.path            = null;
                    input.leftButtonDown = false;
                    return;
                }
                foreach (var bi in a)
                {
                    //判断鼠标停靠位置是否位于可移动范围之内
                    if (bi == input.currentPos)
                    {
                        List <Vector3> path = new List <Vector3> ();
                        path.AddRange(move.pathList);
                        List <Vector3> newPath = FindPath.GetPath(move.pathList [move.pathList.Count - 1], input.currentPos);
                        if (newPath != null)
                        {
                            path.AddRange(newPath);
                        }
                        //调用UI显示路径
                        if (blockType != BlockType.Enemy)
                        {
                            ui.ShowUI(path, 2);
                        }
                        if (input.midButtonDown)
                        {
                            move.pathList = path;
                            move.path     = null;
                        }

                        if (input.rightButtonDown)
                        {
                            move.pathList.Clear();
                            move.path = null;
                        }

                        if (input.leftButtonDown && ap.m_actionPoint != 0)
                        {
                            move.pathList = path;

                            SetPath(move);
                            move.GetComponent <StateComponent>().AnimationStart();

                            move.pathList        = null;
                            move.path            = null;
                            input.leftButtonDown = false;
                            return;
                        }
                    }
                }
            }
        }
    }
    public void ChangePos(Vector3 logPos)
    {
        VoxelBlocks map = GameObject.Find("Voxel Map").transform.GetComponent <VoxelBlocks>();

        map.ChangePos(GetComponent <BlockInfoComponent>().m_logicPosition, logPos);
    }