- added art for house and UI - put population in ResourceManager - create house prefab - added code for adding and removing pop depending on entity - refactor harvesters so they inherit from ally - modify placeholders and buttons so that only units cost population - add events for population and resources changing - add population relative configs to global config - add start resources values to Levels - add debug feature for generating resources for free
68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class GameObjectPlacementButton : UnitPlacementButton
|
|
{
|
|
[SerializeField]
|
|
private GameObject _prefab;
|
|
|
|
protected override bool CanPlace()
|
|
{
|
|
var isBuilding = _prefab.GetComponent<Building>();
|
|
return ResourceManager.Instance.EnoughFor(_rock, _wood, _food, isBuilding ? 0 : GlobalConfig.Instance.Current.populationCostPerUnit) && _button.enabled && _canSpawn;
|
|
}
|
|
|
|
protected override DraggablePlaceholder Place()
|
|
{
|
|
var instance = Instantiate(_prefab);
|
|
|
|
//we need to fetch the detection size before stripping the object
|
|
var detectComp = _prefab.GetComponentInChildren<Detection>();
|
|
Rect detectionRect = default;
|
|
if(detectComp) detectionRect = _prefab.GetComponentInChildren<Detection>().DetectionRectangle;
|
|
|
|
//strip the object
|
|
foreach (var r_body in instance.transform.GetAllComponents<Rigidbody2D>())
|
|
Destroy(r_body);
|
|
foreach (var coll in instance.transform.GetAllComponents<Collider2D>())
|
|
Destroy(coll);
|
|
foreach (var script in instance.transform.GetAllComponents<MonoBehaviour>())
|
|
Destroy(script);
|
|
|
|
var placeholder = instance.AddComponent<ObjectPlaceholder>();
|
|
|
|
placeholder.Prefab = _prefab;
|
|
|
|
//assign outline material to all renderers of the placeholder
|
|
foreach (var rend in instance.transform.GetAllComponents<SpriteRenderer>())
|
|
{
|
|
var color = rend.color;
|
|
color.a = 0.6f;
|
|
rend.color = color;
|
|
|
|
rend.material = _outlineMaterial;
|
|
placeholder.OutlineRenderers.Add(rend);
|
|
}
|
|
|
|
CreateRange(placeholder, detectionRect);
|
|
|
|
return placeholder;
|
|
}
|
|
|
|
void CreateRange(DraggablePlaceholder placeholder, Rect detectionRect)
|
|
{
|
|
if (detectionRect == default) return;
|
|
|
|
var detection = new GameObject("Detection");
|
|
|
|
var rend = detection.AddComponent<SpriteRenderer>();
|
|
rend.sprite = _detectionRangeSprite;
|
|
rend.sortingLayerName = "Character";
|
|
rend.sortingOrder = 0;
|
|
rend.color = new Color(1, 1, 1, 0.2f);
|
|
|
|
detection.transform.SetParent(placeholder.transform);
|
|
detection.transform.localPosition = detectionRect.center;
|
|
detection.transform.localScale = detectionRect.size;
|
|
}
|
|
} |