109 lines
3.9 KiB
C#
109 lines
3.9 KiB
C#
using System.ComponentModel;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class GameObjectPlacementButton : UnitPlacementButton, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
private TooltipManager _tooltipManager;
|
|
[SerializeField]
|
|
private GameObject _prefab;
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
_tooltipManager = TooltipManager.Instance;
|
|
}
|
|
|
|
protected override bool CanPlace()
|
|
{
|
|
var isBuilding = _prefab.GetComponent<Building>();
|
|
var defaultPopCost = GlobalConfig.Instance.Current.populationCostPerUnit;
|
|
var hasEnoughPopulation = isBuilding || ResourceManager.Instance.EnoughPopulationFor(defaultPopCost);
|
|
return ResourceManager.Instance.EnoughFor(_unitCardInformation.Rock, _unitCardInformation.Wood, _unitCardInformation.Food)
|
|
&& hasEnoughPopulation
|
|
&& base.CanPlace();
|
|
}
|
|
|
|
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);
|
|
if(instance.GetComponentsInChildren<Animator>().Length > 0) instance.GetComponentInChildren<Animator>().enabled = false;
|
|
|
|
//foreach(var animator in instance.GetComponentInChildren<Animator>().runtimeAnimatorController.animationClips)
|
|
//{
|
|
// //animator
|
|
// foreach(var animEvent in animator.events)
|
|
// {
|
|
// //animEvent = null;
|
|
// }
|
|
//}
|
|
|
|
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 = 1;
|
|
rend.color = new Color(1, 1, 1, 0.2f);
|
|
|
|
detection.transform.SetParent(placeholder.transform);
|
|
detection.transform.localPosition = detectionRect.position;
|
|
detection.transform.localScale = detectionRect.size;
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
Ally go = _prefab.GetComponent<Ally>();
|
|
if (_unitCardInformation.TooltipString.Length > 0)
|
|
{
|
|
_tooltipManager.ShowTooltip(_unitCardInformation.TooltipString, go.Hp.ToString(), go.AttackDamage.ToString(), go.AttackInterval.ToString());
|
|
}
|
|
else
|
|
{
|
|
_tooltipManager.ShowTooltip("Exemple de description d'unité pour " + _unitCardInformation.name.ToLower(), go.Hp.ToString(), go.AttackDamage.ToString(), go.AttackInterval.ToString());
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
_tooltipManager.HideToolTip();
|
|
}
|
|
} |