Example #1
0
    protected override void OnPrimaryItemPerformed(InputAction.CallbackContext _context)
    {
        base.OnPrimaryItemPerformed(_context);

        spellCast?.CastSpell();
    }
    void Update()
    {
        //Mouse/ray setup

        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;


        if (Physics.Raycast(ray, out hitInfo))
        {
            hitObject = hitInfo.transform.gameObject;

            if (hitObject.CompareTag("Tile"))
            {
                //if (selected)
                //    selected.GetComponent<Renderer>().material = selected.BaseMaterial;
                selected = hitObject.gameObject.GetComponent <Tile>();

                if (selected.myType == Tile.BlockType.BaseBlock && selected != previousTile)
                {
                    // Debug.Log("Tämä on baseblock");


                    if (previousTile)
                    {
                        Renderer pr = previousTile.GetComponent <Renderer>();
                        pr.material  = previousTile.GetComponent <Tile>().BaseMaterial;
                        previousTile = null;
                        if (targetedTiles != null)
                        {
                            ResetTileMaterials(targetedTiles);
                            targetedTiles = null;
                            ChangeTileMaterials(rangeTiles, rangeMaterial);
                            ChangeTileMaterials(nullTiles, rangeNullMaterial);
                        }
                    }
                    previousTile = selected;
                    Renderer sr = selected.GetComponent <Renderer>();
                    sr.material = hovermaterial;
                }
            }
            //Jos hovertile on jotain muuta, resettaa hovertilet
            if (!hitObject.CompareTag("Tile") || (hitObject.CompareTag("Tile") && selected.myType != Tile.BlockType.BaseBlock))
            {
                if (previousTile)
                {
                    Renderer pr = previousTile.GetComponent <Renderer>();
                    pr.material  = previousTile.GetComponent <Tile>().BaseMaterial;
                    previousTile = null;

                    if (targetedTiles != null)
                    {
                        ResetTileMaterials(targetedTiles);
                        targetedTiles = null;
                        ChangeTileMaterials(rangeTiles, rangeMaterial);
                        ChangeTileMaterials(nullTiles, rangeNullMaterial);
                    }
                }
            }
        }

        if (hitInfo.transform == null)
        {
            selected = null;
        }

        // kun spell nappulaa on painettu
        if (spellCast.spellOpen == true)
        {
            if (currentMovement)
            {
                ResetTileMaterials(tilesToBeReset);
                tilesToBeReset.Clear();
                rangeTilesPainted = false;
            }
            //pitäisi maalata target range
            if (rangeTiles == null)
            {
                if (spellCast.currentSpell.needLineOfSight == true)
                {
                    nullTiles = abilities.RangeType(spellCast.currentSpell.mySpellRangeType, true);
                    foreach (var tile in nullTiles)
                    {
                        Renderer aR = tile.GetComponent <Renderer>();
                        aR.material = rangeNullMaterial;
                    }

                    rangeTiles = abilities.RangeType(spellCast.currentSpell.mySpellRangeType, false);
                    foreach (var tile in rangeTiles)
                    {
                        Renderer aR = tile.GetComponent <Renderer>();
                        aR.material = rangeMaterial;
                    }
                }
                else
                {
                    rangeTiles = abilities.RangeType(spellCast.currentSpell.mySpellRangeType, false);
                    foreach (var tile in rangeTiles)
                    {
                        Renderer aR = tile.GetComponent <Renderer>();
                        aR.material = rangeMaterial;
                    }
                }
            }

            //pitäisi maalata AOE tilet
            targetedTiles = abilities.AreaType(spellCast.currentSpell.mySpellAreaType);
            foreach (var tile in rangeTiles)
            {
                if (tile == selected)
                {
                    ChangeTileMaterials(targetedTiles, targetMaterial);
                }
            }

            // kun spell castataan
            if (Input.GetMouseButtonDown(0) && !stillMoving && spellCast.currentSpell != null)
            {
                if (abilities.CheckCastability(spellCast.currentSpell, selected) == true)
                {
                    if (targetedTiles != null)
                    {
                        spellCast.CastSpell(spellCast.currentSpell, playerBehaviour.currentCharacter, selected);
                        foreach (var tile in rangeTiles)
                        {
                            Renderer tr = tile.GetComponent <Renderer>();
                            tr.material = tile.GetComponent <Tile>().BaseMaterial;
                        }
                        rangeTiles = null;
                        foreach (var target in targetedTiles)
                        {
                            Renderer ar = target.GetComponent <Renderer>();
                            ar.material = target.GetComponent <Tile>().BaseMaterial;
                        }
                        targetedTiles = null;
                        spellCast.Aftermath();
                    }
                    else
                    {
                        spellCast.SpellCancel();
                    }
                }
                else
                {
                    Debug.Log("cannot be cast");
                    spellCast.SpellCancel();
                }
            }


            // spell cansellataan
            if (Input.GetMouseButtonDown(1))
            {
                //if (rangeTiles != null)
                //{
                //    ResetTileMaterials(rangeTiles);
                //    rangeTiles = null;
                //}
                //if (targetedTiles != null)
                //{
                //    ResetTileMaterials(targetedTiles);
                //    targetedTiles = null;
                //}
                spellCast.SpellCancel();
            }
            movementEnabled = false;
        }
        else
        {
            movementEnabled = true;
        }

        if (movementEnabled)
        {
            if (!currentMovement)
            {
                Debug.Log("Movement still not set!");
            }

            if (!rangeTilesPainted || selected == null)
            {
                ResetTileMaterials(tilesToBeReset);
                tilesToBeReset.Clear();
                movementRangeTiles = currentMovement.TilesInRange();
                ChangeTileMaterials(movementRangeTiles, movementMaterial);
                tilesToBeReset.AddRange(movementRangeTiles);
                rangeTilesPainted = true;
            }

            ChangeTileMaterials(movementRangeTiles, movementMaterial);  //otherwise hovering over resets the tiles
            if (movementRangeTiles.Contains(selected))
            {
                //Hae reitti ja maalaa se
                PlayerMovement.PathTile tempTest = currentMovement.pathTiles.Where(x => x._tile == selected).FirstOrDefault();
                if (tempTest != null)
                {
                    tilesInPath = PlayerMovement.CalculateRouteBack(tempTest);
                    ChangeTileMaterials(tilesInPath, pathMaterial);
                    Renderer sr = selected.GetComponent <Renderer>();
                    sr.material = pathSelectedMaterial;
                    huijaus     = tempTest;
                }
                else
                {
                    huijaus = null;
                }
            }

            if (Input.GetMouseButtonDown(0) &&
                !stillMoving &&
                selected != currentMovement.CurrentTile &&
                movementRangeTiles.Contains(selected))
            {
                if (currentMovement.MoveToTile(selected, PlayerMovement.MovementMethod.Walk, huijaus))
                {
                    if (!currentMovement.unlimitedMovementPoints)
                    {
                        currentMovement.playerInfo.thisCharacter.currentMp -= (tilesInPath.Count() - 2);
                    }
                }

                ResetTileMaterials(tilesToBeReset);
                tilesToBeReset.Clear();
                rangeTilesPainted = false;
                spellCast.UpdateHpApMp();
            }
        }
    }