add: functional riffle and inventory for bullets

This commit is contained in:
KOSMOGOR
2025-04-26 13:53:06 +03:00
parent 1b106e4e18
commit df5602dc00
8 changed files with 258 additions and 416 deletions
+21
View File
@@ -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);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4cc7bc1baad25a9459ee673b6d502363
+4 -3
View File
@@ -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;
}
}