72 lines
2.6 KiB
C#
72 lines
2.6 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
/// <summary>
|
|
/// Gère la production de ressource et sert de minuteur pour laisser la ressource sur le sol.
|
|
/// </summary>
|
|
public class ResourceMaker : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private int _resourceMakingAmount;
|
|
[SerializeField]
|
|
private Enum.ResourceChoice _resourceChoice;
|
|
private ResourceManager _resourceManagerInstance;
|
|
[SerializeField]
|
|
private Vector2 _endPosition;
|
|
private Vector2 _startPosition;
|
|
private float _desiredTime = 1.5f;
|
|
private float _timePassed = 0f;
|
|
private bool _isPlaying = false;
|
|
|
|
private void Start()
|
|
{
|
|
_resourceManagerInstance = ResourceManager.Instance;
|
|
_startPosition = transform.position;
|
|
transform.position = new Vector2(transform.position.x, transform.position.y + 0.5f);
|
|
gameObject.GetComponent<Rigidbody2D>().AddForce(transform.up * 20.0f);
|
|
gameObject.GetComponent<Rigidbody2D>().gravityScale = 0.1f;
|
|
}
|
|
|
|
/// <summary>
|
|
/// D'après le choix de resource à générer, choisi le prefab à instancier
|
|
/// </summary>
|
|
private void Update()
|
|
{
|
|
if (Vector2.Distance(transform.position, _startPosition) < 0.01f)
|
|
{
|
|
gameObject.GetComponent<Rigidbody2D>().gravityScale = 0.0f;
|
|
gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
|
|
}
|
|
|
|
if (_isPlaying)
|
|
{
|
|
_timePassed += Time.deltaTime;
|
|
float duration = _timePassed / _desiredTime;
|
|
duration = duration * duration * (3.0f - 2.0f * duration);
|
|
transform.position = Vector2.Lerp(transform.position, _endPosition, duration);
|
|
if(Vector2.Distance(transform.position,_endPosition) < 0.001f)
|
|
{
|
|
_isPlaying = false;
|
|
switch (_resourceChoice)
|
|
{
|
|
case Enum.ResourceChoice.Rock:
|
|
_resourceManagerInstance.RockAmount = _resourceMakingAmount;
|
|
break;
|
|
case Enum.ResourceChoice.Wood:
|
|
_resourceManagerInstance.WoodAmount = _resourceMakingAmount;
|
|
break;
|
|
case Enum.ResourceChoice.Food:
|
|
_resourceManagerInstance.FoodAmount = _resourceMakingAmount;
|
|
break;
|
|
}
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void GenerateResource(){
|
|
_isPlaying = true;
|
|
gameObject.GetComponent<Rigidbody2D>().gravityScale = 0.0f;
|
|
gameObject.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
|
|
}
|
|
}
|