Almost Final Version

This commit is contained in:
2022-05-16 01:13:50 +07:00
parent bb6aea8720
commit 150efde055
47 changed files with 1120 additions and 4707 deletions

View File

@ -1,24 +1,26 @@
using System;
using System.Collections.Generic;
using Animators.Leonid_Animator;
using Animators.Leonid_Animator.Bot;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine;
[RequireComponent(typeof(MovementController),typeof(BufferSensorComponent))]
[RequireComponent(typeof(MovementController))]
[RequireComponent(typeof(BufferSensorComponent))]
[RequireComponent(typeof(DecisionRequester))]
[RequireComponent(typeof(BotLocomotion))]
public class NPC : Agent, ICharacter
{
[HideInInspector]
private Character _agentCharacter;
private CharacterCondition _condition;
private FlagZone _flagZone = null;
private INpcBaseState NpcState { get; set; }
public INpcBaseBodyState NpcBodyState { get; private set; }
public Character GetCharacter => _agentCharacter;
[field: HideInInspector]
public Character GetCharacter { get; private set; }
private NpcDirectPointState _directState;
private NpcInCoverState _coverState;
@ -29,12 +31,13 @@ public class NPC : Agent, ICharacter
private MovementController _moveController;
private BufferSensorComponent _bufferSensor;
private AnimatorHandler _animatorHandler;
private AimAssistant _assistant;
private BotLocomotion _botLocomotion;
private Dictionary<int, NavPoint> _navPointIdDict;
public bool IsFiring => _assistant.fireAnimation;
#region UnityEvents and ML
private void Awake()
{
@ -47,13 +50,13 @@ public class NPC : Agent, ICharacter
_standingState = new NpcStandingState();
NpcBodyState = _standingState;
_agentCharacter = new Character();
_condition = _agentCharacter.Condition;
GetCharacter = new Character();
_condition = GetCharacter.Condition;
_moveController = gameObject.GetComponent<MovementController>();
_bufferSensor = gameObject.GetComponent<BufferSensorComponent>();
_animatorHandler = gameObject.GetComponent<AnimatorHandler>();
_assistant = gameObject.GetComponent<AimAssistant>();
_botLocomotion = gameObject.GetComponent<BotLocomotion>();
_flagZone = GameObject.FindObjectOfType<FlagZone>();
if (_flagZone is null)
@ -69,6 +72,12 @@ public class NPC : Agent, ICharacter
Debug.LogWarning("Pooled object was destroyed");
}
private void Update()
{
print(IsFiring);
_botLocomotion.UpdateAnimatorValues();
}
public override void OnEpisodeBegin()
{
if (_navPointIdDict is null)
@ -87,9 +96,9 @@ public class NPC : Agent, ICharacter
var candidates = _moveController.GetPointsCandidate();
//common sensors
sensor.AddObservation(GameManager.IsHaveSeenByEnemy(_agentCharacter.Team.GetOppositeTeam(),
sensor.AddObservation(GameManager.IsHaveSeenByEnemy(GetCharacter.Team.GetOppositeTeam(),
NpcBodyState.GetPointToHit(gameObject)).ToInt());
sensor.AddObservation(_agentCharacter.LastTimeHit);
sensor.AddObservation(GetCharacter.LastTimeHit);
sensor.AddObservation((!_flagZone.IsNotOccup).ToInt());
sensor.AddObservation(_condition.GetHealthPointsInQuantile());
sensor.AddObservation(_condition.GetArmourPointsInQuantile());
@ -100,7 +109,7 @@ public class NPC : Agent, ICharacter
//state sensors
sensor.AddObservation((int)NpcState.State);
sensor.AddObservation((int)NpcBodyState.State);
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, _agentCharacter.Team));
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, GetCharacter.Team));
sensor.AddObservation(_navPointIdDict[_moveController.PointStartID].DeathAttr);
sensor.AddObservation(_navPointIdDict[_moveController.PointEndID].DeathAttr);
sensor.AddObservation(_moveController.FlagDistance);
@ -116,9 +125,9 @@ public class NPC : Agent, ICharacter
//4 flagEnemyDistance
GameManager.IsCloserToFlagFromNextNavPoint(point, position).ToInt(),
//5 EnemyVsNavPointDistance
GameManager.IsCloserToEnemyThanToNextNavPoint(point, position, _agentCharacter.Team.GetOppositeTeam()).ToInt(),
GameManager.IsCloserToEnemyThanToNextNavPoint(point, position, GetCharacter.Team.GetOppositeTeam()).ToInt(),
//6 Have been seen by enemy in this point
GameManager.IsHaveSeenByEnemy(_agentCharacter.Team.GetOppositeTeam(),
GameManager.IsHaveSeenByEnemy(GetCharacter.Team.GetOppositeTeam(),
point.Position).ToInt()
});
}
@ -127,9 +136,7 @@ public class NPC : Agent, ICharacter
public override void OnActionReceived(ActionBuffers actions)
{
// Debug.Log("Actions recieved!");
var result = actions.DiscreteActions;
// Debug.Log(result[0] + " " + result[1]);
if (result[0] == 0)
{
if (_navPointIdDict[_moveController.PointStartID].navType != NavPointType.Cover)
@ -145,16 +152,12 @@ public class NPC : Agent, ICharacter
default: throw new ArgumentException("Undefined Action recieved");
}
}
// Debug.Log(result[0] == 1);
if (result[0] == 1)
{
// Debug.Log("BEFORE SOme shitty if >:(");
if (_navPointIdDict[_moveController.PointStartID].navType != NavPointType.Direction)
{
// Debug.Log("SOme shitty if >:(");
return;
}
// Debug.Log("FUCK");
switch (result[1])
{
@ -176,15 +179,6 @@ public class NPC : Agent, ICharacter
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
@ -202,13 +196,13 @@ public class NPC : Agent, ICharacter
}
public event Action<bool> OnDeathEvent;
public event Action<int, Team> OnDamageRecieved;
public event Action<int, Team> OnDamageReceived;
public void GetDamage(int damage)
{
_agentCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
GetCharacter.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);
OnDamageReceived?.Invoke(damage, GetCharacter.Team);
if (_condition.HealthPoints < 0)
{