BloodAndGore/Assets/Scripts/Guns/Grenade.cs
2019-08-22 00:11:10 +03:00

59 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Saltosion.OneWeapon.Effects;
namespace Saltosion.OneWeapon.Guns {
[RequireComponent(typeof(Bullet))]
public class Grenade : MonoBehaviour {
private Bullet Bullet;
private CustomLight Light;
[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>();
Light = GetComponent<CustomLight>();
Body.SetRotation(Bullet.InitialRotation);
Body.AddForce(Bullet.Direction.normalized * ShootForce, ForceMode2D.Impulse);
Body.AddTorque(Random.value * Torque - Torque / 2, ForceMode2D.Impulse);
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, 1f);
}
if (Lifetime < 0) {
Light.LightIntensity = Mathf.Max(2 + Lifetime * 6, 0);
}
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);
}
}
}