94 lines
3.1 KiB
C#
94 lines
3.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Assets.Scripts.Player;
|
|
using UnityEngine;
|
|
|
|
namespace Assets.Scripts.InteractableObjects
|
|
{
|
|
public abstract class InteractableObject : MonoBehaviour
|
|
{
|
|
public AnimationClip clip;
|
|
private bool particlePlayed;
|
|
private bool animationPlayed;
|
|
public bool keepAlive;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
particlePlayed = false;
|
|
animationPlayed = false;
|
|
keepAlive = false;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
destroyObject();
|
|
}
|
|
|
|
public void OnInteraction(GameObject player)
|
|
{
|
|
//StartCoroutine(playParticle(player));
|
|
|
|
//StartCoroutine(playAnimation(player));
|
|
handleInteraction(player);
|
|
}
|
|
|
|
public abstract void handleInteraction(GameObject player);
|
|
|
|
IEnumerator playParticle(GameObject player)
|
|
{
|
|
if (GetComponent<ParticleSystem>() != null)
|
|
{
|
|
Vector3 playerPos = player.transform.position;
|
|
playerPos.y = transform.position.y;
|
|
Quaternion newRotation = Quaternion.LookRotation(playerPos - transform.position, transform.TransformDirection(Vector3.up));
|
|
ParticleSystem particleSystem = GetComponent<ParticleSystem>();
|
|
ParticleSystem.ShapeModule shape = particleSystem.shape;
|
|
shape.rotation = newRotation.eulerAngles;
|
|
particleSystem.Play();
|
|
yield return new WaitUntil(() => !particleSystem.isPlaying);
|
|
}
|
|
particlePlayed = true;
|
|
}
|
|
|
|
IEnumerator playAnimation(GameObject player)
|
|
{
|
|
if (clip != null)
|
|
{
|
|
Animator animator = player.GetComponent<Animator>();
|
|
AnimationClip[] clips = animator.runtimeAnimatorController.animationClips;
|
|
float length = 0;
|
|
foreach (AnimationClip clip in clips)
|
|
{
|
|
if (clip.name.ToLower() == "disarming")
|
|
{
|
|
length = clip.length;
|
|
break;
|
|
}
|
|
}
|
|
if (player.GetComponent<PlayerGameObject>().isArmed)
|
|
{
|
|
player.GetComponent<Animator>().Play("Disarming");
|
|
yield return new WaitForSeconds(length);
|
|
}
|
|
player.GetComponent<Animator>().Play(clip.name);
|
|
yield return new WaitForSeconds(clip.length);
|
|
if (player.GetComponent<PlayerGameObject>().isArmed)
|
|
{
|
|
player.GetComponent<Animator>().Play("Arming");
|
|
yield return new WaitForSeconds(length);
|
|
}
|
|
|
|
}
|
|
animationPlayed = true;
|
|
}
|
|
|
|
public void destroyObject()
|
|
{
|
|
if (particlePlayed && animationPlayed && !keepAlive)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|
|
} |