Exemple #1
0
        public void OnEndDrag(PointerEventData eventData)
        {
            if (Tower && allowPlacement)
            {
                // set object to white (default colours) when it is dropped
                Renderer[] rend = Tower.GetComponentsInChildren <Renderer>();
                foreach (Renderer r in rend)
                {
                    r.material.SetColor("_BaseColor", Color.white);
                }

                // once the tower is placed, set the towerObject of the tile (used to decide whether or not towers can connect)
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out RaycastHit hit))
                {
                    // recalculate these variables as the click is released or sometimes on the rare occasion,
                    // the tower is placed on one square, but appears to be on another
                    Vector3 clickInWorldspace = hit.collider.gameObject.transform.position;
                    Vector3 clickInTilespace  = new Vector3(Mathf.RoundToInt(clickInWorldspace.x), 0.0f, Mathf.RoundToInt(clickInWorldspace.z)); // this line is effectively the same as the one above, but when you remove it, conveyors break about 15% of the time.

                    Tower.transform.position = clickInTilespace;
                    Tile t = Tile.Vector3ToTile(clickInTilespace);
                    t.SetTower(Tower);
                    if (Tower.TryGetComponent <Tower>(out Tower towerClassInstance))
                    {
                        towerClassInstance.OnPlaced(t, moneyCost, ironCost);
                        //t.isWalkable = false; // tile can no longer be walked on since there has been a tower placed on it
                    }
                    if (Tower.TryGetComponent <Conveyors.ConveyorManager>(out Conveyors.ConveyorManager conveyorClassInstance))
                    {
                        conveyorClassInstance.OnPlaced(moneyCost, ironCost);
                    }


                    if (!developerMode)
                    {
                        PlayerResources.AddMoney(-moneyCost);
                        PlayerResources.AddIron(-ironCost);
                    }
                }
                Tower = null;
            }
            if (Tower && !allowPlacement)
            {
                Destroy(Tower);
            }
        }
 public void EnemyDeath()
 {
     Destroy(this.gameObject);
     PlayerResources.AddMoney(1);
 }