add: functional riffle and inventory for bullets
This commit is contained in:
@@ -12,6 +12,7 @@ public class GameController : MonoBehaviour
|
||||
public SceneAsset deployScene;
|
||||
public List<LevelInfo> levels;
|
||||
public List<EnemyInfo> enemies;
|
||||
public Dictionary<BulletType, int> inventory = new();
|
||||
|
||||
public void Start() {
|
||||
if (i) { Destroy(gameObject); return; }
|
||||
@@ -25,7 +26,10 @@ public class GameController : MonoBehaviour
|
||||
levels.ForEach(li => {
|
||||
AddEnemyInLevel(SelectEnemyWeighted().prefab, li.scene.name);
|
||||
});
|
||||
|
||||
foreach (BulletType i in Enum.GetValues(typeof(BulletType))) {
|
||||
if (i == BulletType.None) continue;
|
||||
inventory[i] = 10;
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<GameObject, int> PopEnemies(string sceneName) {
|
||||
@@ -72,6 +76,18 @@ public class GameController : MonoBehaviour
|
||||
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
|
||||
Cursor.visible = !locked;
|
||||
}
|
||||
|
||||
public int GetBulletCount(BulletType bulletType) {
|
||||
return inventory[bulletType];
|
||||
}
|
||||
|
||||
public bool ConsumeBullet(BulletType bulletType, int n = 1) {
|
||||
if (inventory[bulletType] >= n) {
|
||||
inventory[bulletType] -= n;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
@@ -79,11 +95,11 @@ public struct LevelInfo {
|
||||
public SceneAsset scene;
|
||||
public int hp;
|
||||
public int maxHp;
|
||||
public FactoryType factory;
|
||||
public BulletType factory;
|
||||
public Dictionary<GameObject, int> enemies;
|
||||
}
|
||||
|
||||
public enum FactoryType {
|
||||
public enum BulletType {
|
||||
None,
|
||||
Rifle,
|
||||
Rocket,
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Rifle : Weapon
|
||||
{
|
||||
public GameObject projectilePrefab;
|
||||
public Transform spawnPosition;
|
||||
public float fireRate = 3f;
|
||||
|
||||
[SerializeField] float currentDelay = 0;
|
||||
|
||||
public override void Shoot(bool keyDown, bool keyHold) {
|
||||
if (keyHold && (fireRate == 0 || currentDelay >= 1 / fireRate) && GameController.i.ConsumeBullet(BulletType.Rifle)) {
|
||||
Instantiate(projectilePrefab, spawnPosition.position, spawnPosition.rotation);
|
||||
currentDelay = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if (fireRate != 0) currentDelay = Mathf.Clamp(currentDelay += Time.deltaTime, 0, 1 / fireRate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4cc7bc1baad25a9459ee673b6d502363
|
||||
@@ -9,11 +9,12 @@ public class WeaponController : MonoBehaviour
|
||||
|
||||
void Start() {
|
||||
weapons = GetComponentsInChildren<Weapon>();
|
||||
if (weapons.Length > currentWeapon) weapons[currentWeapon].SetWeaponState(true);
|
||||
for (int i = 0; i < weapons.Length; ++i) weapons[i].SetWeaponState(i == currentWeapon);
|
||||
}
|
||||
|
||||
void Update() {
|
||||
ChangeActiveWeapon((currentWeapon + (int)Input.mouseScrollDelta.y) % weapons.Length);
|
||||
if (weapons.Length == 0) return;
|
||||
ChangeActiveWeapon((currentWeapon + (int)Input.mouseScrollDelta.y + weapons.Length) % weapons.Length);
|
||||
CheckButtonsForChangeWeapon();
|
||||
if (Input.GetMouseButton(0) && weapons[currentWeapon].isActive) weapons[currentWeapon].Shoot(Input.GetMouseButtonDown(0), Input.GetMouseButton(0));
|
||||
}
|
||||
@@ -28,7 +29,7 @@ public class WeaponController : MonoBehaviour
|
||||
void ChangeActiveWeapon(int ind) {
|
||||
if (ind < 0 || ind >= weapons.Length || ind == currentWeapon) return;
|
||||
weapons[currentWeapon].SetWeaponState(false);
|
||||
weapons[ind].SetWeaponState(false);
|
||||
weapons[ind].SetWeaponState(true);
|
||||
currentWeapon = ind;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user