using System.Collections; using System.Collections.Generic; using System.ComponentModel; using Unity.PlasticSCM.Editor.WebApi; using UnityEngine; public class NPC_Behavior : MonoBehaviour { [SerializeField] GameObject confettiEffect; [SerializeField] private double _caffeineLevel = 0.0; // max 100, min 0 [SerializeField] private double _hungerLevel = 0.0; // max 100, min 0 [SerializeField] private double _happinessLevel = 100.0; // max 100, min 0 [SerializeField] private double _baseDevelopementPower = 100.0; // max 100, min 0 [SerializeField] private double _developementPower = 100.0; // max unlimited, min 0 [SerializeField] double eventRate = 1.0; // max 60, min 0 -> how many events are requested per minute /// /// Gibt das Koffeinlevel des Entwicklers zurück /// public double CaffeineLevel => _caffeineLevel; /// /// Gibt das Hungerlevel des Entwicklers zurück /// public double HungerLevel => _hungerLevel; /// /// Gibt das Zufriedenheitslevel des Entwicklers zurück /// public double HappnessLevel => _happinessLevel; /// /// Gibt die Basisentwicklungskraft des Entwicklers zurück /// public double BaseDevelopementPower => _baseDevelopementPower; /// /// Gibt die jetzige Entwicklungskraft des Entwicklers zurück /// public double DevelopmentPower => _developementPower; private GameManager _gameManager; [SerializeField] private bool _fullfillNeedManually = false; [SerializeField] private float _timer; [SerializeField] private float _newNeedDelay = 3.0f; [SerializeField] private List _lastTenNeeds = new List(); private DeveloperNeeds _developerNeeds; private float _timeBetweenEvents; private GameObject _currentNeed = null; private float _newNeedDelayTimer = 0.0f; private float _effectCreationTime; private GameObject _effect; /// /// The name of the current need /// public string CurrentNeed => _lastTenNeeds[_lastTenNeeds.Count - 1]; /// /// Indicates if the developer has a need right now /// public bool HasNeed = false; // Start is called before the first frame update void Start() { _gameManager = GameManager.Instance; _developerNeeds = GetComponent(); ResetTimer(); } // Update is called once per frame void Update() { WatchEffect(); _timer -= Time.deltaTime; if (_newNeedDelayTimer <= 0) { if (_timer <= 0 && _currentNeed == null) { GenerateNeed(); ResetTimer(); } } else { _newNeedDelayTimer -= Time.deltaTime; } // for Debugging if (_fullfillNeedManually) { _fullfillNeedManually = false; NeedFullfilled(); } } private void ResetTimer() { if (eventRate <= 0) { _timeBetweenEvents = float.MaxValue; } else { _timeBetweenEvents = 60f / (float)eventRate; _timer = Random.Range(0.5f * _timeBetweenEvents, 1.25f * _timeBetweenEvents); } } private void WatchEffect() { if (_effect != null && GetEffectLifetime() >= 3.0f) { RemoveEffect(); } } private float GetEffectLifetime() { return Time.time - _effectCreationTime; } private bool RemoveEffect() { Destroy(_effect); return _effect != null; } public bool GenerateNeed() { List needs = new List() { "coffee", "mate", "toilet", "money" }; string need = needs[UnityEngine.Random.Range(0, needs.Count)]; _currentNeed = _developerNeeds.spawnNeed(need); HasNeed = true; _lastTenNeeds.Add(need); if (_lastTenNeeds.Count > 10) { _lastTenNeeds.RemoveAt(0); } return _currentNeed != null; } /// /// Deletes the current need and its gameobject. /// /// bool: wether successfully deleted the gameobject or not public bool NeedFullfilled() { Destroy(_currentNeed); if (HasNeed) { HasNeed = false; _newNeedDelayTimer = _newNeedDelay; _effect = Instantiate(confettiEffect, new Vector3(0.0f, 1.5f, 0.0f), confettiEffect.transform.rotation); _effect.transform.SetParent(transform, false); _effectCreationTime = Time.time; } return _currentNeed == null; } public Dictionary GetStats() { return new Dictionary { { "Caffeine", _caffeineLevel }, { "Hunger", _hungerLevel }, { "Happiness", _happinessLevel }, { "Developement Power", _developementPower } }; } public void SetStats(Dictionary stats) { foreach (string key in stats.Keys) { switch (key) { case "Caffeine": _caffeineLevel = stats[key]; break; case "Hunger": _hungerLevel = stats[key]; break; case "Happiness": _happinessLevel = stats[key]; break; case "Developement Power": _developementPower = stats[key]; break; default: Debug.LogError("Unknown Stat/Key"); break; } } } }