Пример #1
0
//	void MoveBackToPath(Vector2 _nodePos){
//		if (Vector2.Distance (transform.position, _nodePos) == 0) {
//			GetPath ();
//			movingBackToPath = false;
//		} else {
//
//			transform.position = Vector2.MoveTowards(transform.position, _nodePos ,movementSpeed * Time.deltaTime);
//		}
//	}


    void Update()
    {
        if (currentPath != null)
        {
            int currNode = 0;
            while (currNode < currentPath.Count - 1)
            {
                Vector3 start = resourceGrid.TileCoordToWorldCoord(currentPath [currNode].x, currentPath [currNode].y);
                Vector3 end   = resourceGrid.TileCoordToWorldCoord(currentPath [currNode + 1].x, currentPath [currNode + 1].y);
                ;
                Debug.DrawLine(start, end, Color.blue);
                currNode++;
            }
        }

        if (moving && !isAttacking)
        {
            // Have we moved close enough to the target tile that we can move to next tile in current path?
            if (Vector2.Distance(transform.position, resourceGrid.TileCoordToWorldCoord(posX, posY)) < (0.1))
            {
                MoveToNextTile();
            }
            transform.position = Vector2.MoveTowards(transform.position,
                                                     resourceGrid.TileCoordToWorldCoord(posX, posY),
                                                     mStats.curMoveSpeed * Time.deltaTime);
            // ANIMATION CONTROLS:
            if (posX > transform.position.x)
            {
                anim.SetTrigger("movingRight");
                anim.ResetTrigger("movingLeft");
            }
            else if (posX < transform.position.x)
            {
                anim.SetTrigger("movingLeft");
                anim.ResetTrigger("movingRight");
            }
        }


        // BUDDY SYSTEM CALLED ONLY IF MY BUDDY IS NOT NULL
        if (myBuddy != null)
        {
            BuddySystem();
        }
    }
    void Update()
    {
        // I need to change this later to the script that handles all Player clicking/controls,
        // instead of here - a script that should only control a selected Unit's movement.


        if (isSelected && Input.GetMouseButtonDown(1))
        {
            // If this unit is selected and Player left clicks
            // Store the mouse position in world coords
            Vector3 m = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            mX = Mathf.RoundToInt(m.x);
            mY = Mathf.RoundToInt(m.y);
            // Making sure that we are not trying to path outside the BOUNDARIES of the GRID
            if (mX > resourceGrid.mapSizeX - 1)
            {
                mX = resourceGrid.mapSizeX - 1;
            }
            if (mY > resourceGrid.mapSizeY - 1)
            {
                mY = resourceGrid.mapSizeY - 1;
            }
            if (mX < 0)
            {
                mX = 0;
            }
            if (mY < 0)
            {
                mY = 0;
            }

            // give resource grid this gameobject as unit to path
            resourceGrid.unitOnPath = gameObject;
            // and generate a path using mouse coords ROUNDED as int
            resourceGrid.GenerateWalkPath(mX, mY, true, posX, posY);
            // THIS WILL NOT WORK if ACTUAL tiles BOTTOM LEFT is NOT 0.0!!
            moving     = true;
            isSelected = false;
        }

        if (currentPath != null)
        {
            int currNode = 0;
            while (currNode < currentPath.Count - 1)
            {
                Vector3 start = resourceGrid.TileCoordToWorldCoord(currentPath [currNode].x, currentPath [currNode].y);
                Vector3 end   = resourceGrid.TileCoordToWorldCoord(currentPath [currNode + 1].x, currentPath [currNode + 1].y);
                ;
                Debug.DrawLine(start, end, Color.red);
                currNode++;
            }
        }
        if (moving)
        {
            // Have we moved close enough to the target tile that we can move to next tile in current path?
            if (Vector2.Distance(transform.position, resourceGrid.TileCoordToWorldCoord(posX, posY)) < 1f)
            {
                MoveToNextTile();
            }
            if (Vector2.Distance(transform.position, resourceGrid.TileCoordToWorldCoord(mX, mY)) < 0.1f)
            {
                movingToAttack = false;
                moving         = false;
                anim.SetTrigger("idle");
                anim.ResetTrigger("movingRight");
                anim.ResetTrigger("movingLeft");
                anim.ResetTrigger("attackRight");
                anim.ResetTrigger("attackLeft");
            }
            else
            {
                if (mX > transform.position.x)
                {
                    if (movingToAttack)
                    {
                        anim.SetTrigger("attackRight");
                        anim.ResetTrigger("movingRight");
                        anim.ResetTrigger("idle");
                    }
                    else
                    {
                        anim.SetTrigger("movingRight");
                        anim.ResetTrigger("idle");
                        anim.ResetTrigger("movingLeft");
                        anim.ResetTrigger("attackRight");
                    }
                }
                if (mX < transform.position.x)
                {
                    if (movingToAttack)
                    {
                        anim.SetTrigger("attackLeft");
                        anim.ResetTrigger("movingLeft");
                        anim.ResetTrigger("idle");
                    }
                    else
                    {
                        anim.SetTrigger("movingLeft");
                        anim.ResetTrigger("idle");
                        anim.ResetTrigger("movingRight");
                        anim.ResetTrigger("attackLeft");
                    }
                }
                // DO ANIMATION CALCULATION HERE:
                //				anim.SetTrigger ("moving");
                //				anim.ResetTrigger("idle");
            }

            if (movingToAttack)
            {
                transform.position = Vector2.MoveTowards(transform.position,
                                                         new Vector2(mX, mY),
                                                         movementSpeed * Time.deltaTime);
            }
            else
            {
                // This MOVEMENT will animate towards the next position
                transform.position = Vector2.MoveTowards(transform.position,
                                                         resourceGrid.TileCoordToWorldCoord(posX, posY),
                                                         movementSpeed * Time.deltaTime);
            }
        }
    }