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

131 lines
3.0 KiB
C#
Raw Normal View History

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