unity-atoms/Examples/Assets/Common/DecreaseHealth.cs

43 lines
1.0 KiB
C#
Raw Normal View History

2020-03-17 21:04:33 -04:00
using System.Collections.Generic;
2020-03-04 18:48:39 -05:00
using UnityEngine;
using UnityEngine.Assertions;
using UnityAtoms.BaseAtoms;
using UnityAtoms.Tags;
2020-03-17 21:04:33 -04:00
namespace UnityAtoms.Examples
2020-03-04 18:48:39 -05:00
{
2020-03-17 21:04:33 -04:00
public class DecreaseHealth : MonoBehaviour
{
public List<StringConstant> TagsAffected { get => _tags; }
2020-03-04 18:48:39 -05:00
2020-03-17 21:04:33 -04:00
[SerializeField]
private IntReference _decreaseBy;
2020-03-04 18:48:39 -05:00
2020-03-17 21:04:33 -04:00
[SerializeField]
private List<StringConstant> _tags;
2020-03-04 18:48:39 -05:00
2020-03-17 21:04:33 -04:00
[SerializeField]
private VoidBaseEventReference _didCollide;
2020-03-15 19:18:26 -04:00
2020-03-17 21:04:33 -04:00
void Start()
2020-03-04 18:48:39 -05:00
{
2020-03-17 21:04:33 -04:00
Assert.IsNotNull(_decreaseBy);
Assert.IsNotNull(_tags);
2020-03-04 18:48:39 -05:00
}
2020-03-08 07:32:41 -04:00
2020-03-17 21:04:33 -04:00
public void Do(Collider2D collider)
2020-03-08 07:32:41 -04:00
{
2020-03-17 21:04:33 -04:00
if (collider == null) return;
if (collider.gameObject.HasAnyTag(_tags))
{
collider.GetComponent<UnitHealth>().Health -= _decreaseBy;
}
if (_didCollide != null && _didCollide.Event != null)
{
_didCollide.Event.Raise();
}
2020-03-08 07:32:41 -04:00
}
2020-03-04 18:48:39 -05:00
}
2020-03-17 21:04:33 -04:00
}