public SerializedBattleGridPoint(NavmeshLayserSerializer ns, BattleGridPoint point)
 {
     position    = point.positionV3;
     gridX       = point.gridX;
     gridZ       = point.gridZ;
     id          = ns.GetBattleGridID(point);
     passability = (int)point.passability;
     neighbours  = new int[4];
     for (int i = 0; i < 4; i++)
     {
         neighbours[i] = point.neighbours[i] != null?ns.GetBattleGridID(point.neighbours[i]) : -1;
     }
 }
        //battle grid
        public void DeserializeBattleGridPoints()
        {
            BattleGridPoint[]      pool   = deserializer.bgPool;
            List <BattleGridPoint> points = new List <BattleGridPoint>();

            foreach (var p in serializedGraph.battleGrid.points)
            {
                BattleGridPoint newP = new BattleGridPoint(p.position, (Passability)p.passability, new VectorInt.Vector2Int(p.gridX, p.gridZ));
                pool[p.id] = newP;
                points.Add(newP);
            }

            targetGraph.battleGrid = new BattleGrid(serializedGraph.battleGrid.lengthX, serializedGraph.battleGrid.lengthZ, points);
        }
        public void ConnectBattleGridPoints()
        {
            BattleGridPoint[] pool = deserializer.bgPool;
            foreach (var p in serializedGraph.battleGrid.points)
            {
                BattleGridPoint curP = pool[p.id];

                for (int i = 0; i < 4; i++)
                {
                    if (p.neighbours[i] != -1)
                    {
                        curP.neighbours[i] = pool[p.neighbours[i]];
                    }
                }
            }
        }
 public int GetBattleGridID(BattleGridPoint p)
 {
     return(bgIDs[p]);
 }
Exemplo n.º 5
0
        void Start()
        {
            _agent      = GetComponent <PathFinderAgent>();
            _properties = _agent.properties;

            PathFinder.QueueGraph(new Bounds(transform.position, Vector3.one * 20), _properties); //queue some space in advance
            _controler = GetComponent <CharacterController>();

            //this will happen every time agent recieve new information about grid
            //Idea:
            //1) Take some point that for sure accessible and in close proximity to agent
            //2) Test point visibility at some height
            //3) Take closest one and order path to it
            //this way agent know it can hide in accessible place from something else
            _agent.SetRecieveBattleGridDelegate((IEnumerable <BattleGridPoint> points) => {
                //take some values
                float agentHeight  = _properties.height;
                int layers         = _properties.includedLayers;
                Vector3 testTarget = target.transform.position;

                //update small list with subdivisions
                _heightTests.Clear();
                for (int i = 0; i < sightSubdivisions; i++)
                {
                    _heightTests.Add(1f - (1f / sightSubdivisions * i));
                }

                BattleGridPoint targetPoint = null;
                float highestValue          = 0;

                //debug
                int pointsLength    = points.Count();
                Vector3[] meshVerts = new Vector3[pointsLength * 4];
                int[] meshTris      = new int[pointsLength * 6];
                int debugIndex      = 0;

                foreach (var point in points)
                {
                    //take point position and raycast to it in different heights to test visibility
                    float value = 1f;
                    for (int v = 0; v < _heightTests.Count; v++)
                    {
                        if (Physics.Linecast(testTarget, point.positionV3 + (Vector3.up * _heightTests[v] * agentHeight), layers) == false)
                        {
                            value = _heightTests[v];
                        }
                        else
                        {
                            break;
                        }
                    }

                    //multipy it by normalized distance to point. 1f is closest and 0f is farthest
                    value *= Mathf.Clamp01((10 - Vector3.Distance(transform.position, point.positionV3)) / 10);

                    if (value > highestValue)
                    {
                        highestValue = value;
                        targetPoint  = point;
                    }

                    //debug
                    //drawing lots of small lines to debug this value.
                    //cause Debuger_K dont exist in builded project for reasons
                    Vector3 cameraKeyPoint        = Vector3.Cross(cam.transform.position - point.positionV3, Vector3.up).normalized;
                    meshVerts[debugIndex * 4]     = point.positionV3 + (cameraKeyPoint * -debugWidth);                                          //LB
                    meshVerts[debugIndex * 4 + 1] = point.positionV3 + (cameraKeyPoint * debugWidth);                                           //RB
                    meshVerts[debugIndex * 4 + 2] = point.positionV3 + (cameraKeyPoint * -debugWidth) + new Vector3(0, value * debugHeight, 0); //LT
                    meshVerts[debugIndex * 4 + 3] = point.positionV3 + (cameraKeyPoint * debugWidth) + new Vector3(0, value * debugHeight, 0);  //RT

                    meshTris[debugIndex * 6 + 0] = debugIndex * 4 + 1;
                    meshTris[debugIndex * 6 + 1] = debugIndex * 4 + 0;
                    meshTris[debugIndex * 6 + 2] = debugIndex * 4 + 2;
                    meshTris[debugIndex * 6 + 3] = debugIndex * 4 + 1;
                    meshTris[debugIndex * 6 + 4] = debugIndex * 4 + 2;
                    meshTris[debugIndex * 6 + 5] = debugIndex * 4 + 3;
                    debugIndex++;
                }

                //also debug
                Mesh mesh        = new Mesh();
                mesh.vertices    = meshVerts;
                mesh.triangles   = meshTris;
                _meshFilter.mesh = mesh;

                //send agent to this point
                if (targetPoint != null)
                {
                    _agent.SetGoalMoveHere(targetPoint.positionV3);
                }
            },
                                                AgentDelegateMode.ThreadSafe);//to sure it's in main thread

            StartCoroutine(UpdateBattleGridCall());

            _debugGO                    = new GameObject("debug battle grid GO");
            _debugMeshRenderer          = _debugGO.AddComponent <MeshRenderer>();
            _meshFilter                 = _debugGO.AddComponent <MeshFilter>();
            _debugMeshRenderer.material = debugMaterial;
        }