Files
HCI_GazeLab/Assets/SpaceShooterIgor/Scripts/Combat/MaterialDrop3D.cs
T

54 lines
1.4 KiB
C#

using UnityEngine;
using Spaceshooter.Player;
namespace Spaceshooter.Combat
{
[RequireComponent(typeof(Rigidbody2D), typeof(Collider2D))]
public class MaterialDrop3D : MonoBehaviour, IClickable3D
{
[SerializeField] private int scrapValue = 10;
private Rigidbody2D rb;
private bool isCollected = false;
public int ScrapValue => scrapValue;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
rb.gravityScale = 0f;
rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
}
public void Initialize(int value, Vector2 initialVelocity)
{
this.scrapValue = value;
if (rb == null) rb = GetComponent<Rigidbody2D>();
rb.velocity = initialVelocity;
rb.angularVelocity = Random.Range(-180f, 180f);
}
private void Update()
{
// Cleanup if it drifts far off screen
if (transform.position.x < -20f || Mathf.Abs(transform.position.y) > 15f)
{
Destroy(gameObject);
}
}
public void OnClickedByPlayer(Vector2 hitPoint)
{
if (isCollected) return;
isCollected = true;
if (PlayerShip3D.Instance != null)
{
PlayerShip3D.Instance.AddScrap(scrapValue);
}
Destroy(gameObject);
}
}
}