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
This commit is contained in:
Felix Boucher 2023-08-05 15:55:54 -04:00
parent b9908ab717
commit 0aa3327433
13 changed files with 135 additions and 64 deletions

View File

@ -21,7 +21,8 @@ public class Ally : Entity
void AttackEnemy()
{
//Attack Cooldown
if(AttackSpeed < AttackSpeedWait) {
if (AttackSpeed < AttackSpeedWait)
{
Animation.PlayAttackAnim();

View File

@ -4,22 +4,48 @@ using UnityEngine;
public class Detection : MonoBehaviour
{
private Vector2 detectionRange;
private BoxCollider2D _collider;
public Rect DetectionRectangle
{
get
{
var collider = GetComponent<Collider2D>();
var bounds = collider.bounds;
if (!_collider) _collider = GetComponent<BoxCollider2D>();
var bounds = _collider.bounds;
return new Rect(bounds.min - transform.position, bounds.size);
}
}
[SerializeField]
private Entity _entityLinked;
protected virtual void Start()
{
_collider = GetComponent<BoxCollider2D>();
detectionRange = _collider.size;
}
void ResizeCollider()
{
var range = GlobalConfig.Instance.Current.enemyBaseRange;
var size = _collider.size;
size.x = detectionRange.x * range.x;
size.y = detectionRange.y * range.y;
_collider.size = size;
var offset = _collider.offset;
if (offset == Vector2.zero) return;
offset.x = size.x / 2;
_collider.offset = offset;
}
//If it's a projectile damage > 0
private int _projectileDamage = 0;
private float _distanceMin = 100f;
protected virtual void Update()
{
ResizeCollider();
}
void OnTriggerEnter2D(Collider2D other)
{
//Projectiles detection + damage deal
@ -82,6 +108,7 @@ public class Detection : MonoBehaviour
}
//Getter and Setter
public Entity EntityLinked
{

View File

@ -1,7 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Extensions;
public class Entity : LevelObject
{
@ -90,29 +89,13 @@ public class Entity : LevelObject
//GETTERS AND SETTERS
public int Hp
{
get { return _hp; }
set { _hp = value; }
}
public int Hp => (int)(_hp * GlobalConfig.Instance.Current.enemyBaseLife);
public float Speed
{
get { return _speed; }
set { _speed = value; }
}
public float Speed => _speed;
public int AttackDamage
{
get { return _attack_damage; }
set { _attack_damage = value; }
}
public int AttackDamage => (int)(_attack_damage * GlobalConfig.Instance.Current.enemyBaseDamage);
public float AttackSpeed
{
get { return _attack_speed; }
set { _attack_speed = value; }
}
public float AttackSpeed => _attack_speed * GlobalConfig.Instance.Current.enemyBaseAttackSpeed;
public float AttackSpeedWait
{

View File

@ -4,6 +4,7 @@ using BindingFlags = System.Reflection.BindingFlags;
using UnityEngine;
using System.Collections;
using GatherAndDefend.LevelEditor;
using System.Linq;
public static class Extensions
{
@ -96,4 +97,22 @@ public static class Extensions
{
return Vector2.Distance(vect, tilePosition) < 0.5f;
}
public static T Minimum<T>(this IEnumerable<T> list, Func<T, float> func)
{
if (list.Count() < 1) throw new Exception("in " + nameof(Minimum) + " : Cannot find minimum of empty list : " + nameof(list));
T minT = list.ElementAt(0);
float minVal = func(minT);
foreach (var obj in list)
{
var newVal = func(obj);
if (minVal > newVal)
{
minT = obj;
minVal = newVal;
}
}
return minT;
}
}

View File

@ -4,5 +4,15 @@ public class GlobalConfig : SingletonBehaviour<GlobalConfig>
{
[SerializeField]
private GlobalConfigFile _current;
public GlobalConfigFile Current => _current;
public GlobalConfigFile Current
{
get
{
if (!_current)
{
_current = ScriptableObject.CreateInstance<GlobalConfigFile>();
}
return _current;
}
}
}

View File

@ -10,16 +10,17 @@ public class GlobalConfigFile : ScriptableObject
public const string project_name = "Gather And Defend";
[Header("Enemies")]
public float enemyBaseDamage;
public float enemyBaseLife;
public float enemyBaseRange;
public float enemyBaseAttackSpeed;
public float enemyBaseDamage = 1;
public float enemyBaseLife = 1;
public Vector2 enemyBaseRange = Vector2.one;
public float enemyBaseAttackSpeed = 1;
public float damageFlashIntensity;
public float damageFlashIntensity = 1;
[Header("resources")]
public float harvestDuration;
public float harvestAmount;
public float harvestRandomDurationMinimum;
public float harvestRandomDurationMaximum;
public float baseHarvestDuration = 1;
public float baseHarvestAmount = 1;
public bool useRandomHarvestDuration = false;
public float randomHarvestDurationMinimum = 0;
public float randomHarvestDurationMaximum = 0;
}

View File

@ -43,6 +43,7 @@ public class ResourceMaker : MonoBehaviour
if (_isPlaying)
{
var amountMult = GlobalConfig.Instance.Current.baseHarvestAmount;
_timePassed += Time.deltaTime;
float duration = _timePassed / _desiredTime;
duration = duration * duration * (3.0f - 2.0f * duration);
@ -53,13 +54,13 @@ public class ResourceMaker : MonoBehaviour
switch (_resourceChoice)
{
case Enum.ResourceChoice.Rock:
_resourceManagerInstance.RockAmount = _resourceMakingAmount;
_resourceManagerInstance.RockAmount = (int)(_resourceMakingAmount * amountMult);
break;
case Enum.ResourceChoice.Wood:
_resourceManagerInstance.WoodAmount = _resourceMakingAmount;
_resourceManagerInstance.WoodAmount = (int)(_resourceMakingAmount * amountMult);
break;
case Enum.ResourceChoice.Food:
_resourceManagerInstance.FoodAmount = _resourceMakingAmount;
_resourceManagerInstance.FoodAmount = (int)(_resourceMakingAmount * amountMult);
break;
}
Destroy(gameObject);

View File

@ -12,38 +12,67 @@ public class ResourceTile : LevelTile
public GameObject YieldPrefab => _yieldPrefab;
private string YieldPrefabName => _yieldPrefab.name;
[SerializeField]
private float _yieldSpeed = 1; //resource per second
[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();
_yieldCounter += Time.deltaTime * _yieldSpeed;
if (_yieldCounter < 1) return;
if (_yieldPrefab != null)
{
_yieldCounter = 0;
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
&& _yieldSpeed == otherRes._yieldSpeed
&& _yieldDuration == otherRes._yieldDuration
&& Occupied == otherRes.Occupied;
}
public override Dictionary<string, object> ToDictionary()
@ -51,7 +80,7 @@ public class ResourceTile : LevelTile
var dict = base.ToDictionary();
dict[nameof(YieldPrefabName)] = YieldPrefabName;
dict[nameof(_yieldSpeed)] = _yieldSpeed;
dict[nameof(_yieldDuration)] = _yieldDuration;
dict[nameof(Occupied)] = Occupied;
return dict;
}
@ -60,7 +89,7 @@ public class ResourceTile : LevelTile
base.LoadDictionary(dict);
var prefabName = dict[nameof(YieldPrefabName)].ToString();
_yieldPrefab = Database.Instance.Prefabs[prefabName];
_yieldSpeed = dict[nameof(_yieldSpeed)].ToFloat();
_yieldDuration = dict[nameof(_yieldDuration)].ToFloat();
_yieldCounter = dict[nameof(_yieldCounter)].ToFloat();
Occupied = dict[nameof(Occupied)].ToBool();
}

View File

@ -15,5 +15,5 @@ MonoBehaviour:
_sprite: {fileID: 21300000, guid: ccca3e050cb082b45af0a099790463f6, type: 3}
_isCollidable: 0
_yieldPrefab: {fileID: 6962989255644195630, guid: a2dc5d9672c10074fa9c35c12f6339c1, type: 3}
_yieldSpeed: 0.1
_yieldDuration: 5
_randomPositionConfig: 0.25

View File

@ -15,5 +15,5 @@ MonoBehaviour:
_sprite: {fileID: 21300000, guid: 43582b3c6b60fd144bc56d8ab3b14349, type: 3}
_isCollidable: 0
_yieldPrefab: {fileID: 6962989255644195630, guid: f20569b5452c2b341a95d656b7534b7e, type: 3}
_yieldSpeed: 0.1
_yieldDuration: 5
_randomPositionConfig: 0.5

View File

@ -15,5 +15,5 @@ MonoBehaviour:
_sprite: {fileID: 21300000, guid: b1e6b8ebeb2e25f4f8c5de93a31dd6a2, type: 3}
_isCollidable: 0
_yieldPrefab: {fileID: 6962989255644195630, guid: a2dc5d9672c10074fa9c35c12f6339c1, type: 3}
_yieldSpeed: 0.1
_yieldDuration: 5
_randomPositionConfig: 0.5

View File

@ -15,5 +15,5 @@ MonoBehaviour:
_sprite: {fileID: 21300000, guid: 6298844400e212d40bce870425ac2a5b, type: 3}
_isCollidable: 0
_yieldPrefab: {fileID: 6962989255644195630, guid: 484f0eca1c74ae34694692de56a36739, type: 3}
_yieldSpeed: 0.1
_yieldDuration: 5
_randomPositionConfig: 0.5