58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using BumpCombat.Constants;
|
|
using UnityEngine;
|
|
|
|
namespace BumpCombat.Progression
|
|
{
|
|
[RequireComponent(typeof(Rigidbody2D), typeof(CircleCollider2D))]
|
|
public sealed class ExperienceOrb : MonoBehaviour
|
|
{
|
|
private Rigidbody2D body;
|
|
private ExperienceSystem experienceSystem;
|
|
private Transform player;
|
|
private int value;
|
|
private bool pickedUp;
|
|
|
|
private void Awake()
|
|
{
|
|
body = GetComponent<Rigidbody2D>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
experienceSystem = FindAnyObjectByType<ExperienceSystem>();
|
|
player = experienceSystem != null ? experienceSystem.transform : null;
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (player == null || pickedUp)
|
|
{
|
|
return;
|
|
}
|
|
|
|
float distance = Vector2.Distance(body.position, player.position);
|
|
ItemConstants tuning = GameplayConstants.Current.Items;
|
|
if (distance <= tuning.ExperienceOrbPickupRadius)
|
|
{
|
|
pickedUp = true;
|
|
experienceSystem.CollectExperienceOrb(value);
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
if (distance <= tuning.ExperienceOrbMagnetRadius)
|
|
{
|
|
body.MovePosition(Vector2.MoveTowards(
|
|
body.position,
|
|
player.position,
|
|
tuning.ExperienceOrbMagnetSpeed * Time.fixedDeltaTime));
|
|
}
|
|
}
|
|
|
|
public void Initialize(int experienceValue)
|
|
{
|
|
value = Mathf.Max(0, experienceValue);
|
|
}
|
|
}
|
|
}
|