57 lines
1.9 KiB
C#
57 lines
1.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Animations;
|
|
using Saltosion.OneWeapon.Guns;
|
|
using Saltosion.OneWeapon.Utils;
|
|
|
|
namespace Saltosion.OneWeapon.Environment {
|
|
public class VendingMachine : MonoBehaviour {
|
|
|
|
[Header("General")]
|
|
public float GunLaunchForceMultiplier = 1;
|
|
public Transform HoleTransform;
|
|
|
|
[Header("Gunstuff")]
|
|
public int GunsLeft = 3;
|
|
public List<GameObject> PossibleGuns = new List<GameObject>();
|
|
|
|
[Header("Graphics")]
|
|
public SpriteRenderer Sprite;
|
|
public List<Sprite> Sprites = new List<Sprite>();
|
|
|
|
[Header("Debug")]
|
|
public bool DebugExpelGun = false;
|
|
|
|
void Start() {
|
|
GunsLeft = Mathf.Clamp(GunsLeft, 0, 5);
|
|
Sprite.sprite = Sprites[GunsLeft];
|
|
}
|
|
|
|
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;
|
|
Sprite.sprite = Sprites[GunsLeft];
|
|
if (GunsLeft == 0) {
|
|
Destroy(this);
|
|
}
|
|
}
|
|
}
|
|
}
|