Projet-IA-Madelaine/Scripts/GameObjectsScripts/MissileScript.cs

83 lines
1.8 KiB
C#
Raw Normal View History

2024-06-12 21:03:42 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MissileScript : MonoBehaviour
{
#region PROPERTIES
public int TargetId { get; set; }
#endregion
#region VARIABLES
[SerializeField] private float speed = 15.0f;
[SerializeField] private GameObject explosion;
private bool renderSprite;
private GameHandler _gameHandler;
#endregion
#region METHODS
public void Explode()
{
StopParticles();
if (renderSprite)
{
var rotation = transform.rotation;
rotation.z = 180;
Instantiate(explosion, transform.position, rotation);
}
Destroy(gameObject);
return;
}
public void SetRenderSprite(bool render)
{
renderSprite = render;
}
public void SetTarget(int id)
{
TargetId = id;
}
public void StopParticles()
{
if (transform.childCount == 0) return;
var emit = transform?.GetChild(0);
emit.parent = null;
emit.GetComponent<ParticleSystem>().Stop(true, ParticleSystemStopBehavior.StopEmitting);
// This finds the particleAnimator associated with the emitter and then
// sets it to automatically delete itself when it runs out of particles
}
#endregion
#region LIFECYCLE
void Start()
{
_gameHandler = GameHandler.Instance;
}
// Update is called once per frame
void Update()
{
if(transform.position.x < -20)
{
StopParticles();
Destroy(gameObject);
return;
}
//Go up because up is the direction of the head of the missile, even if he goes left the player's POV
transform.Translate(Vector3.up * (speed+_gameHandler.MapSpeed) * Time.deltaTime);
}
#endregion
}