BloodAndGore/Assets/Scripts/Environment/GunDropper.cs
2019-08-22 20:26:55 +03:00

70 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using Saltosion.OneWeapon.Guns;
using Saltosion.OneWeapon.Utils;
using Saltosion.OneWeapon.Effects;
namespace Saltosion.OneWeapon.Environment {
[RequireComponent(typeof(Explodable))]
public class GunDropper : MonoBehaviour {
[Header("General")]
public float GunLaunchForceMultiplier = 1;
public Transform HoleTransform;
public Explodable Explodable;
public bool ExplodeOnEmpty = false;
[Header("Gunstuff")]
public int GunsLeft = 3;
public List<GameObject> PossibleGuns = new List<GameObject>();
[Header("Graphics (optional)")]
public SpriteChanger SpriteChanger;
[Header("Debug")]
public bool DebugExpelGun = false;
void Start() {
if (SpriteChanger != null) {
GunsLeft = Mathf.Clamp(GunsLeft, 0, SpriteChanger.Sprites.Count - 1);
SpriteChanger.SetSprite(GunsLeft);
}
}
void Update() {
if (DebugExpelGun) {
ExpelGun();
DebugExpelGun = false;
}
}
public void ExpelGun() {
if (GunsLeft <= 0) {
return;
}
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);
}
if (GunsLeft == 0) {
if (ExplodeOnEmpty) {
Explodable.Explode(true);
}
Destroy(this);
}
}
}
}