to new git
This commit is contained in:
@ -7,12 +7,6 @@ public class Character
|
||||
|
||||
public Character()
|
||||
{
|
||||
Debug.Log("init");
|
||||
Condition = new CharacterCondition();
|
||||
}
|
||||
}
|
||||
|
||||
public interface ICharacter
|
||||
{
|
||||
Character GetCharacter { get; }
|
||||
}
|
@ -9,17 +9,17 @@ public class CharacterCondition
|
||||
public event Action<int> OnChangeAmmunitionEvent;
|
||||
|
||||
private int health;
|
||||
public int HealthPoints
|
||||
{
|
||||
get
|
||||
{
|
||||
return health;
|
||||
}
|
||||
public int HealthPoints
|
||||
{
|
||||
get
|
||||
{
|
||||
return health;
|
||||
}
|
||||
private set
|
||||
{
|
||||
health = value;
|
||||
OnChangeHealthEvent?.Invoke(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int GetHealthPointsInQuantile()
|
||||
@ -30,7 +30,7 @@ public class CharacterCondition
|
||||
return 1;
|
||||
else if (health < 75)
|
||||
return 2;
|
||||
else return 3;
|
||||
else return 3;
|
||||
}
|
||||
private int armour;
|
||||
public int ArmourPoints
|
||||
@ -45,6 +45,17 @@ public class CharacterCondition
|
||||
OnChangeArmourEvent?.Invoke(value);
|
||||
}
|
||||
}
|
||||
public int GetArmourPointsInQuantile()
|
||||
{
|
||||
if (armour < 25)
|
||||
return 0;
|
||||
else if (armour < 50)
|
||||
return 1;
|
||||
else if (armour < 75)
|
||||
return 2;
|
||||
else return 3;
|
||||
}
|
||||
|
||||
private int ammo;
|
||||
public int Ammunition
|
||||
{
|
||||
@ -60,6 +71,11 @@ public class CharacterCondition
|
||||
}
|
||||
|
||||
public CharacterCondition()
|
||||
{
|
||||
this.Reset();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
var settings = SettingsReader.Instance.GetSettings;
|
||||
ammo = settings.MaxAmmo;
|
||||
|
8
Assets/Scripts/Character/Interfaces.meta
generated
Normal file
8
Assets/Scripts/Character/Interfaces.meta
generated
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f23b6db3be1e4cd469fd18dfe3e39764
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
5
Assets/Scripts/Character/Interfaces/ICharacter.cs
Normal file
5
Assets/Scripts/Character/Interfaces/ICharacter.cs
Normal file
@ -0,0 +1,5 @@
|
||||
public interface ICharacter
|
||||
{
|
||||
Character GetCharacter { get; }
|
||||
void ResetCharacter();
|
||||
}
|
11
Assets/Scripts/Character/Interfaces/ICharacter.cs.meta
generated
Normal file
11
Assets/Scripts/Character/Interfaces/ICharacter.cs.meta
generated
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6dfb78244ae35c4db1326d5f5b73375
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
17
Assets/Scripts/Character/Interfaces/INpcBaseState.cs
Normal file
17
Assets/Scripts/Character/Interfaces/INpcBaseState.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using UnityEngine;
|
||||
|
||||
public interface INpcBaseState
|
||||
{
|
||||
NpcEnumState State { get; }
|
||||
bool InCover { get; }
|
||||
bool IsRunning { get; }
|
||||
bool InDirectPoint { get; }
|
||||
float HitChance { get; }
|
||||
float DoDamageChance { get; }
|
||||
}
|
||||
|
||||
public interface INpcBaseBodyState
|
||||
{
|
||||
NpcBodyState State { get; }
|
||||
Vector3 GetPointToHit(GameObject go);
|
||||
}
|
11
Assets/Scripts/Character/Interfaces/INpcBaseState.cs.meta
generated
Normal file
11
Assets/Scripts/Character/Interfaces/INpcBaseState.cs.meta
generated
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58b7e1962495ada4c8e6ee6219c99a20
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,24 +1,32 @@
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
public class MovementController : MonoBehaviour
|
||||
{
|
||||
public NavPoint CurrentNavPoint { get; set; }
|
||||
public int PointStartID { get; set; }
|
||||
public int PointEndID { get; private set; }
|
||||
public float FlagDistance { get; private set; }
|
||||
private GameObject flag;
|
||||
private const float updateFlagPositionDelay = 5;
|
||||
[SerializeField] private NavMeshAgent navMeshAgent;
|
||||
private const float updateFlagPositionDelay = 5;
|
||||
private const float updateReachedDestinationDelay = 5;
|
||||
|
||||
private void Start()
|
||||
[SerializeField] private NavMeshAgent navMeshAgent;
|
||||
[SerializeField] private GameObject flag;
|
||||
public float DistanceToGo { get; private set; }
|
||||
public float RemainingDistance => navMeshAgent.remainingDistance;
|
||||
private Dictionary<int, NavPoint> idNavPointDict;
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
navMeshAgent.speed = SettingsReader.Instance.GetSettings.MovementSpeed;
|
||||
idNavPointDict = MapManager.IDToNavPoint;
|
||||
InvokeRepeating(nameof(UpdateFlagPosition), 0, updateFlagPositionDelay);
|
||||
InvokeRepeating(nameof(ReachedDestination), 0, updateReachedDestinationDelay);
|
||||
}
|
||||
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
CancelInvoke(nameof(UpdateFlagPosition));
|
||||
@ -30,18 +38,46 @@ public class MovementController : MonoBehaviour
|
||||
}
|
||||
|
||||
public void MoveToRandomPoint()
|
||||
{
|
||||
Debug.Log(MapManager.navPoints == null);
|
||||
goToNextNavPoint(MapManager.navPoints[Random.Range(0, MapManager.navPoints.Count)]);
|
||||
{
|
||||
Debug.Log(MapManager.NavPoints == null);
|
||||
GoToNextNavPoint(MapManager.NavPoints[Random.Range(0, MapManager.NavPoints.Count)]);
|
||||
}
|
||||
|
||||
public List<NavPoint> getPointsCandidate()
|
||||
public List<NavPoint> GetPointsCandidate()
|
||||
{
|
||||
return MapManager.navPoints
|
||||
.Where(point => (CurrentNavPoint.position - point.position).magnitude < SettingsReader.Instance.GetSettings.MovementSpeed)
|
||||
return MapManager.NavPoints
|
||||
.Where(point =>
|
||||
(idNavPointDict[PointStartID].Position - point.Position).magnitude < SettingsReader.Instance.GetSettings.MovementDistance)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void goToNextNavPoint(NavPoint destination) =>
|
||||
navMeshAgent.SetDestination(destination.position);
|
||||
public void GoToNextNavPoint(NavPoint destination)
|
||||
{
|
||||
if (navMeshAgent.isStopped == true) navMeshAgent.isStopped = false;
|
||||
PointStartID = PointEndID;
|
||||
PointEndID = destination.PointId;
|
||||
navMeshAgent.SetDestination(destination.Position);
|
||||
DistanceToGo = navMeshAgent.remainingDistance;
|
||||
}
|
||||
|
||||
public void ReturnToStartPoint()
|
||||
{
|
||||
if (navMeshAgent.isStopped == true) navMeshAgent.isStopped = false;
|
||||
navMeshAgent.SetDestination(idNavPointDict[PointStartID].Position);
|
||||
PointEndID = PointStartID;
|
||||
PointStartID = -1;
|
||||
}
|
||||
|
||||
public void StopOnPath()
|
||||
{
|
||||
navMeshAgent.isStopped = true;
|
||||
PointStartID = -1;
|
||||
PointEndID = -1;
|
||||
}
|
||||
|
||||
public void ReachedDestination()
|
||||
{
|
||||
if ((navMeshAgent.isStopped == false) && (navMeshAgent.velocity.magnitude < 0.1))
|
||||
PointStartID = PointEndID;
|
||||
}
|
||||
}
|
||||
|
@ -1,108 +1,170 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.Sensors;
|
||||
using Unity.MLAgents.Actuators;
|
||||
using Unity.MLAgents.Sensors;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(MovementController))]
|
||||
[RequireComponent(typeof(MovementController),typeof(BufferSensor))]
|
||||
public class NPC : Agent, ICharacter
|
||||
{
|
||||
[HideInInspector]
|
||||
public Character AgentCharacter;
|
||||
private Character AgentCharacter;
|
||||
public CharacterCondition Condition;
|
||||
private FlagZone flagZone;
|
||||
private FlagZone flagZone = null;
|
||||
|
||||
public NPC_BaseState NPC_State { get; private set; }
|
||||
public INpcBaseState NpcState { get; private set; }
|
||||
public INpcBaseBodyState NpcBodyState { get; private set; }
|
||||
|
||||
public Character GetCharacter => AgentCharacter;
|
||||
|
||||
private NPC_DirectPointState DirectState;
|
||||
private NPC_InCoverState CoverState;
|
||||
private NPC_RunningState RunningState;
|
||||
private NpcDirectPointState DirectState;
|
||||
private NpcInCoverState CoverState;
|
||||
private NpcRunningState RunningState;
|
||||
|
||||
private NpcStandingState StandingState;
|
||||
private NpcCrouchingState CrouchingState;
|
||||
|
||||
private MovementController moveController;
|
||||
private BufferSensorComponent bufferSensor;
|
||||
|
||||
private Dictionary<int, NavPoint> navPointIdDict;
|
||||
|
||||
#region UnityEvents and ML
|
||||
private void Awake()
|
||||
{
|
||||
DirectState = new NPC_DirectPointState();
|
||||
CoverState = new NPC_InCoverState();
|
||||
RunningState = new NPC_RunningState();
|
||||
NPC_State = DirectState;
|
||||
DirectState = new NpcDirectPointState();
|
||||
CoverState = new NpcInCoverState();
|
||||
RunningState = new NpcRunningState();
|
||||
NpcState = DirectState;
|
||||
|
||||
CrouchingState = new NpcCrouchingState();
|
||||
StandingState = new NpcStandingState();
|
||||
NpcBodyState = StandingState;
|
||||
|
||||
AgentCharacter = new Character();
|
||||
Condition = AgentCharacter.Condition;
|
||||
|
||||
moveController = gameObject.GetComponent<MovementController>();
|
||||
bufferSensor = gameObject.GetComponent<BufferSensorComponent>();
|
||||
}
|
||||
|
||||
|
||||
public void ResetCharacter()
|
||||
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
if (flagZone == null)
|
||||
Debug.LogError("Flag Is Not Setted");
|
||||
|
||||
navPointIdDict = MapManager.IDToNavPoint;
|
||||
if (navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Condition = new CharacterCondition();
|
||||
EndEpisode();
|
||||
Debug.LogWarning("Pooled object was destroyed");
|
||||
}
|
||||
|
||||
public override void OnEpisodeBegin()
|
||||
{
|
||||
NPC_State = DirectState;
|
||||
{
|
||||
NpcState = DirectState;
|
||||
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
}
|
||||
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
var candidates = moveController.getPointsCandidate();
|
||||
var candidates = moveController.GetPointsCandidate();
|
||||
|
||||
sensor.AddObservation(Condition.HealthPoints);
|
||||
sensor.AddObservation(Condition.ArmourPoints);
|
||||
sensor.AddObservation(Condition.Ammunition);
|
||||
sensor.AddObservation((int)NPC_State.State);
|
||||
sensor.AddObservation((!flagZone.isNotOccup).ToInt());
|
||||
//common sensors
|
||||
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(candidates.Count);
|
||||
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, AgentCharacter.Team));
|
||||
|
||||
sensor.AddObservation(moveController.PointStartID);
|
||||
sensor.AddObservation(moveController.PointEndID);
|
||||
//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);
|
||||
|
||||
//point sensors
|
||||
foreach (var point in candidates)
|
||||
{
|
||||
Debug.Log((float)moveController.CurrentNavPoint.PointId);
|
||||
|
||||
bufferSensor.AppendObservation(new float[] {
|
||||
//1 position in navpointId
|
||||
(float)moveController.CurrentNavPoint.PointId,
|
||||
//2 distance to flag
|
||||
moveController.FlagDistance,
|
||||
//3 death count in point
|
||||
moveController.CurrentNavPoint.DeathAttr,
|
||||
point.DeathAttr,
|
||||
(int)point.navType,
|
||||
//4 flagEnemyDistance
|
||||
GameManager.IsCloserToFlagFromNextNavPoint(point, transform.position).ToInt(),
|
||||
//5 EnemyVsNavPointDistance
|
||||
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team).ToInt()
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public override void Heuristic(in ActionBuffers actionsOut)
|
||||
{
|
||||
var discreteActionsOut = actionsOut.DiscreteActions;
|
||||
if (Input.GetKeyDown(KeyCode.W))
|
||||
{
|
||||
discreteActionsOut[0] = 1;
|
||||
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team.GetOppositeTeam()).ToInt(),
|
||||
//6 Have been seen by enemy in this point
|
||||
GameManager.IsHaveSeenByEnemy(AgentCharacter.Team.GetOppositeTeam(),
|
||||
point.Position).ToInt()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnActionReceived(ActionBuffers actions)
|
||||
{
|
||||
if (actions.DiscreteActions[0] == 1)
|
||||
var result = actions.DiscreteActions;
|
||||
if (result[0] == 0)
|
||||
{
|
||||
moveController.MoveToRandomPoint();
|
||||
NPC_State = RunningState;
|
||||
if (navPointIdDict[moveController.PointStartID].navType != NavPointType.Cover)
|
||||
return;
|
||||
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;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
if (result[0] == 1)
|
||||
{
|
||||
if (navPointIdDict[moveController.PointStartID].navType != NavPointType.Direction)
|
||||
return;
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: moveController.GoToNextNavPoint(navPointIdDict[result[2]]);
|
||||
NpcState = RunningState; 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)
|
||||
return;
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: moveController.StopOnPath(); NpcState = DirectState; break;
|
||||
case 1: moveController.ReturnToStartPoint(); NpcState = RunningState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public event Action<object> OnKilledEvent;
|
||||
public event Action<NpcBodyState> OnChangePosition;
|
||||
private void Peek()
|
||||
{
|
||||
OnChangePosition?.Invoke(global::NpcBodyState.Standing);
|
||||
NpcBodyState = StandingState;
|
||||
}
|
||||
|
||||
private void Cover()
|
||||
{
|
||||
OnChangePosition?.Invoke(global::NpcBodyState.Crouching);
|
||||
NpcBodyState = CrouchingState;
|
||||
}
|
||||
|
||||
public event Action<int, Team> OnDamageRecieved;
|
||||
public void GetDamage(float damage)
|
||||
{
|
||||
AgentCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
|
||||
@ -111,13 +173,17 @@ public class NPC : Agent, ICharacter
|
||||
|
||||
if (Condition.HealthPoints < 0)
|
||||
{
|
||||
OnKilledEvent?.Invoke(this);
|
||||
moveController.CurrentNavPoint.DeathAttr += 1;
|
||||
MapManager.AddDeathAttributeToPoints(moveController.PointStartID, moveController.PointEndID,
|
||||
moveController.DistanceToGo, moveController.RemainingDistance);
|
||||
var pos = gameObject.transform.position;
|
||||
var id = moveController.PointStartID;
|
||||
CharacterFactory.Instance.ReSpawn(this, ref pos, ref id);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
public void ResetCharacter()
|
||||
{
|
||||
Debug.LogWarning("Pooled object was destroyed");
|
||||
Condition.Reset();
|
||||
EndEpisode();
|
||||
}
|
||||
}
|
||||
|
@ -1,46 +0,0 @@
|
||||
public enum NPC_EnumState
|
||||
{
|
||||
InCover,
|
||||
InDirectPoint,
|
||||
InRunning,
|
||||
}
|
||||
|
||||
public interface NPC_BaseState
|
||||
{
|
||||
NPC_EnumState State { get; }
|
||||
bool InCover { get; }
|
||||
bool IsRunning { get; }
|
||||
bool InDirectPoint { get; }
|
||||
float HitChance { get; }
|
||||
float DoDamageChance { get; }
|
||||
}
|
||||
|
||||
public class NPC_DirectPointState : NPC_BaseState
|
||||
{
|
||||
public bool InCover => false;
|
||||
public bool IsRunning => false;
|
||||
public bool InDirectPoint => false;
|
||||
public float HitChance => SettingsReader.Instance.GetSettings.GetHitChanceInDirectPoint;
|
||||
public float DoDamageChance => SettingsReader.Instance.GetSettings.DoDamageChanceInDirectPoint;
|
||||
public NPC_EnumState State => NPC_EnumState.InDirectPoint;
|
||||
}
|
||||
|
||||
public class NPC_RunningState : NPC_BaseState
|
||||
{
|
||||
public bool InCover => false;
|
||||
public bool IsRunning => true;
|
||||
public bool InDirectPoint => false;
|
||||
public float HitChance => SettingsReader.Instance.GetSettings.GetHitChanceInRunning;
|
||||
public float DoDamageChance => SettingsReader.Instance.GetSettings.DoDamageChanceInRunning;
|
||||
public NPC_EnumState State => NPC_EnumState.InRunning;
|
||||
}
|
||||
|
||||
public class NPC_InCoverState : NPC_BaseState
|
||||
{
|
||||
public bool InCover => true;
|
||||
public bool IsRunning => false;
|
||||
public bool InDirectPoint => false;
|
||||
public float HitChance => SettingsReader.Instance.GetSettings.GetHitChanceInCover;
|
||||
public float DoDamageChance => SettingsReader.Instance.GetSettings.DoDamageChanceInCover;
|
||||
public NPC_EnumState State => NPC_EnumState.InCover;
|
||||
}
|
68
Assets/Scripts/Character/NpcState.cs
Normal file
68
Assets/Scripts/Character/NpcState.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using UnityEngine;
|
||||
|
||||
public enum NpcEnumState
|
||||
{
|
||||
InCover,
|
||||
InDirectPoint,
|
||||
InRunning,
|
||||
}
|
||||
|
||||
public enum NpcBodyState
|
||||
{
|
||||
Crouching,
|
||||
Standing,
|
||||
}
|
||||
|
||||
public class NpcCrouchingState : INpcBaseBodyState
|
||||
{
|
||||
public NpcBodyState State => NpcBodyState.Crouching;
|
||||
|
||||
public Vector3 GetPointToHit(GameObject go)
|
||||
{
|
||||
MeshRenderer meshRenderer;
|
||||
go.TryGetComponent<MeshRenderer>(out meshRenderer);
|
||||
return meshRenderer.bounds.center;
|
||||
}
|
||||
}
|
||||
|
||||
public class NpcStandingState : INpcBaseBodyState
|
||||
{
|
||||
public NpcBodyState State => NpcBodyState.Standing;
|
||||
|
||||
public Vector3 GetPointToHit(GameObject go)
|
||||
{
|
||||
MeshRenderer meshRenderer;
|
||||
go.TryGetComponent<MeshRenderer>(out meshRenderer);
|
||||
return meshRenderer.bounds.center;
|
||||
}
|
||||
}
|
||||
|
||||
public class NpcDirectPointState : INpcBaseState
|
||||
{
|
||||
public bool InCover => false;
|
||||
public bool IsRunning => false;
|
||||
public bool InDirectPoint => false;
|
||||
public float HitChance => SettingsReader.Instance.GetSettings.GetHitChanceInDirectPoint;
|
||||
public float DoDamageChance => SettingsReader.Instance.GetSettings.DoDamageChanceInDirectPoint;
|
||||
public NpcEnumState State => NpcEnumState.InDirectPoint;
|
||||
}
|
||||
|
||||
public class NpcRunningState : INpcBaseState
|
||||
{
|
||||
public bool InCover => false;
|
||||
public bool IsRunning => true;
|
||||
public bool InDirectPoint => false;
|
||||
public float HitChance => SettingsReader.Instance.GetSettings.GetHitChanceInRunning;
|
||||
public float DoDamageChance => SettingsReader.Instance.GetSettings.DoDamageChanceInRunning;
|
||||
public NpcEnumState State => NpcEnumState.InRunning;
|
||||
}
|
||||
|
||||
public class NpcInCoverState : INpcBaseState
|
||||
{
|
||||
public bool InCover => true;
|
||||
public bool IsRunning => false;
|
||||
public bool InDirectPoint => false;
|
||||
public float HitChance => SettingsReader.Instance.GetSettings.GetHitChanceInCover;
|
||||
public float DoDamageChance => SettingsReader.Instance.GetSettings.DoDamageChanceInCover;
|
||||
public NpcEnumState State => NpcEnumState.InCover;
|
||||
}
|
@ -15,9 +15,9 @@ public class Player : MonoBehaviour, ICharacter
|
||||
Condition = PlayerCharacter.Condition;
|
||||
}
|
||||
|
||||
public void ResetCharacter()
|
||||
private void OnDestroy()
|
||||
{
|
||||
Condition = new CharacterCondition();
|
||||
Debug.LogWarning("Pooled object was destroyed");
|
||||
}
|
||||
|
||||
public event Action<object> OnKilledEvent;
|
||||
@ -27,12 +27,12 @@ public class Player : MonoBehaviour, ICharacter
|
||||
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);
|
||||
if (Condition.HealthPoints < 0)
|
||||
OnKilledEvent?.Invoke(this);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
public void ResetCharacter()
|
||||
{
|
||||
Debug.LogWarning("Pooled object was destroyed");
|
||||
Condition = new CharacterCondition();
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Barracuda;
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
|
||||
using static scr_Models;
|
||||
using static scr_Models;
|
||||
|
||||
public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
@ -14,7 +10,7 @@ public class scr_CharacterController : MonoBehaviour
|
||||
private Vector2 input_Movement;
|
||||
[HideInInspector]
|
||||
public Vector2 input_View;
|
||||
|
||||
|
||||
private Vector3 newCameraRotation;
|
||||
private Vector3 newCharacterRotation;
|
||||
|
||||
@ -22,14 +18,14 @@ public class scr_CharacterController : MonoBehaviour
|
||||
public Transform cameraHolder;
|
||||
public Transform feetTransform;
|
||||
|
||||
[Header("Settings")]
|
||||
[Header("Settings")]
|
||||
public PlayerSettingsModel playerSettings;
|
||||
|
||||
public float ViewClampYMin = -70;
|
||||
public float ViewClampYMax = 80;
|
||||
public LayerMask playerMask;
|
||||
|
||||
[Header("Gravity")]
|
||||
|
||||
[Header("Gravity")]
|
||||
public float gravityAmount;
|
||||
public float gravityMin;
|
||||
private float playerGravity;
|
||||
@ -37,14 +33,14 @@ public class scr_CharacterController : MonoBehaviour
|
||||
public Vector3 jumpingForce;
|
||||
private Vector3 jumpingForceVelocity;
|
||||
|
||||
[Header("Stance")]
|
||||
[Header("Stance")]
|
||||
public PlayerStance playerStance;
|
||||
public float playerStanceSmoothing;
|
||||
public CharacterStance playerStandStance;
|
||||
public CharacterStance playerCrouchStance;
|
||||
public CharacterStance playerProneStance;
|
||||
private float stanceCheckErrorMargin = 0.05f;
|
||||
|
||||
|
||||
private float cameraHeight;
|
||||
private float cameraHeightVelocity;
|
||||
|
||||
@ -61,13 +57,13 @@ public class scr_CharacterController : MonoBehaviour
|
||||
defaultInput.Character.Movement.performed += e => input_Movement = e.ReadValue<Vector2>();
|
||||
defaultInput.Character.View.performed += e => input_View = e.ReadValue<Vector2>();
|
||||
defaultInput.Character.Jump.performed += e => Jump();
|
||||
|
||||
|
||||
defaultInput.Character.Crouch.performed += e => Crouch();
|
||||
defaultInput.Character.Prone.performed += e => Prone();
|
||||
|
||||
|
||||
defaultInput.Character.Sprint.performed += e => ToggleSprint();
|
||||
defaultInput.Character.SprintReleased.performed += e => StopSprint();
|
||||
|
||||
|
||||
defaultInput.Enable();
|
||||
|
||||
newCameraRotation = cameraHolder.localRotation.eulerAngles;
|
||||
@ -80,7 +76,7 @@ public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
currentWeapon.Initialise(this);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@ -95,10 +91,10 @@ public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
newCharacterRotation.y += playerSettings.ViewXSensetivity * (playerSettings.ViewXInverted ? -input_View.x : input_View.x) * Time.deltaTime;
|
||||
transform.localRotation = Quaternion.Euler(newCharacterRotation);
|
||||
|
||||
|
||||
newCameraRotation.x += playerSettings.ViewYSensetivity * (playerSettings.ViewYInverted ? input_View.y : -input_View.y) * Time.deltaTime;
|
||||
newCameraRotation.x = Mathf.Clamp(newCameraRotation.x, ViewClampYMin, ViewClampYMax);
|
||||
|
||||
|
||||
cameraHolder.localRotation = Quaternion.Euler(newCameraRotation);
|
||||
}
|
||||
|
||||
@ -108,7 +104,7 @@ public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
isSprinting = false;
|
||||
}
|
||||
|
||||
|
||||
var verticalSpeed = playerSettings.WalkingForwardSpeed;
|
||||
var horizontalSpeed = playerSettings.WalkingStrafeSpeed;
|
||||
|
||||
@ -117,17 +113,17 @@ public class scr_CharacterController : MonoBehaviour
|
||||
verticalSpeed = playerSettings.RunningForwardSpeed;
|
||||
horizontalSpeed = playerSettings.RunningStrafeSpeed;
|
||||
}
|
||||
|
||||
|
||||
// Effectors
|
||||
if (!characterController.isGrounded)
|
||||
{
|
||||
playerSettings.SpeedEffector = playerSettings.FallingSpeedEffector;
|
||||
}
|
||||
else if(playerStance == PlayerStance.Crouch)
|
||||
else if (playerStance == PlayerStance.Crouch)
|
||||
{
|
||||
playerSettings.SpeedEffector = playerSettings.CrouchSpeedEffector;
|
||||
}
|
||||
else if(playerStance == PlayerStance.Prone)
|
||||
}
|
||||
else if (playerStance == PlayerStance.Prone)
|
||||
{
|
||||
playerSettings.SpeedEffector = playerSettings.ProneSpeedEffector;
|
||||
}
|
||||
@ -135,15 +131,15 @@ public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
playerSettings.SpeedEffector = 1;
|
||||
}
|
||||
|
||||
|
||||
verticalSpeed *= playerSettings.SpeedEffector;
|
||||
horizontalSpeed *= playerSettings.SpeedEffector;
|
||||
|
||||
|
||||
newMovementSpeed = Vector3.SmoothDamp(newMovementSpeed,
|
||||
new Vector3(horizontalSpeed * input_Movement.x * Time.deltaTime,
|
||||
0, verticalSpeed * input_Movement.y * Time.deltaTime),
|
||||
ref newMovementSpeedVelocity, characterController.isGrounded ? playerSettings.MovementSmoothing : playerSettings.FallingSmoothing);
|
||||
|
||||
|
||||
var MovementSpeed = transform.TransformDirection(newMovementSpeed);
|
||||
|
||||
if (playerGravity > gravityMin)
|
||||
@ -158,7 +154,7 @@ public class scr_CharacterController : MonoBehaviour
|
||||
|
||||
MovementSpeed.y += playerGravity;
|
||||
MovementSpeed += jumpingForce * Time.deltaTime;
|
||||
|
||||
|
||||
characterController.Move(MovementSpeed);
|
||||
}
|
||||
|
||||
@ -179,7 +175,7 @@ public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
stanceHeight = playerProneStance.CameraHeight;
|
||||
}
|
||||
|
||||
|
||||
cameraHeight = Mathf.SmoothDamp(cameraHolder.localPosition.y, stanceHeight, ref cameraHeightVelocity, playerStanceSmoothing);
|
||||
|
||||
cameraHolder.localPosition = new Vector3(cameraHolder.localPosition.x, cameraHeight, cameraHolder.localPosition.z);
|
||||
@ -190,7 +186,7 @@ public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (playerStance == PlayerStance.Crouch)
|
||||
{
|
||||
if (StanceCheck(playerStandStance.StanceCollider.height))
|
||||
@ -200,7 +196,7 @@ public class scr_CharacterController : MonoBehaviour
|
||||
playerStance = PlayerStance.Stand;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Jump
|
||||
jumpingForce = Vector3.up * playerSettings.JumpingHeight;
|
||||
playerGravity = 0;
|
||||
@ -233,8 +229,8 @@ public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
var start = new Vector3(feetTransform.position.x, feetTransform.position.y + characterController.radius + stanceCheckErrorMargin, feetTransform.position.z);
|
||||
var end = new Vector3(feetTransform.position.x, feetTransform.position.y - characterController.radius - stanceCheckErrorMargin + stanceCheckheight, feetTransform.position.z);
|
||||
|
||||
|
||||
|
||||
|
||||
return Physics.CheckCapsule(start, end, characterController.radius, playerMask);
|
||||
}
|
||||
|
||||
@ -247,7 +243,7 @@ public class scr_CharacterController : MonoBehaviour
|
||||
}
|
||||
isSprinting = !isSprinting;
|
||||
}
|
||||
|
||||
|
||||
private void StopSprint()
|
||||
{
|
||||
if (playerSettings.SprintingHold)
|
||||
@ -255,5 +251,5 @@ public class scr_CharacterController : MonoBehaviour
|
||||
isSprinting = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -3,73 +3,73 @@ using UnityEngine;
|
||||
|
||||
public static class scr_Models
|
||||
{
|
||||
#region Player
|
||||
|
||||
public enum PlayerStance
|
||||
{
|
||||
Stand,
|
||||
Crouch,
|
||||
Prone
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class PlayerSettingsModel
|
||||
{
|
||||
[Header("View Settings")]
|
||||
public float ViewXSensetivity;
|
||||
public float ViewYSensetivity;
|
||||
#region Player
|
||||
|
||||
public bool ViewXInverted;
|
||||
public bool ViewYInverted;
|
||||
public enum PlayerStance
|
||||
{
|
||||
Stand,
|
||||
Crouch,
|
||||
Prone
|
||||
}
|
||||
|
||||
[Header("Movement Settings")]
|
||||
public bool SprintingHold;
|
||||
public float MovementSmoothing;
|
||||
|
||||
[Header("Movement - Running")]
|
||||
public float RunningForwardSpeed;
|
||||
public float RunningStrafeSpeed;
|
||||
|
||||
[Header("Movement - Walking")]
|
||||
public float WalkingForwardSpeed;
|
||||
public float WalkingBackwardSpeed;
|
||||
public float WalkingStrafeSpeed;
|
||||
[Serializable]
|
||||
public class PlayerSettingsModel
|
||||
{
|
||||
[Header("View Settings")]
|
||||
public float ViewXSensetivity;
|
||||
public float ViewYSensetivity;
|
||||
|
||||
[Header("Jumping")]
|
||||
public float JumpingHeight;
|
||||
public float JumpingFalloff;
|
||||
public float FallingSmoothing;
|
||||
public bool ViewXInverted;
|
||||
public bool ViewYInverted;
|
||||
|
||||
[Header("Speed Effectors")]
|
||||
public float SpeedEffector = 1;
|
||||
public float CrouchSpeedEffector;
|
||||
public float ProneSpeedEffector;
|
||||
public float FallingSpeedEffector;
|
||||
}
|
||||
[Header("Movement Settings")]
|
||||
public bool SprintingHold;
|
||||
public float MovementSmoothing;
|
||||
|
||||
[Serializable]
|
||||
public class CharacterStance
|
||||
{
|
||||
public float CameraHeight;
|
||||
public CapsuleCollider StanceCollider;
|
||||
}
|
||||
[Header("Movement - Running")]
|
||||
public float RunningForwardSpeed;
|
||||
public float RunningStrafeSpeed;
|
||||
|
||||
#endregion
|
||||
[Header("Movement - Walking")]
|
||||
public float WalkingForwardSpeed;
|
||||
public float WalkingBackwardSpeed;
|
||||
public float WalkingStrafeSpeed;
|
||||
|
||||
#region - Weapons -
|
||||
[Header("Jumping")]
|
||||
public float JumpingHeight;
|
||||
public float JumpingFalloff;
|
||||
public float FallingSmoothing;
|
||||
|
||||
[Serializable]
|
||||
public class WeaponSettingsModel
|
||||
{
|
||||
[Header("Sway")]
|
||||
public float SwayAmount;
|
||||
public bool SwayYInverted;
|
||||
public bool SwayXInverted;
|
||||
public float SwaySmoothing;
|
||||
public float SwayResetSmoothing;
|
||||
public float SwayClampX;
|
||||
public float SwayClampY;
|
||||
}
|
||||
[Header("Speed Effectors")]
|
||||
public float SpeedEffector = 1;
|
||||
public float CrouchSpeedEffector;
|
||||
public float ProneSpeedEffector;
|
||||
public float FallingSpeedEffector;
|
||||
}
|
||||
|
||||
#endregion
|
||||
[Serializable]
|
||||
public class CharacterStance
|
||||
{
|
||||
public float CameraHeight;
|
||||
public CapsuleCollider StanceCollider;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Weapons -
|
||||
|
||||
[Serializable]
|
||||
public class WeaponSettingsModel
|
||||
{
|
||||
[Header("Sway")]
|
||||
public float SwayAmount;
|
||||
public bool SwayYInverted;
|
||||
public bool SwayXInverted;
|
||||
public float SwaySmoothing;
|
||||
public float SwayResetSmoothing;
|
||||
public float SwayClampX;
|
||||
public float SwayClampY;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
Reference in New Issue
Block a user