BloodAndGore/Assets/Scripts/Guns/Rocket.cs

82 lines
2.6 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Saltosion.OneWeapon.Player;
using Saltosion.OneWeapon.Enemies;
using Saltosion.OneWeapon.Effects;
using Saltosion.OneWeapon.Environment;
namespace Saltosion.OneWeapon.Guns {
[RequireComponent(typeof(Bullet))]
public class Rocket : MonoBehaviour {
public Rigidbody2D Body;
public GameObject Sprite;
public ParticleSystem Trail;
public ParticleSystem Explosion;
[Range(0, 10)]
public float ExplodeRadius;
private float DeathTimer = 0;
private Bullet Bullet;
void Start() {
Bullet = GetComponent<Bullet>();
Vector2 Direction = Bullet.Direction;
float Rot = Bullet.InitialRotation;
Body.velocity = Direction * 8;
Body.rotation = Rot - 90;
Explosion.Stop();
}
void Update() {
DeathTimer += Time.deltaTime;
if (DeathTimer > 20) {
Destroy(gameObject);
}
}
void OnTriggerEnter2D(Collider2D collider) {
if (collider.GetComponent<PlayerController>() != null) {
// don't hit the player!
return;
}
Bullet.HasExploded = true;
Destroy(Sprite);
Destroy(Body);
Destroy(GetComponent<CapsuleCollider2D>());
Explosion.Play();
Trail.Stop();
DeathTimer = 19;
Collider2D[] NearbyColliders = Physics2D.OverlapCircleAll(transform.position, ExplodeRadius);
foreach (Collider2D curr in NearbyColliders) {
2019-08-15 00:08:07 +02:00
Vector2 Delta = (curr.transform.position - transform.position);
Vector2 Dir = Delta.normalized;
float ScaledDistance = 1 - Mathf.Clamp((Delta.magnitude - (ExplodeRadius / 3)) / (ExplodeRadius - (ExplodeRadius / 3)), 0, 1);
Bullet.DoDamage(ScaledDistance * 20, ScaledDistance * 2, curr, Dir);
if (curr.attachedRigidbody != null && curr.attachedRigidbody.bodyType == RigidbodyType2D.Dynamic) {
// FIXME: AcceleratedMovement currently overrides Rigidbody.velocity so this doesn't do anything
curr.attachedRigidbody.AddForce(Dir * 10f, ForceMode2D.Impulse);
}
}
// Do damage here, kill everyone
}
private void OnDrawGizmosSelected() {
Gizmos.color = new Color(1.0f, 0.2f, 0.2f, 0.8f);
Gizmos.DrawWireSphere(transform.position, ExplodeRadius);
}
}
}