コード例 #1
0
    private void NpcTest()
    {
        _npcController.Npcs[0].transform.position = new Vector3(5f, 1.5f, 1f);

        Debug.Log(_npcController.Npcs[0].GridPosition);
        _npcController.Npcs[0].Path = InfluenceAStar.GetPath(
            _npcController.Npcs[0].GridPosition, new Vector2Int(5, 20), _drawMaps[0], DrawPos[0]).ToList();

        _npcController.Npcs[0].TargetLocation = _npcController.Npcs[0].transform.position;
    }
コード例 #2
0
ファイル: NpcSystem.cs プロジェクト: LarssonSv/InfluenceMaps
    public void OnUpdate()
    {
        IM.ResetMap(MapType.Npc);

        foreach (Npc npc in Npcs.Values)
        {
            IM.CalculateMap(); //this should be optimezed to only multiple updated npc map pos with the same grid from start of frame
            Vector2Int temp = npc.GridPosition;

            if (npc.Path.Count < 1 && npc.GridPosition != npc.Goal)
            {
                npc.Path = InfluenceAStar.GetPath(npc.GridPosition, npc.Goal).ToList();
            }
            else
            {
                npc.Path = InfluenceAStar.RecalculatePath(npc.GridPosition, npc.Path);
            }

            if (npc.Path.Count > 4)
            {
                for (int i = 0; i < 4; i++)
                {
                    IM.DrawOnMap(MapType.Npc, npc.Path[i].x, npc.Path[i].y, 0f);
                }
            }

            if (Vector3.Distance(npc.transform.position, npc.TargetLocation) < 0.1f)
            {
                if (npc.Path.Count > 0)
                {
                    npc.Path.RemoveAt(0);
                }

                if (npc.Path.Count < 1)
                {
                    continue;
                }

                //npc.TargetLocation = InfluenceMapper.MP.DrawPos[0][npc.Path[0].x][npc.Path[0].y];
                npc.TargetLocation = IM.GridToWorld(npc.Path[0]);
            }

            npc.transform.position =
                Vector3.Lerp(npc.transform.position, npc.TargetLocation, npc.Speed * Time.deltaTime);

            Debug.DrawLine(npc.transform.position, npc.TargetLocation, Color.magenta);
        }
    }