196 lines
5.8 KiB
C#
196 lines
5.8 KiB
C#
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
|
|
|
|
/// <summary>
|
|
/// Gibt das Koffeinlevel des Entwicklers zurück
|
|
/// </summary>
|
|
public double CaffeineLevel => _caffeineLevel;
|
|
/// <summary>
|
|
/// Gibt das Hungerlevel des Entwicklers zurück
|
|
/// </summary>
|
|
public double HungerLevel => _hungerLevel;
|
|
/// <summary>
|
|
/// Gibt das Zufriedenheitslevel des Entwicklers zurück
|
|
/// </summary>
|
|
public double HappnessLevel => _happinessLevel;
|
|
/// <summary>
|
|
/// Gibt die Basisentwicklungskraft des Entwicklers zurück
|
|
/// </summary>
|
|
public double BaseDevelopementPower => _baseDevelopementPower;
|
|
/// <summary>
|
|
/// Gibt die jetzige Entwicklungskraft des Entwicklers zurück
|
|
/// </summary>
|
|
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<string> _lastTenNeeds = new List<string>();
|
|
private DeveloperNeeds _developerNeeds;
|
|
private float _timeBetweenEvents;
|
|
private GameObject _currentNeed = null;
|
|
private float _newNeedDelayTimer = 0.0f;
|
|
private float _effectCreationTime;
|
|
private GameObject _effect;
|
|
|
|
/// <summary>
|
|
/// The name of the current need
|
|
/// </summary>
|
|
public string CurrentNeed => _lastTenNeeds[_lastTenNeeds.Count - 1];
|
|
/// <summary>
|
|
/// Indicates if the developer has a need right now
|
|
/// </summary>
|
|
public bool HasNeed = false;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
_gameManager = GameManager.Instance;
|
|
_developerNeeds = GetComponent<DeveloperNeeds>();
|
|
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<string> needs = new List<string>() { "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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes the current need and its gameobject.
|
|
/// </summary>
|
|
/// <returns>bool: wether successfully deleted the gameobject or not</returns>
|
|
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<string, double> GetStats()
|
|
{
|
|
return new Dictionary<string, double>
|
|
{
|
|
{ "Caffeine", _caffeineLevel },
|
|
{ "Hunger", _hungerLevel },
|
|
{ "Happiness", _happinessLevel },
|
|
{ "Developement Power", _developementPower }
|
|
};
|
|
}
|
|
|
|
public void SetStats(Dictionary<string, double> 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;
|
|
}
|
|
}
|
|
}
|
|
}
|