BloodAndGore/Assets/Scripts/Environment/VendingMachine.cs

57 lines
1.9 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;
namespace Saltosion.OneWeapon.Environment {
public class VendingMachine : MonoBehaviour {
2019-08-14 20:00:55 +02:00
[Header("General")]
public float GunLaunchForceMultiplier = 1;
public Transform HoleTransform;
[Header("Gunstuff")]
2019-08-14 19:05:16 +02:00
public int GunsLeft = 3;
public List<GameObject> PossibleGuns = new List<GameObject>();
2019-08-14 20:00:55 +02:00
[Header("Graphics")]
public SpriteRenderer Sprite;
public List<Sprite> Sprites = new List<Sprite>();
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() {
GunsLeft = Mathf.Clamp(GunsLeft, 0, 5);
Sprite.sprite = Sprites[GunsLeft];
}
2019-08-14 19:05:16 +02:00
void Update() {
if (DebugExpelGun) {
ExpelGun();
DebugExpelGun = false;
}
}
public void ExpelGun() {
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-14 20:00:55 +02:00
Sprite.sprite = Sprites[GunsLeft];
2019-08-14 19:05:16 +02:00
if (GunsLeft == 0) {
Destroy(this);
}
}
}
}