Esempio n. 1
0
        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 (spellButtons.spellOpen == true)
            {
                if (currentMovement)
                {
                    ResetTileMaterials(tilesToBeReset);
                    tilesToBeReset.Clear();
                    rangeTilesPainted = false;
                }


                //pitäisi maalata target range
                if (rangeTiles == null)
                {
                    if (spellButtons.currentSpell.needLineOfSight == true)
                    {
                        nullTiles = spellHandler.RangeType(spellButtons.currentSpell.rangeType, true);
                        foreach (var tile in nullTiles)
                        {
                            Renderer aR = tile.GetComponent <Renderer>();
                            aR.material = rangeNullMaterial;
                        }

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

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

                //kun spell castataan
                if (Input.GetMouseButtonDown(0) && !stillMoving && spellButtons.currentSpell != null)
                {
                    if (spellChecker.CheckCastability(spellButtons.currentSpell, selected) == true)
                    {
                        if (targetedTiles != null)
                        {
                            spellCasting.CastSpell(spellButtons.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;
                            spellCasting.Aftermath();
                        }
                        else
                        {
                            spellButtons.SpellCancel();
                        }
                    }
                    else
                    {
                        Debug.Log("cannot be cast");
                        spellButtons.SpellCancel();
                    }
                }


                // spell cansellataan
                if (Input.GetMouseButtonDown(1))
                {
                    if (rangeTiles != null)
                    {
                        ResetTileMaterials(rangeTiles);
                        rangeTiles = null;
                    }
                    if (targetedTiles != null)
                    {
                        ResetTileMaterials(targetedTiles);
                        targetedTiles = null;
                    }
                    spellButtons.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;
                    spellButtons.UpdateHpApMp();
                }
            }
        }