From 0aa3327433bc88082c45f7d37bda41b44a3c216e Mon Sep 17 00:00:00 2001 From: Felix Boucher Date: Sat, 5 Aug 2023 15:55:54 -0400 Subject: [PATCH] =?UTF-8?q?appliquer=20global=20config=20aux=20diff=C3=A9r?= =?UTF-8?q?ents=20endroits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Assets/Scripts/Ally/Ally.cs | 13 ++--- Assets/Scripts/Detection.cs | 31 +++++++++++- Assets/Scripts/Entity.cs | 33 ++++--------- Assets/Scripts/General/Extensions.cs | 19 ++++++++ Assets/Scripts/General/GlobalConfig.cs | 12 ++++- Assets/Scripts/General/GlobalConfigFile.cs | 19 ++++---- Assets/Scripts/Resource/ResourceMaker.cs | 7 +-- Assets/Scripts/Root.cs | 2 +- Assets/Scripts/Tiles/ResourceTile.cs | 55 +++++++++++++++++----- Assets/Tiles/Farm.asset | 2 +- Assets/Tiles/Forest.asset | 2 +- Assets/Tiles/Meat.asset | 2 +- Assets/Tiles/RockNode.asset | 2 +- 13 files changed, 135 insertions(+), 64 deletions(-) diff --git a/Assets/Scripts/Ally/Ally.cs b/Assets/Scripts/Ally/Ally.cs index 4615d32..9e81acd 100644 --- a/Assets/Scripts/Ally/Ally.cs +++ b/Assets/Scripts/Ally/Ally.cs @@ -18,17 +18,18 @@ public class Ally : Entity } - void AttackEnemy() + void AttackEnemy() { //Attack Cooldown - if(AttackSpeed < AttackSpeedWait) { + if (AttackSpeed < AttackSpeedWait) + { - Animation.PlayAttackAnim(); + Animation.PlayAttackAnim(); - AttackSpeedWait = 0f; - } + AttackSpeedWait = 0f; + } - AttackSpeedWait += Time.deltaTime; + AttackSpeedWait += Time.deltaTime; } } diff --git a/Assets/Scripts/Detection.cs b/Assets/Scripts/Detection.cs index dda1736..f668583 100644 --- a/Assets/Scripts/Detection.cs +++ b/Assets/Scripts/Detection.cs @@ -4,22 +4,48 @@ using UnityEngine; public class Detection : MonoBehaviour { + private Vector2 detectionRange; + private BoxCollider2D _collider; public Rect DetectionRectangle { get { - var collider = GetComponent(); - var bounds = collider.bounds; + if (!_collider) _collider = GetComponent(); + var bounds = _collider.bounds; return new Rect(bounds.min - transform.position, bounds.size); } } [SerializeField] private Entity _entityLinked; + protected virtual void Start() + { + _collider = GetComponent(); + 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 { diff --git a/Assets/Scripts/Entity.cs b/Assets/Scripts/Entity.cs index 598493b..9d8a097 100644 --- a/Assets/Scripts/Entity.cs +++ b/Assets/Scripts/Entity.cs @@ -1,7 +1,6 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -using static Extensions; public class Entity : LevelObject { @@ -90,35 +89,19 @@ 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 -{ - get { return _attack_speed_wait; } - set { _attack_speed_wait = value; } -} + { + get { return _attack_speed_wait; } + set { _attack_speed_wait = value; } + } public bool IsEnemyDetected { diff --git a/Assets/Scripts/General/Extensions.cs b/Assets/Scripts/General/Extensions.cs index c938c49..ad694e2 100644 --- a/Assets/Scripts/General/Extensions.cs +++ b/Assets/Scripts/General/Extensions.cs @@ -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(this IEnumerable list, Func 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; + } } \ No newline at end of file diff --git a/Assets/Scripts/General/GlobalConfig.cs b/Assets/Scripts/General/GlobalConfig.cs index 4135a7b..a954bd2 100644 --- a/Assets/Scripts/General/GlobalConfig.cs +++ b/Assets/Scripts/General/GlobalConfig.cs @@ -4,5 +4,15 @@ public class GlobalConfig : SingletonBehaviour { [SerializeField] private GlobalConfigFile _current; - public GlobalConfigFile Current => _current; + public GlobalConfigFile Current + { + get + { + if (!_current) + { + _current = ScriptableObject.CreateInstance(); + } + return _current; + } + } } diff --git a/Assets/Scripts/General/GlobalConfigFile.cs b/Assets/Scripts/General/GlobalConfigFile.cs index 94612de..8a277c7 100644 --- a/Assets/Scripts/General/GlobalConfigFile.cs +++ b/Assets/Scripts/General/GlobalConfigFile.cs @@ -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; } diff --git a/Assets/Scripts/Resource/ResourceMaker.cs b/Assets/Scripts/Resource/ResourceMaker.cs index b52a821..d5598d7 100644 --- a/Assets/Scripts/Resource/ResourceMaker.cs +++ b/Assets/Scripts/Resource/ResourceMaker.cs @@ -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); diff --git a/Assets/Scripts/Root.cs b/Assets/Scripts/Root.cs index 91cfaf4..eb71304 100644 --- a/Assets/Scripts/Root.cs +++ b/Assets/Scripts/Root.cs @@ -13,7 +13,7 @@ public class Root : MonoBehaviour private Transform _projectileSpawn; void Attack() { - _entity.Enemy.Hit( _entity.AttackDamage); + _entity.Enemy.Hit(_entity.AttackDamage); if(_entity.Enemy.Hp <= 0) { _entity.Enemy.Death(); _entity.IsEnemyDetected = false; diff --git a/Assets/Scripts/Tiles/ResourceTile.cs b/Assets/Scripts/Tiles/ResourceTile.cs index 0327ef5..afcfe1f 100644 --- a/Assets/Scripts/Tiles/ResourceTile.cs +++ b/Assets/Scripts/Tiles/ResourceTile.cs @@ -12,30 +12,59 @@ 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(x => x.Position.IsContainedIn(Position)); if (!hasFarmer) return; + _yieldCounter += Time.deltaTime; + if (_yieldCounter < _realYieldDuration) return; - _yieldCounter += Time.deltaTime * _yieldSpeed; - if (_yieldCounter < 1) return; - if(_yieldPrefab != null) + ResetYieldDuration(); + + if (_yieldPrefab != null) { - _yieldCounter = 0; - 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); + 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) @@ -43,7 +72,7 @@ public class ResourceTile : LevelTile return other is ResourceTile otherRes && base.Equals(otherRes) && _yieldPrefab == otherRes._yieldPrefab - && _yieldSpeed == otherRes._yieldSpeed + && _yieldDuration == otherRes._yieldDuration && Occupied == otherRes.Occupied; } public override Dictionary 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(); } diff --git a/Assets/Tiles/Farm.asset b/Assets/Tiles/Farm.asset index 80c131e..326eb50 100644 --- a/Assets/Tiles/Farm.asset +++ b/Assets/Tiles/Farm.asset @@ -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 diff --git a/Assets/Tiles/Forest.asset b/Assets/Tiles/Forest.asset index acbedaa..88f485a 100644 --- a/Assets/Tiles/Forest.asset +++ b/Assets/Tiles/Forest.asset @@ -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 diff --git a/Assets/Tiles/Meat.asset b/Assets/Tiles/Meat.asset index 6ec392b..ddf80a2 100644 --- a/Assets/Tiles/Meat.asset +++ b/Assets/Tiles/Meat.asset @@ -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 diff --git a/Assets/Tiles/RockNode.asset b/Assets/Tiles/RockNode.asset index 90feadb..2899c44 100644 --- a/Assets/Tiles/RockNode.asset +++ b/Assets/Tiles/RockNode.asset @@ -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