movement, automated enemies, automated players, basic abilities
This commit is contained in:
parent
23576e137e
commit
f4344c4700
31 changed files with 4343 additions and 70 deletions
42
Assets/Scripts/DetectEntities.cs
Normal file
42
Assets/Scripts/DetectEntities.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class DetectEntities : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Entity thisEntity;
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (!other.CompareTag(thisEntity.tag) && other.TryGetComponent(out Entity isEntity))
|
||||
{
|
||||
thisEntity.entitiesInRange.Add(isEntity);
|
||||
thisEntity.closestEntity = FindClosestEntity();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other)
|
||||
{
|
||||
if (other.TryGetComponent(out Entity isEntity) && thisEntity.entitiesInRange.Contains(isEntity))
|
||||
{
|
||||
thisEntity.entitiesInRange.Remove(isEntity);
|
||||
if (thisEntity.closestEntity == isEntity)
|
||||
{
|
||||
thisEntity.closestEntity = FindClosestEntity();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Entity FindClosestEntity()
|
||||
{
|
||||
Entity currentClosestEntity = null;
|
||||
foreach (Entity entityFound in thisEntity.entitiesInRange)
|
||||
{
|
||||
if (!currentClosestEntity ||
|
||||
Vector3.Distance(entityFound.transform.position, thisEntity.transform.position) <
|
||||
Vector3.Distance(currentClosestEntity.transform.position, thisEntity.transform.position))
|
||||
{
|
||||
currentClosestEntity = entityFound;
|
||||
}
|
||||
}
|
||||
return currentClosestEntity;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue