Felix Boucher 0aa3327433 appliquer global config aux différents endroits
PROBLÈME :

la config existait mais n'était pas appliquée nulle part

SOLUTION :

maintenant elle l'est

NOTES :

Elle n'est pas encore appliquée au flash de dégat
2023-08-05 15:55:54 -04:00

96 lines
3.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using static Extensions;
[CreateAssetMenu(menuName = project_name + "/" + nameof(ResourceTile))]
public class ResourceTile : LevelTile
{
[SerializeField]
[Tooltip("the prefab of the currency that will be spawned when mining this resource")]
private GameObject _yieldPrefab;
public GameObject YieldPrefab => _yieldPrefab;
private string YieldPrefabName => _yieldPrefab.name;
[SerializeField][Tooltip("mesure en seconde / ressource")]
private float _yieldDuration = 1; //resource per second
private float _realYieldDuration;
[SerializeField]
[Range(0.0f, 5.0f)]
private float _randomPositionConfig = 0.5f;
private float _yieldCounter = 0;
public bool Occupied { get; set; }
public override void LevelStart()
{
base.LevelStart();
_realYieldDuration = _yieldDuration;
ResetYieldDuration();
}
public override void LevelUpdate()
{
//check if there is an harvester unit on top
var hasFarmer = LevelManager.Instance.Get<Harvester>(x => x.Position.IsContainedIn(Position));
if (!hasFarmer) return;
_yieldCounter += Time.deltaTime;
if (_yieldCounter < _realYieldDuration) return;
ResetYieldDuration();
if (_yieldPrefab != null)
{
YieldResource();
}
}
private void YieldResource()
{
float rangeConfig = 0.5f + _randomPositionConfig;
Vector3 yieldPosition = new Vector3(Position.x + Random.Range(-rangeConfig, rangeConfig), Position.y, Position.z);
var yielded = Instantiate(_yieldPrefab, yieldPosition, Quaternion.identity);
yielded.transform.SetParent(LevelManager.Instance.LevelTransform);
}
private void ResetYieldDuration()
{
_yieldCounter = 0;
var config = GlobalConfig.Instance.Current;
if (!config) return;
if (config.useRandomHarvestDuration)
{
float min = config.randomHarvestDurationMinimum,
max = config.randomHarvestDurationMaximum;
_realYieldDuration = _yieldDuration * Random.Range(min, max);
}
else
{
_realYieldDuration = _yieldDuration * config.baseHarvestDuration;
}
}
public override bool Equals(ILevelObject other)
{
return other is ResourceTile otherRes
&& base.Equals(otherRes)
&& _yieldPrefab == otherRes._yieldPrefab
&& _yieldDuration == otherRes._yieldDuration
&& Occupied == otherRes.Occupied;
}
public override Dictionary<string, object> ToDictionary()
{
var dict = base.ToDictionary();
dict[nameof(YieldPrefabName)] = YieldPrefabName;
dict[nameof(_yieldDuration)] = _yieldDuration;
dict[nameof(Occupied)] = Occupied;
return dict;
}
public override void LoadDictionary(Dictionary<string, object> dict)
{
base.LoadDictionary(dict);
var prefabName = dict[nameof(YieldPrefabName)].ToString();
_yieldPrefab = Database.Instance.Prefabs[prefabName];
_yieldDuration = dict[nameof(_yieldDuration)].ToFloat();
_yieldCounter = dict[nameof(_yieldCounter)].ToFloat();
Occupied = dict[nameof(Occupied)].ToBool();
}
}