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

@ -18,17 +18,18 @@ public class Ally : Entity
} }
void AttackEnemy() void AttackEnemy()
{ {
//Attack Cooldown //Attack Cooldown
if(AttackSpeed < AttackSpeedWait) { if (AttackSpeed < AttackSpeedWait)
{
Animation.PlayAttackAnim(); Animation.PlayAttackAnim();
AttackSpeedWait = 0f; AttackSpeedWait = 0f;
} }
AttackSpeedWait += Time.deltaTime; AttackSpeedWait += Time.deltaTime;
} }
} }

View File

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

View File

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

View File

@ -4,6 +4,7 @@ using BindingFlags = System.Reflection.BindingFlags;
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
using GatherAndDefend.LevelEditor; using GatherAndDefend.LevelEditor;
using System.Linq;
public static class Extensions public static class Extensions
{ {
@ -96,4 +97,22 @@ public static class Extensions
{ {
return Vector2.Distance(vect, tilePosition) < 0.5f; 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] [SerializeField]
private GlobalConfigFile _current; 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"; public const string project_name = "Gather And Defend";
[Header("Enemies")] [Header("Enemies")]
public float enemyBaseDamage; public float enemyBaseDamage = 1;
public float enemyBaseLife; public float enemyBaseLife = 1;
public float enemyBaseRange; public Vector2 enemyBaseRange = Vector2.one;
public float enemyBaseAttackSpeed; public float enemyBaseAttackSpeed = 1;
public float damageFlashIntensity; public float damageFlashIntensity = 1;
[Header("resources")] [Header("resources")]
public float harvestDuration; public float baseHarvestDuration = 1;
public float harvestAmount; public float baseHarvestAmount = 1;
public float harvestRandomDurationMinimum; public bool useRandomHarvestDuration = false;
public float harvestRandomDurationMaximum; public float randomHarvestDurationMinimum = 0;
public float randomHarvestDurationMaximum = 0;
} }

View File

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

View File

@ -13,7 +13,7 @@ public class Root : MonoBehaviour
private Transform _projectileSpawn; private Transform _projectileSpawn;
void Attack() { void Attack() {
_entity.Enemy.Hit( _entity.AttackDamage); _entity.Enemy.Hit(_entity.AttackDamage);
if(_entity.Enemy.Hp <= 0) { if(_entity.Enemy.Hp <= 0) {
_entity.Enemy.Death(); _entity.Enemy.Death();
_entity.IsEnemyDetected = false; _entity.IsEnemyDetected = false;

View File

@ -12,30 +12,59 @@ public class ResourceTile : LevelTile
public GameObject YieldPrefab => _yieldPrefab; public GameObject YieldPrefab => _yieldPrefab;
private string YieldPrefabName => _yieldPrefab.name; private string YieldPrefabName => _yieldPrefab.name;
[SerializeField] [SerializeField][Tooltip("mesure en seconde / ressource")]
private float _yieldSpeed = 1; //resource per second private float _yieldDuration = 1; //resource per second
private float _realYieldDuration;
[SerializeField] [SerializeField]
[Range(0.0f, 5.0f)] [Range(0.0f, 5.0f)]
private float _randomPositionConfig = 0.5f; private float _randomPositionConfig = 0.5f;
private float _yieldCounter = 0; private float _yieldCounter = 0;
public bool Occupied { get; set; } public bool Occupied { get; set; }
public override void LevelStart()
{
base.LevelStart();
_realYieldDuration = _yieldDuration;
ResetYieldDuration();
}
public override void LevelUpdate() public override void LevelUpdate()
{ {
//check if there is an harvester unit on top //check if there is an harvester unit on top
var hasFarmer = LevelManager.Instance.Get<Harvester>(x => x.Position.IsContainedIn(Position)); var hasFarmer = LevelManager.Instance.Get<Harvester>(x => x.Position.IsContainedIn(Position));
if (!hasFarmer) return; if (!hasFarmer) return;
_yieldCounter += Time.deltaTime;
if (_yieldCounter < _realYieldDuration) return;
_yieldCounter += Time.deltaTime * _yieldSpeed; ResetYieldDuration();
if (_yieldCounter < 1) return;
if(_yieldPrefab != null) if (_yieldPrefab != null)
{ {
_yieldCounter = 0; 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); private void YieldResource()
yielded.transform.SetParent(LevelManager.Instance.LevelTransform); {
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) public override bool Equals(ILevelObject other)
@ -43,7 +72,7 @@ public class ResourceTile : LevelTile
return other is ResourceTile otherRes return other is ResourceTile otherRes
&& base.Equals(otherRes) && base.Equals(otherRes)
&& _yieldPrefab == otherRes._yieldPrefab && _yieldPrefab == otherRes._yieldPrefab
&& _yieldSpeed == otherRes._yieldSpeed && _yieldDuration == otherRes._yieldDuration
&& Occupied == otherRes.Occupied; && Occupied == otherRes.Occupied;
} }
public override Dictionary<string, object> ToDictionary() public override Dictionary<string, object> ToDictionary()
@ -51,7 +80,7 @@ public class ResourceTile : LevelTile
var dict = base.ToDictionary(); var dict = base.ToDictionary();
dict[nameof(YieldPrefabName)] = YieldPrefabName; dict[nameof(YieldPrefabName)] = YieldPrefabName;
dict[nameof(_yieldSpeed)] = _yieldSpeed; dict[nameof(_yieldDuration)] = _yieldDuration;
dict[nameof(Occupied)] = Occupied; dict[nameof(Occupied)] = Occupied;
return dict; return dict;
} }
@ -60,7 +89,7 @@ public class ResourceTile : LevelTile
base.LoadDictionary(dict); base.LoadDictionary(dict);
var prefabName = dict[nameof(YieldPrefabName)].ToString(); var prefabName = dict[nameof(YieldPrefabName)].ToString();
_yieldPrefab = Database.Instance.Prefabs[prefabName]; _yieldPrefab = Database.Instance.Prefabs[prefabName];
_yieldSpeed = dict[nameof(_yieldSpeed)].ToFloat(); _yieldDuration = dict[nameof(_yieldDuration)].ToFloat();
_yieldCounter = dict[nameof(_yieldCounter)].ToFloat(); _yieldCounter = dict[nameof(_yieldCounter)].ToFloat();
Occupied = dict[nameof(Occupied)].ToBool(); Occupied = dict[nameof(Occupied)].ToBool();
} }

View File

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

View File

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

View File

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

View File

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