evershade/Assets/Scripts/Components/TileComponent.cs

108 lines
3.0 KiB
C#

using TMPro;
using UnityEngine;
public class TileComponent : MonoBehaviour
{
public int x;
public int y;
public bool isOccupied;
public bool isOccupiedByCrop;
public bool isOccupiedByInteractableItem;
public GridSpace gridSpace;
public TMP_Text coordinatesText;
// Start is called before the first frame update
private void Start()
{
}
public void SetTile(int x, int y)
{
this.x = x;
this.y = y;
}
public void SetOccupied(bool isOccupied)
{
this.isOccupied = isOccupied;
}
public void SetCoordinatesText()
{
coordinatesText.text = x + "x" + y;
}
public void SetOccupiedByCrop(bool isOccupiedByCrop)
{
this.isOccupiedByCrop = isOccupiedByCrop;
}
public void SetOccupiedByInteractableItem(bool isOccupiedByInteractableItem)
{
this.isOccupiedByInteractableItem = isOccupiedByInteractableItem;
}
public void Initialize(GridSpace gSpace)
{
gridSpace = gSpace;
// check if tile is occupied by crop or interactable item
isOccupied = gridSpace != null;
isOccupiedByCrop = gridSpace != null && gridSpace.Crop != null;
isOccupiedByInteractableItem = gridSpace != null && gridSpace.InteractableItem != null;
SetTile(x, y);
SetCoordinatesText();
DrawTileBorder();
}
private void Debug()
{
// add black border around tile and white border around tile
// if tile is occupied by crop or interactable item
if (isOccupied)
{
// add black border around tile
// if tile is occupied by crop
if (isOccupiedByCrop)
{
// add white border around tile
}
// if tile is occupied by interactable item
if (isOccupiedByInteractableItem)
{
// add white border around tile
}
}
}
private void DrawTileBorder()
{
// Create a new GameObject for the border
var border = new GameObject("Border");
// Add a LineRenderer component to the border
var lineRenderer = border.AddComponent<LineRenderer>();
// Set the material of the LineRenderer to the gridMaterial
// lineRenderer.material = gridMaterial;
// Set the positions of the LineRenderer to form a square around the tile
lineRenderer.positionCount = 5;
lineRenderer.SetPositions(new Vector3[]
{
new(transform.position.x, transform.position.y, 0),
new(transform.position.x + GridManager.Instance.tileSize, transform.position.y, 0),
new(transform.position.x + GridManager.Instance.tileSize,
transform.position.y + GridManager.Instance.tileSize, 0),
new(transform.position.x, transform.position.y + GridManager.Instance.tileSize, 0),
new(transform.position.x, transform.position.y, 0)
});
}
}