ResourceMaker détecte le click sur le Yield Les yields font partis du ResourceTile
57 lines
1.9 KiB
C#
57 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using static Extensions;
|
|
|
|
[CreateAssetMenu(menuName = "Gather And Defend/Resource Tile")]
|
|
public class ResourceTile : LevelTile
|
|
{
|
|
[SerializeField]
|
|
[Tooltip("the prefab of the currency that will be spawned when mining this resource")]
|
|
private GameObject _yieldPrefab;
|
|
|
|
private string YieldPrefabName => _yieldPrefab.name;
|
|
|
|
[SerializeField]
|
|
private float _yieldSpeed = 1; //resource per second
|
|
private float _yieldCounter = 0;
|
|
public bool Occupied { get; set; }
|
|
|
|
public override void LevelUpdate()
|
|
{
|
|
//if (!Occupied) return;
|
|
|
|
|
|
_yieldCounter += Time.deltaTime * _yieldSpeed;
|
|
if (_yieldCounter < 1) return;
|
|
|
|
_yieldCounter = 0;
|
|
var yielded = Instantiate(_yieldPrefab, Position, Quaternion.identity);
|
|
yielded.transform.SetParent(LevelManager.Instance.LevelTransform);
|
|
}
|
|
public override bool Equals(ILevelObject other)
|
|
{
|
|
return other is ResourceTile otherRes
|
|
&& base.Equals(otherRes)
|
|
&& _yieldPrefab == otherRes._yieldPrefab
|
|
&& _yieldSpeed == otherRes._yieldSpeed
|
|
&& Occupied == otherRes.Occupied;
|
|
}
|
|
public override Dictionary<string, object> ToDictionary()
|
|
{
|
|
var dict = base.ToDictionary();
|
|
|
|
dict[nameof(YieldPrefabName)] = YieldPrefabName;
|
|
dict[nameof(_yieldSpeed)] = _yieldSpeed;
|
|
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];
|
|
_yieldSpeed = dict[nameof(_yieldSpeed)].ToFloat();
|
|
_yieldCounter = dict[nameof(_yieldCounter)].ToFloat();
|
|
Occupied = dict[nameof(Occupied)].ToBool();
|
|
}
|
|
} |