Example #1
0
    //interraction avec l'objet sur lequel l'utilisateur clique
    void interract(Vector2 screenPosition)
    {
        RaycastHit2D hit = Physics2D.Raycast(screenPosition, Vector2.zero);

        if (hit.transform != null && hit.transform.gameObject.layer == 5)
        {
            return;                                                                  //use to prevent from clickink through UI
        }
        StopCoroutine("move");
        deselection();
        //rotateToTarget(screenPosition);
        if (hit.collider != null)
        {
            playerGridPos = new Vector2(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y));
            Vector2 hitGridPos = new Vector2(Mathf.Round(hit.collider.transform.position.x), Mathf.Round(hit.collider.transform.position.y));

            if (hit.collider.tag == "Destructible" &&         //check if a destructible is hit
                Vector2.Distance(hit.collider.transform.position, gameObject.transform.position) <= maxInteractionDistance)                   //check if the distance between the play and the object is correct
            {
                if (hit.collider.gameObject.GetComponent <OreVein> () != null)
                {
                    OreVein oreVein = hit.collider.gameObject.GetComponent <OreVein> ();
                    oreVein.damage(digDamages [oreVein.getType()]);
                }
                else
                {
                    hit.collider.gameObject.GetComponent <Destructible> ().damage(digDamagesToRocks);
                }
                rotateToTarget(hit.collider.transform.position);
                audioSource.PlayOneShot(digSound);
                endPlaceBuilding();
            }
            else if ((hit.collider.tag == "Floor" && placingObject) &&           //building construction
                     Vector2.Distance(hit.collider.transform.position, gameObject.transform.position) <= maxConstructionDistance &&               //check if the distance between the play and the object is correct
                     (Mathf.Abs(playerGridPos.x - hitGridPos.x) > 0.01f || Mathf.Abs(playerGridPos.y - hitGridPos.y) > 0.01f))                      //check if the selected case is not player's case
            {
                if (enoughRessources(buildingToPlace.GetComponent <Building>().getCosts()))
                {
                    mapManager.placeBuilding(buildingToPlace, hit.collider.transform.position);
                }
                else
                {
                    PopupMessage.instance.ShowMessage("Not enough ressources");
                }
                //endPlaceBuilding ();
            }
            else if ((hit.collider.tag == "Item") &&          //item collection
                     Vector2.Distance(hit.collider.transform.position, gameObject.transform.position) <= maxInteractionDistance)                 //check if the distance between the play and the object is correct
            {
                if (hit.collider.gameObject.GetComponent <Item> ().collect())
                {
                    audioSource.PlayOneShot(collectSound);
                }
            }
            else if (hit.collider.tag == "Building" && !placingObject)              //building selection
            {
                selectedTile    = hit.collider.gameObject;
                buildingToPlace = null;
                if (itemSlot != null && itemSlot.GetComponent <UsableItem> () != null &&
                    selectedTile.GetComponent <Building> ().hasUpgrade(itemSlot.GetComponent <UsableItem> ().type))
                {
                    buildingToPlace = selectedTile;
                }
                //UIManager.instance.getCostsBar().updateDisplay ();
                selectedTile.GetComponent <Building> ().selection();
                if (OnSelectBuildingEvent != null)
                {
                    OnSelectBuildingEvent();
                }
            }
            else
            {
                Debug.Log("Rien touché");
            }
            if (placingObject)
            {
                endPlaceBuilding();
            }
        }
    }