Almost Final Version
This commit is contained in:
8
Assets/Scripts/Animators/Leonid Animator/Bot.meta
generated
Normal file
8
Assets/Scripts/Animators/Leonid Animator/Bot.meta
generated
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09d06d136b63f664ca69050e45726199
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Animators.Leonid_Animator.Bot
|
||||
{
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[RequireComponent(typeof(AnimatorHandler))]
|
||||
[RequireComponent(typeof(MovementController))]
|
||||
[RequireComponent(typeof(NPC))]
|
||||
public class BotLocomotion : MonoBehaviour
|
||||
{
|
||||
private Vector3 _moveDirection;
|
||||
private Transform _myTransform;
|
||||
|
||||
private AnimatorHandler _myAnimatorHandler;
|
||||
private MovementController _movementController;
|
||||
private NPC _npc;
|
||||
private Rigidbody _myRigidbody;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_myRigidbody = GetComponent<Rigidbody>();
|
||||
_myAnimatorHandler = GetComponent<AnimatorHandler>();
|
||||
_movementController = GetComponent<MovementController>();
|
||||
_npc = GetComponent<NPC>();
|
||||
|
||||
_myTransform = transform;
|
||||
_myAnimatorHandler.Initialize();
|
||||
}
|
||||
|
||||
public void UpdateAnimatorValues()
|
||||
{
|
||||
var movementDir = _movementController.Velocity;
|
||||
|
||||
_myAnimatorHandler.UpdateAnimatorValues(movementDir.y, movementDir.x,
|
||||
false, _npc.NpcBodyState is NpcCrouchingState, _npc.IsFiring);
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Animators/Leonid Animator/Bot/BotLocomotion.cs.meta
generated
Normal file
3
Assets/Scripts/Animators/Leonid Animator/Bot/BotLocomotion.cs.meta
generated
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ad80a2bc9a9477f93ab26af02a07582
|
||||
timeCreated: 1652616279
|
8
Assets/Scripts/Animators/Leonid Animator/Player.meta
generated
Normal file
8
Assets/Scripts/Animators/Leonid Animator/Player.meta
generated
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0f4aef398dbeb02429a5834250b01855
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,6 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Animators.Leonid_Animator
|
||||
namespace Animators.Leonid_Animator.Player
|
||||
{
|
||||
[RequireComponent(
|
||||
typeof(Rigidbody),
|
||||
@ -17,13 +17,11 @@ namespace Animators.Leonid_Animator
|
||||
|
||||
public Rigidbody myRigidbody;
|
||||
[SerializeField] public float jumpForce;
|
||||
public GameObject normalCamera;
|
||||
|
||||
[Header("Stats")]
|
||||
[SerializeField] private float movementSpeed = 5;
|
||||
[SerializeField] private float rotationSpeed = 10;
|
||||
|
||||
public float health = 100f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
myRigidbody = GetComponent<Rigidbody>();
|
||||
@ -37,12 +35,6 @@ namespace Animators.Leonid_Animator
|
||||
|
||||
private void Update()
|
||||
{
|
||||
|
||||
if (Input.GetButtonDown("Fire1") && health > 0)
|
||||
{
|
||||
health -= 5;
|
||||
}
|
||||
|
||||
var deltaTime = Time.deltaTime;
|
||||
_inputHandler.TickInput(deltaTime);
|
||||
_moveDirection = _cameraObject.forward * _inputHandler.vertical
|
@ -1,7 +1,7 @@
|
||||
using CameraScripts;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Animators.Leonid_Animator
|
||||
namespace Animators.Leonid_Animator.Player
|
||||
{
|
||||
public class InputHandler : MonoBehaviour
|
||||
{
|
@ -38,32 +38,30 @@ public class CharacterFactory : MonoBehaviour
|
||||
throw new System.ArgumentException("Can be only one human player");
|
||||
|
||||
for (int i = 0; i < attcNum - humanAtc; i++)
|
||||
InstanciateEntity(Team.Attackers, TypeAI.D0DiskAI,
|
||||
InstantiateEntity(Team.Attackers, TypeAI.D0DiskAI,
|
||||
spawnPointsForAttackersTeam[Random.Range(0, spawnPointsForAttackersTeam.Count)]);
|
||||
for (int i = 0; i < defNum - humanDef; i++)
|
||||
InstanciateEntity(Team.Defenders, TypeAI.D0DiskAI,
|
||||
InstantiateEntity(Team.Defenders, TypeAI.D0DiskAI,
|
||||
spawnPointsForDefendersTeam[Random.Range(0, spawnPointsForDefendersTeam.Count)]);
|
||||
if (humanAtc == 1)
|
||||
InstanciateEntity(Team.Attackers, TypeAI.HumanAI,
|
||||
InstantiateEntity(Team.Attackers, TypeAI.HumanAI,
|
||||
spawnPointsForAttackersTeam[Random.Range(0, spawnPointsForAttackersTeam.Count)]);
|
||||
if (humanDef == 1)
|
||||
InstanciateEntity(Team.Defenders, TypeAI.HumanAI,
|
||||
InstantiateEntity(Team.Defenders, TypeAI.HumanAI,
|
||||
spawnPointsForDefendersTeam[Random.Range(0, spawnPointsForDefendersTeam.Count)]);
|
||||
|
||||
GameManager.OnResetScene += ResetCharacters;
|
||||
}
|
||||
|
||||
private void InstanciateEntity(Team team, TypeAI typeAi, NavPoint spawnPoint)
|
||||
private void InstantiateEntity(Team team, TypeAI typeAi, NavPoint spawnPoint)
|
||||
{
|
||||
var gameobject = GameObject.Instantiate(
|
||||
typeAi == TypeAI.HumanAI ? PlayerPrefab : AIPrefab,
|
||||
spawnPoint.Position,
|
||||
Quaternion.identity);
|
||||
ApplyMaterial(team, gameobject);
|
||||
gameobject.SetActive(true);
|
||||
if (team == Team.Attackers)
|
||||
gameObject.tag = "Attacker";
|
||||
else
|
||||
gameObject.tag = "Defender";
|
||||
gameObject.tag = team == Team.Attackers ? "Attacker" : "Defender";
|
||||
|
||||
if (typeAi == TypeAI.HumanAI)
|
||||
{
|
||||
@ -73,13 +71,35 @@ public class CharacterFactory : MonoBehaviour
|
||||
else
|
||||
{
|
||||
var npc = gameobject.GetComponent<NPC>();
|
||||
if (team == Team.Attackers)
|
||||
GameManager._attackersTeam.RegisterAgent(npc);
|
||||
else
|
||||
{
|
||||
GameManager._defendersTeam.RegisterAgent(npc);
|
||||
}
|
||||
npc.GetCharacter.Team = team;
|
||||
npc.SetModel(team.ToString(), ScriptableObject.CreateInstance<NNModel>(), InferenceDevice.Default );
|
||||
npc.SetModel(team.ToString(), null, InferenceDevice.Default );
|
||||
gameobject.GetComponent<MovementController>().PointStartID = spawnPoint.PointId;
|
||||
bots.Add(gameobject);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyMaterial(Team team, GameObject go)
|
||||
{
|
||||
var mat = team == Team.Attackers
|
||||
? SettingsReader.Instance.GetSettings.atcMaterial
|
||||
: SettingsReader.Instance.GetSettings.defMaterial;
|
||||
|
||||
for (var i = 0; i < go.transform.childCount; i++)
|
||||
{
|
||||
if (go.transform.GetChild(i)
|
||||
.TryGetComponent<SkinnedMeshRenderer>(out var skin))
|
||||
{
|
||||
skin.material = mat;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ReSpawn(ICharacter character, ref Vector3 pos, ref int startPointId)
|
||||
{
|
||||
character.ResetCharacter();
|
||||
|
@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Animators.Leonid_Animator;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem.XR.Haptics;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
public class HealthBarScript : MonoBehaviour
|
||||
{
|
||||
public Image HealthBar;
|
||||
public float CurrentHealth;
|
||||
public float MaxHealth = 100f;
|
||||
public CharacterLocomotion Player;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
//HealthBar = GetComponent<Image>();
|
||||
//Player = FindObjectOfType<CharacterLocomotion>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
CurrentHealth = Player.health;
|
||||
HealthBar.fillAmount = CurrentHealth / MaxHealth;
|
||||
}
|
||||
}
|
11
Assets/Scripts/Character/HealthBarScript.cs.meta
generated
11
Assets/Scripts/Character/HealthBarScript.cs.meta
generated
@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b91f3cc71f5c4a343b1ec3269f69a9d5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -4,5 +4,7 @@ public interface ICharacter
|
||||
{
|
||||
Character GetCharacter { get; }
|
||||
void ResetCharacter();
|
||||
|
||||
void GetDamage(int damage);
|
||||
event Action<bool> OnDeathEvent;
|
||||
}
|
@ -1,11 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
public class MovementController : MonoBehaviour
|
||||
{
|
||||
private Transform _firePointTransform;
|
||||
public int PointStartID { get; set; }
|
||||
public int PointEndID { get; private set; }
|
||||
public float FlagDistance { get; private set; }
|
||||
@ -17,7 +20,6 @@ public class MovementController : MonoBehaviour
|
||||
public float DistanceToGo { get; private set; }
|
||||
public float RemainingDistance => navMeshAgent.remainingDistance;
|
||||
public Vector3 Velocity => navMeshAgent.velocity;
|
||||
|
||||
private Dictionary<int, NavPoint> _idNavPointDict;
|
||||
|
||||
|
||||
@ -25,10 +27,11 @@ public class MovementController : MonoBehaviour
|
||||
{
|
||||
navMeshAgent.speed = SettingsReader.Instance.GetSettings.MovementSpeed;
|
||||
_idNavPointDict = MapManager.Instance.IDToNavPoint;
|
||||
_firePointTransform = transform.GetChild(0);
|
||||
InvokeRepeating(nameof(UpdateFlagPosition), 0, UpdateFlagPositionDelay);
|
||||
InvokeRepeating(nameof(ReachedDestination), 0, UpdateReachedDestinationDelay);
|
||||
}
|
||||
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
CancelInvoke(nameof(UpdateFlagPosition));
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -19,9 +19,7 @@ public class NpcCrouchingState : INpcBaseBodyState
|
||||
|
||||
public Vector3 GetPointToHit(GameObject go)
|
||||
{
|
||||
MeshRenderer meshRenderer;
|
||||
go.TryGetComponent<MeshRenderer>(out meshRenderer);
|
||||
return meshRenderer.bounds.center;
|
||||
return go.transform.position + new Vector3(0, 1, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -31,9 +29,7 @@ public class NpcStandingState : INpcBaseBodyState
|
||||
|
||||
public Vector3 GetPointToHit(GameObject go)
|
||||
{
|
||||
MeshRenderer meshRenderer;
|
||||
go.TryGetComponent<MeshRenderer>(out meshRenderer);
|
||||
return meshRenderer.bounds.center;
|
||||
return go.transform.position + new Vector3(0, 1.8f, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ public class Player : MonoBehaviour, ICharacter
|
||||
}
|
||||
|
||||
public event Action<bool> OnDeathEvent;
|
||||
public void GetDamage(float damage)
|
||||
public void GetDamage(int damage)
|
||||
{
|
||||
PlayerCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
|
||||
Condition.GiveHealth(-Mathf.RoundToInt(damage * (1 - Condition.ArmourPoints * 0.5f)));
|
||||
|
@ -8,8 +8,8 @@ public class GameManager : MonoBehaviour
|
||||
private static GameManager _instance;
|
||||
public static GameManager Instance => _instance;
|
||||
|
||||
private static SimpleMultiAgentGroup _defendersTeam = new SimpleMultiAgentGroup();
|
||||
private static SimpleMultiAgentGroup _attackersTeam = new SimpleMultiAgentGroup();
|
||||
public static SimpleMultiAgentGroup _defendersTeam = new SimpleMultiAgentGroup();
|
||||
public static SimpleMultiAgentGroup _attackersTeam = new SimpleMultiAgentGroup();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@ -27,16 +27,6 @@ public class GameManager : MonoBehaviour
|
||||
Academy.Instance.OnEnvironmentReset += ResetScene;
|
||||
GlobalEventManager.OnCaptureFlag += FlagCaptured;
|
||||
GlobalEventManager.OnTimeLeft += TimeOut;
|
||||
|
||||
var agents = GameObject.FindObjectsOfType<Agent>();
|
||||
foreach (var item in agents)
|
||||
{
|
||||
var agent = item as NPC;
|
||||
if (agent.GetCharacter.Team == Team.Attackers)
|
||||
_attackersTeam.RegisterAgent(item);
|
||||
else
|
||||
_defendersTeam.RegisterAgent(item);
|
||||
}
|
||||
}
|
||||
|
||||
private static SimpleMultiAgentGroup GetAgentList(Team team)
|
||||
@ -91,17 +81,19 @@ public class GameManager : MonoBehaviour
|
||||
public static List<GameObject> GetVisibleEnemies(Team oppositeTeam, Vector3 position)
|
||||
{
|
||||
var agentGroup = GetAgentList(oppositeTeam);
|
||||
var enemies = new List<GameObject>();
|
||||
|
||||
RaycastHit rayHit = new RaycastHit();
|
||||
foreach (var agent in agentGroup.GetRegisteredAgents() )
|
||||
{
|
||||
var npc = agent as NPC;
|
||||
if (Physics.Raycast(position,
|
||||
if (npc != null && Physics.Raycast(position,
|
||||
(npc.NpcBodyState.GetPointToHit(npc.gameObject) - position).normalized,
|
||||
out rayHit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
if (rayHit.collider.gameObject.GetComponent<ICharacter>() != null)
|
||||
return null;
|
||||
enemies.Add(rayHit.collider.gameObject);
|
||||
}
|
||||
}
|
||||
if ((SettingsReader.Instance.GetSettings.HasHumanAttacker == true && oppositeTeam == Team.Attackers) ||
|
||||
@ -114,10 +106,10 @@ public class GameManager : MonoBehaviour
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
if (rayHit.collider.gameObject.GetComponent<ICharacter>() != null)
|
||||
return null;
|
||||
enemies.Add(rayHit.collider.gameObject);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return enemies;
|
||||
}
|
||||
|
||||
private void FlagCaptured(Team team)
|
||||
@ -133,7 +125,6 @@ public class GameManager : MonoBehaviour
|
||||
ResetScene();
|
||||
break;
|
||||
}
|
||||
ResetScene();
|
||||
}
|
||||
|
||||
private void TimeOut()
|
||||
|
@ -25,7 +25,6 @@ public class MapManager : MonoBehaviour
|
||||
for (var i=0; i < count; i++)
|
||||
NavPoints.Add(navPointSet.transform.GetChild(i)
|
||||
.gameObject.GetComponent<NavPoint>());
|
||||
print(NavPoints.Count);
|
||||
NavPointSetToID();
|
||||
}
|
||||
|
||||
|
@ -36,6 +36,13 @@ public class Settings : ScriptableObject
|
||||
public float DoDamageChanceInDirectPoint;
|
||||
public float DoDamageChanceInRunning;
|
||||
public float DoDamageChanceInCover;
|
||||
|
||||
public int RifleDamage;
|
||||
public int RateOfFire;
|
||||
|
||||
public float CrouchingCoefficient;
|
||||
|
||||
public Material atcMaterial;
|
||||
public Material defMaterial;
|
||||
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ public class StatisticManager : MonoBehaviour
|
||||
private void Awake()
|
||||
{
|
||||
foreach (var npc in GameObject.FindObjectsOfType<NPC>())
|
||||
npc.OnDamageRecieved += RegisterDamage;
|
||||
npc.OnDamageReceived += RegisterDamage;
|
||||
|
||||
GlobalEventManager.OnCaptureFlag += RegisterWin;
|
||||
GlobalEventManager.OnTimeLeft += RegisterTimeOut;
|
||||
|
@ -1,62 +1,97 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(NPC))]
|
||||
public class AimAssistant : MonoBehaviour
|
||||
{
|
||||
public GameObject enemy;
|
||||
public bool _isFiring = false;
|
||||
[HideInInspector] public GameObject enemy;
|
||||
private Shooting _shooting;
|
||||
public bool isFiring = false;
|
||||
private ICharacter _myNpc;
|
||||
public float lookSpeed = 200f;
|
||||
[SerializeField] private float lookSpeed = 400f;
|
||||
private Transform _myTransform;
|
||||
private float _fireCountdown;
|
||||
|
||||
//Я пытался придумать как обойти костыли с корутиной, но как видите ничего кроме других костылей не придумал.
|
||||
private bool _firelock = false;
|
||||
public bool fireAnimation = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_myNpc = GetComponent<NPC>();
|
||||
_shooting = GetComponent<Shooting>();
|
||||
_myTransform = transform;
|
||||
_fireCountdown = 1f / SettingsReader.Instance.GetSettings.RateOfFire;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
private void FixedUpdate()
|
||||
{
|
||||
//Ищем противника на сцене.
|
||||
if (_isFiring == false)
|
||||
if (isFiring == false)
|
||||
{
|
||||
var enemies = GameManager.GetVisibleEnemies(_myNpc.GetCharacter.Team, transform.position);
|
||||
enemy = enemies[new System.Random().Next(enemies.Count)];
|
||||
var enemies = GameManager.GetVisibleEnemies(_myNpc.GetCharacter.Team.GetOppositeTeam(), transform.position);
|
||||
if (enemies.Count == 0)
|
||||
{
|
||||
StopShooting();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
enemy = enemies[new System.Random().Next(enemies.Count)];
|
||||
var character = enemy.GetComponent<ICharacter>();
|
||||
character.OnDeathEvent += _ => _isFiring = false;
|
||||
_isFiring = true;
|
||||
character.OnDeathEvent += _ => isFiring = false;
|
||||
isFiring = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Raycast до противника enemy
|
||||
var range = 200f;
|
||||
while (true)
|
||||
if (Physics.Raycast(_myTransform.position, _myTransform.forward,
|
||||
out var hit, SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
if (!Physics.Raycast(this.transform.position, this.transform.forward, out var hit, range))
|
||||
if (hit.collider.gameObject != enemy)
|
||||
{
|
||||
_isFiring = false;
|
||||
StopCoroutine(nameof(Shooting));
|
||||
StopShooting();
|
||||
enemy = null;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetComponent<Shooting>().Shoot();
|
||||
}
|
||||
}
|
||||
|
||||
if (_firelock == false)
|
||||
{
|
||||
fireAnimation = true;
|
||||
StartCoroutine(nameof(Shooting));
|
||||
}
|
||||
_firelock = true;
|
||||
}
|
||||
}
|
||||
|
||||
var direction = enemy.transform.position - gameObject.transform.position;
|
||||
var targetRotation = Quaternion.LookRotation(direction);
|
||||
var lookAt = Quaternion.RotateTowards(gameObject.transform.rotation, targetRotation,
|
||||
Time.deltaTime * lookSpeed);
|
||||
lookAt.z = 0;
|
||||
lookAt.x = 0;
|
||||
gameObject.transform.rotation = lookAt;
|
||||
if (enemy != null)
|
||||
{
|
||||
var direction = enemy.transform.position - gameObject.transform.position;
|
||||
var targetRotation = Quaternion.LookRotation(direction);
|
||||
var lookAt = Quaternion.RotateTowards(gameObject.transform.rotation, targetRotation,
|
||||
Time.deltaTime * lookSpeed);
|
||||
lookAt.z = 0;
|
||||
lookAt.x = 0;
|
||||
gameObject.transform.rotation = lookAt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void StopShooting()
|
||||
{
|
||||
print("stop shooting");
|
||||
_firelock = false;
|
||||
isFiring = false;
|
||||
fireAnimation = false;
|
||||
}
|
||||
|
||||
private IEnumerator Shooting()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
print("in Shooting");
|
||||
_shooting.Shoot();
|
||||
yield return new WaitForSeconds(_fireCountdown);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,47 +1,46 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Shooting : MonoBehaviour
|
||||
{
|
||||
public float damage = 10f;
|
||||
public float range = 100f;
|
||||
|
||||
public GameObject raycast;
|
||||
|
||||
public GameObject FirePoint;
|
||||
public GameObject[] Prefabs;
|
||||
public GameObject firePoint;
|
||||
[SerializeField] private GameObject projectilePrefab;
|
||||
|
||||
private float hSliderValue = 0.1f;
|
||||
private float fireCountdown = 0f;
|
||||
|
||||
private int Prefab = 1;
|
||||
|
||||
private Ray RayMouse;
|
||||
private Vector3 direction;
|
||||
private Quaternion rotation;
|
||||
private float _fireCountdown = 1f;
|
||||
|
||||
public GameObject gun;
|
||||
void Update()
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetMouseButton(0) && fireCountdown <= 0f)
|
||||
{
|
||||
Instantiate(Prefabs[Prefab], FirePoint.transform.position, FirePoint.transform.rotation);
|
||||
fireCountdown = 0;
|
||||
fireCountdown += hSliderValue;
|
||||
Shoot();
|
||||
}
|
||||
fireCountdown -= Time.deltaTime;
|
||||
HandleMouseButton();
|
||||
}
|
||||
|
||||
private void HandleMouseButton()
|
||||
{
|
||||
if (Input.GetMouseButton(0) && _fireCountdown <= 0f)
|
||||
{
|
||||
_fireCountdown = 0;
|
||||
_fireCountdown += hSliderValue;
|
||||
Shoot();
|
||||
}
|
||||
_fireCountdown -= Time.deltaTime;
|
||||
}
|
||||
|
||||
public void Shoot()
|
||||
{
|
||||
if (Physics.Raycast(raycast.transform.position, raycast.transform.forward, out var hit, range))
|
||||
if (Physics.Raycast(raycast.transform.position,
|
||||
raycast.transform.forward, out var hit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
Debug.Log(hit.transform.name);
|
||||
|
||||
if (hit.transform.TryGetComponent<Target>(out var target))
|
||||
target.TakeDamage(damage);
|
||||
if (hit.transform.TryGetComponent<NPC>(out var target))
|
||||
{
|
||||
Instantiate(projectilePrefab, firePoint.transform.position, firePoint.transform.rotation);
|
||||
target.GetDamage(SettingsReader.Instance.GetSettings.RifleDamage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user