Add random point moving.
This commit is contained in:
@ -1,39 +1,71 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class CharacterCondition : MonoBehaviour
|
||||
public enum NPCState
|
||||
{
|
||||
public event Action<object> OnKilledEvent;
|
||||
public event Action<int> OnDamageHealthTakenEvent;
|
||||
public event Action<int> OnDamageArmourTakenEvent;
|
||||
public event Action<int> OnAmmunitionTakenEvent;
|
||||
InCover,
|
||||
InBlancPoint,
|
||||
InRunning,
|
||||
}
|
||||
|
||||
[SerializeField] private int HealthPoints;
|
||||
[SerializeField] private int ArmourPoints;
|
||||
[SerializeField] private int Ammunition;
|
||||
public class CharacterCondition
|
||||
{
|
||||
public event Action<int> OnChangeHealthEvent;
|
||||
public event Action<int> OnChangeArmourEvent;
|
||||
public event Action<int> OnChangeAmmunitionEvent;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
|
||||
private int health;
|
||||
public int HealthPoints
|
||||
{
|
||||
get
|
||||
{
|
||||
return health;
|
||||
}
|
||||
private set
|
||||
{
|
||||
health = value;
|
||||
OnChangeHealthEvent?.Invoke(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void GetDamage(float damage)
|
||||
private int armour;
|
||||
public int ArmourPoints
|
||||
{
|
||||
HealthPoints -= Mathf.RoundToInt(damage * (1 - ArmourPoints * 0.5f));
|
||||
ArmourPoints -= Mathf.RoundToInt(Mathf.Sqrt(damage) * 5);
|
||||
|
||||
OnDamageHealthTakenEvent?.Invoke(HealthPoints);
|
||||
OnDamageArmourTakenEvent?.Invoke(ArmourPoints);
|
||||
if (HealthPoints < 0)
|
||||
OnKilledEvent?.Invoke(gameObject);
|
||||
get
|
||||
{
|
||||
return armour;
|
||||
}
|
||||
private set
|
||||
{
|
||||
armour = value;
|
||||
OnChangeArmourEvent?.Invoke(value);
|
||||
}
|
||||
}
|
||||
private int ammo;
|
||||
public int Ammunition
|
||||
{
|
||||
get
|
||||
{
|
||||
return ammo;
|
||||
}
|
||||
private set
|
||||
{
|
||||
ammo = value;
|
||||
OnChangeAmmunitionEvent?.Invoke(value);
|
||||
}
|
||||
}
|
||||
|
||||
[HideInInspector]
|
||||
public NPCState npcState { get; private set; }
|
||||
|
||||
public CharacterCondition()
|
||||
{
|
||||
var settings = SettingsReader.Instance.GetSettings;
|
||||
ammo = settings.maxAmmo;
|
||||
health = settings.maxHealth;
|
||||
armour = settings.maxArmour;
|
||||
}
|
||||
|
||||
public void GiveHealth(int health) => HealthPoints = Mathf.Clamp(health + HealthPoints, 0, 100);
|
||||
public void SetHealth(int health) => HealthPoints = Mathf.Clamp(health, 0, 100);
|
||||
public void GiveArmour(int armour) => ArmourPoints = Mathf.Clamp(armour + ArmourPoints, 0, 100);
|
||||
public void SetArmour(int armour) => ArmourPoints = Mathf.Clamp(armour, 0, 100);
|
||||
public void TakeAmmo(int ammo)
|
||||
{
|
||||
Ammunition += ammo;
|
||||
OnAmmunitionTakenEvent?.Invoke(Ammunition);
|
||||
}
|
||||
public void TakeAmmo(int ammo) => Ammunition += ammo;
|
||||
}
|
@ -7,7 +7,6 @@ using UnityEngine.AI;
|
||||
public class MovementController : MonoBehaviour
|
||||
{
|
||||
public NavPoint currentPosition { get; private set; }
|
||||
[SerializeField] private MapManager mapManager;
|
||||
[SerializeField] private NavMeshAgent navMeshAgent;
|
||||
|
||||
private void Start()
|
||||
@ -21,18 +20,21 @@ public class MovementController : MonoBehaviour
|
||||
goToNextNavPoint(pointCandidate);
|
||||
}
|
||||
|
||||
|
||||
// todo внутри сенсора передавать в mlagents как variable length observations: https://github.com/Unity-Technologies/ml-agents/blob/main/docs/Learning-Environment-Design-Agents.md#variable-length-observations
|
||||
public void MoveToRandomPoint()
|
||||
{
|
||||
Debug.Log(MapManager.navPoints == null);
|
||||
goToNextNavPoint(MapManager.navPoints[Random.Range(0, MapManager.navPoints.Count)]);
|
||||
}
|
||||
|
||||
private NavPoint getPointCandidate()
|
||||
{
|
||||
var NavPointsPositions = mapManager.navPoints
|
||||
var NavPointsPositions = MapManager.navPoints
|
||||
.Select(point => point.transform.position)
|
||||
.Where(point => (currentPosition.transform.position - point).magnitude <= SettingsReader.Instance.GetSettings.movementSpeed)
|
||||
.ToList();
|
||||
//TODO AI
|
||||
return null;
|
||||
}
|
||||
|
||||
private void goToNextNavPoint(NavPoint destination) =>
|
||||
public void goToNextNavPoint(NavPoint destination) =>
|
||||
navMeshAgent.SetDestination(destination.transform.position);
|
||||
}
|
||||
|
@ -1,37 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.Sensors;
|
||||
using Unity.MLAgents.Actuators;
|
||||
|
||||
[RequireComponent(typeof(MovementController))]
|
||||
public class NPC : Agent
|
||||
{
|
||||
public float LastTimeHit;
|
||||
private BaseBehaviour NPCBehaviour;
|
||||
public List<Action> ActionList;
|
||||
|
||||
[SerializeField]
|
||||
private List<ISensor> SensorList; // todo тут интерфейс должен быть наш
|
||||
|
||||
public void SetBehaviour(BaseBehaviour behaviour) => NPCBehaviour = behaviour;
|
||||
public Team Team { get; set; }
|
||||
|
||||
[HideInInspector]
|
||||
private float LastTimeHit;
|
||||
public CharacterCondition Condition;
|
||||
|
||||
public MovementController moveController;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
|
||||
Condition = new CharacterCondition();
|
||||
moveController = gameObject.GetComponent<MovementController>();
|
||||
}
|
||||
|
||||
|
||||
public override void OnEpisodeBegin()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
// Target and Agent positions
|
||||
foreach (var _sensor in SensorList)
|
||||
sensor.AddObservation(Condition.HealthPoints);
|
||||
sensor.AddObservation(Condition.ArmourPoints);
|
||||
sensor.AddObservation(Condition.Ammunition);
|
||||
sensor.AddObservation((int)Condition.npcState);
|
||||
}
|
||||
|
||||
public override void Heuristic(in ActionBuffers actionsOut)
|
||||
{
|
||||
var discreteActionsOut = actionsOut.DiscreteActions;
|
||||
if (Input.GetKeyDown(KeyCode.W))
|
||||
{
|
||||
sensor.AddObservation(1); // todo
|
||||
// sensor.AddObservation(_sensor.GetValue());
|
||||
discreteActionsOut[0] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
|
||||
public override void OnActionReceived(ActionBuffers actions)
|
||||
{
|
||||
//NPCBehaviour.DoAction();
|
||||
if (actions.DiscreteActions[0] == 1)
|
||||
moveController.MoveToRandomPoint();
|
||||
}
|
||||
|
||||
public event Action<object> OnKilledEvent;
|
||||
public void GetDamage(float damage)
|
||||
{
|
||||
Condition.GiveHealth(-Mathf.RoundToInt(damage * (1 - Condition.ArmourPoints * 0.5f)));
|
||||
Condition.GiveArmour(-Mathf.RoundToInt(Mathf.Sqrt(damage) * 5));
|
||||
|
||||
if (Condition.HealthPoints < 0)
|
||||
OnKilledEvent?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user