Added placing buildings
This commit is contained in:
46
Assets/Scripts/Game/Buildings/BuildingManager.cs
Normal file
46
Assets/Scripts/Game/Buildings/BuildingManager.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Fruitomation.Game
|
||||
{
|
||||
public class BuildingManager : MonoBehaviour
|
||||
{
|
||||
private class BuildingInfo
|
||||
{
|
||||
public BuildingInfo(BuildingBase b, RectTransform rt)
|
||||
{
|
||||
Building = b;
|
||||
Rect = rt;
|
||||
}
|
||||
|
||||
public BuildingBase Building { get; }
|
||||
public RectTransform Rect { get; }
|
||||
}
|
||||
|
||||
private bool[,] InhabitedCells { get; } = new bool[96, 49];
|
||||
private List<BuildingInfo> Buildings { get; } = new();
|
||||
|
||||
public bool AddBuildingAt(Vector2Int position, GameObject prefab, bool updateCellMap = false)
|
||||
{
|
||||
GameObject go = Instantiate(prefab, transform);
|
||||
BuildingInfo info = new
|
||||
(
|
||||
go.GetComponent<BuildingBase>(),
|
||||
go.GetComponent<RectTransform>()
|
||||
);
|
||||
|
||||
Vector2Int p0 = position * 40;
|
||||
Vector2Int p1 = p0 + new Vector2Int(20, 100);
|
||||
|
||||
Vector2 off0 = info.Building.SizeOnGrid - Vector2.one;
|
||||
Vector2 off1 = off0 * 20;
|
||||
|
||||
info.Rect.sizeDelta = info.Building.SizeOnGrid * 40;
|
||||
info.Rect.anchoredPosition = p1 + off1;
|
||||
|
||||
Buildings.Add(info);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Game/Buildings/BuildingManager.cs.meta
Normal file
3
Assets/Scripts/Game/Buildings/BuildingManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b26cbb6d9a64e44ad6f1b2022ed62fe
|
||||
timeCreated: 1774967593
|
||||
@@ -19,6 +19,7 @@ namespace Fruitomation.UI
|
||||
[SerializeField] private RectTransform RectTransform;
|
||||
[SerializeField] private Image BuildingPreview;
|
||||
[SerializeField] private Image CursorImage;
|
||||
[SerializeField] private BuildingManager BuildingManager;
|
||||
|
||||
[Header("Dev")]
|
||||
[SerializeField] private GameObject BuildingPrefab;
|
||||
@@ -156,6 +157,12 @@ namespace Fruitomation.UI
|
||||
Vector2 o1 = o0 * 20;
|
||||
Vector2 p4 = p3 + o1;
|
||||
BuildingPreview.rectTransform.anchoredPosition = p4;
|
||||
|
||||
//
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
BuildingManager.AddBuildingAt(p1, BuildingPrefab);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user