Add random point moving.

This commit is contained in:
2022-04-13 11:00:07 +07:00
parent 5371fe0f9c
commit b9b2f3ddf7
43 changed files with 3408 additions and 711 deletions

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 9fbac871417fccb40831b0f935247a29
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,17 +0,0 @@
using Unity;
using UnityEngine;
public abstract class BaseBehaviour
{
protected NPC thisNPC;
protected IDoActivity DoActivity;
protected BaseBehaviour(NPC npc)
{
thisNPC = npc;
}
public void DoAction()
{
DoActivity?.DoActivity();
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 60bdb2d866ca9324cbe3639e7c47ae23
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 362d340e6754883459f3dc89c9ddc476
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
public class DumbAttacker : BaseBehaviour
{
public DumbAttacker(NPC npc) : base(npc)
{
DoActivity = new DumbAttackerBehaviour();
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 138d13d4fe8a06444acb1da6bfc55aa7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +0,0 @@
public class DumbDefender : BaseBehaviour
{
public DumbDefender(NPC npc) : base(npc)
{
DoActivity = new DumbDefenderBehaviour();
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 64552bae354dc614d8012f07511a51e8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,7 +0,0 @@
public class Human : BaseBehaviour
{
public Human(NPC npc) : base(npc)
{
DoActivity = new HumanBehaviour();
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 59d3d253756147e469e418971625a04c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 93364cc95c8c9764e83d70fcce9da482
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,9 +0,0 @@
using UnityEngine;
class DumbAttackerBehaviour : IDoActivity
{
public void DoActivity()
{
Debug.Log("I do attackers things!");
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d2cc7b0640887454e96d42f6bd066750
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,9 +0,0 @@
using UnityEngine;
class DumbDefenderBehaviour : IDoActivity
{
public void DoActivity()
{
Debug.Log("I do defenders things!");
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 27106553be3f26b4da5220e39a3098c3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
class HumanBehaviour : IDoActivity
{
public void DoActivity()
{
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: d207d14aaf634504e84f3e3bd8b52428
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,4 +0,0 @@
public interface IDoActivity
{
void DoActivity();
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 8e7275817852aa941963daa476581224
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 93133f9c3db1b944d9120ea789988f9b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,45 +0,0 @@
public abstract class AbstractCharacterFactory
{
protected IDoActivity behaviour;
protected AbstractCharacterFactory() { }
public abstract BaseBehaviour CreateCharacterBehaviour(NPC npc);
}
public class DumbDefenderFactory : AbstractCharacterFactory
{
public DumbDefenderFactory()
{
behaviour = new DumbDefenderBehaviour();
}
public override BaseBehaviour CreateCharacterBehaviour(NPC npc)
{
return new DumbDefender(npc);
}
}
public class DumbAttackerFactory : AbstractCharacterFactory
{
public DumbAttackerFactory()
{
behaviour = new DumbAttackerBehaviour();
}
public override BaseBehaviour CreateCharacterBehaviour(NPC npc)
{
return new DumbAttacker(npc);
}
}
public class HumanFactory : AbstractCharacterFactory
{
public HumanFactory()
{
behaviour = new HumanBehaviour();
}
public override BaseBehaviour CreateCharacterBehaviour(NPC npc)
{
return new Human(npc);
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 4f973f98c4f699745a605d09e2c1e46e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,55 +0,0 @@
using System.Collections.Generic;
using System;
using UnityEngine;
public class CharacterSpawner : MonoBehaviour
{
[SerializeField] private GameObject botPrefab;
[SerializeField] private List<NavPoint> defendersSpawnPoints;
[SerializeField] private List<NavPoint> attackersSpawnPoints;
private static Dictionary<(TypeAI, Team), Func<NPC, BaseBehaviour>> behaviourDictionary;
private static System.Random random;
public void Start()
{
behaviourDictionary = new Dictionary<(TypeAI, Team), Func<NPC, BaseBehaviour>>()
{
{ (TypeAI.DumbAlgorithm, Team.Attackers), new Func<NPC,BaseBehaviour>( npc => new DumbAttacker(npc))},
{ (TypeAI.DumbAlgorithm, Team.Defenders), new Func<NPC,BaseBehaviour>( npc => new DumbDefender(npc)) },
{ (TypeAI.HumanAI, Team.Defenders), new Func<NPC,BaseBehaviour>( npc => new Human(npc))},
{ (TypeAI.HumanAI, Team.Attackers), new Func<NPC,BaseBehaviour>( npc => new Human(npc))},
//And Other behaviours
};
if (SettingsReader.Instance.GetSettings.hasHumanAttacker && SettingsReader.Instance.GetSettings.hasHumanDefender)
throw new System.Exception("Not allowed to have two players");
else if (SettingsReader.Instance.GetSettings.hasHumanAttacker == true)
{
spawnCharacter(behaviourDictionary[(TypeAI.HumanAI, Team.Attackers)], Team.Attackers);
}
else if (SettingsReader.Instance.GetSettings.hasHumanDefender == true)
{
spawnCharacter(behaviourDictionary[(TypeAI.HumanAI, Team.Defenders)], Team.Defenders);
}
for (int i = 0; i < SettingsReader.Instance.GetSettings.numOfAttackers - (SettingsReader.Instance.GetSettings.hasHumanAttacker ? 1 : 0); i++)
{
spawnCharacter(behaviourDictionary[(SettingsReader.Instance.GetSettings.atcTeamAI, Team.Attackers)], Team.Attackers);
}
for (int i = 0; i < SettingsReader.Instance.GetSettings.numOfAttackers - (SettingsReader.Instance.GetSettings.hasHumanDefender ? 1 : 0); i++)
{
spawnCharacter(behaviourDictionary[(SettingsReader.Instance.GetSettings.defTeamAI, Team.Defenders)], Team.Defenders);
}
}
private void spawnCharacter(Func<NPC, BaseBehaviour> behaviourFunc, Team team)
{
var spawnPoint = team == Team.Defenders ?
defendersSpawnPoints[random.Next(0, defendersSpawnPoints.Count)].position :
attackersSpawnPoints[random.Next(0, attackersSpawnPoints.Count)].position;
var entity = Instantiate(botPrefab, spawnPoint, Quaternion.identity);
var npc = entity.GetComponent<NPC>();
npc.SetBehaviour(behaviourFunc(npc));
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 672f5411fc3ccb74d8a17a6efdee9df4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -3,11 +3,15 @@ using UnityEngine;
public class MapManager : MonoBehaviour
{
public List<NavPoint> navPoints { get; private set; }
public static List<NavPoint> navPoints { get; private set; }
private void Start()
{
navPoints = new List<NavPoint>();
var navPointsGameObj = GameObject.FindGameObjectsWithTag("Point");
foreach (var gameobj in navPointsGameObj)
{
Debug.Log(" a ");
navPoints.Add(gameobj.GetComponent<NavPoint>());
}
}
}

View File

@ -17,7 +17,6 @@ public class FlagZone : MonoBehaviour
{
timeForWin = SettingsReader.Instance.GetSettings.timeToWin;
Debug.Log("32");
TimeStayAttackers = 0;
TimeStayDefenders = 0;
occupAttackers = 0;

View File

@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
private void Start()
{
}
void Update()
{
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 88ea68cab52d1a64c8fc9976e156a380
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,6 +3,8 @@
[CreateAssetMenu(fileName ="Game Settings", menuName = "Game/Settings", order = 51)]
public class Settings : ScriptableObject
{
public bool isTesting;
public float timeToWin;
public float timeOut;
@ -21,4 +23,8 @@ public class Settings : ScriptableObject
public int armourPickupAmount;
public int ammunitionPickupAmount;
public int pickupsAmount;
public int maxHealth;
public int maxArmour;
public int maxAmmo;
}

View File

@ -9,7 +9,6 @@ public class SettingsReader : MonoBehaviour
private void Awake()
{
Debug.Log("init");
instance = this;
}

View File

@ -1,5 +0,0 @@
public interface ISensor<T>
{
T GetValue();
SensorType GetSensorType();
}

View File

@ -0,0 +1,4 @@
using System.Collections.Generic;
using Unity.MLAgents.Sensors;

View File

View File

@ -739,8 +739,4 @@ public abstract class DictionaryDrawer<TK, TV> : PropertyDrawer
UnityEngine.Debug.Log(e.Message);
}
}
}
[Serializable] public class DictionaryOfTeamsAndNavPoints : SerializableDictionary<Team, NavPoint> { }
[CustomPropertyDrawer(typeof(DictionaryOfTeamsAndNavPoints))]
public class DictionaryOfTeamsAndNavPointsDrawer : DictionaryDrawer<Team, NavPoint> { }
}