GlobalGameJame/Assets/Scripts/EnemyManager.cs

244 lines
8.5 KiB
C#
Raw Normal View History

2020-02-01 07:52:46 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
2021-08-17 16:42:47 +00:00
public class EnemyManager : MonoBehaviour
2020-02-01 07:52:46 +00:00
{
public float health = 100;
2020-02-01 11:11:56 +00:00
public float damage = 10;
public float attackRate = 2;
private float attackTime = 2;
2020-02-01 13:26:43 +00:00
public float actualHealth;
public float findRate = 1;
private float findTime = 1;
public List<AudioClip> deadSounds = new List<AudioClip>();
public AudioSource audio;
2020-02-01 07:52:46 +00:00
private NavMeshAgent navMeshAgent;
private PlayerManager player;
2020-02-01 15:20:10 +00:00
public BarricadeManager barricadeManager;
2020-02-01 09:12:47 +00:00
public List<GameObject> bodyParts = new List<GameObject>();
2020-02-01 07:52:46 +00:00
// Start is called before the first frame update
void Start()
{
findTime = findRate;
2020-02-01 11:11:56 +00:00
attackTime = attackRate;
2020-02-01 09:12:47 +00:00
actualHealth = health;
2020-02-01 07:52:46 +00:00
player = FindObjectOfType<PlayerManager>();
navMeshAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
2020-02-01 09:12:47 +00:00
DropBodyPart();
if (actualHealth <= 0)
2020-02-01 07:52:46 +00:00
{
if (deadSounds.Count > 0)
{
int rand = Random.Range(0, deadSounds.Count);
audio.PlayOneShot(deadSounds[rand]);
}
2020-02-01 13:26:43 +00:00
Destroy(transform.gameObject, 0.1f);
2020-02-01 11:11:56 +00:00
}
DestroyBarricades();
2020-02-01 13:26:43 +00:00
Attack();
2020-02-01 15:20:10 +00:00
if (navMeshAgent != null && barricadeManager == null && findTime < Time.time)
2020-02-01 11:11:56 +00:00
{
navMeshAgent.SetDestination(player.transform.position);
findTime = findRate + Time.time;
2020-02-01 11:11:56 +00:00
}
else if (navMeshAgent != null && barricadeManager != null && findTime < Time.time)
2020-02-01 11:11:56 +00:00
{
navMeshAgent.SetDestination(barricadeManager.transform.position);
findTime = findRate + Time.time;
2020-02-01 09:12:47 +00:00
}
}
void FixedUpdate()
{
if (actualHealth > 0 && barricadeManager == null)
2020-02-01 11:11:56 +00:00
{
Vector3 _dir = player.transform.position - transform.position;
_dir.Normalize();
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_dir), 5 * Time.deltaTime);
}
else if (actualHealth > 0 && barricadeManager != null)
{
Vector3 _dir = barricadeManager.transform.position - transform.position;
_dir.Normalize();
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_dir), 5 * Time.deltaTime);
2020-02-01 11:11:56 +00:00
}
2020-02-01 07:52:46 +00:00
}
2020-02-01 21:54:57 +00:00
public void Damage(float damageLoc = 0)
2020-02-01 07:52:46 +00:00
{
2020-02-01 21:54:57 +00:00
if (damageLoc == 0) {
WeaponManager weaponManager = player.weaponList[player.selectedWeapon].GetComponent<WeaponManager>();
if (weaponManager != null) {
actualHealth -= weaponManager.damage;
}
}
else
{
actualHealth -= damageLoc;
2020-02-01 09:12:47 +00:00
}
}
2020-02-01 11:11:56 +00:00
void DestroyBarricades()
{
2020-02-01 13:26:43 +00:00
NavMeshPath path = new NavMeshPath();
2020-02-01 15:20:10 +00:00
if (navMeshAgent != null) {
navMeshAgent.CalculatePath(player.transform.position, path);
Debug.Log(path.status);
2020-02-01 15:20:10 +00:00
if (path.status == NavMeshPathStatus.PathPartial)
2020-02-01 11:11:56 +00:00
{
2020-02-01 15:20:10 +00:00
BarricadeManager[] barricadeManagers = GameObject.FindObjectsOfType<BarricadeManager>();
foreach (BarricadeManager localBarricadeManager in barricadeManagers)
2020-02-01 11:11:56 +00:00
{
2020-02-01 15:20:10 +00:00
if (barricadeManager == null)
{
2020-02-01 11:11:56 +00:00
barricadeManager = localBarricadeManager;
}
2020-02-01 15:20:10 +00:00
else
{
if (localBarricadeManager.health < barricadeManager.health) {
barricadeManager = localBarricadeManager;
continue;
}
if (Vector3.Distance(barricadeManager.transform.position, transform.position) > Vector3.Distance(localBarricadeManager.transform.position, transform.position))
{
2020-02-01 15:20:10 +00:00
barricadeManager = localBarricadeManager;
continue;
2020-02-01 15:20:10 +00:00
}
}
2020-02-01 11:11:56 +00:00
}
}
2020-02-01 15:20:10 +00:00
else
{
barricadeManager = null;
}
2020-02-01 13:26:43 +00:00
}
2020-02-01 11:11:56 +00:00
}
2020-02-01 09:12:47 +00:00
void DropBodyPart()
{
if (bodyParts.Count > 0)
{
2020-02-01 13:26:43 +00:00
float number = health / bodyParts.Count;
2020-02-01 09:12:47 +00:00
for (int i = 0; i < bodyParts.Count; i++)
{
2020-02-01 13:26:43 +00:00
if (bodyParts.Count - 1 != i)
{
if ((health - actualHealth) / number > i)
{
if (bodyParts[i] != null && bodyParts[i].transform.parent != null)
{
bodyParts[i].transform.parent = null;
bodyParts[i].AddComponent<Rigidbody>();
Rigidbody rigidBodyEnemy = bodyParts[i].GetComponent<Rigidbody>() as Rigidbody;
bodyParts[i].AddComponent<MeshCollider>();
MeshCollider meshCollider = bodyParts[i].GetComponent<MeshCollider>() as MeshCollider;
meshCollider.convex = true;
rigidBodyEnemy.mass = 10 * bodyParts.Count;
Destroy(bodyParts[i], 4);
}
}
}
else
2020-02-01 09:12:47 +00:00
{
2020-02-01 13:26:43 +00:00
if (actualHealth == 0)
{
if (bodyParts[i] != null && bodyParts[i].transform.parent != null)
{
bodyParts[i].transform.parent = null;
bodyParts[i].AddComponent<Rigidbody>();
Rigidbody rigidBodyEnemy = bodyParts[i].GetComponent<Rigidbody>() as Rigidbody;
bodyParts[i].AddComponent<MeshCollider>();
MeshCollider meshCollider = bodyParts[i].GetComponent<MeshCollider>() as MeshCollider;
meshCollider.convex = true;
2020-02-01 11:11:56 +00:00
navMeshAgent.isStopped = true;
navMeshAgent.enabled = false;
navMeshAgent = null;
rigidBodyEnemy.velocity = bodyParts[i].transform.forward;
2020-02-01 13:26:43 +00:00
rigidBodyEnemy.mass = 10 * bodyParts.Count;
Destroy(bodyParts[i], 4);
2020-02-01 11:11:56 +00:00
}
2020-02-01 09:39:29 +00:00
}
2020-02-01 11:11:56 +00:00
}
}
}
}
void Attack()
{
RaycastHit hit;
if (Physics.Raycast(transform.position + new Vector3(0,0.5f,0),transform.forward,out hit,1f) && attackTime <= Time.time)
2020-02-01 11:11:56 +00:00
{
if (hit.transform.CompareTag("Player"))
{
player.Damage(damage);
2020-02-01 15:20:10 +00:00
attackTime = attackRate + Time.time;
2020-02-01 11:11:56 +00:00
}
2020-02-01 15:20:10 +00:00
if (hit.transform.CompareTag("BarricadeField"))
2020-02-01 11:11:56 +00:00
{
BarricadeManager barricadeManager = hit.transform.GetComponent<BarricadeManager>();
if (barricadeManager != null)
{
barricadeManager.Damage(damage);
2020-02-01 15:20:10 +00:00
attackTime = attackRate + Time.time;
2020-02-01 09:12:47 +00:00
}
}
2020-02-01 07:52:46 +00:00
}
}
private void OnParticleCollision(GameObject other)
{
if (other.transform.tag == "Bullets")
{
Damage();
}
}
WeaponManager weaponManager = null;
bool localHit = false;
2020-02-01 17:17:29 +00:00
void OnTriggerEnter(Collider collider)
{
if (collider.transform.CompareTag("Meele"))
{
weaponManager = collider.transform.GetComponent<WeaponManager>();
if (weaponManager == null)
{
weaponManager = collider.transform.parent.GetComponent<WeaponManager>();
}
localHit = weaponManager.hit;
if (weaponManager != null && localHit) {
Damage();
localHit = false;
}
}
}
void OnTriggerStay(Collider collider)
{
if (collider.transform.CompareTag("Meele"))
{
bool offOn = false;
if (weaponManager.hit == false) {
offOn = true;
}
if (weaponManager.hit == true && offOn)
{
localHit = weaponManager.hit;
offOn = false;
}
if (weaponManager != null && localHit)
{
2020-02-01 21:54:57 +00:00
Damage();
localHit = false;
2020-02-01 21:54:57 +00:00
}
2020-02-01 17:17:29 +00:00
}
}
2020-02-01 07:52:46 +00:00
}