Advertisement
SHenryL

RealmRush - Tiles

Sep 15th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Tile : MonoBehaviour
  6. {
  7.     [SerializeField] Tower towerPrefab;
  8.  
  9.    
  10.     [SerializeField] bool isPlaceable;
  11.     public bool IsPlaceable  { get{return isPlaceable;}}// this is a property, nothing inside gets altered from outside
  12.     // The property is grouped next to the isPlaceable field for neatness.
  13.  
  14.     GridManager gridManager;
  15.     Vector2Int coordinates = new Vector2Int();
  16.     void Awake()
  17.     {
  18.         gridManager = FindObjectOfType<GridManager>();
  19.     }
  20.  
  21.     void Start()
  22.     {
  23.         if(gridManager != null)
  24.         {
  25.             coordinates = gridManager.GetCoordinatesFromPosition(transform.position);
  26.  
  27.             if(!isPlaceable)
  28.             {
  29.                 gridManager.BlockNode(coordinates);
  30.             }
  31.         }
  32.     }
  33.  
  34.     void OnMouseDown() {
  35.         if(isPlaceable)
  36.         {
  37.             bool isPlaced =  towerPrefab.CreateTower(towerPrefab, transform.position);
  38.             isPlaceable = !isPlaced;
  39.         }
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement