Ejemplo n.º 1
0
    public static void CreateGrid()
    {
        GridOfArena = new NodeA[ArenaSizeX, ArenaSizeY];
        Texture2D Map           = Resources.LoadAll <Texture2D> ("Marcer") [0];
        Transform MiniNodaLevel = Resources.LoadAll <Transform> ("Marcer") [0];

        Vector3 BotomLeft = Vector3.zero - Vector3.right * (ArenaSizeX / 2) - Vector3.forward * (ArenaSizeY / 2);

        for (int x = 0; x < ArenaSizeX; x++)
        {
            for (int y = 0; y < ArenaSizeY; y++)
            {
                if (Map.GetPixel(x, y) == Color.green || Map.GetPixel(x, y) == Color.blue)
                {
                    GridOfArena[x, y] = new NodeA(BotomLeft + Vector3.right * x + Vector3.forward * y, x, y, NodeA.NodeType.Walkable);
                    Vector3   positionofnode = (BotomLeft + Vector3.right * x + Vector3.forward * y);
                    Transform node           = Instantiate(MiniNodaLevel, positionofnode, Quaternion.identity).transform;
                    node.eulerAngles = new Vector3(90, 0, 0);
                    node.localScale *= 2f;
                }
                else
                {
                    GridOfArena[x, y] = new NodeA(BotomLeft + Vector3.right * x + Vector3.forward * y, x, y, NodeA.NodeType.Wall);
                }
            }
        }

        foreach (Shelter n in ShelterList)
        {
            n.SetSelterBlock();
        }
    }
Ejemplo n.º 2
0
 public NodeA(Node startNode, Node endNode, int ID)
 {
     from    = 0;
     to      = Math.Abs(startNode.column - endNode.column) + Math.Abs(startNode.row - endNode.row);
     total   = to;
     parent  = null;
     this.ID = ID;
 }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var nodeB2 = new NodeB();
            var nodeA2 = new NodeA(nodeB2);
            var nodeA1 = new NodeA(nodeB2, nodeA2, new NodeC());

            nodeA1.Accept(new PrintVisitor());
        }
Ejemplo n.º 4
0
 public NodeA()
 {
     from   = 0;
     to     = 0;
     total  = 0;
     ID     = -1;
     parent = null;
 }
Ejemplo n.º 5
0
 public NodeA(Node startNode, Node endNode, Node currentNode, NodeA parent, int ID)
 {
     from        = Math.Abs(startNode.column - currentNode.column) + Math.Abs(startNode.row - currentNode.row);
     to          = Math.Abs(currentNode.column - endNode.column) + Math.Abs(currentNode.row - endNode.row);
     total       = to + from;
     this.parent = parent;
     this.ID     = ID;
 }
Ejemplo n.º 6
0
        static void Bridge_Exemplo1()
        {
            var bridge = new Bridge();
            var nodeA  = new NodeA();
            var nodeB  = new NodeB();

            bridge.ReachTo(nodeA);
            bridge.ReachTo(nodeB);
        }
Ejemplo n.º 7
0
 public override int GetHashCode()
 {
     unchecked
     {
         int hashCode = 105289569;
         hashCode = hashCode * -1521134295 + NodeA.GetHashCode();
         hashCode = hashCode * -1521134295 + NodeB.GetHashCode();
         return(hashCode);
     }
 }
Ejemplo n.º 8
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = NodeA.GetHashCode();
         hashCode = hashCode ^ NodeB.GetHashCode();
         hashCode = (hashCode * 397) ^ Distance.GetHashCode();
         return(hashCode);
     }
 }
Ejemplo n.º 9
0
 public virtual void MakeShoot(NodeA node)
 {
     if (_unit == null)
     {
         _unit = transform.parent.GetComponent <NetworkElement> ();
     }
     if (_bullet == null)
     {
         _bullet = Resources.Load <GameObject> ("Staff/Bullet") as GameObject;
     }
     _target = node.UnitOnNode.unit;
     StartCoroutine(MoveBullet(node));
 }
