update scripts
This commit is contained in:
@ -25,10 +25,10 @@ public class CharacterFactory : MonoBehaviour
|
|||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
var attcNum = SettingsReader.Instance.GetSettings.numOfAttackers;
|
var attcNum = SettingsReader.Instance.GetSettings.NumOfAttackers;
|
||||||
var defNum = SettingsReader.Instance.GetSettings.numOfDefenders;
|
var defNum = SettingsReader.Instance.GetSettings.NumOfDefenders;
|
||||||
var humanDef = SettingsReader.Instance.GetSettings.hasHumanDefender == true ? 1 : 0;
|
var humanDef = SettingsReader.Instance.GetSettings.HasHumanDefender == true ? 1 : 0;
|
||||||
var humanAtc = SettingsReader.Instance.GetSettings.hasHumanAttacker == true ? 1 : 0;
|
var humanAtc = SettingsReader.Instance.GetSettings.HasHumanAttacker == true ? 1 : 0;
|
||||||
|
|
||||||
if (humanAtc == 1 && humanDef == 1)
|
if (humanAtc == 1 && humanDef == 1)
|
||||||
throw new System.ArgumentException("Can be only one human player");
|
throw new System.ArgumentException("Can be only one human player");
|
||||||
@ -56,6 +56,10 @@ public class CharacterFactory : MonoBehaviour
|
|||||||
spawnPoint.position,
|
spawnPoint.position,
|
||||||
Quaternion.identity);
|
Quaternion.identity);
|
||||||
gameobject.SetActive(true);
|
gameobject.SetActive(true);
|
||||||
|
if (team == Team.Attackers)
|
||||||
|
gameObject.tag = "Attacker";
|
||||||
|
else
|
||||||
|
gameObject.tag = "Defender";
|
||||||
|
|
||||||
if (typeAi == TypeAI.HumanAI)
|
if (typeAi == TypeAI.HumanAI)
|
||||||
{
|
{
|
||||||
@ -65,7 +69,7 @@ public class CharacterFactory : MonoBehaviour
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
gameobject.GetComponent<NPC>().GetCharacter.Team = team;
|
gameobject.GetComponent<NPC>().GetCharacter.Team = team;
|
||||||
gameobject.GetComponent<MovementController>().currentPosition = spawnPoint;
|
gameobject.GetComponent<MovementController>().CurrentNavPoint = spawnPoint;
|
||||||
Bots.Add(gameobject);
|
Bots.Add(gameobject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,8 +85,8 @@ public class CharacterFactory : MonoBehaviour
|
|||||||
else
|
else
|
||||||
bot.transform.position = spawnPointsForDefendersTeam[Random.Range(0, spawnPointsForDefendersTeam.Count)].position;
|
bot.transform.position = spawnPointsForDefendersTeam[Random.Range(0, spawnPointsForDefendersTeam.Count)].position;
|
||||||
}
|
}
|
||||||
var player = Player.GetComponent<Player>();
|
Player player;
|
||||||
if (player != null)
|
if (TryGetComponent<Player>(out player))
|
||||||
{
|
{
|
||||||
player.ResetCharacter();
|
player.ResetCharacter();
|
||||||
if (player.GetCharacter.Team == Team.Attackers)
|
if (player.GetCharacter.Team == Team.Attackers)
|
||||||
|
@ -21,6 +21,17 @@ public class CharacterCondition
|
|||||||
OnChangeHealthEvent?.Invoke(value);
|
OnChangeHealthEvent?.Invoke(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int GetHealthPointsInQuantile()
|
||||||
|
{
|
||||||
|
if (health < 25)
|
||||||
|
return 0;
|
||||||
|
else if (health < 50)
|
||||||
|
return 1;
|
||||||
|
else if (health < 75)
|
||||||
|
return 2;
|
||||||
|
else return 3;
|
||||||
|
}
|
||||||
private int armour;
|
private int armour;
|
||||||
public int ArmourPoints
|
public int ArmourPoints
|
||||||
{
|
{
|
||||||
@ -51,9 +62,9 @@ public class CharacterCondition
|
|||||||
public CharacterCondition()
|
public CharacterCondition()
|
||||||
{
|
{
|
||||||
var settings = SettingsReader.Instance.GetSettings;
|
var settings = SettingsReader.Instance.GetSettings;
|
||||||
ammo = settings.maxAmmo;
|
ammo = settings.MaxAmmo;
|
||||||
health = settings.maxHealth;
|
health = settings.MaxHealth;
|
||||||
armour = settings.maxArmour;
|
armour = settings.MaxArmour;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GiveHealth(int health) => HealthPoints = Mathf.Clamp(health + HealthPoints, 0, 100);
|
public void GiveHealth(int health) => HealthPoints = Mathf.Clamp(health + HealthPoints, 0, 100);
|
||||||
|
@ -2,16 +2,31 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.AI;
|
using UnityEngine.AI;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
[RequireComponent(typeof(NavMeshAgent))]
|
[RequireComponent(typeof(NavMeshAgent))]
|
||||||
public class MovementController : MonoBehaviour
|
public class MovementController : MonoBehaviour
|
||||||
{
|
{
|
||||||
public NavPoint currentPosition { get; set; }
|
public NavPoint CurrentNavPoint { get; set; }
|
||||||
|
public float FlagDistance { get; private set; }
|
||||||
|
private GameObject flag;
|
||||||
|
private const float updateFlagPositionDelay = 5;
|
||||||
[SerializeField] private NavMeshAgent navMeshAgent;
|
[SerializeField] private NavMeshAgent navMeshAgent;
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
navMeshAgent.speed = SettingsReader.Instance.GetSettings.movementSpeed;
|
navMeshAgent.speed = SettingsReader.Instance.GetSettings.MovementSpeed;
|
||||||
|
InvokeRepeating(nameof(UpdateFlagPosition), 0, updateFlagPositionDelay);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
CancelInvoke(nameof(UpdateFlagPosition));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateFlagPosition()
|
||||||
|
{
|
||||||
|
FlagDistance = (flag.transform.position - gameObject.transform.position).magnitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MoveToRandomPoint()
|
public void MoveToRandomPoint()
|
||||||
@ -23,7 +38,7 @@ public class MovementController : MonoBehaviour
|
|||||||
public List<NavPoint> getPointsCandidate()
|
public List<NavPoint> getPointsCandidate()
|
||||||
{
|
{
|
||||||
return MapManager.navPoints
|
return MapManager.navPoints
|
||||||
.Where(point => (currentPosition.position - point.position).magnitude < SettingsReader.Instance.GetSettings.movementSpeed)
|
.Where(point => (CurrentNavPoint.position - point.position).magnitude < SettingsReader.Instance.GetSettings.MovementSpeed)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ using UnityEngine;
|
|||||||
using Unity.MLAgents;
|
using Unity.MLAgents;
|
||||||
using Unity.MLAgents.Sensors;
|
using Unity.MLAgents.Sensors;
|
||||||
using Unity.MLAgents.Actuators;
|
using Unity.MLAgents.Actuators;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
[RequireComponent(typeof(MovementController))]
|
[RequireComponent(typeof(MovementController))]
|
||||||
public class NPC : Agent, ICharacter
|
public class NPC : Agent, ICharacter
|
||||||
@ -11,6 +10,7 @@ public class NPC : Agent, ICharacter
|
|||||||
[HideInInspector]
|
[HideInInspector]
|
||||||
public Character AgentCharacter;
|
public Character AgentCharacter;
|
||||||
public CharacterCondition Condition;
|
public CharacterCondition Condition;
|
||||||
|
private FlagZone flagZone;
|
||||||
|
|
||||||
public NPC_BaseState NPC_State { get; private set; }
|
public NPC_BaseState NPC_State { get; private set; }
|
||||||
|
|
||||||
@ -47,29 +47,38 @@ public class NPC : Agent, ICharacter
|
|||||||
public override void OnEpisodeBegin()
|
public override void OnEpisodeBegin()
|
||||||
{
|
{
|
||||||
NPC_State = DirectState;
|
NPC_State = DirectState;
|
||||||
|
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void CollectObservations(VectorSensor sensor)
|
public override void CollectObservations(VectorSensor sensor)
|
||||||
{
|
{
|
||||||
|
var candidates = moveController.getPointsCandidate();
|
||||||
|
|
||||||
sensor.AddObservation(Condition.HealthPoints);
|
sensor.AddObservation(Condition.HealthPoints);
|
||||||
sensor.AddObservation(Condition.ArmourPoints);
|
sensor.AddObservation(Condition.ArmourPoints);
|
||||||
sensor.AddObservation(Condition.Ammunition);
|
sensor.AddObservation(Condition.Ammunition);
|
||||||
sensor.AddObservation((int)NPC_State.State);
|
sensor.AddObservation((int)NPC_State.State);
|
||||||
|
sensor.AddObservation((!flagZone.isNotOccup).ToInt());
|
||||||
|
sensor.AddObservation(AgentCharacter.LastTimeHit);
|
||||||
|
sensor.AddObservation(Condition.GetHealthPointsInQuantile());
|
||||||
|
sensor.AddObservation(candidates.Count);
|
||||||
|
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, AgentCharacter.Team));
|
||||||
|
|
||||||
var candidates = moveController.getPointsCandidate();
|
|
||||||
foreach (var point in candidates)
|
foreach (var point in candidates)
|
||||||
{
|
{
|
||||||
|
Debug.Log((float)moveController.CurrentNavPoint.PointId);
|
||||||
|
|
||||||
bufferSensor.AppendObservation(new float[] {
|
bufferSensor.AppendObservation(new float[] {
|
||||||
//1 position in navpointId
|
//1 position in navpointId
|
||||||
(float)moveController.currentPosition.PointId,
|
(float)moveController.CurrentNavPoint.PointId,
|
||||||
//2 distance to flag
|
//2 distance to flag
|
||||||
moveController.currentPosition.FlagDistance,
|
moveController.FlagDistance,
|
||||||
//3 death count in point
|
//3 death count in point
|
||||||
moveController.currentPosition.DeathAttr,
|
moveController.CurrentNavPoint.DeathAttr,
|
||||||
//4 flagEnemyDistance
|
//4 flagEnemyDistance
|
||||||
GameManager.IsCloserToFlagFromNextNavPoint(point, transform.position)==true?1:0,
|
GameManager.IsCloserToFlagFromNextNavPoint(point, transform.position).ToInt(),
|
||||||
//5 EnemyVsNavPointDistance
|
//5 EnemyVsNavPointDistance
|
||||||
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team)==true?1:0
|
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team).ToInt()
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -103,7 +112,7 @@ public class NPC : Agent, ICharacter
|
|||||||
if (Condition.HealthPoints < 0)
|
if (Condition.HealthPoints < 0)
|
||||||
{
|
{
|
||||||
OnKilledEvent?.Invoke(this);
|
OnKilledEvent?.Invoke(this);
|
||||||
moveController.currentPosition.DeathAttr += 1;
|
moveController.CurrentNavPoint.DeathAttr += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,6 +51,20 @@ public class GameManager : MonoBehaviour
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsEnemyNearby(Vector3 currentTransform, Team team)
|
||||||
|
{
|
||||||
|
SimpleMultiAgentGroup agentGroup;
|
||||||
|
if (team == Team.Attackers)
|
||||||
|
agentGroup = AttackersTeam;
|
||||||
|
else
|
||||||
|
agentGroup = DefendersTeam;
|
||||||
|
|
||||||
|
foreach (var agent in agentGroup.GetRegisteredAgents())
|
||||||
|
if ((currentTransform - agent.transform.position).magnitude < SettingsReader.Instance.GetSettings.ViewDistance)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public static bool IsCloserToFlagFromNextNavPoint(NavPoint navPoint, Vector3 currentTransform)
|
public static bool IsCloserToFlagFromNextNavPoint(NavPoint navPoint, Vector3 currentTransform)
|
||||||
=> navPoint.FlagDistance < (currentTransform - GameObject.FindGameObjectWithTag("Flag").transform.position).magnitude;
|
=> navPoint.FlagDistance < (currentTransform - GameObject.FindGameObjectWithTag("Flag").transform.position).magnitude;
|
||||||
|
|
||||||
|
@ -9,14 +9,14 @@ public class FlagZone : MonoBehaviour
|
|||||||
public float TimeStayDefenders { get; private set; }
|
public float TimeStayDefenders { get; private set; }
|
||||||
private int occupDefenders;
|
private int occupDefenders;
|
||||||
private int occupAttackers;
|
private int occupAttackers;
|
||||||
private bool isOccupBoth => (occupDefenders>0) && (occupAttackers>0);
|
public bool isOccupBoth => (occupDefenders>0) && (occupAttackers>0);
|
||||||
private bool isNotOccup => (occupDefenders == 0) && (occupAttackers == 0);
|
public bool isNotOccup => (occupDefenders == 0) && (occupAttackers == 0);
|
||||||
private float timeForWin;
|
private float timeForWin;
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
|
|
||||||
timeForWin = SettingsReader.Instance.GetSettings.timeToWin;
|
timeForWin = SettingsReader.Instance.GetSettings.TimeToWin;
|
||||||
TimeStayAttackers = 0;
|
TimeStayAttackers = 0;
|
||||||
TimeStayDefenders = 0;
|
TimeStayDefenders = 0;
|
||||||
occupAttackers = 0;
|
occupAttackers = 0;
|
||||||
|
@ -3,30 +3,32 @@
|
|||||||
[CreateAssetMenu(fileName ="Game Settings", menuName = "Game/Settings", order = 51)]
|
[CreateAssetMenu(fileName ="Game Settings", menuName = "Game/Settings", order = 51)]
|
||||||
public class Settings : ScriptableObject
|
public class Settings : ScriptableObject
|
||||||
{
|
{
|
||||||
public bool isTesting;
|
public bool IsTesting;
|
||||||
|
|
||||||
public float timeToWin;
|
public float TimeToWin;
|
||||||
public float timeOut;
|
public float TimeOut;
|
||||||
|
|
||||||
[Header("movement")]
|
[Header("movement")]
|
||||||
public float movementDistance;
|
public float MovementDistance;
|
||||||
public float movementSpeed;
|
public float MovementSpeed;
|
||||||
|
|
||||||
public TypeAI defTeamAI;
|
public TypeAI DefTeamAI;
|
||||||
public TypeAI atcTeamAI;
|
public TypeAI AtcTeamAI;
|
||||||
public int numOfDefenders;
|
public int NumOfDefenders;
|
||||||
public int numOfAttackers;
|
public int NumOfAttackers;
|
||||||
public bool hasHumanDefender;
|
public bool HasHumanDefender;
|
||||||
public bool hasHumanAttacker;
|
public bool HasHumanAttacker;
|
||||||
|
|
||||||
public int healthPickupAmount;
|
public int HealthPickupAmount;
|
||||||
public int armourPickupAmount;
|
public int ArmourPickupAmount;
|
||||||
public int ammunitionPickupAmount;
|
public int AmmunitionPickupAmount;
|
||||||
public int pickupsAmount;
|
public int PickupsAmount;
|
||||||
|
|
||||||
public int maxHealth;
|
public int MaxHealth;
|
||||||
public int maxArmour;
|
public int MaxArmour;
|
||||||
public int maxAmmo;
|
public int MaxAmmo;
|
||||||
|
|
||||||
|
public float ViewDistance;
|
||||||
|
|
||||||
public float GetHitChanceInDirectPoint;
|
public float GetHitChanceInDirectPoint;
|
||||||
public float GetHitChanceInRunning;
|
public float GetHitChanceInRunning;
|
||||||
|
@ -13,7 +13,7 @@ public class AmmoPickUp : MonoBehaviour, IPickable
|
|||||||
|
|
||||||
public void PickObject(GameObject obj)
|
public void PickObject(GameObject obj)
|
||||||
{
|
{
|
||||||
obj.GetComponent<ICharacter>()?.GetCharacter.Condition.TakeAmmo(SettingsReader.Instance.GetSettings.ammunitionPickupAmount);
|
obj.GetComponent<ICharacter>()?.GetCharacter.Condition.TakeAmmo(SettingsReader.Instance.GetSettings.AmmunitionPickupAmount);
|
||||||
gameObject.SetActive(false);
|
gameObject.SetActive(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ public class ArmourPickUp : MonoBehaviour, IPickable
|
|||||||
|
|
||||||
public void PickObject(GameObject obj)
|
public void PickObject(GameObject obj)
|
||||||
{
|
{
|
||||||
obj.GetComponent<ICharacter>()?.GetCharacter.Condition.GiveArmour(SettingsReader.Instance.GetSettings.armourPickupAmount);
|
obj.GetComponent<ICharacter>()?.GetCharacter.Condition.GiveArmour(SettingsReader.Instance.GetSettings.ArmourPickupAmount);
|
||||||
gameObject.SetActive(false);
|
gameObject.SetActive(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ public class HealthPickUp : MonoBehaviour, IPickable
|
|||||||
|
|
||||||
public void PickObject(GameObject obj)
|
public void PickObject(GameObject obj)
|
||||||
{
|
{
|
||||||
obj.GetComponent<ICharacter>()?.GetCharacter.Condition.GiveHealth(SettingsReader.Instance.GetSettings.healthPickupAmount);
|
obj.GetComponent<ICharacter>()?.GetCharacter.Condition.GiveHealth(SettingsReader.Instance.GetSettings.HealthPickupAmount);
|
||||||
gameObject.SetActive(false);
|
gameObject.SetActive(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ public class PickUpSpawner : MonoBehaviour
|
|||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
pickups = new List<GameObject>();
|
pickups = new List<GameObject>();
|
||||||
var amount = SettingsReader.Instance.GetSettings.pickupsAmount;
|
var amount = SettingsReader.Instance.GetSettings.PickupsAmount;
|
||||||
for (int i = 0; i < amount; i++)
|
for (int i = 0; i < amount; i++)
|
||||||
pickups.Add(GameObject.Instantiate(healthPrefab, spawnPoints[Random.Range(0, spawnPoints.Count)].transform.position, Quaternion.identity));
|
pickups.Add(GameObject.Instantiate(healthPrefab, spawnPoints[Random.Range(0, spawnPoints.Count)].transform.position, Quaternion.identity));
|
||||||
for (int i = 0; i < amount; i++)
|
for (int i = 0; i < amount; i++)
|
||||||
|
7
Assets/Scripts/Utils/BoolToInteger.cs
Normal file
7
Assets/Scripts/Utils/BoolToInteger.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
public static class BoolExtension
|
||||||
|
{
|
||||||
|
public static int ToInt(this bool _bool)
|
||||||
|
{
|
||||||
|
return _bool == true ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Scripts/Utils/BoolToInteger.cs.meta
generated
Normal file
11
Assets/Scripts/Utils/BoolToInteger.cs.meta
generated
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: f48fff3c2eda14d4fba923fe8875f651
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Reference in New Issue
Block a user