SetPosition() public method

public SetPosition ( Int3 position ) : void
position Int3
return void
Ejemplo n.º 1
0
        public bool RecalculateCell(int x, int z, bool preserveExistingNodes)
        {
            LinkedLevelCell linkedLevelCell = new LinkedLevelCell();
            Vector3         position        = this.matrix.MultiplyPoint3x4(new Vector3((float)x + 0.5f, 0f, (float)z + 0.5f));

            RaycastHit[] array = this.collision.CheckHeightAll(position);
            for (int i = 0; i < array.Length / 2; i++)
            {
                RaycastHit raycastHit = array[i];
                array[i] = array[array.Length - 1 - i];
                array[array.Length - 1 - i] = raycastHit;
            }
            bool result = false;

            if (array.Length > 0)
            {
                LinkedLevelNode linkedLevelNode = null;
                for (int j = 0; j < array.Length; j++)
                {
                    LinkedLevelNode linkedLevelNode2 = new LinkedLevelNode();
                    linkedLevelNode2.position = array[j].point;
                    if (linkedLevelNode != null && linkedLevelNode2.position.y - linkedLevelNode.position.y <= this.mergeSpanRange)
                    {
                        linkedLevelNode.position = linkedLevelNode2.position;
                        linkedLevelNode.hit      = array[j];
                        linkedLevelNode.walkable = this.collision.Check(linkedLevelNode2.position);
                    }
                    else
                    {
                        linkedLevelNode2.walkable = this.collision.Check(linkedLevelNode2.position);
                        linkedLevelNode2.hit      = array[j];
                        linkedLevelNode2.height   = float.PositiveInfinity;
                        if (linkedLevelCell.first == null)
                        {
                            linkedLevelCell.first = linkedLevelNode2;
                            linkedLevelNode       = linkedLevelNode2;
                        }
                        else
                        {
                            linkedLevelNode.next   = linkedLevelNode2;
                            linkedLevelNode.height = linkedLevelNode2.position.y - linkedLevelNode.position.y;
                            linkedLevelNode        = linkedLevelNode.next;
                        }
                    }
                }
            }
            else
            {
                linkedLevelCell.first = new LinkedLevelNode
                {
                    position = position,
                    height   = float.PositiveInfinity,
                    walkable = !this.collision.unwalkableWhenNoGround
                };
            }
            uint            graphIndex       = (uint)this.active.astarData.GetGraphIndex(this);
            LinkedLevelNode linkedLevelNode3 = linkedLevelCell.first;
            int             num = 0;
            int             k   = 0;

            while (true)
            {
                if (k >= this.layerCount)
                {
                    if (k + 1 > 255)
                    {
                        break;
                    }
                    this.AddLayers(1);
                    result = true;
                }
                LevelGridNode levelGridNode = this.nodes[z * this.width + x + this.width * this.depth * k];
                if (levelGridNode == null || !preserveExistingNodes)
                {
                    this.nodes[z * this.width + x + this.width * this.depth * k] = new LevelGridNode(this.active);
                    levelGridNode            = this.nodes[z * this.width + x + this.width * this.depth * k];
                    levelGridNode.Penalty    = this.initialPenalty;
                    levelGridNode.GraphIndex = graphIndex;
                    result = true;
                }
                levelGridNode.SetPosition((Int3)linkedLevelNode3.position);
                levelGridNode.Walkable        = linkedLevelNode3.walkable;
                levelGridNode.WalkableErosion = levelGridNode.Walkable;
                if (linkedLevelNode3.hit.normal != Vector3.zero)
                {
                    float num2 = Vector3.Dot(linkedLevelNode3.hit.normal.normalized, this.collision.up);
                    if (this.penaltyAngle)
                    {
                        levelGridNode.Penalty += (uint)Mathf.RoundToInt((1f - num2) * this.penaltyAngleFactor);
                    }
                    float num3 = Mathf.Cos(this.maxSlope * 0.0174532924f);
                    if (num2 < num3)
                    {
                        levelGridNode.Walkable = false;
                    }
                }
                levelGridNode.NodeInGridIndex = z * this.width + x;
                if (linkedLevelNode3.height < this.characterHeight)
                {
                    levelGridNode.Walkable = false;
                }
                num++;
                linkedLevelNode3 = linkedLevelNode3.next;
                k++;
                if (linkedLevelNode3 == null)
                {
                    goto Block_14;
                }
            }
            Debug.LogError("Too many layers, a maximum of LevelGridNode.MaxLayerCount are allowed (required " + (k + 1) + ")");
            return(result);

Block_14:
            while (k < this.layerCount)
            {
                this.nodes[z * this.width + x + this.width * this.depth * k] = null;
                k++;
            }
            linkedLevelCell.count = num;
            return(result);
        }
