BloodAndGore/Assets/Scripts/Guns/Grenade.cs

59 lines
1.8 KiB
C#
Raw Normal View History

2019-08-18 20:46:22 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2019-08-21 23:11:10 +02:00
using Saltosion.OneWeapon.Effects;
2019-08-18 20:46:22 +02:00
namespace Saltosion.OneWeapon.Guns {
[RequireComponent(typeof(Bullet))]
public class Grenade : MonoBehaviour {
private Bullet Bullet;
2019-08-21 23:11:10 +02:00
private CustomLight Light;
2019-08-18 20:46:22 +02:00
[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>();
2019-08-21 23:11:10 +02:00
Light = GetComponent<CustomLight>();
2019-08-18 20:46:22 +02:00
Body.SetRotation(Bullet.InitialRotation);
Body.AddForce(Bullet.Direction.normalized * ShootForce, ForceMode2D.Impulse);
2019-08-18 20:55:34 +02:00
Body.AddTorque(Random.value * Torque - Torque / 2, ForceMode2D.Impulse);
2019-08-18 20:46:22 +02:00
Explosion.Stop();
}
void Update() {
Lifetime -= Time.deltaTime;
if (Lifetime < 0 && !Bullet.HasExploded) {
Explosion.Play();
Bullet.HasExploded = true;
Destroy(Body);
Destroy(Sprite);
2019-08-21 23:11:10 +02:00
Bullet.DoDamageAOE(18, ExplodeRadius, 1f);
}
if (Lifetime < 0) {
Light.LightIntensity = Mathf.Max(2 + Lifetime * 6, 0);
2019-08-18 20:46:22 +02:00
}
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);
}
}
}