This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Enemies/AI/SkinlessMonsterAnimator.cs

135 lines
3.4 KiB
C#
Raw Permalink Normal View History

2023-04-18 06:29:51 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2023-06-01 20:25:46 +02:00
namespace Enemy {
public class SkinlessMonsterAnimator : MonoBehaviour
{
[SerializeField] private Animator animator;
2023-04-18 06:29:51 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField]
[Tooltip("This is the object with the skin dissolve material")]
private GameObject modelObject;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private List<GameObject> objectsThatFallOnDeath = new();
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private float deathSpeed = 5f;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private float fastDeathduration = 1f;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private float deathDuration = 5f;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
private float curDeathSpeed = 5f;
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
private Material dissolveMaterial;
private bool isAlive = true;
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
private float speed;
public bool IsRunning { get; private set; }
2023-04-21 09:30:43 +02:00
2023-04-18 06:29:51 +02:00
2023-06-01 20:25:46 +02:00
// Start is called before the first frame update
private void Start()
{
dissolveMaterial = modelObject.GetComponent<SkinnedMeshRenderer>().materials[0];
curDeathSpeed = deathSpeed;
}
2023-04-18 06:29:51 +02:00
2023-06-01 20:25:46 +02:00
// Update is called once per frame
private void Update()
2023-04-21 09:30:43 +02:00
{
2023-06-01 20:25:46 +02:00
if (isAlive)
{
animator.SetFloat("Speed", speed);
}
else
{
var quantity = dissolveMaterial.GetFloat("_DissolveQuantity");
quantity = Mathf.Lerp(quantity, -2, Time.deltaTime * curDeathSpeed);
dissolveMaterial.SetFloat("_DissolveQuantity", quantity);
}
2023-04-21 09:30:43 +02:00
}
2023-06-01 20:25:46 +02:00
public void StartMoving()
2023-04-21 09:30:43 +02:00
{
2023-06-01 20:25:46 +02:00
if (isAlive)
{
IsRunning = true;
speed = 1;
}
2023-04-21 09:30:43 +02:00
}
2023-04-18 06:29:51 +02:00
2023-06-01 20:25:46 +02:00
public void StopMoving()
2023-04-21 09:30:43 +02:00
{
2023-06-01 20:25:46 +02:00
if (isAlive)
{
speed = 0;
IsRunning = false;
}
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
public void Attack()
2023-04-21 09:30:43 +02:00
{
2023-06-01 20:25:46 +02:00
if (isAlive) animator.SetTrigger("Attack");
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
/// <summary>
/// 0,1,2,3
/// </summary>
/// <param name="attackType"></param>
public void SetAttackType(int attackType)
{
animator.SetInteger("AttackIndex", attackType);
}
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
public void InLight()
{
if (isAlive)
animator.SetBool("InLight", true);
}
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
public void NotInLight()
{
if (isAlive)
animator.SetBool("InLight", false);
}
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
public void AttackScream()
{
if (isAlive)
animator.SetTrigger("AttackScream");
}
2023-04-18 06:29:51 +02:00
2023-06-01 20:25:46 +02:00
public void Kill(bool fast = false)
{
//animator.speed = 0;
InLight();
2023-04-18 06:29:51 +02:00
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
isAlive = false;
foreach (var obj in objectsThatFallOnDeath)
{
obj.AddComponent<Rigidbody>();
if (obj.GetComponent<Collider>() != null) obj.GetComponent<Collider>().enabled = false;
}
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
var dur = deathDuration;
if (fast)
{
dur = fastDeathduration;
curDeathSpeed = deathSpeed * (deathDuration / fastDeathduration);
}
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
StartCoroutine(destroyAfterSeconds(dur));
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
private IEnumerator destroyAfterSeconds(float duration)
{
yield return new WaitForSeconds(duration);
Destroy(gameObject);
}
2023-04-21 09:30:43 +02:00
}
2023-06-01 20:25:46 +02:00
}