private void OnTriggerStay2D(Collider2D collision)
{
if(collision.TryGetComponent<Weapon>(out Weapon weapon) && Input.GetKeyDown(KeyCode.F))
{
pickUp(weapon, weapon.transform.position);
}
}
private void pickUp(Weapon weapon, Vector3 weaponPosition)
{
if (_Weapon != null)
{
_Weapon.transform.parent = null;
}
_Weapon = weapon;
_Weapon.transform.parent = transform;
_Weapon.transform.position = _PointPossitionWeapon.position;
}
public class Weapon : MonoBehaviour
{
public void Hide()
{
gameObject.SetActive(false);
}
}
public class PlayerInput : MonoBehaviour
{
public event Action InteractionButtonPressed = delegate { };
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
InteractionButtonPressed();
}
}
}
public class PickUpArea : MonoBehaviour
{
private readonly List<Weapon> _closeWeapons = new();
public bool HasItemsAround => _closeWeapons.Count > 0;
public void PickUp(PlayableCharacter character)
{
var closestWeapon = GetClosestWeapon();
_closeWeapons.Remove(closestWeapon);
character.Give(closestWeapon);
}
private Weapon GetClosestWeapon()
{
Weapon closest = null;
var minDistance = float.MaxValue;
foreach (var weapon in _closeWeapons)
{
var distance = (transform.position - weapon.transform.position).sqrMagnitude;
if (distance < minDistance)
{
closest = weapon;
minDistance = distance;
}
}
return closest;
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.TryGetComponent(out Weapon weapon))
{
_closeWeapons.Add(weapon);
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.TryGetComponent(out Weapon weapon))
{
_closeWeapons.Remove(weapon);
}
}
}
public class PlayableCharacter : MonoBehaviour
{
[SerializeField] private PlayerInput _input;
[SerializeField] private PickUpArea _pickUpArea;
[SerializeField] private List<Weapon> _weapons = new();
private void Start()
{
_input.InteractionButtonPressed += TryPickUp;
}
private void OnDestroy()
{
_input.InteractionButtonPressed -= TryPickUp;
}
public void Give(Weapon weapon)
{
weapon.Hide();
_weapons.Add(weapon);
}
private void TryPickUp()
{
if (_pickUpArea.HasItemsAround)
{
_pickUpArea.PickUp(this);
}
}
}