Ejemplo n.º 10
0
    public void TryNewParent(Node startNode, Node endNode, Node currentNode, NodeA parent)
    {
        double fromTmp  = Math.Abs(startNode.column - currentNode.column) + Math.Abs(startNode.row - currentNode.row);
        double toTmp    = Math.Abs(currentNode.column - endNode.column) + Math.Abs(currentNode.row - endNode.row);
        double totalTmp = to + from;

        if (totalTmp < this.total)
        {
            this.to     = toTmp;
            this.from   = fromTmp;
            this.total  = totalTmp;
            this.parent = parent;
        }
    }
Ejemplo n.º 11
0
    public IEnumerator LerpMove(NodeA targetNode)
    {
        unit.MoveUse();
        Vector3 finalePose = targetNode.UnitPosition;
        float   timer      = 0;

        while (Vector3.Distance(transform.position, finalePose) > 0.1f && timer < 1f)
        {
            timer += Time.deltaTime;
            transform.position = Vector3.Lerp(transform.position, finalePose, 0.1f);
            yield return(new WaitForEndOfFrame());
        }
        transform.position    = finalePose;
        targetNode.UnitOnNode = this;
    }
Ejemplo n.º 12
0
    public static NodeA NodeFromWorldPoint(Vector3 worldPosition)
    {
        try {
            float percentX = (worldPosition.x + ArenaSizeX / 2) / ArenaSizeX;
            float percentY = (worldPosition.z + ArenaSizeY / 2) / ArenaSizeY;
            percentX = Mathf.Clamp01(percentX);
            percentY = Mathf.Clamp01(percentY);

            int x = Mathf.RoundToInt((ArenaSizeX) * percentX);
            int y = Mathf.RoundToInt((ArenaSizeY) * percentY);
            lastNodeA = GridOfArena[x, y];
            return(GridOfArena[x, y]);
        } catch {
            return(lastNodeA);
        }
    }
Ejemplo n.º 13
0
    void CreateGrid()
    {
        GridOfArena = new NodeA[ArenaSizeX, ArenaSizeY];

        Vector3 BotomLeft = transform.position - Vector3.right * (ArenaSizeX / 2) - Vector3.forward * (ArenaSizeY / 2);

        for (int x = 0; x < ArenaSizeX; x++)
        {
            for (int y = 0; y < ArenaSizeY; y++)
            {
                GridOfArena[x, y] = new NodeA(BotomLeft + Vector3.right * x + Vector3.right * 0.5f + Vector3.forward * y, x, y, WhatTypeNode(Map.GetPixel(x, y)));
                if (Map.GetPixel(x, y) == Color.green || Map.GetPixel(x, y) == Color.blue)
                {
                    Vector3   positionofnode = (BotomLeft + Vector3.right * x + Vector3.right * 0.5f + Vector3.forward * y);
                    Transform node           = Instantiate(MiniNodaLevel, positionofnode, Quaternion.identity).transform;
                    node.eulerAngles = new Vector3(90, 0, 0);
                    node.localScale *= 0.6f;
                }
            }
        }
    }
Ejemplo n.º 14
0
        public GridGraph(int[,] data)
        {
            this.nodes  = new List <GridNode>(); //Creates a new list of nodes
            this.width  = data.GetLength(0);     //Sets the width
            this.height = data.GetLength(1);     //Sets the height

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    this.nodes.Add(new GridNode(new Vector2(x, y), data[x, y])); //Creates a node for each position
                }
            }

            //Neighbours
            foreach (GridNode NodeA in this.nodes)                                                                                     //Runs through each node
            {
                foreach (GridNode NodeB in this.nodes)                                                                                 //Runs through every other node
                {
                    NodeA.SetNeighbour(NodeB, (int)(NodeB.Location.X - NodeA.Location.X), (int)(NodeB.Location.Y - NodeA.Location.Y)); //Evaluates to see if they are neighbours
                }
            }
        }
Ejemplo n.º 15
0
        public GridGraph(int[,] data)
        {
            this.nodes  = new List <GridNode>();
            this.width  = data.GetLength(0);
            this.height = data.GetLength(1);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    this.nodes.Add(new GridNode(new Vector2(x, y), data[x, y]));
                }
            }

            //Neighbours
            foreach (GridNode NodeA in this.nodes)
            {
                foreach (GridNode NodeB in this.nodes)
                {
                    NodeA.SetNeighbour(NodeB, NodeB.Location.X - NodeA.Location.X, NodeB.Location.Y - NodeA.Location.Y);
                }
            }
        }
