Felix Boucher b1cfb714a6 changer l'apparence du harvester
problème :

- le harvester avait une apparence non-changeante dépendant du genre de ressource sur lequel il était placé.

solution :

- ajouter une logique qui change le prefab choisi dépendant de sur quelle tuile on place le harvester.

- also, quelques micro refactorings (changement de noms, ajout de doc)
2023-07-23 11:53:07 -04:00

48 lines
1.7 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using static Enum;
public class Harvester : Entity
{
[SerializeField][Tooltip("helps choose the right skin for the harvester depending on resource")]
private List<HarvesterResourcePair> _harvesterResourcePairs;
protected ResourceChoice ResourceChoice => _resourceChoice;
[SerializeField]
private ResourceChoice _resourceChoice;
public override sealed void Start()
{
base.Start();
ChooseSkinForHarvester();
}
/// <summary>
/// changes the harvester's appearance depending on the resource they're harvesting
/// </summary>
private void ChooseSkinForHarvester()
{
//get the tile we're on
var tile = LevelManager.Instance.Get<ResourceTile>(t => t.Position == Position);
if (tile == default) return;
//get the resource of the tile we're on
var yieldPrefab = tile.YieldPrefab;
if (!yieldPrefab) return;
var resourceMaker = yieldPrefab.GetComponent<ResourceMaker>();
if (!resourceMaker) return;
//if we already have the right harvester for the resource, no need to change
var resourceChoice = resourceMaker.ResourceChoice;
if (resourceChoice == this.ResourceChoice) return;
//get the right pair for the resource we're on
var harvResPair = _harvesterResourcePairs.Find(hrp => hrp.Resource == resourceChoice);
if (harvResPair == null) return;
//exchange harvesters
var newHarvester = Instantiate(harvResPair.HarvesterPrefab, transform.position, Quaternion.identity);
newHarvester.transform.SetParent(transform.parent);
Destroy(gameObject);
}
}