You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.9 KiB
C#
101 lines
3.9 KiB
C#
|
3 years ago
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using UnityEngine;
|
||
|
|
using SiegeSong;
|
||
|
|
|
||
|
|
namespace SiegeSong
|
||
|
|
{
|
||
|
|
public class ThreatAwareness : MonoBehaviour
|
||
|
|
{
|
||
|
|
public StatisticsManager Actor;
|
||
|
|
public Motor Motor;
|
||
|
|
public Navigator Navigator;
|
||
|
|
public AIHostility HostilityLevel;
|
||
|
|
|
||
|
|
public float MaxNearDistance = 5.0f;
|
||
|
|
public float MaxMediumDistance = 15.0f;
|
||
|
|
public float MaxFarDistance = 100.0f;
|
||
|
|
|
||
|
|
int SoftNoiseThreshold = 30;
|
||
|
|
int MediumNoiseThreshold = 70;
|
||
|
|
int LoudNoiseThreshold = 70;
|
||
|
|
|
||
|
|
float rotationTimer;
|
||
|
|
float rotationSpeed = 0.01f;
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool InvestigateThreat(StatisticsManager threatActor, bool navigateTowards = false, bool guarded = false)
|
||
|
|
{
|
||
|
|
Motor.RotateToDirection(Quaternion.Lerp(gameObject.transform.rotation, Quaternion.LookRotation(threatActor.gameObject.transform.position - gameObject.transform.position, Vector3.up), rotationTimer * rotationSpeed).eulerAngles);
|
||
|
|
if (navigateTowards)
|
||
|
|
{
|
||
|
|
Navigator.CurrentWayPointPosition = threatActor.gameObject.transform.position;
|
||
|
|
Navigator.MoveToWayPoint(guarded);
|
||
|
|
RaycastHit hit;
|
||
|
|
if (Physics.Linecast(gameObject.transform.position, gameObject.transform.forward, out hit))
|
||
|
|
{
|
||
|
|
var actor = hit.transform.gameObject.GetComponent<Statistics>();
|
||
|
|
if (actor != null)
|
||
|
|
{
|
||
|
|
var actorFactionRelationship = Actor.FactionManager.GetActorFactionRelationship(actor);
|
||
|
|
if (actorFactionRelationship == FactionRelationship.Rivals
|
||
|
|
|| actorFactionRelationship == FactionRelationship.Enemies
|
||
|
|
|| actorFactionRelationship == FactionRelationship.Combatants)
|
||
|
|
{
|
||
|
|
HostilityLevel = AIHostility.Hostile;
|
||
|
|
Actor.Stats.InCombat = true;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void CheckForLoudActors()
|
||
|
|
{
|
||
|
|
var hearingDistance = MaxNearDistance;
|
||
|
|
var guarded = false;
|
||
|
|
if (HostilityLevel == AIHostility.GuardingNonHostile || HostilityLevel == AIHostility.SearchingNotHostile)
|
||
|
|
{
|
||
|
|
hearingDistance = MaxMediumDistance;
|
||
|
|
guarded = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
var collidersInRange = Physics.OverlapSphere(gameObject.transform.position, hearingDistance);
|
||
|
|
|
||
|
|
StatisticsManager loudestThreat = null;
|
||
|
|
var threatIsLoud = false;
|
||
|
|
|
||
|
|
foreach (Collider collider in collidersInRange)
|
||
|
|
{
|
||
|
|
var actor = collider.transform.gameObject.GetComponent<StatisticsManager>();
|
||
|
|
if (actor != null)
|
||
|
|
{
|
||
|
|
var noiseBeingProduced = actor.Stats.NoiseBeingProduced;
|
||
|
|
|
||
|
|
if ((guarded && noiseBeingProduced > SoftNoiseThreshold && noiseBeingProduced <= MediumNoiseThreshold)
|
||
|
|
|| (noiseBeingProduced > MediumNoiseThreshold && noiseBeingProduced <= LoudNoiseThreshold))
|
||
|
|
if (loudestThreat == null || actor.Stats.NoiseBeingProduced > loudestThreat.Stats.NoiseBeingProduced)
|
||
|
|
loudestThreat = actor;
|
||
|
|
if (noiseBeingProduced > LoudNoiseThreshold)
|
||
|
|
{
|
||
|
|
if (loudestThreat == null || actor.Stats.NoiseBeingProduced > loudestThreat.Stats.NoiseBeingProduced)
|
||
|
|
loudestThreat = actor;
|
||
|
|
threatIsLoud = true;
|
||
|
|
}
|
||
|
|
InvestigateThreat(loudestThreat, threatIsLoud);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|