Ejemplo n.º 16
0
    public virtual IEnumerator MoveBullet(NodeA targetNode)
    {
        _unit.unit.AttackUse();
        yield return(new WaitForSeconds(0.25f));

        for (int i = 0; i < BulletPerShoot; i++)
        {
            _target.Manger.PlaySpecific(Sound);

            Vector3        targetPosition = targetNode.Position + Vector3.up * (Random.Range(-5, 11) / 20f) + Vector3.up * 0.6f;
            Vector3        selfPosition   = transform.position + (targetPosition - (transform.position + Vector3.up * 0.3f)).normalized * 0.25f + Vector3.up * 0.3f;
            NetworkElement targetUnit     = targetNode.UnitOnNode;
            GameObject     localBullet    = Instantiate(_bullet, selfPosition, Quaternion.identity);
            float          distantion     = Vector3.Distance(selfPosition, targetPosition);
            while (Vector3.Distance(localBullet.transform.position, targetPosition) > 0.05f)
            {
                localBullet.transform.Translate((targetPosition - selfPosition) / valuespeedBullet);
                yield return(new WaitForFixedUpdate());
            }

            float   n = Random.Range(0, 100);
            Shelter shelter;

            float DistanceTarget = Vector3.Distance(selfPosition, targetUnit.transform.position + Vector3.up * (Random.Range(-5, 11) / 20f));

            float Range = NormalDist / DistanceTarget;
            float targetShelterValue = targetUnit.GetShelterLevel(out shelter);
            if (Range > 1)
            {
                Range = 1;
            }

            if (n < Range * 100f - targetShelterValue)
            {
                FinalDamage += Damage;
            }
            else
            {
                if (Random.Range(0, 101) > targetShelterValue * 0.75f)
                {
                    if (shelter != null)
                    {
                        Instantiate(shelter.FX, shelter.transform.position, Quaternion.identity);
                        shelter.ShelterLevl -= Damage * 3;
                        if (shelter.ShelterLevl < 1)
                        {
                            Destroy(shelter.gameObject);
                        }
                    }
                }
            }
            Ammo--;
            Destroy(localBullet);
            yield return(new WaitForSeconds(WaitTime));
        }

        if (_unit.Controlled)
        {
            if (FinalDamage > 0)
            {
                ClientReciverMK1.WriteMsg("/Damage" + _unit.unit.Manger.GetUnitId(targetNode.UnitOnNode) + ":" + FinalDamage + ":" + 0 + ":" + 0);
                _target.GetDamage(FinalDamage);
                FinalDamage = 0;
            }
            else
            {
                ClientReciverMK1.WriteMsg("/Damage" + _unit.unit.Manger.GetUnitId(targetNode.UnitOnNode) + ":" + 0 + ":" + 0 + ":" + -1);
                _target.GetDamage(0, 0, -1);
            }
        }
    }
Ejemplo n.º 17
0
 public string ToDetailedString(DataFields dataFields)
 {
     return(NodeA.ToDetailedString(dataFields) + " => " + NodeB.ToDetailedString(dataFields) + " || " + GetCalculationsAsString());
 }
Ejemplo n.º 18
0
 public override int GetHashCode()
 {
     return(NodeA.GetHashCode() ^ NodeB.GetHashCode() ^ Values.GetHashCode());
 }
