...
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Animators.Leonid_Animator;
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.Actuators;
|
||||
using Unity.MLAgents.Sensors;
|
||||
@ -10,51 +10,57 @@ using UnityEngine;
|
||||
public class NPC : Agent, ICharacter
|
||||
{
|
||||
[HideInInspector]
|
||||
private Character AgentCharacter;
|
||||
public CharacterCondition Condition;
|
||||
private FlagZone flagZone = null;
|
||||
private Character _agentCharacter;
|
||||
|
||||
public INpcBaseState NpcState { get; private set; }
|
||||
private CharacterCondition _condition;
|
||||
private FlagZone _flagZone = null;
|
||||
|
||||
private INpcBaseState NpcState { get; set; }
|
||||
public INpcBaseBodyState NpcBodyState { get; private set; }
|
||||
|
||||
public Character GetCharacter => AgentCharacter;
|
||||
public Character GetCharacter => _agentCharacter;
|
||||
|
||||
private NpcDirectPointState DirectState;
|
||||
private NpcInCoverState CoverState;
|
||||
private NpcRunningState RunningState;
|
||||
private NpcDirectPointState _directState;
|
||||
private NpcInCoverState _coverState;
|
||||
private NpcRunningState _runningState;
|
||||
|
||||
private NpcStandingState StandingState;
|
||||
private NpcCrouchingState CrouchingState;
|
||||
private NpcStandingState _standingState;
|
||||
private NpcCrouchingState _crouchingState;
|
||||
|
||||
private MovementController moveController;
|
||||
private BufferSensorComponent bufferSensor;
|
||||
private MovementController _moveController;
|
||||
private BufferSensorComponent _bufferSensor;
|
||||
private AnimatorHandler _animatorHandler;
|
||||
private AimAssistant _assistant;
|
||||
|
||||
private Dictionary<int, NavPoint> _navPointIdDict;
|
||||
|
||||
private Dictionary<int, NavPoint> navPointIdDict;
|
||||
|
||||
#region UnityEvents and ML
|
||||
private void Awake()
|
||||
{
|
||||
DirectState = new NpcDirectPointState();
|
||||
CoverState = new NpcInCoverState();
|
||||
RunningState = new NpcRunningState();
|
||||
NpcState = DirectState;
|
||||
_directState = new NpcDirectPointState();
|
||||
_coverState = new NpcInCoverState();
|
||||
_runningState = new NpcRunningState();
|
||||
NpcState = _directState;
|
||||
|
||||
CrouchingState = new NpcCrouchingState();
|
||||
StandingState = new NpcStandingState();
|
||||
NpcBodyState = StandingState;
|
||||
_crouchingState = new NpcCrouchingState();
|
||||
_standingState = new NpcStandingState();
|
||||
NpcBodyState = _standingState;
|
||||
|
||||
AgentCharacter = new Character();
|
||||
Condition = AgentCharacter.Condition;
|
||||
_agentCharacter = new Character();
|
||||
_condition = _agentCharacter.Condition;
|
||||
|
||||
moveController = gameObject.GetComponent<MovementController>();
|
||||
bufferSensor = gameObject.GetComponent<BufferSensorComponent>();
|
||||
_moveController = gameObject.GetComponent<MovementController>();
|
||||
_bufferSensor = gameObject.GetComponent<BufferSensorComponent>();
|
||||
_animatorHandler = gameObject.GetComponent<AnimatorHandler>();
|
||||
_assistant = gameObject.GetComponent<AimAssistant>();
|
||||
|
||||
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
if (flagZone is null)
|
||||
_flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
if (_flagZone is null)
|
||||
Debug.LogError("Flag Is Not Set");
|
||||
|
||||
navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (navPointIdDict is null)
|
||||
_navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (_navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
}
|
||||
|
||||
@ -65,54 +71,54 @@ public class NPC : Agent, ICharacter
|
||||
|
||||
public override void OnEpisodeBegin()
|
||||
{
|
||||
if (navPointIdDict is null)
|
||||
if (_navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
|
||||
NpcState = DirectState;
|
||||
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
NpcState = _directState;
|
||||
_flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
}
|
||||
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
// Debug.Log("Collect observations called!");
|
||||
navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (navPointIdDict is null)
|
||||
_navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (_navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
var candidates = moveController.GetPointsCandidate();
|
||||
var candidates = _moveController.GetPointsCandidate();
|
||||
|
||||
//common sensors
|
||||
sensor.AddObservation(GameManager.IsHaveSeenByEnemy(AgentCharacter.Team.GetOppositeTeam(),
|
||||
sensor.AddObservation(GameManager.IsHaveSeenByEnemy(_agentCharacter.Team.GetOppositeTeam(),
|
||||
NpcBodyState.GetPointToHit(gameObject)).ToInt());
|
||||
sensor.AddObservation(AgentCharacter.LastTimeHit);
|
||||
sensor.AddObservation((!flagZone.IsNotOccup).ToInt());
|
||||
sensor.AddObservation(Condition.GetHealthPointsInQuantile());
|
||||
sensor.AddObservation(Condition.GetArmourPointsInQuantile());
|
||||
sensor.AddObservation(_agentCharacter.LastTimeHit);
|
||||
sensor.AddObservation((!_flagZone.IsNotOccup).ToInt());
|
||||
sensor.AddObservation(_condition.GetHealthPointsInQuantile());
|
||||
sensor.AddObservation(_condition.GetArmourPointsInQuantile());
|
||||
sensor.AddObservation(candidates.Count);
|
||||
sensor.AddObservation(moveController.PointStartID);
|
||||
sensor.AddObservation(moveController.PointEndID);
|
||||
sensor.AddObservation(_moveController.PointStartID);
|
||||
sensor.AddObservation(_moveController.PointEndID);
|
||||
// Debug.Log("Done common!");
|
||||
//state sensors
|
||||
sensor.AddObservation((int)NpcState.State);
|
||||
sensor.AddObservation((int)NpcBodyState.State);
|
||||
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, AgentCharacter.Team));
|
||||
sensor.AddObservation(navPointIdDict[moveController.PointStartID].DeathAttr);
|
||||
sensor.AddObservation(navPointIdDict[moveController.PointEndID].DeathAttr);
|
||||
sensor.AddObservation(moveController.FlagDistance);
|
||||
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, _agentCharacter.Team));
|
||||
sensor.AddObservation(_navPointIdDict[_moveController.PointStartID].DeathAttr);
|
||||
sensor.AddObservation(_navPointIdDict[_moveController.PointEndID].DeathAttr);
|
||||
sensor.AddObservation(_moveController.FlagDistance);
|
||||
// Debug.Log("Done state sensors!");
|
||||
|
||||
//point sensors
|
||||
foreach (var point in candidates)
|
||||
{
|
||||
var position = transform.position;
|
||||
bufferSensor.AppendObservation(new float[] {
|
||||
_bufferSensor.AppendObservation(new float[] {
|
||||
point.DeathAttr,
|
||||
(int)point.navType,
|
||||
//4 flagEnemyDistance
|
||||
GameManager.IsCloserToFlagFromNextNavPoint(point, position).ToInt(),
|
||||
//5 EnemyVsNavPointDistance
|
||||
GameManager.IsCloserToEnemyThanToNextNavPoint(point, 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(),
|
||||
GameManager.IsHaveSeenByEnemy(_agentCharacter.Team.GetOppositeTeam(),
|
||||
point.Position).ToInt()
|
||||
});
|
||||
}
|
||||
@ -126,16 +132,16 @@ public class NPC : Agent, ICharacter
|
||||
// Debug.Log(result[0] + " " + result[1]);
|
||||
if (result[0] == 0)
|
||||
{
|
||||
if (navPointIdDict[moveController.PointStartID].navType != NavPointType.Cover)
|
||||
if (_navPointIdDict[_moveController.PointStartID].navType != NavPointType.Cover)
|
||||
return;
|
||||
NpcState = CoverState;
|
||||
NpcState = _coverState;
|
||||
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: Peek(); break;
|
||||
case 1: Cover(); break;
|
||||
case 3: Peek(); moveController.GoToNextNavPoint(navPointIdDict[result[2]]); break;
|
||||
case 4: NpcState = DirectState; break;
|
||||
case 3: Peek(); _moveController.GoToNextNavPoint(_navPointIdDict[result[2]]); break;
|
||||
case 4: NpcState = _directState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
@ -143,7 +149,7 @@ public class NPC : Agent, ICharacter
|
||||
if (result[0] == 1)
|
||||
{
|
||||
// Debug.Log("BEFORE SOme shitty if >:(");
|
||||
if (navPointIdDict[moveController.PointStartID].navType != NavPointType.Direction)
|
||||
if (_navPointIdDict[_moveController.PointStartID].navType != NavPointType.Direction)
|
||||
{
|
||||
// Debug.Log("SOme shitty if >:(");
|
||||
return;
|
||||
@ -152,64 +158,72 @@ public class NPC : Agent, ICharacter
|
||||
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: moveController.GoToNextNavPoint(navPointIdDict[result[2]]);
|
||||
NpcState = RunningState; Debug.Log("Go to point " + result[2]);break;
|
||||
case 1: NpcState = DirectState; break;
|
||||
case 0: _moveController.GoToNextNavPoint(_navPointIdDict[result[2]]);
|
||||
NpcState = _runningState; Debug.Log("Go to point " + result[2]);break;
|
||||
case 1: NpcState = _directState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
|
||||
if (result[0] == 2)
|
||||
{
|
||||
if (moveController.PointStartID == moveController.PointEndID && moveController.PointEndID != -1)
|
||||
if (_moveController.PointStartID == _moveController.PointEndID && _moveController.PointEndID != -1)
|
||||
return;
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: moveController.StopOnPath(); NpcState = DirectState; break;
|
||||
case 1: moveController.ReturnToStartPoint(); NpcState = RunningState; break;
|
||||
case 0: _moveController.StopOnPath(); NpcState = _directState; break;
|
||||
case 1: _moveController.ReturnToStartPoint(); NpcState = _runningState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
// Debug.Log("Actions processed!");
|
||||
}
|
||||
|
||||
private void UpdateAnimatorValues()
|
||||
{
|
||||
var movementDir = _moveController.Velocity;
|
||||
//Тут может быть косяк, так как я не помню горизонтальное по x или y.
|
||||
_animatorHandler.UpdateAnimatorValues(movementDir.x, movementDir.y,
|
||||
false, NpcBodyState == _crouchingState, _assistant._isFiring);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public event Action<NpcBodyState> OnChangePosition;
|
||||
private void Peek()
|
||||
{
|
||||
OnChangePosition?.Invoke(global::NpcBodyState.Standing);
|
||||
NpcBodyState = StandingState;
|
||||
NpcBodyState = _standingState;
|
||||
}
|
||||
|
||||
private void Cover()
|
||||
{
|
||||
OnChangePosition?.Invoke(global::NpcBodyState.Crouching);
|
||||
NpcBodyState = CrouchingState;
|
||||
NpcBodyState = _crouchingState;
|
||||
}
|
||||
|
||||
public event Action<bool> OnDeathEvent;
|
||||
public event Action<int, Team> OnDamageRecieved;
|
||||
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);
|
||||
_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)
|
||||
if (_condition.HealthPoints < 0)
|
||||
{
|
||||
MapManager.AddDeathAttributeToPoints(moveController.PointStartID, moveController.PointEndID,
|
||||
moveController.DistanceToGo, moveController.RemainingDistance);
|
||||
OnDeathEvent?.Invoke(true);
|
||||
MapManager.AddDeathAttributeToPoints(_moveController.PointStartID, _moveController.PointEndID,
|
||||
_moveController.DistanceToGo, _moveController.RemainingDistance);
|
||||
var pos = gameObject.transform.position;
|
||||
var id = moveController.PointStartID;
|
||||
var id = _moveController.PointStartID;
|
||||
CharacterFactory.Instance.ReSpawn(this, ref pos, ref id);
|
||||
}
|
||||
}
|
||||
|
||||
//public event OnDeathEvent<
|
||||
|
||||
public void ResetCharacter()
|
||||
{
|
||||
Condition.Reset();
|
||||
_condition.Reset();
|
||||
EndEpisode();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user