files add

This commit is contained in:
2022-05-07 22:02:15 +07:00
parent 87568c4077
commit f9a3b06ec3
8 changed files with 281 additions and 213 deletions

View File

@ -5,7 +5,7 @@ using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine;
[RequireComponent(typeof(MovementController),typeof(BufferSensor))]
[RequireComponent(typeof(MovementController),typeof(BufferSensorComponent))]
public class NPC : Agent, ICharacter
{
[HideInInspector]
@ -47,12 +47,12 @@ public class NPC : Agent, ICharacter
moveController = gameObject.GetComponent<MovementController>();
bufferSensor = gameObject.GetComponent<BufferSensorComponent>();
flagZone = GameObject.FindObjectOfType<FlagZone>();
if (flagZone == null)
Debug.LogError("Flag Is Not Setted");
navPointIdDict = MapManager.IDToNavPoint;
if (flagZone is null)
Debug.LogError("Flag Is Not Set");
navPointIdDict = MapManager.Instance.IDToNavPoint;
if (navPointIdDict is null)
Debug.LogError("Cant Find Nav Point Dictionary");
}
@ -64,12 +64,18 @@ public class NPC : Agent, ICharacter
public override void OnEpisodeBegin()
{
if (navPointIdDict is null)
Debug.LogError("Cant Find Nav Point Dictionary");
NpcState = DirectState;
flagZone = GameObject.FindObjectOfType<FlagZone>();
}
public override void CollectObservations(VectorSensor sensor)
{
navPointIdDict = MapManager.Instance.IDToNavPoint;
if (navPointIdDict is null)
Debug.LogError("Cant Find Nav Point Dictionary");
var candidates = moveController.GetPointsCandidate();
//common sensors
@ -93,13 +99,14 @@ public class NPC : Agent, ICharacter
//point sensors
foreach (var point in candidates)
{
var position = transform.position;
bufferSensor.AppendObservation(new float[] {
point.DeathAttr,
(int)point.navType,
//4 flagEnemyDistance
GameManager.IsCloserToFlagFromNextNavPoint(point, transform.position).ToInt(),
GameManager.IsCloserToFlagFromNextNavPoint(point, position).ToInt(),
//5 EnemyVsNavPointDistance
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team.GetOppositeTeam()).ToInt(),
GameManager.IsCloserToEnemyThanToNextNavPoint(point, position, AgentCharacter.Team.GetOppositeTeam()).ToInt(),
//6 Have been seen by enemy in this point
GameManager.IsHaveSeenByEnemy(AgentCharacter.Team.GetOppositeTeam(),
point.Position).ToInt()
@ -165,11 +172,12 @@ public class NPC : Agent, ICharacter
}
public event Action<int, Team> OnDamageRecieved;
public void GetDamage(float damage)
public void GetDamage(int damage)
{
AgentCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
Condition.GiveHealth(-Mathf.RoundToInt(damage * (1 - Condition.ArmourPoints * 0.5f)));
Condition.GiveArmour(-Mathf.RoundToInt(Mathf.Sqrt(damage) * 5));
OnDamageRecieved?.Invoke(damage, AgentCharacter.Team);
if (Condition.HealthPoints < 0)
{