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;
|
2019-08-16 20:46:34 +02:00
|
|
|
|
using Saltosion.OneWeapon.Effects;
|
2019-08-14 19:05:16 +02:00
|
|
|
|
|
|
|
|
|
namespace Saltosion.OneWeapon.Environment {
|
2019-08-16 20:46:34 +02:00
|
|
|
|
[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;
|
2019-08-16 20:46:34 +02:00
|
|
|
|
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>();
|
|
|
|
|
|
2019-08-16 20:46:34 +02:00
|
|
|
|
[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() {
|
2019-08-16 20:46:34 +02:00
|
|
|
|
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;
|
2019-08-16 20:46:34 +02:00
|
|
|
|
if (SpriteChanger != null) {
|
|
|
|
|
SpriteChanger.SetSprite(GunsLeft);
|
|
|
|
|
}
|
2019-08-14 19:05:16 +02:00
|
|
|
|
if (GunsLeft == 0) {
|
2019-08-16 20:46:34 +02:00
|
|
|
|
if (ExplodeOnEmpty) {
|
2019-08-22 21:50:50 +02:00
|
|
|
|
Explodable.Explode(true, true);
|
2019-08-16 20:46:34 +02:00
|
|
|
|
}
|
2019-08-22 19:26:55 +02:00
|
|
|
|
Destroy(this);
|
2019-08-14 19:05:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|