Exemplo n.º 1
0
    void OnMouseOver()
    {
        if ((Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) && !gameManager.isGameOver)
        {
            isSelected = true;

            foreach (UnitScript unitScript in gameManager.unitScriptList)
            {
                if (unitScript.isSelected && !unitScript.hasAttacked && !unitScript.isAbilityUsed && currentUnit != null && isTileTaken)
                {
                    RaycastHit2D[] hits = Physics2D.RaycastAll(currentUnit.transform.position, unitScript.gameObject.transform.position);

                    foreach (RaycastHit2D hit in hits)
                    {
                        TileScript tileScriptHit = null;

                        if (hit.collider.tag == "Tile" || hit.collider.tag == "P1BaseTile" || hit.collider.tag == "P2BaseTile")
                        {
                            tileScriptHit = hit.collider.gameObject.GetComponent <TileScript>();

                            if (tileScriptHit.currentUnit != null) // Controllo sull'unità
                            {
                                if (unitScript.gameObject != currentUnit.gameObject)
                                {
                                    if ((Input.GetMouseButtonDown(0) /*&& Input.GetKey(KeyCode.A)*/))
                                    {
                                        if (tileScriptHit.currentUnit.ownerIndex != unitScript.ownerIndex)
                                        {
                                            StartCoroutine(tileScriptHit.currentUnit.GetDamage(unitScript, tileScriptHit));
                                            break;
                                        }
                                    }
                                    else if (Input.GetMouseButtonDown(1))
                                    {
                                        if (unitScript.roleIndex == 5 && !unitScript.isAbilityInCooldown)
                                        {
                                            StartCoroutine(tileScriptHit.currentUnit.AbilitySwap(unitScript, tileScriptHit, GetComponent <TileScript>()));
                                        }
                                        else if (tileScriptHit.currentUnit.ownerIndex != unitScript.ownerIndex) // Avversario
                                        {
                                            if (unitScript.roleIndex == 4 && !unitScript.isAbilityInCooldown)
                                            {
                                                StartCoroutine(tileScriptHit.currentUnit.AbilityStun(unitScript));
                                            }
                                            else if (unitScript.roleIndex == 2 && !unitScript.isAbilityInCooldown)
                                            {
                                                StartCoroutine(tileScriptHit.currentUnit.AbilityCripple(unitScript));
                                            }
                                        }
                                        else if (tileScriptHit.currentUnit.ownerIndex == unitScript.ownerIndex) // Alleato
                                        {
                                            if (unitScript.roleIndex == 3 && !unitScript.isAbilityInCooldown)
                                            {
                                                StartCoroutine(tileScriptHit.currentUnit.AbilityCure(unitScript));
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (Input.GetMouseButtonDown(1))
                                    {
                                        if (unitScript.roleIndex == 1 && !unitScript.isAbilityInCooldown)
                                        {
                                            StartCoroutine(currentUnit.AbilityInvisibility(unitScript));
                                        }
                                        else if (unitScript.roleIndex == 0 && !unitScript.isAbilityInCooldown)
                                        {
                                            StartCoroutine(currentUnit.AbilityRetaliation(unitScript));
                                        }
                                        else if (unitScript.roleIndex == 3 && !unitScript.isAbilityInCooldown)
                                        {
                                            StartCoroutine(currentUnit.AbilityCure(unitScript));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (unitScript.isSelected && unitScript.currentMoveCount > 0 && isInRange && !Input.GetMouseButtonDown(1))
                {
                    foreach (TileScript tile in gameManager.tileScriptList)
                    {
                        tile.isInRange = false;
                    }

                    if (!soundsAudioSource.isPlaying)
                    {
                        soundsAudioSource.clip = movementSoundEffect;
                        soundsAudioSource.Play();
                    }

                    Vector3 movementDestinationTemp = transform.position;
                    movementDestinationTemp.y += 0.3f;
                    movementDestinationTemp.z  = -1;

                    unitScript.movementDestination = movementDestinationTemp;
                    unitScript.currentMoveCount   -= 1;

                    isTileTaken          = false;
                    currentUnit          = null;
                    spriteRenderer.color = Color.white;

                    break;
                }

                /*else if (!isInRange)
                 * {
                 *  unitScript.isSelected = false;
                 * }*/
            }
        }
    }