private void Update()
    {
        if (placing == trap)
        {
            info.text = "Tile Info:\n" + "Trap\nCost: " + traptile.GetTileCost() + "\nDamage: " + traptile.GetTileDamage();
        }
        else if (placing == magic)
        {
            info.text = "Tile Info:\n" + "Magic\nCost: " + magictile.GetTileCost() + "\nDamage: " + magictile.GetTileDamage();
        }
        else if (placing == enemy)
        {
            info.text = "Tile Info:\n" + "Enemy\nCost: " + enemytile.GetTileCost() + "\nDamage: " + enemytile.GetTileDamage();
        }


        Vector3    pos        = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3Int coordinate = grid.WorldToCell(pos);

        if (Input.GetMouseButton(0))
        {
            if (!deleting)
            {
                if (Path.GetTile(coordinate) && !obstacle.GetTile(coordinate))
                {
                    obstacle.SetTile(coordinate, placing);

                    numActions++;

                    if (lastDeleted)
                    {
                        lastDeleted = false;
                    }

                    if (placing == trap)
                    {
                        list.Add(new Action(coordinate, new TrapTile()));
                        man.usedMoney += list[numActions - 1].tile.GetTileCost();
                        trapSound.Play();
                    }
                    else if (placing == magic)
                    {
                        list.Add(new Action(coordinate, new MagicTile()));
                        man.usedMoney += list[numActions - 1].tile.GetTileCost();
                        magicSound.Play();
                    }
                    else
                    {
                        list.Add(new Action(coordinate, new EnemyTile()));
                        man.usedMoney += list[numActions - 1].tile.GetTileCost();
                        enemySound.Play();
                    }
                    undone.Clear();
                }
            }
            else
            {
                if (obstacle.GetTile(coordinate))
                {
                    obstacle.SetTile(coordinate, null);
                    numActions--;
                    lastDeleted = true;


                    for (int i = 0; i < list.Count; i++)
                    {
                        if (!obstacle.GetTile(list[i].position))
                        {
                            if (list[i].tile.GetTileType() == TileBase.tileType.trap)
                            {
                                man.usedMoney -= list[i].tile.GetTileCost();
                            }
                            else if (list[i].tile.GetTileType() == TileBase.tileType.magic)
                            {
                                man.usedMoney -= list[i].tile.GetTileCost();
                            }
                            else
                            {
                                man.usedMoney -= list[i].tile.GetTileCost();
                            }

                            undone.Push(list[i]);
                            list.RemoveAt(i);
                        }
                    }
                }
            }
        }

        if (Wall.GetTile(coordinate))
        {
            if (deleting)
            {
                Misc.SetTile(coordinate, deleteGlow);
            }
            else
            {
                Misc.SetTile(coordinate, placeGlow);
            }
        }
        if (oldCoord != coordinate)
        {
            Misc.SetTile(oldCoord, null);
            oldCoord = coordinate;
        }
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        if (placing == trap)
        {
            info.text = "Tile Info:" + "Trap\nCost: " + traptile.GetTileCost() + "\nDamage: " + traptile.GetTileDamage() + "\nRogues take Half";
        }
        else if (placing == magic)
        {
            info.text = "Tile Info:" + "Magic\nCost: " + magictile.GetTileCost() + "\nDamage: " + magictile.GetTileDamage() + "\nWizards take Half";
        }
        else if (placing == enemy)
        {
            info.text = "Tile Info:" + "Enemy\nCost: " + enemytile.GetTileCost() + "\nDamage: " + enemytile.GetTileDamage() + "\nFighters take Half";
        }

        if (GameManager.current == GameManager.GameState.placing)
        {
            mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            hit      = Physics2D.Raycast(mousePos, Vector2.zero, Mathf.Infinity, paths);
            tile     = Physics2D.Raycast(mousePos, Vector2.zero, Mathf.Infinity, placable);
        }
        if (hit)
        {
            if (glow == null)
            {
                if (deleting)
                {
                    glow = Instantiate(deleteGlow, hit.collider.transform.position, Quaternion.identity);
                }
                else
                {
                    glow = Instantiate(placeGlow, hit.collider.transform.position, Quaternion.identity);
                }
            }
            else if (glow.transform.position != hit.collider.gameObject.transform.position)
            {
                Destroy(glow);
                if (deleting)
                {
                    glow = Instantiate(deleteGlow, hit.collider.transform.position, Quaternion.identity);
                }
                else
                {
                    glow = Instantiate(placeGlow, hit.collider.transform.position, Quaternion.identity);
                }
            }

            if (!deleting)
            {
                if (Input.GetMouseButton(0) && !placed.Contains(hit.collider.transform.position) && placing != null)
                {
                    numActions++;

                    if (lastDeleted)
                    {
                        lastDeleted = false;
                    }


                    if (placing == trap)
                    {
                        man.usedMoney += traptile.GetTileCost();
                        if (TrapSound != null)
                        {
                            TrapSound.Play();
                        }
                    }
                    else if (placing == magic)
                    {
                        man.usedMoney += magictile.GetTileCost();
                        if (MagicSound != null)
                        {
                            MagicSound.Play();
                        }
                    }
                    else if (placing == enemy)
                    {
                        man.usedMoney += enemytile.GetTileCost();
                        if (EnemySound != null)
                        {
                            EnemySound.Play();
                        }
                    }

                    actions.Push(Instantiate(placing, hit.collider.transform.position, Quaternion.identity));
                    placed.Add(hit.collider.transform.position);

                    for (int i = 0; i < undoneCount; i++)
                    {
                        Destroy(undone.Pop());
                    }
                    undoneCount = 0;
                    actions.TrimExcess();
                }
            }
            else
            {
                if (Input.GetMouseButton(0) && tile)
                {
                    numActions--;
                    undoneCount++;
                    GameObject holder = tile.collider.gameObject;
                    placed.Remove(hit.collider.transform.position);
                    undone.Push(holder);
                    holder.transform.GetChild(0).GetComponent <SpriteRenderer>().enabled = false;
                    holder.transform.GetChild(1).GetComponent <SpriteRenderer>().enabled = false;
                    holder.GetComponent <BoxCollider2D>().enabled = false;
                    lastDeleted = true;
                    actions.TrimExcess();
                    SellSound.Play();
                    if (holder.name == "Trap(Clone)")
                    {
                        man.usedMoney -= traptile.GetTileCost();
                    }
                    else if (holder.name == "Magic(Clone)")
                    {
                        man.usedMoney -= magictile.GetTileCost();
                    }
                    else if (holder.name == "Enemy(Clone)")
                    {
                        man.usedMoney -= enemytile.GetTileCost();
                    }
                }
            }
        }
        else
        {
            if (glow != null)
            {
                Destroy(glow);
            }
        }
    }