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

154 lines
3.4 KiB
C#
Raw Normal View History

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