BloodAndGore/Assets/Scripts/Environment/GunDropper.cs

70 lines
2.3 KiB
C#
Raw Normal View History

2019-08-14 19:05:16 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2019-08-14 20:00:55 +02:00
using UnityEngine.Animations;
2019-08-14 19:05:16 +02:00
using Saltosion.OneWeapon.Guns;
using Saltosion.OneWeapon.Utils;
using Saltosion.OneWeapon.Effects;
2019-08-14 19:05:16 +02:00
namespace Saltosion.OneWeapon.Environment {
[RequireComponent(typeof(Explodable))]
public class GunDropper : MonoBehaviour {
2019-08-14 19:05:16 +02:00
2019-08-14 20:00:55 +02:00
[Header("General")]
public float GunLaunchForceMultiplier = 1;
public Transform HoleTransform;
public Explodable Explodable;
public bool ExplodeOnEmpty = false;
2019-08-14 20:00:55 +02:00
[Header("Gunstuff")]
2019-08-14 19:05:16 +02:00
public int GunsLeft = 3;
public List<GameObject> PossibleGuns = new List<GameObject>();
[Header("Graphics (optional)")]
public SpriteChanger SpriteChanger;
2019-08-14 19:05:16 +02:00
2019-08-14 20:00:55 +02:00
[Header("Debug")]
2019-08-14 19:05:16 +02:00
public bool DebugExpelGun = false;
2019-08-14 20:00:55 +02:00
void Start() {
if (SpriteChanger != null) {
GunsLeft = Mathf.Clamp(GunsLeft, 0, SpriteChanger.Sprites.Count - 1);
SpriteChanger.SetSprite(GunsLeft);
}
2019-08-14 20:00:55 +02:00
}
2019-08-14 19:05:16 +02:00
void Update() {
if (DebugExpelGun) {
ExpelGun();
DebugExpelGun = false;
}
}
public void ExpelGun() {
2019-08-22 19:26:55 +02:00
if (GunsLeft <= 0) {
return;
}
2019-08-14 19:05:16 +02:00
GameObject Gun = PossibleGuns[Util.RandomValueBetween(0, PossibleGuns.Count - 1)];
GameObject ShotGun = GameObject.Instantiate(Gun, HoleTransform.position, new Quaternion());
Rigidbody2D Body = ShotGun.GetComponent<Rigidbody2D>();
if (Body == null) {
Debug.LogError("Shotted Gun has no RigidBody2D!");
} else {
Vector2 DownwardForce = new Vector2(0, -1) * (Random.value + 0.2f) * GunLaunchForceMultiplier;
Vector2 SidewaysForce = new Vector2(1, 0) * (Random.value * 0.4f - 0.2f) * GunLaunchForceMultiplier;
Body.AddForce(DownwardForce + SidewaysForce, ForceMode2D.Impulse);
}
GunsLeft -= 1;
if (SpriteChanger != null) {
SpriteChanger.SetSprite(GunsLeft);
}
2019-08-14 19:05:16 +02:00
if (GunsLeft == 0) {
if (ExplodeOnEmpty) {
Explodable.Explode(true);
}
2019-08-22 19:26:55 +02:00
Destroy(this);
2019-08-14 19:05:16 +02:00
}
}
}
}