Ejemplo n.º 2
0
        public override void ScanInternal(OnScanStatus status)
        {
            this.scans++;
            if (this.nodeSize <= 0f)
            {
                return;
            }
            base.GenerateMatrix();
            if (this.width > 1024 || this.depth > 1024)
            {
                Debug.LogError("One of the grid's sides is longer than 1024 nodes");
                return;
            }
            this.lastScannedWidth = this.width;
            this.lastScannedDepth = this.depth;
            this.SetUpOffsetsAndCosts();
            LevelGridNode.SetGridGraph(this.active.astarData.GetGraphIndex(this), this);
            this.maxClimb = Mathf.Clamp(this.maxClimb, 0f, this.characterHeight);
            LinkedLevelCell[] array = new LinkedLevelCell[this.width * this.depth];
            if (this.collision == null)
            {
                this.collision = new GraphCollision();
            }
            this.collision.Initialize(this.matrix, this.nodeSize);
            for (int i = 0; i < this.depth; i++)
            {
                for (int j = 0; j < this.width; j++)
                {
                    array[i * this.width + j] = new LinkedLevelCell();
                    LinkedLevelCell linkedLevelCell = array[i * this.width + j];
                    Vector3         position        = this.matrix.MultiplyPoint3x4(new Vector3((float)j + 0.5f, 0f, (float)i + 0.5f));
                    RaycastHit[]    array2          = this.collision.CheckHeightAll(position);
                    for (int k = 0; k < array2.Length / 2; k++)
                    {
                        RaycastHit raycastHit = array2[k];
                        array2[k] = array2[array2.Length - 1 - k];
                        array2[array2.Length - 1 - k] = raycastHit;
                    }
                    if (array2.Length > 0)
                    {
                        LinkedLevelNode linkedLevelNode = null;
                        for (int l = 0; l < array2.Length; l++)
                        {
                            LinkedLevelNode linkedLevelNode2 = new LinkedLevelNode();
                            linkedLevelNode2.position = array2[l].point;
                            if (linkedLevelNode != null && linkedLevelNode2.position.y - linkedLevelNode.position.y <= this.mergeSpanRange)
                            {
                                linkedLevelNode.position = linkedLevelNode2.position;
                                linkedLevelNode.hit      = array2[l];
                                linkedLevelNode.walkable = this.collision.Check(linkedLevelNode2.position);
                            }
                            else
                            {
                                linkedLevelNode2.walkable = this.collision.Check(linkedLevelNode2.position);
                                linkedLevelNode2.hit      = array2[l];
                                linkedLevelNode2.height   = float.PositiveInfinity;
                                if (linkedLevelCell.first == null)
                                {
                                    linkedLevelCell.first = linkedLevelNode2;
                                    linkedLevelNode       = linkedLevelNode2;
                                }
                                else
                                {
                                    linkedLevelNode.next   = linkedLevelNode2;
                                    linkedLevelNode.height = linkedLevelNode2.position.y - linkedLevelNode.position.y;
                                    linkedLevelNode        = linkedLevelNode.next;
                                }
                            }
                        }
                    }
                    else
                    {
                        linkedLevelCell.first = new LinkedLevelNode
                        {
                            position = position,
                            height   = float.PositiveInfinity,
                            walkable = !this.collision.unwalkableWhenNoGround
                        };
                    }
                }
            }
            int num = 0;

            this.layerCount = 0;
            for (int m = 0; m < this.depth; m++)
            {
                for (int n = 0; n < this.width; n++)
                {
                    LinkedLevelCell linkedLevelCell2 = array[m * this.width + n];
                    LinkedLevelNode linkedLevelNode3 = linkedLevelCell2.first;
                    int             num2             = 0;
                    do
                    {
                        num2++;
                        num++;
                        linkedLevelNode3 = linkedLevelNode3.next;
                    }while (linkedLevelNode3 != null);
                    this.layerCount = ((num2 <= this.layerCount) ? this.layerCount : num2);
                }
            }
            if (this.layerCount > 255)
            {
                Debug.LogError("Too many layers, a maximum of LevelGridNode.MaxLayerCount are allowed (found " + this.layerCount + ")");
                return;
            }
            this.nodes = new LevelGridNode[this.width * this.depth * this.layerCount];
            for (int num3 = 0; num3 < this.nodes.Length; num3++)
            {
                this.nodes[num3]         = new LevelGridNode(this.active);
                this.nodes[num3].Penalty = this.initialPenalty;
            }
            int   num4 = 0;
            float num5 = Mathf.Cos(this.maxSlope * 0.0174532924f);

            for (int num6 = 0; num6 < this.depth; num6++)
            {
                for (int num7 = 0; num7 < this.width; num7++)
                {
                    LinkedLevelCell linkedLevelCell3 = array[num6 * this.width + num7];
                    LinkedLevelNode linkedLevelNode4 = linkedLevelCell3.first;
                    linkedLevelCell3.index = num4;
                    int num8 = 0;
                    int num9 = 0;
                    do
                    {
                        LevelGridNode levelGridNode = this.nodes[num6 * this.width + num7 + this.width * this.depth * num9];
                        levelGridNode.SetPosition((Int3)linkedLevelNode4.position);
                        levelGridNode.Walkable = linkedLevelNode4.walkable;
                        if (linkedLevelNode4.hit.normal != Vector3.zero && (this.penaltyAngle || num5 < 1f))
                        {
                            float num10 = Vector3.Dot(linkedLevelNode4.hit.normal.normalized, this.collision.up);
                            if (this.penaltyAngle)
                            {
                                levelGridNode.Penalty += (uint)Mathf.RoundToInt((1f - num10) * this.penaltyAngleFactor);
                            }
                            if (num10 < num5)
                            {
                                levelGridNode.Walkable = false;
                            }
                        }
                        levelGridNode.NodeInGridIndex = num6 * this.width + num7;
                        if (linkedLevelNode4.height < this.characterHeight)
                        {
                            levelGridNode.Walkable = false;
                        }
                        levelGridNode.WalkableErosion = levelGridNode.Walkable;
                        num4++;
                        num8++;
                        linkedLevelNode4 = linkedLevelNode4.next;
                        num9++;
                    }while (linkedLevelNode4 != null);
                    while (num9 < this.layerCount)
                    {
                        this.nodes[num6 * this.width + num7 + this.width * this.depth * num9] = null;
                        num9++;
                    }
                    linkedLevelCell3.count = num8;
                }
            }
            this.nodeCellIndices = new int[array.Length];
            for (int num11 = 0; num11 < this.depth; num11++)
            {
                for (int num12 = 0; num12 < this.width; num12++)
                {
                    for (int num13 = 0; num13 < this.layerCount; num13++)
                    {
                        GraphNode node = this.nodes[num11 * this.width + num12 + this.width * this.depth * num13];
                        this.CalculateConnections(this.nodes, node, num12, num11, num13);
                    }
                }
            }
            uint graphIndex = (uint)this.active.astarData.GetGraphIndex(this);

            for (int num14 = 0; num14 < this.nodes.Length; num14++)
            {
                LevelGridNode levelGridNode2 = this.nodes[num14];
                if (levelGridNode2 != null)
                {
                    this.UpdatePenalty(levelGridNode2);
                    levelGridNode2.GraphIndex = graphIndex;
                    if (!levelGridNode2.HasAnyGridConnections())
                    {
                        levelGridNode2.Walkable        = false;
                        levelGridNode2.WalkableErosion = levelGridNode2.Walkable;
                    }
                }
            }
            this.ErodeWalkableArea(0, 0, this.width, this.depth);
        }