GlobalGameJame/Assets/Scripts/EnemyManager.cs

151 lines
5.1 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;
public class EnemyManager : MonoBehaviour
{
public float health = 100;
2020-02-01 11:11:56 +00:00
public float damage = 10;
public float attackRate = 2;
private float attackTime = 2;
private float actualHealth;
2020-02-01 07:52:46 +00:00
private NavMeshAgent navMeshAgent;
private PlayerManager player;
2020-02-01 11:11:56 +00:00
private 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()
{
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
{
2020-02-01 11:11:56 +00:00
Destroy(this.gameObject);
}
DestroyBarricades();
if (navMeshAgent != null && barricadeManager == null)
{
navMeshAgent.SetDestination(player.transform.position);
}
else if (navMeshAgent != null && barricadeManager != null)
{
navMeshAgent.SetDestination(barricadeManager.transform.position);
2020-02-01 09:12:47 +00:00
}
}
void FixedUpdate()
{
2020-02-01 11:11:56 +00:00
if (actualHealth > 0 && barricadeManager == null)
2020-02-01 09:12:47 +00:00
{
Vector3 _dir = player.transform.position - transform.position;
_dir.Normalize();
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_dir), 5 * Time.deltaTime);
2020-02-01 07:52:46 +00:00
}
2020-02-01 11:11:56 +00:00
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 07:52:46 +00:00
}
public void Damage()
{
WeaponManager weaponManager = player.weaponList[player.selectedWeapon].GetComponent<WeaponManager>();
if (weaponManager != null) {
2020-02-01 09:12:47 +00:00
actualHealth -= weaponManager.damage;
}
}
2020-02-01 11:11:56 +00:00
void DestroyBarricades()
{
Debug.Log(navMeshAgent.pathPending);
if (navMeshAgent.pathStatus == NavMeshPathStatus.PathInvalid)
{
BarricadeManager[] barricadeManagers = FindObjectsOfType<BarricadeManager>();
foreach(BarricadeManager localBarricadeManager in barricadeManagers)
{
if (barricadeManager == null)
{
barricadeManager = localBarricadeManager;
}
else
{
if (Vector3.Distance(barricadeManager.transform.position, transform.position) > Vector3.Distance(localBarricadeManager.transform.position, transform.position)) {
barricadeManager = localBarricadeManager;
}
}
}
}
}
2020-02-01 09:12:47 +00:00
void DropBodyPart()
{
if (bodyParts.Count > 0)
{
float number = health / (bodyParts.Count + 1);
for (int i = 0; i < bodyParts.Count; i++)
{
if ((health - actualHealth) / number > i)
{
2020-02-01 11:11:56 +00:00
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;
if (i == bodyParts.Count - 1) {
navMeshAgent.isStopped = true;
navMeshAgent.enabled = false;
navMeshAgent = null;
rigidBodyEnemy.velocity = bodyParts[i].transform.forward;
}
rigidBodyEnemy.mass = 10 * bodyParts.Count;
Destroy(bodyParts[i], 4);
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,1,0),transform.forward,out hit,2) && attackTime <= Time.time)
{
if (hit.transform.CompareTag("Player"))
{
player.Damage(damage);
}
else if (hit.transform.CompareTag("BarricadeField"))
{
BarricadeManager barricadeManager = hit.transform.GetComponent<BarricadeManager>();
if (barricadeManager != null)
{
barricadeManager.Damage(damage);
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();
}
}
}