Ejemplo n.º 19
0
    void Update()
    {
        if (manager.CorrectUnit == ThisUnit && manager.PercUse)
        {
            RaycastHit hit;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            Physics.Raycast(ray, out hit, Mathf.Infinity);



            if (AParm == 1 && hit.rigidbody != null)
            {
                Debug.Log(hit.rigidbody.name);
                UnitBehevior PointedTarget = hit.rigidbody.GetComponent <UnitBehevior>();

                if (ThisUnit.TimeToReusePerc <= 0 && manager.PercUse && Input.GetKeyDown(KeyCode.Mouse0) && IsTarget(PointedTarget) && Vector3.Distance(transform.position, PointedTarget.transform.position) < Radious)
                {
                    GridManager.CleatGrid();
                    manager.CleanJunk();


                    UnitBehevior unitTarget = manager.CorrectUnit;



                    if (unitTarget.transform.position.x - manager.CorrectUnit.transform.position.x > 1)
                    {
                        unitTarget.Weapon.SpriteRendererValue.sortingOrder = 1;
                        unitTarget.Weapon.SpriteRendererValue.flipX        = false;

                        unitTarget.HandRenderer.sortingOrder = 1;
                        unitTarget.HandRenderer.flipX        = false;

                        unitTarget.BodyRenderer.sprite = unitTarget.data.IconBodyFace;
                    }

                    if (unitTarget.transform.position.x - manager.CorrectUnit.transform.position.x < -1)
                    {
                        unitTarget.Weapon.SpriteRendererValue.sortingOrder = -2;
                        unitTarget.Weapon.SpriteRendererValue.flipX        = true;

                        unitTarget.HandRenderer.sortingOrder = -2;
                        unitTarget.HandRenderer.flipX        = true;

                        unitTarget.BodyRenderer.sprite = unitTarget.data.IconBodyBack;
                    }



                    manager.CorrectUnit.PercBehevior.UsePerc(manager.CorrectUnit.Enemy, PointedTarget);
                    manager.PercUse = false;
                    Destroy(Show);
                    manager.CorrectUnit.TimeToReusePerc = data.TimeToReuse;
                    manager.EndTurn();
                }
            }


            if (AParm == 2 && ThisUnit.TimeToReusePerc < 1)
            {
                if (!VisualEffect.gameObject.activeInHierarchy)
                {
                    VisualEffect.gameObject.SetActive(true);
                }

                GridManager.CleatGrid();
                point           = GridManager.NodeFromWorldPoint(ray.GetPoint(15)).Position;
                vectorsOfGrande = GetMatrix();
                VisualEffect.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);


                var     vel = VisualEffect.velocityOverLifetime;
                Vector3 dir = (point - transform.position).normalized;


                float dist = Vector3.Distance(point, transform.position);
                if (dist > Radious)
                {
                    dist = Radious;
                }
                vel.x = new ParticleSystem.MinMaxCurve(dir.x * dist);
                vel.z = new ParticleSystem.MinMaxCurve(dir.z * dist);


                VisualEffect.Play();

                Vector3 vector = GridManager.NodeFromWorldPoint(transform.position + dist * dir).Position;
                for (int i = 0; i < vectorsOfGrande.Length; i++)
                {
                    vectorsOfGrande[i] += new Vector2(vector.x, vector.z);
                    GridManager.CreateNodeShow(Color.cyan, new Vector3(vectorsOfGrande[i].x, 0, vectorsOfGrande[i].y));
                }


                if (Input.GetKeyDown(KeyCode.Mouse0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
                {
                    var granadeLineFx = Instantiate(manager.OnTurnEffectFX[8].GetComponent <ParticleSystem>(), transform.position, Quaternion.identity);
                    granadeLineFx.Stop();
                    var fxvel = granadeLineFx.velocityOverLifetime;

                    fxvel.x = new ParticleSystem.MinMaxCurve(dir.x * dist);
                    fxvel.z = new ParticleSystem.MinMaxCurve(dir.z * dist);

                    granadeLineFx.gameObject.SetActive(true);
                    granadeLineFx.Play();

                    VisualEffect.gameObject.SetActive(false);

                    point = vector;
                    Invoke("MakeBoom", 1);



                    ThisUnit.TimeToReusePerc = data.TimeToReuse;
                    manager.RetarderCall(1.5f);
                }
            }

            if (AParm == 3 && ThisUnit.TimeToReusePerc < 1)
            {
                NodeA node = new NodeA(Vector3.right * 1000, 1111, 1111, NodeA.TypeOfNode.Wall);

                if (Vector3.Distance(point, ray.GetPoint(15)) > 0.1f)
                {
                    if (!VisualEffect.gameObject.activeInHierarchy)
                    {
                        VisualEffect.gameObject.SetActive(true);
                    }

                    VisualEffect.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
                    var vel = VisualEffect.velocityOverLifetime;

                    GridManager.CleatGrid();
                    point = GridManager.NodeFromWorldPoint(ray.GetPoint(15)).Position;
                    Vector3 dir = (point - transform.position).normalized;

                    float dist = Vector3.Distance(point, transform.position);
                    if (dist > Radious)
                    {
                        dist = Radious;
                    }
                    vel.x = new ParticleSystem.MinMaxCurve(dir.x * dist);
                    vel.z = new ParticleSystem.MinMaxCurve(dir.z * dist);


                    VisualEffect.Play();

                    node = GridManager.NodeFromWorldPoint(transform.position + dir * dist);

                    if (node._Bloc == NodeA.TypeOfNode.Walkable && node.UnitOnNode == null)
                    {
                        GridManager.CreateNodeShow(manager.ColorOfIfo[1], node.Position);
                    }
                }
                if (Input.GetKeyDown(KeyCode.Mouse0) && node._Bloc == NodeA.TypeOfNode.Walkable && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
                {
                    GameObject fx = Instantiate(manager.OnTurnEffectFX[10 + CAParm], node.Position, Quaternion.identity);
                    fx.transform.eulerAngles = new Vector3(0, -87.69f, 0);
                    VisualEffect.gameObject.SetActive(false);
                    ThisUnit.TimeToReusePerc = data.TimeToReuse;
                    manager.RetarderCall(1f);
                }
            }
        }
        else if (VisualEffect != null && VisualEffect.gameObject.activeInHierarchy)
        {
            VisualEffect.gameObject.SetActive(false);
        }
    }
Ejemplo n.º 20
0
 public CheckedNode(NodeA node, int cost)
 {
     Node = node;
     Cost = cost;
 }
Ejemplo n.º 21
0
 public new string ToString()
 {
     return(NodeA.ToString().PadRight(10) + "<-> " + NodeB.ToString().PadRight(10) + "@".PadLeft(5) + distance.ToString("0.00"));
 }
 public VectorClockConflict <T> ToModel()
 {
     return(new VectorClockConflict <T>(NodeA.ToModel(), NodeB.ToModel()));
 }
Ejemplo n.º 23
0
 public override string ToString()
 {
     return(NodeA.ToString() + " -> " + NodeB.ToString());
 }
Ejemplo n.º 24
0
    //Given coordinates of two points function will draw path between them
    public List <Vector2> GoFromTo(double xStart, double yStart, double xEnd, double yEnd)
    {
        int  rowStart        = tileMap.GetLength(0) - (int)((yStart + mapTranslation.y) / tileSize.y); //-1 * ((tileMap.GetLength(1) - randX) * width + mapTranslation.x);
        int  columnStart     = (int)((xStart + mapTranslation.x) / tileSize.x);
        int  rowEnd          = tileMap.GetLength(0) - (int)((yEnd + mapTranslation.y) / tileSize.y);   //-1 * ((tileMap.GetLength(1) - randX) * width + mapTranslation.x);
        int  columnEnd       = (int)((xEnd + mapTranslation.x) / tileSize.x);
        int  rowStartLanding = 0;
        int  rowEndLanding   = 0;
        Node startNode       = null;
        Node endNode         = null;
        //Return list with points that AI should go through
        List <Vector2> path = new List <Vector2>();

        //Instantiate(spikes, new Vector3(columnStart * tileSize.x + mapTranslation.x, (tileMap.GetLength(0) - rowStart) * tileSize.y + mapTranslation.y, 0), Quaternion.identity);
        //Instantiate(spikes, new Vector3(columnEnd * tileSize.x + mapTranslation.x, (tileMap.GetLength(0) - rowEnd) * tileSize.y + mapTranslation.y, 0), Quaternion.identity);

        //**********************************************************************************************************************************************************************************************//
        //********************************************************       FIND START AND END NODES ******************************************************************************************************//
        //**********************************************************************************************************************************************************************************************//

        //Check if start is on platform
        //If not it means that unit is in air and we draw vertical path to land it
        if (tileMap[ClampRow(rowStart + 1), ClampColumn(columnStart)] != 1)
        {
            for (int i = 0; i < tileMap.GetLength(0); i++)
            {
                if (tileMap[ClampRow(rowStart + 1 + i), ClampColumn(columnStart)] == 1)
                {
                    //Debug.DrawLine(new Vector3((columnStart) * tileSize.x, (tileMap.GetLength(0) - (rowStart + i)) * tileSize.y, 0),
                    //               new Vector3((columnStart) * tileSize.x, (tileMap.GetLength(0) - rowStart) * tileSize.y, 0),
                    //               Color.white, 1, false);
                    rowStartLanding = rowStart + i;
                    break;
                }
            }
        }
        else
        {
            rowStartLanding = rowStart;
        }

        //Check if end is on platform
        //If not it means that unit is in air and we draw vertical path to land it
        if (tileMap[ClampRow(rowEnd + 1), ClampColumn(columnEnd)] != 1)
        {
            for (int i = 0; i < tileMap.GetLength(0); i++)
            {
                if (tileMap[ClampRow(rowEnd + 1 + i), ClampColumn(columnEnd)] == 1)
                {
                    //Debug.DrawLine(new Vector3((columnEnd) * tileSize.x, (tileMap.GetLength(0) - (rowEnd + i)) * tileSize.y, 0),
                    //               new Vector3((columnEnd) * tileSize.x, (tileMap.GetLength(0) - rowEnd) * tileSize.y, 0),
                    //               Color.white, 1, false);
                    rowEndLanding = rowEnd + i;
                    break;
                }
            }
        }
        else
        {
            rowEndLanding = rowEnd;
        }
        //Now we look for closets node on platform where unit is or just landed
        //Looking for it we move 1 tile right and left each iteration untill we uncounter node
        bool leftIsOk  = true;
        bool rightIsOk = true;

        //Search for start node
        for (int i = 0; i < tileMap.GetLength(1); i++)
        {
            int tmp = ClampColumn(columnStart + i);
            if (tileMap[ClampRow(rowStartLanding), ClampColumn(columnStart + i)] == 1)
            {
                rightIsOk = false;
            }
            if (tileMap[ClampRow(rowStartLanding), ClampColumn(columnStart - i)] == 1)
            {
                leftIsOk = false;
            }
            if (nodeMap[ClampRow(rowStartLanding), ClampColumn(columnStart + i)].Count != 0 && rightIsOk)
            {
                startNode = graph[nodeMap[ClampRow(rowStartLanding), ClampColumn(columnStart + i)][0]];
                break;
            }
            if (nodeMap[ClampRow(rowStartLanding), ClampColumn(columnStart - i)].Count != 0 && leftIsOk)
            {
                startNode = graph[nodeMap[ClampRow(rowStartLanding), ClampColumn(columnStart - i)][0]];
                break;
            }
        }

        leftIsOk  = true;
        rightIsOk = true;

        //Search for end node
        for (int i = 0; i < tileMap.GetLength(1); i++)
        {
            if (tileMap[ClampRow(rowEndLanding), ClampColumn(columnEnd + i)] == 1)
            {
                rightIsOk = false;
            }
            if (tileMap[ClampRow(rowEndLanding), ClampColumn(columnEnd - i)] == 1)
            {
                leftIsOk = false;
            }
            if (nodeMap[ClampRow(rowEndLanding), ClampColumn(columnEnd + i)].Count != 0 && rightIsOk)
            {
                endNode = graph[nodeMap[ClampRow(rowEndLanding), ClampColumn(columnEnd + i)][0]];
                break;
            }
            if (nodeMap[ClampRow(rowEndLanding), ClampColumn(columnEnd - i)].Count != 0 && leftIsOk)
            {
                endNode = graph[nodeMap[ClampRow(rowEndLanding), ClampColumn(columnEnd - i)][0]];
                break;
            }
        }

        //Unsolved bug when strat or end cannot be found
        if (startNode == null || endNode == null)
        {
            return(new List <Vector2>());
        }
        //Instantiate(spikes, new Vector3(columnStart * tileSize.x + mapTranslation.x, (tileMap.GetLength(0) - rowStartLanding) * tileSize.y + mapTranslation.y, 0), Quaternion.identity);
        //Instantiate(spikes, new Vector3(columnEnd * tileSize.x + mapTranslation.x, (tileMap.GetLength(0) - rowEndLanding) * tileSize.y + mapTranslation.y, 0), Quaternion.identity);


        //**********************************************************************************************************************************************************************************************//
        //***********************************r*********************       FIND PATH FROM START TO END NODE        ***************************************************************************************//
        //**********************************************************************************************************************************************************************************************//

        List <int>   open   = new List <int>();
        List <int>   closed = new List <int>();
        List <NodeA> nodesA = new List <NodeA>();
        int          currentNodeAID;
        Node         currentNode;

        //Init nodesA
        for (int i = 0; i < graph.Count; i++)
        {
            nodesA.Add(null);
        }
        //Create first nodeA
        nodesA[startNode.ID]        = new NodeA(startNode, endNode, startNode.ID);
        nodesA[startNode.ID].opened = true;
        currentNodeAID = nodesA[startNode.ID].ID;
        currentNode    = startNode;
        for (int i = 0; i < graph.Count; i++)
        {
            //If currnet node ID equal to end node ID then we found path
            if (currentNodeAID == endNode.ID)
            {
                break;
            }
            //Go through all neighbors
            for (int j = 0; j < currentNode.neighbors.Count; j++)
            {
                //If neighbors is not in open list we create it and fill with data
                if (nodesA[currentNode.neighbors[j].ID] == null)
                {
                    nodesA[currentNode.neighbors[j].ID]        = new NodeA(startNode, endNode, currentNode, nodesA[currentNodeAID], currentNode.neighbors[j].ID);
                    nodesA[currentNode.neighbors[j].ID].opened = true;
                    open.Add(currentNode.neighbors[j].ID);
                }
                //Else we check if my path to this neighbor has lower total cast
                else if (!nodesA[currentNode.neighbors[j].ID].closed)
                {
                    nodesA[currentNode.neighbors[j].ID].TryNewParent(startNode, endNode, currentNode, nodesA[currentNodeAID]);
                }
            }
            //Closing current node and adding it to closed list
            nodesA[currentNodeAID].closed = true;
            closed.Add(nodesA[currentNodeAID].ID);
            //Find new current node by choosing first nodeA with smallest "total" value from open list
            double minTotal = Double.MaxValue;
            int    minID    = -1;
            for (int j = 0; j < open.Count; j++)
            {
                if (minTotal > nodesA[open[j]].total)
                {
                    minID    = j;
                    minTotal = nodesA[open[j]].total;
                }
            }
            //Code below can render all steps that A*took
            if (i != 0)
            {
                Debug.DrawLine(new Vector3((currentNode.column) * tileSize.x, (tileMap.GetLength(0) - (currentNode.row)) * tileSize.y, 0),
                               new Vector3((graph[nodesA[currentNodeAID].parent.ID].column) * tileSize.x, (tileMap.GetLength(0) - graph[nodesA[currentNodeAID].parent.ID].row) * tileSize.y, 0),
                               Color.black, 1, false);
            }

            if (minID == -1 || open.Count == 0)
            {
                return(path);
            }
            currentNodeAID = nodesA[open[minID]].ID;
            currentNode    = graph[currentNodeAID];
            open.RemoveAt(minID);
        }
        List <Vector2> pathTmp = new List <Vector2>();

        while (currentNodeAID != startNode.ID)
        {
            NodeA nextNodeA = nodesA[currentNodeAID].parent;
            Debug.DrawLine(new Vector3((graph[currentNodeAID].column) * tileSize.x, (tileMap.GetLength(0) - (graph[currentNodeAID].row)) * tileSize.y, 0),
                           new Vector3((graph[nextNodeA.ID].column) * tileSize.x, (tileMap.GetLength(0) - (graph[nextNodeA.ID].row)) * tileSize.y, 0),
                           Color.white, 1, false);
            currentNodeAID = nodesA[currentNodeAID].parent.ID;
            pathTmp.Add(new Vector2(graph[currentNodeAID].column * tileSize.x,
                                    (tileMap.GetLength(0) - graph[currentNodeAID].row) * tileSize.y));
        }

        //PrintGraph();
        //Reverce path
        for (int i = 0; i < pathTmp.Count; i++)
        {
            path.Add(pathTmp[pathTmp.Count - 1 - i]);
        }
        return(path);
    }
Ejemplo n.º 25
0
 public bool Equals(EdgeDistance other)
 {
     return(((NodeA.Equals(other.NodeA) && NodeB.Equals(other.NodeB)) ||
             (NodeA.Equals(other.NodeB) && NodeB.Equals(other.NodeA))) &&
            Distance.Equals(other.Distance));
 }