Felix Boucher 0aa3327433 appliquer global config aux différents endroits
PROBLÈME :

la config existait mais n'était pas appliquée nulle part

SOLUTION :

maintenant elle l'est

NOTES :

Elle n'est pas encore appliquée au flash de dégat
2023-08-05 15:55:54 -04:00

126 lines
3.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Detection : MonoBehaviour
{
private Vector2 detectionRange;
private BoxCollider2D _collider;
public Rect DetectionRectangle
{
get
{
if (!_collider) _collider = GetComponent<BoxCollider2D>();
var bounds = _collider.bounds;
return new Rect(bounds.min - transform.position, bounds.size);
}
}
[SerializeField]
private Entity _entityLinked;
protected virtual void Start()
{
_collider = GetComponent<BoxCollider2D>();
detectionRange = _collider.size;
}
void ResizeCollider()
{
var range = GlobalConfig.Instance.Current.enemyBaseRange;
var size = _collider.size;
size.x = detectionRange.x * range.x;
size.y = detectionRange.y * range.y;
_collider.size = size;
var offset = _collider.offset;
if (offset == Vector2.zero) return;
offset.x = size.x / 2;
_collider.offset = offset;
}
//If it's a projectile damage > 0
private int _projectileDamage = 0;
private float _distanceMin = 100f;
protected virtual void Update()
{
ResizeCollider();
}
void OnTriggerEnter2D(Collider2D other)
{
//Projectiles detection + damage deal
if(_entityLinked != null) {
if(_projectileDamage > 0 && other.gameObject.GetComponent<Entity>() == _entityLinked) {
_entityLinked.Hit(_projectileDamage);
//Kill if no hp
if(other.gameObject.GetComponent<Entity>().Hp <= 0) {
other.gameObject.GetComponent<Entity>().Death();
}
_entityLinked = null;
}
}
}
void OnTriggerStay2D(Collider2D other)
{
if(_entityLinked != null && _projectileDamage == 0) {
//Detect the enemy and inform the Ally
if (other.gameObject.tag == "Opponent" && _entityLinked.gameObject.tag == "Ally") {
if(other.gameObject.transform.position.x <= _distanceMin) {
_distanceMin = other.gameObject.transform.position.x;
_entityLinked.IsEnemyDetected = true;
_entityLinked.Enemy = other.gameObject.GetComponent<Entity>();
}
}
//Detect the enemy and inform the Opponent
if (other.gameObject.tag == "Ally" && _entityLinked.gameObject.tag == "Opponent" ) {
if(other.gameObject.transform.position.x <= _distanceMin) {
_distanceMin = other.gameObject.transform.position.x;
_entityLinked.IsEnemyDetected = true;
_entityLinked.Enemy = other.gameObject.GetComponent<Entity>();
}
}
if(_entityLinked.Enemy == null && _distanceMin != 100f) {
_distanceMin = 100f;
}
}
}
void OnTriggerExit2D(Collider2D other)
{
if(_entityLinked != null) {
if(_projectileDamage == 0) {
if ((other.gameObject.tag == "Opponent" && _entityLinked is Ally) || (other.gameObject.tag == "Ally" && _entityLinked is Opponent)) {
_entityLinked.IsEnemyDetected = false;
}
}
}
}
//Getter and Setter
public Entity EntityLinked
{
get { return _entityLinked; }
set { _entityLinked = value; }
}
public int ProjectileDamage
{
get { return _projectileDamage; }
set { _projectileDamage = value; }
}
}