From 34099af40502826c1cea21f8014ba2bce68c6c29 Mon Sep 17 00:00:00 2001 From: Stedd Date: Sat, 5 Nov 2022 12:04:31 +0100 Subject: [PATCH] Implemented multiple ammo types --- Assets/AmmoBelt.cs | 79 +++++++++++++++++++ Assets/AmmoBelt.cs.meta | 11 +++ .../Prefabs/PlayerCapsule.prefab | 26 ++++++ Assets/Scenes/Sandbox.unity | 68 +++++++--------- Assets/Weapons/Ammo.cs | 25 +++--- Assets/Weapons/Crossbow.prefab | 9 ++- Assets/Weapons/Handgun.prefab | 13 +-- Assets/Weapons/MachineGun.prefab | 8 +- Assets/Weapons/Revolver.prefab | 13 +-- Assets/Weapons/Weapon.cs | 3 +- 10 files changed, 185 insertions(+), 70 deletions(-) create mode 100644 Assets/AmmoBelt.cs create mode 100644 Assets/AmmoBelt.cs.meta diff --git a/Assets/AmmoBelt.cs b/Assets/AmmoBelt.cs new file mode 100644 index 0000000..445e86f --- /dev/null +++ b/Assets/AmmoBelt.cs @@ -0,0 +1,79 @@ +using System; +using System.ComponentModel; +using System.Linq; +using UnityEngine; + +public enum AmmoType +{ + [Description("1A")] + CrossbowBolt, + [Description("5.56mm")] + Five_FiveSix, + [Description("9mm")] + Nine, + [Description("11.5mm")] + Eleven_Five, +} + +public class AmmoBelt : MonoBehaviour +{ + [Serializable] private class AmmoSlot + { + [field: SerializeField] public AmmoType AmmoType { get; set; } + [field: SerializeField] public int MaxBeltAmmoAmount { get; set; } + [field: SerializeField] public int CurrentBeltAmmoAmount { get; set; } + } + + [SerializeField] private AmmoSlot[] _ammoSlots; + + private void Awake() + { + InitializeAmmoBelt(); + } + + private void InitializeAmmoBelt() + { + foreach (AmmoSlot ammoSlot in _ammoSlots) + { + ammoSlot.CurrentBeltAmmoAmount = ammoSlot.MaxBeltAmmoAmount; + } + } + + public int GetBeltCurrentAmmoAmount(AmmoType ammoType) + { + return (from ammoSlot in _ammoSlots + where ammoType == ammoSlot.AmmoType + select ammoSlot.CurrentBeltAmmoAmount) + .FirstOrDefault(); + } + + public int GetBeltMaxAmmoAmount(AmmoType ammoType) + { + return (from ammoSlot in _ammoSlots + where ammoType == ammoSlot.AmmoType + select ammoSlot.MaxBeltAmmoAmount) + .FirstOrDefault(); + } + + public void SetBeltCurrentAmmoAmount(AmmoType ammoType, int value) + { + foreach (AmmoSlot ammoSlot in _ammoSlots) + { + if (ammoType == ammoSlot.AmmoType) + { + ammoSlot.CurrentBeltAmmoAmount = value; + } + } + } + + public void ModifyBeltCurrentAmmoAmount(AmmoType ammoType, int value) + { + foreach (AmmoSlot ammoSlot in _ammoSlots) + { + if (ammoType == ammoSlot.AmmoType) + { + ammoSlot.CurrentBeltAmmoAmount += value; + } + } + } +} \ No newline at end of file diff --git a/Assets/AmmoBelt.cs.meta b/Assets/AmmoBelt.cs.meta new file mode 100644 index 0000000..d7cd626 --- /dev/null +++ b/Assets/AmmoBelt.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dbcfad096f7809942bd5a514e82d5678 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ImportedAssets/StarterAssets/FirstPersonController/Prefabs/PlayerCapsule.prefab b/Assets/ImportedAssets/StarterAssets/FirstPersonController/Prefabs/PlayerCapsule.prefab index a225a0a..7242ad8 100644 --- a/Assets/ImportedAssets/StarterAssets/FirstPersonController/Prefabs/PlayerCapsule.prefab +++ b/Assets/ImportedAssets/StarterAssets/FirstPersonController/Prefabs/PlayerCapsule.prefab @@ -10,6 +10,7 @@ GameObject: m_Component: - component: {fileID: 2265701993784981172} - component: {fileID: 7253208606534141378} + - component: {fileID: 8838302087844949975} m_Layer: 8 m_Name: Weapons m_TagString: Untagged @@ -50,6 +51,31 @@ MonoBehaviour: m_EditorClassIdentifier: _weapons: [] _currentWeapon: 0 +--- !u!114 &8838302087844949975 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 160269183453626702} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: dbcfad096f7809942bd5a514e82d5678, type: 3} + m_Name: + m_EditorClassIdentifier: + _ammoSlots: + - k__BackingField: 0 + k__BackingField: 10 + k__BackingField: 0 + - k__BackingField: 2 + k__BackingField: 30 + k__BackingField: 0 + - k__BackingField: 1 + k__BackingField: 200 + k__BackingField: 0 + - k__BackingField: 3 + k__BackingField: 15 + k__BackingField: 0 --- !u!1 &4135013735270702863 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/Sandbox.unity b/Assets/Scenes/Sandbox.unity index 510adfc..642359c 100644 --- a/Assets/Scenes/Sandbox.unity +++ b/Assets/Scenes/Sandbox.unity @@ -2119,25 +2119,6 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 925195241} m_CullTransparentMesh: 1 ---- !u!1 &942203718 stripped -GameObject: - m_CorrespondingSourceObject: {fileID: 160269183453626702, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} - m_PrefabInstance: {fileID: 8204767108263278737} - m_PrefabAsset: {fileID: 0} ---- !u!114 &942203720 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 942203718} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 51cf941944fb27e43863c290683f7068, type: 3} - m_Name: - m_EditorClassIdentifier: - _weapons: [] - _currentWeapon: 0 --- !u!1001 &945433442 PrefabInstance: m_ObjectHideFlags: 0 @@ -4336,7 +4317,7 @@ PrefabInstance: m_Modifications: - target: {fileID: 160269183453626702, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} propertyPath: m_Name - value: Weapon + value: Weapons objectReference: {fileID: 0} - target: {fileID: 335461055095249328, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} propertyPath: _fpCamera @@ -4354,14 +4335,30 @@ PrefabInstance: propertyPath: reloadAction.m_Id value: e5dd7aae-807b-46fc-8c1a-a6e621305625 objectReference: {fileID: 0} - - target: {fileID: 335461055095249328, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} - propertyPath: _timeBetweenReloads - value: 1.5 - objectReference: {fileID: 0} - target: {fileID: 335461055095249328, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} propertyPath: reloadAction.m_Name value: Reload objectReference: {fileID: 0} + - target: {fileID: 411911725668603200, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} + propertyPath: _ammoSlot.Array.size + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 411911725668603200, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} + propertyPath: _ammoSlot.Array.data[0].AmmoType + value: 3 + objectReference: {fileID: 0} + - target: {fileID: 411911725668603200, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} + propertyPath: _ammoSlot.Array.data[1].AmmoType + value: 3 + objectReference: {fileID: 0} + - target: {fileID: 411911725668603200, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} + propertyPath: _ammoSlot.Array.data[2].AmmoType + value: 3 + objectReference: {fileID: 0} + - target: {fileID: 411911725668603200, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} + propertyPath: _ammoSlot.Array.data[3].AmmoType + value: 3 + objectReference: {fileID: 0} - target: {fileID: 1218087889432038952, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} propertyPath: m_IsActive value: 1 @@ -4382,10 +4379,6 @@ PrefabInstance: propertyPath: reloadAction.m_Id value: b6e2ceaf-6126-4eb9-8b2e-91b3dc0253b4 objectReference: {fileID: 0} - - target: {fileID: 2132953286740742962, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} - propertyPath: _timeBetweenReloads - value: 1.5 - objectReference: {fileID: 0} - target: {fileID: 2132953286740742962, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} propertyPath: reloadAction.m_Name value: Reload @@ -4398,6 +4391,14 @@ PrefabInstance: propertyPath: m_IsActive value: 1 objectReference: {fileID: 0} + - target: {fileID: 4348065138891612859, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} + propertyPath: _input + value: + objectReference: {fileID: 1859157935} + - target: {fileID: 4348065138891612859, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} + propertyPath: _camera + value: + objectReference: {fileID: 1850161070} - target: {fileID: 4641286401744272850, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} propertyPath: _fpCamera value: @@ -4406,10 +4407,6 @@ PrefabInstance: propertyPath: _BulletImpact value: objectReference: {fileID: 252487699782519274, guid: 82851982cb47c134a8403ffcb052b9d2, type: 3} - - target: {fileID: 4641286401744272850, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} - propertyPath: _timeBetweenReloads - value: 1.5 - objectReference: {fileID: 0} - target: {fileID: 5428686084875064986, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} propertyPath: m_Name value: MuzzleFlash @@ -4434,10 +4431,6 @@ PrefabInstance: propertyPath: reloadAction.m_Id value: 9a13135e-9c14-4a7a-a277-378fc4a31eae objectReference: {fileID: 0} - - target: {fileID: 7060812621224890882, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} - propertyPath: _timeBetweenReloads - value: 1.5 - objectReference: {fileID: 0} - target: {fileID: 7060812621224890882, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} propertyPath: reloadAction.m_Name value: Reload @@ -4534,6 +4527,5 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - m_RemovedComponents: - - {fileID: 9083568455291165773, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} + m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: c5efc39a8aaf6e64ea40e9ad573e9b47, type: 3} diff --git a/Assets/Weapons/Ammo.cs b/Assets/Weapons/Ammo.cs index f0ebf60..92ed839 100644 --- a/Assets/Weapons/Ammo.cs +++ b/Assets/Weapons/Ammo.cs @@ -1,26 +1,23 @@ -using System; -using System.Collections; -using System.Collections.Generic; using UnityEngine; public class Ammo : MonoBehaviour { [Header("Config")] - [SerializeField] private int _ammoType; - [SerializeField] private int _maxBeltAmmoAmount; + [SerializeField] private AmmoType _ammoType; [SerializeField] private int _maxMagAmmoAmount; [Header("State")] - [SerializeField] private int _currentBeltAmmoAmount; [SerializeField] private int _currentMagAmmoAmount; [Header("Connections")] + [SerializeField] private AmmoBelt _ammoBelt; [SerializeField] private FloatVariable _S_currentBeltAmmoAmount; [SerializeField] private FloatVariable _S_currentMagAmmoAmount; private void Awake() { - _currentBeltAmmoAmount = _maxBeltAmmoAmount; + _ammoBelt = GetComponentInParent(); + _currentMagAmmoAmount = _maxMagAmmoAmount; } @@ -31,7 +28,7 @@ public class Ammo : MonoBehaviour #region Public Properties - public int AmmoType + public AmmoType AmmoType { get => _ammoType; set => _ammoType = value; @@ -57,15 +54,15 @@ public class Ammo : MonoBehaviour { int diff = _maxMagAmmoAmount - _currentMagAmmoAmount; print(diff); - if (diff < _currentBeltAmmoAmount) + if (diff < _ammoBelt.GetBeltCurrentAmmoAmount(_ammoType)) { - _currentBeltAmmoAmount -= diff; + _ammoBelt.ModifyBeltCurrentAmmoAmount(_ammoType, -diff); _currentMagAmmoAmount += diff; } else { - _currentMagAmmoAmount += _currentBeltAmmoAmount; - _currentBeltAmmoAmount = 0; + _currentMagAmmoAmount += _ammoBelt.GetBeltCurrentAmmoAmount(_ammoType); + _ammoBelt.SetBeltCurrentAmmoAmount(_ammoType, 0); } AmmoUpdate(); @@ -98,14 +95,14 @@ public class Ammo : MonoBehaviour public float GetBeltAmmoFactor() { - return _currentBeltAmmoAmount / (float)_maxBeltAmmoAmount; + return _ammoBelt.GetBeltCurrentAmmoAmount(_ammoType) / (float)_ammoBelt.GetBeltMaxAmmoAmount(_ammoType); } #endregion private void AmmoUpdate() { - _S_currentBeltAmmoAmount.Value = _currentBeltAmmoAmount; + _S_currentBeltAmmoAmount.Value = _ammoBelt.GetBeltCurrentAmmoAmount(_ammoType); _S_currentMagAmmoAmount.Value = _currentMagAmmoAmount; } } \ No newline at end of file diff --git a/Assets/Weapons/Crossbow.prefab b/Assets/Weapons/Crossbow.prefab index 09ba53d..8fe23b6 100644 --- a/Assets/Weapons/Crossbow.prefab +++ b/Assets/Weapons/Crossbow.prefab @@ -79,13 +79,17 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 01ef216392c8f33409f2efcd91f4e510, type: 3} m_Name: m_EditorClassIdentifier: + _ammoType: 0 + _index: 0 _range: 100 _weaponDamage: 100 _timeBetweenShots: 2 + _timeBetweenReloads: 1.5 _fpCamera: {fileID: 0} _muzzleFlash: {fileID: 0} _bulletImpact: {fileID: 252487699782519274, guid: 82851982cb47c134a8403ffcb052b9d2, type: 3} _ammo: {fileID: 1537282594975695390} + _canShoot: 0 --- !u!114 &1537282594975695390 MonoBehaviour: m_ObjectHideFlags: 0 @@ -98,11 +102,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 97c35481a551c3a4d9d5f0e3fe13a03d, type: 3} m_Name: m_EditorClassIdentifier: - _ammoType: 1 - _maxBeltAmmoAmount: 10 + _ammoType: 0 _maxMagAmmoAmount: 1 - _currentBeltAmmoAmount: 0 _currentMagAmmoAmount: 0 + _ammoBelt: {fileID: 0} _S_currentBeltAmmoAmount: {fileID: 11400000, guid: f19d1fc1d5102a545800f4f8273640b1, type: 2} _S_currentMagAmmoAmount: {fileID: 11400000, guid: 9a77ec14c64717643bed1c6e7a3885ae, type: 2} --- !u!114 &7047714336720636310 diff --git a/Assets/Weapons/Handgun.prefab b/Assets/Weapons/Handgun.prefab index 812ef0a..803e92b 100644 --- a/Assets/Weapons/Handgun.prefab +++ b/Assets/Weapons/Handgun.prefab @@ -129,13 +129,17 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 01ef216392c8f33409f2efcd91f4e510, type: 3} m_Name: m_EditorClassIdentifier: + _ammoType: 2 + _index: 0 _range: 100 - _weaponDamage: 50 - _timeBetweenShots: 1 + _weaponDamage: 25 + _timeBetweenShots: 0.5 + _timeBetweenReloads: 1.5 _fpCamera: {fileID: 0} _muzzleFlash: {fileID: 0} _bulletImpact: {fileID: 252487699782519274, guid: 82851982cb47c134a8403ffcb052b9d2, type: 3} _ammo: {fileID: 4085014607242062707} + _canShoot: 0 --- !u!114 &4085014607242062707 MonoBehaviour: m_ObjectHideFlags: 0 @@ -148,10 +152,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 97c35481a551c3a4d9d5f0e3fe13a03d, type: 3} m_Name: m_EditorClassIdentifier: - _ammoType: 3 - _maxBeltAmmoAmount: 20 + _ammoType: 2 _maxMagAmmoAmount: 4 - _currentBeltAmmoAmount: 0 _currentMagAmmoAmount: 0 + _ammoBelt: {fileID: 0} _S_currentBeltAmmoAmount: {fileID: 11400000, guid: f19d1fc1d5102a545800f4f8273640b1, type: 2} _S_currentMagAmmoAmount: {fileID: 11400000, guid: 9a77ec14c64717643bed1c6e7a3885ae, type: 2} diff --git a/Assets/Weapons/MachineGun.prefab b/Assets/Weapons/MachineGun.prefab index 8bb7c1c..a628059 100644 --- a/Assets/Weapons/MachineGun.prefab +++ b/Assets/Weapons/MachineGun.prefab @@ -48,11 +48,12 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 01ef216392c8f33409f2efcd91f4e510, type: 3} m_Name: m_EditorClassIdentifier: + _ammoType: 1 _index: 0 _range: 100 _weaponDamage: 4 _timeBetweenShots: 0.1 - _timeBetweenReloads: 0 + _timeBetweenReloads: 1.5 _fpCamera: {fileID: 0} _muzzleFlash: {fileID: 0} _bulletImpact: {fileID: 252487699782519274, guid: 82851982cb47c134a8403ffcb052b9d2, type: 3} @@ -70,11 +71,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 97c35481a551c3a4d9d5f0e3fe13a03d, type: 3} m_Name: m_EditorClassIdentifier: - _ammoType: 2 - _maxBeltAmmoAmount: 300 + _ammoType: 1 _maxMagAmmoAmount: 45 - _currentBeltAmmoAmount: 0 _currentMagAmmoAmount: 0 + _ammoBelt: {fileID: 0} _S_currentBeltAmmoAmount: {fileID: 11400000, guid: f19d1fc1d5102a545800f4f8273640b1, type: 2} _S_currentMagAmmoAmount: {fileID: 11400000, guid: 9a77ec14c64717643bed1c6e7a3885ae, type: 2} --- !u!114 &1366121443174139284 diff --git a/Assets/Weapons/Revolver.prefab b/Assets/Weapons/Revolver.prefab index f10359e..ac4dc65 100644 --- a/Assets/Weapons/Revolver.prefab +++ b/Assets/Weapons/Revolver.prefab @@ -50,13 +50,17 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 01ef216392c8f33409f2efcd91f4e510, type: 3} m_Name: m_EditorClassIdentifier: + _ammoType: 3 + _index: 0 _range: 100 - _weaponDamage: 25 - _timeBetweenShots: 0.5 + _weaponDamage: 50 + _timeBetweenShots: 1 + _timeBetweenReloads: 1.5 _fpCamera: {fileID: 0} _muzzleFlash: {fileID: 6070271853189258210} _bulletImpact: {fileID: 252487699782519274, guid: 82851982cb47c134a8403ffcb052b9d2, type: 3} _ammo: {fileID: 8817107657672854186} + _canShoot: 0 --- !u!114 &8817107657672854186 MonoBehaviour: m_ObjectHideFlags: 0 @@ -69,11 +73,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 97c35481a551c3a4d9d5f0e3fe13a03d, type: 3} m_Name: m_EditorClassIdentifier: - _ammoType: 0 - _maxBeltAmmoAmount: 45 + _ammoType: 3 _maxMagAmmoAmount: 7 - _currentBeltAmmoAmount: 0 _currentMagAmmoAmount: 3 + _ammoBelt: {fileID: 0} _S_currentBeltAmmoAmount: {fileID: 11400000, guid: f19d1fc1d5102a545800f4f8273640b1, type: 2} _S_currentMagAmmoAmount: {fileID: 11400000, guid: 9a77ec14c64717643bed1c6e7a3885ae, type: 2} --- !u!114 &3637412066234339666 diff --git a/Assets/Weapons/Weapon.cs b/Assets/Weapons/Weapon.cs index d0a7358..cbc574b 100644 --- a/Assets/Weapons/Weapon.cs +++ b/Assets/Weapons/Weapon.cs @@ -5,7 +5,7 @@ using UnityEngine; public class Weapon : MonoBehaviour { [Header("Config")] - [SerializeField] private int _index; + [SerializeField] private AmmoType _ammoType; [SerializeField] private float _range = 100f; [SerializeField] private float _weaponDamage = 25f; [SerializeField] private float _timeBetweenShots; @@ -18,6 +18,7 @@ public class Weapon : MonoBehaviour [SerializeField] private Ammo _ammo; [Header("State")] + [SerializeField] private int _index; [SerializeField] private bool _canShoot; private void Awake()