Beispiel #1
0
    void ReturnToPlayer()
    {
        if (selectedTurnips.Count == 0)
        {
            for (int i = 0; i < allTurnips.transform.childCount; i++)
            {
                TurnipBehaviour tb = allTurnips.transform.GetChild(i).GetComponent <TurnipBehaviour>();
                if (tb.turnipState != TurnipState.FOLLOWING_PLAYER && tb.turnipState != TurnipState.WAITING_FOR_PLAYER)
                {
                    tb.SetState_FollowPlayer();
                }
            }
        }

        else
        {
            for (int i = 0; i < selectedTurnips.Count; i++)
            {
                if (selectedTurnips[i] == null)
                {
                    continue;
                }
                TurnipBehaviour tb = selectedTurnips[i].GetComponent <TurnipBehaviour>();
                if (tb.turnipState != TurnipState.FOLLOWING_PLAYER && tb.turnipState != TurnipState.WAITING_FOR_PLAYER)
                {
                    tb.SetState_FollowPlayer();
                }
            }
        }
    }
Beispiel #2
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Enemy"))
        {
            Destroy(gameObject);
            GameObject p = Instantiate(deadParticles, gameObject.transform.position, gameObject.transform.rotation);
            GetComponent <Collider2D>().enabled = false;
            Debug.Log("Nabo destroyed");
            GameObject.FindGameObjectWithTag("Player").GetComponent <PlayerStats>().TakeNabos(-1);
            Destroy(p, 3);
            Instantiate(seed, gameObject.transform.position, gameObject.transform.rotation);
        }

        if (turnipState == TurnipState.FOLLOWING_PLAYER && collision.collider.CompareTag("Turnip"))
        {
            TurnipBehaviour tb = collision.gameObject.GetComponent <TurnipBehaviour>();

            if (tb.turnipState == TurnipState.WAITING_FOR_PLAYER)
            {
                // Debug.Log("forceStop");
                pathfinding.Stop();
            }
        }
    }
Beispiel #3
0
    // Update is called once per frame
    void Update()
    {
        //Are we clicking with left mouse or holding down left mouse
        bool isClicking    = false;
        bool isHoldingDown = false;

        Vector3      mousePos = main.ScreenToWorldPoint(Input.mousePosition);
        RaycastHit2D hit      = Physics2D.Raycast(mousePos, Vector2.zero, 0, layerForPoint);

        if (hit.collider != null)
        {
            if (hover != null)
            {
                hover.GetChild(0).gameObject?.SetActive(false);
                hover = null;
            }

            GameObject selectionMark = hit.transform.GetChild(0).gameObject;

            TurnipBehaviour tb = hit.transform.GetComponent <TurnipBehaviour>();
            if (tb == null || tb.selected == false)
            {
                selectionMark.SetActive(true);
                hover = hit.transform;
            }
        }
        else if (hover != null)
        {
            hover.GetChild(0).gameObject?.SetActive(false);
            hover = null;
        }

        //GetFirstDown
        if (Input.GetMouseButtonDown(0))
        {
            clickTime = Time.time;

            squareStartPos = main.ScreenToWorldPoint(Input.mousePosition);

            UnselectAll();
        }

        //Holding down the mouse button
        if (Input.GetMouseButton(0))
        {
            if (Time.time - clickTime > delay)
            {
                isHoldingDown = true;
            }
        }

        //Releasing the button
        if (Input.GetMouseButtonUp(0))
        {
            if (Time.time - clickTime <= delay)
            {
                if (hover != null)
                {
                    if (hover.CompareTag("Turnip"))
                    {
                        SelectTurnip(hover.gameObject);

                        return;
                    }

                    if (hover.CompareTag("PlantableTile"))
                    {
                        ManageTile(hover.gameObject.GetComponent <PlantableTileController>());
                        return;
                    }
                }
            }

            if (hasCreatedSquare)
            {
                hasCreatedSquare = false;

                //Deactivate the square selection image
                selectionSquareTrans.gameObject.SetActive(false);

                //Clear the list with selected unit
                UnselectAll();

                Vector3[] corners = new Vector3[4];
                selectionSquareTrans.GetWorldCorners(corners);

                Vector2 upCorner   = main.ScreenToWorldPoint(corners[1]);
                Vector2 downCorner = main.ScreenToWorldPoint(corners[3]);

                Vector2 size = downCorner - upCorner;
                size.x = Mathf.Abs(size.x);
                size.y = Mathf.Abs(size.y);

                Vector2 origin = Vector2.zero;
                origin = main.ScreenToWorldPoint(selectionSquareTrans.transform.position);

                RaycastHit2D[] hits = Physics2D.BoxCastAll(origin, size, 0, Vector2.zero, 0, layerForSquare);

                for (int i = 0; i < hits.Length; i++)
                {
                    if (hits[i].collider.CompareTag("Turnip"))
                    {
                        SelectTurnip(hits[i].collider.gameObject);
                    }
                }
            }
        }

        //Drag the mouse to select all units within the square
        if (isHoldingDown)
        {
            //Activate the square selection image
            if (!selectionSquareTrans.gameObject.activeInHierarchy)
            {
                selectionSquareTrans.gameObject.SetActive(true);
            }

            //Get the latest coordinate of the square
            squareEndPos = Input.mousePosition;

            //Display the selection with a GUI image
            DisplaySquare();
            return;
        }

        if (Input.GetMouseButton(1))
        {
            if (hover && hover.CompareTag("Enemy"))
            {
                SetDestinationForSelected(hit.collider.transform);
                return;
            }

            mousePos = main.ScreenToWorldPoint(Input.mousePosition);
            hit      = Physics2D.Raycast(mousePos, Vector2.zero);

            if (hit.collider == null)
            {
                if (selectedTurnips.Count > 0)
                {
                    SetDestinationForSelected(mousePos);
                    return;
                }
            }

            return;
        }

        if (Input.GetMouseButtonDown(2))
        {
            ReturnToPlayer();
        }
    }