BloodAndGore/Assets/Scripts/Guns/Grenade.cs

53 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Saltosion.OneWeapon.Guns {
[RequireComponent(typeof(Bullet))]
public class Grenade : MonoBehaviour {
private Bullet Bullet;
[Header("Children")]
public Rigidbody2D Body;
public ParticleSystem Explosion;
public SpriteRenderer Sprite;
[Header("Values")]
public float ShootForce = 8;
public float Torque = 5;
public float Lifetime = 1;
public float ExplodeRadius = 3;
void Start() {
Bullet = GetComponent<Bullet>();
Body = GetComponent<Rigidbody2D>();
Body.SetRotation(Bullet.InitialRotation);
Body.AddForce(Bullet.Direction.normalized * ShootForce, ForceMode2D.Impulse);
Body.AddTorque(Random.value * Torque - Torque / 2);
Explosion.Stop();
}
void Update() {
Lifetime -= Time.deltaTime;
if (Lifetime < 0 && !Bullet.HasExploded) {
Explosion.Play();
Bullet.HasExploded = true;
Destroy(Body);
Destroy(Sprite);
Bullet.DoDamageAOE(18, ExplodeRadius, 1.5f);
}
if (Lifetime < -1) {
Destroy(gameObject);
}
}
private void OnDrawGizmosSelected() {
Gizmos.color = new Color(1.0f, 0.2f, 0.2f, 0.8f);
Gizmos.DrawWireSphere(transform.position, ExplodeRadius);
}
}
}