From 31a93de2cdc345a069802eea2ba5adf873ec6ff5 Mon Sep 17 00:00:00 2001
From: MuhamadAjiW <16521119@mahasiswa.itb.ac.id>
Date: Tue, 23 Apr 2024 01:57:52 +0700
Subject: [PATCH] fix: weapon swap system

---
 Assets/Data/Weapon Data/NoWeapon.meta         |  8 ++++
 .../Weapon Data/NoWeapon/NoWeaponData.asset   | 19 ++++++++
 .../NoWeapon/NoWeaponData.asset.meta          |  8 ++++
 .../Resources/Prefabs/Weapons/NoWeapon.meta   |  8 ++++
 .../Prefabs/Weapons/NoWeapon/NoWeapon.prefab  | 48 +++++++++++++++++++
 .../Weapons/NoWeapon/NoWeapon.prefab.meta     |  7 +++
 .../Scripts/Core/Game/GameStateController.cs  |  2 +-
 .../Core/Objects/Weapons/NoWeapon/NoWeapon.cs |  3 ++
 .../Objects/Weapons/TestWeapon/TestWeapon.cs  |  6 +--
 .../Core/Player/PlayerInputController.cs      |  6 +++
 .../Library/BaseClasses/BaseObjectManager.cs  |  2 +-
 .../Controller/AnimationController.cs         |  6 +--
 .../BaseClasses/EntityObject/AttackObject.cs  |  2 +-
 .../EntityObject/CombatantEntity.cs           | 15 +++---
 .../BaseClasses/EntityObject/WorldEntity.cs   |  2 +-
 Assets/Scripts/Library/Util/ObjectFactory.cs  |  8 ++--
 16 files changed, 128 insertions(+), 22 deletions(-)
 create mode 100644 Assets/Data/Weapon Data/NoWeapon.meta
 create mode 100644 Assets/Data/Weapon Data/NoWeapon/NoWeaponData.asset
 create mode 100644 Assets/Data/Weapon Data/NoWeapon/NoWeaponData.asset.meta
 create mode 100644 Assets/Resources/Prefabs/Weapons/NoWeapon.meta
 create mode 100644 Assets/Resources/Prefabs/Weapons/NoWeapon/NoWeapon.prefab
 create mode 100644 Assets/Resources/Prefabs/Weapons/NoWeapon/NoWeapon.prefab.meta

diff --git a/Assets/Data/Weapon Data/NoWeapon.meta b/Assets/Data/Weapon Data/NoWeapon.meta
new file mode 100644
index 00000000..263d8dab
--- /dev/null
+++ b/Assets/Data/Weapon Data/NoWeapon.meta	
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 13ea90e4dc39c0a4dbe069150555b0da
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Data/Weapon Data/NoWeapon/NoWeaponData.asset b/Assets/Data/Weapon Data/NoWeapon/NoWeaponData.asset
new file mode 100644
index 00000000..24c9777c
--- /dev/null
+++ b/Assets/Data/Weapon Data/NoWeapon/NoWeaponData.asset	
@@ -0,0 +1,19 @@
+%YAML 1.1
+%TAG !u! tag:unity3d.com,2011:
+--- !u!114 &11400000
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 0}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: abc4ca3f3ac6a5646addd6dfbe0b996b, type: 3}
+  m_Name: NoWeaponData
+  m_EditorClassIdentifier: 
+  baseDamage: 0
+  knockbackPower: 0
+  attackInterval: 0
+  alternateAttackInterval: 0
+  model: {fileID: 0}
diff --git a/Assets/Data/Weapon Data/NoWeapon/NoWeaponData.asset.meta b/Assets/Data/Weapon Data/NoWeapon/NoWeaponData.asset.meta
new file mode 100644
index 00000000..d306dba1
--- /dev/null
+++ b/Assets/Data/Weapon Data/NoWeapon/NoWeaponData.asset.meta	
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 1f732bd8cfb50da4dad418d1fcdedd01
+NativeFormatImporter:
+  externalObjects: {}
+  mainObjectFileID: 11400000
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Resources/Prefabs/Weapons/NoWeapon.meta b/Assets/Resources/Prefabs/Weapons/NoWeapon.meta
new file mode 100644
index 00000000..f40f7a8c
--- /dev/null
+++ b/Assets/Resources/Prefabs/Weapons/NoWeapon.meta
@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 2f2b6e976f72f4c499e805867cfefad0
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Resources/Prefabs/Weapons/NoWeapon/NoWeapon.prefab b/Assets/Resources/Prefabs/Weapons/NoWeapon/NoWeapon.prefab
new file mode 100644
index 00000000..0e5dbfdd
--- /dev/null
+++ b/Assets/Resources/Prefabs/Weapons/NoWeapon/NoWeapon.prefab
@@ -0,0 +1,48 @@
+%YAML 1.1
+%TAG !u! tag:unity3d.com,2011:
+--- !u!1 &834808351098187952
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 7056211513795390601}
+  - component: {fileID: 2798277747015640978}
+  m_Layer: 6
+  m_Name: NoWeapon
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!4 &7056211513795390601
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 834808351098187952}
+  serializedVersion: 2
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_ConstrainProportionsScale: 0
+  m_Children: []
+  m_Father: {fileID: 0}
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!114 &2798277747015640978
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 834808351098187952}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: aeafa263f26404c4a9f653e7d0889d83, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  prefabPath: Prefabs/Weapons/NoWeapon/NoWeapon
+  data: {fileID: 11400000, guid: 1f732bd8cfb50da4dad418d1fcdedd01, type: 2}
diff --git a/Assets/Resources/Prefabs/Weapons/NoWeapon/NoWeapon.prefab.meta b/Assets/Resources/Prefabs/Weapons/NoWeapon/NoWeapon.prefab.meta
new file mode 100644
index 00000000..806bf767
--- /dev/null
+++ b/Assets/Resources/Prefabs/Weapons/NoWeapon/NoWeapon.prefab.meta
@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: 967e1612f343be242bd63968a7374248
+PrefabImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Scripts/Core/Game/GameStateController.cs b/Assets/Scripts/Core/Game/GameStateController.cs
index df1ecb8f..ac2f5719 100644
--- a/Assets/Scripts/Core/Game/GameStateController.cs
+++ b/Assets/Scripts/Core/Game/GameStateController.cs
@@ -73,7 +73,7 @@ public class GameStateController
     // Internal Functions
     private void LogGameStateEvent(GameStateChangeArgs e)
     {
-        Debug.Log(string.Format("GameState {0}; Current gamestate is {1}", e.EventType, e.NewGameState));
+        Debug.Log($"GameState {e.EventType}; Current gamestate is {e.NewGameState}");
     }
 
     private void SetState(GameState gameState)
diff --git a/Assets/Scripts/Core/Objects/Weapons/NoWeapon/NoWeapon.cs b/Assets/Scripts/Core/Objects/Weapons/NoWeapon/NoWeapon.cs
index 31cbc8cb..50b41880 100644
--- a/Assets/Scripts/Core/Objects/Weapons/NoWeapon/NoWeapon.cs
+++ b/Assets/Scripts/Core/Objects/Weapons/NoWeapon/NoWeapon.cs
@@ -1,4 +1,7 @@
 public class NoWeapon : WeaponObject{
+    // Constants
+    public const string weaponPrefab = "Prefabs/Weapons/NoWeapon/NoWeapon";
+    
     // Functions
     protected override void OnAttack()
     {
diff --git a/Assets/Scripts/Core/Objects/Weapons/TestWeapon/TestWeapon.cs b/Assets/Scripts/Core/Objects/Weapons/TestWeapon/TestWeapon.cs
index 25364a3d..0adf13ea 100644
--- a/Assets/Scripts/Core/Objects/Weapons/TestWeapon/TestWeapon.cs
+++ b/Assets/Scripts/Core/Objects/Weapons/TestWeapon/TestWeapon.cs
@@ -3,9 +3,9 @@ using UnityEngine;
 public class TestWeapon : WeaponObject
 {
     // Constants
-    private static readonly string weaponPrefab = "Prefabs/Weapons/TestWeapon/TestWeapon";
-    private static readonly string hitboxPrefab = "Prefabs/Weapons/TestWeapon/WeaponHitbox";
-    private static readonly string projectilePrefab = "Prefabs/Weapons/TestWeapon/TestProjectile";
+    public const string weaponPrefab = "Prefabs/Weapons/TestWeapon/TestWeapon";
+    private const string hitboxPrefab = "Prefabs/Weapons/TestWeapon/WeaponHitbox";
+    private const string projectilePrefab = "Prefabs/Weapons/TestWeapon/TestProjectile";
     
     // Attribute
     public float fireRange = 100;
diff --git a/Assets/Scripts/Core/Player/PlayerInputController.cs b/Assets/Scripts/Core/Player/PlayerInputController.cs
index afe5b3b5..e0f77e0d 100644
--- a/Assets/Scripts/Core/Player/PlayerInputController.cs
+++ b/Assets/Scripts/Core/Player/PlayerInputController.cs
@@ -7,6 +7,7 @@ public class PlayerInputController
     private readonly Player player;
     public float movementInputX;
     public float movementInputZ;
+    public float movementInputScroll;
     public bool movementInputJump;
 
     // Events
@@ -23,11 +24,16 @@ public class PlayerInputController
     {
         movementInputX = Input.GetAxisRaw("Horizontal");
         movementInputZ = Input.GetAxisRaw("Vertical");
+        movementInputScroll = Input.GetAxisRaw("Mouse ScrollWheel");
 
         if(Input.GetButtonDown("Jump") && player.Grounded)
         {
             OnJumpEvent?.Invoke();
         }
+        if(movementInputScroll != 0)
+        {
+            player.EquipWeapon(player.WeaponIndex + (int)(movementInputScroll * 10));
+        }
 
         if(Input.GetKeyDown(GameInput.instance.attackButton))
         {
diff --git a/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs b/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs
index 2114ae63..c99848bc 100644
--- a/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs
+++ b/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs
@@ -22,7 +22,7 @@ public class BaseObjectManager : MonoBehaviour
         }
         idArray += "]";
         
-        Debug.Log(string.Format("Object ids in {0}: {1}", ManagerName, idArray));
+        Debug.Log($"Object ids in {ManagerName}: {idArray}");
     }
 
     public virtual WorldObject[] GetWorldObjects()
diff --git a/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs b/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs
index aa119af6..428dc3f9 100644
--- a/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs
+++ b/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs
@@ -16,15 +16,15 @@ public abstract class AnimationController
 
         if(model == null) 
         {
-            Debug.LogWarning("Animated object of " + animable.name + " does not have a model");
+            Debug.LogWarning($"Animated object of {animable.name} does not have a model");
         }
         if(animator == null)
         {
-            Debug.LogWarning("Animated object of " + animable.name + " does not have an animator in its model");
+            Debug.LogWarning($"Animated object of {animable.name} does not have an animator in its model");
         }
         if(meshRenderer == null)
         {
-            Debug.LogWarning("Animated object of " + animable.name + " does not have an meshRenderer in its model");
+            Debug.LogWarning($"Animated object of {animable.name} does not have an meshRenderer in its model");
         }
 
     }
diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/AttackObject.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/AttackObject.cs
index 78046ffe..71e72f84 100644
--- a/Assets/Scripts/Library/BaseClasses/EntityObject/AttackObject.cs
+++ b/Assets/Scripts/Library/BaseClasses/EntityObject/AttackObject.cs
@@ -57,7 +57,7 @@ public class AttackObject : MonoBehaviour, IDamaging, IKnockback
         
         if(damageableObject.Damageable)
         {
-            Debug.Log(string.Format("Hit in hitbox of {0} by {1} with damage of {2}", transform.name, objectParent.name, Damage));
+            Debug.Log($"Hit in hitbox of {transform.name} by {objectParent.name} with damage of {Damage}");
             
             damageableObject.InflictDamage(Damage);
             OnDamageEvent?.Invoke();
diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/CombatantEntity.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/CombatantEntity.cs
index a837c75c..5eefbdd8 100644
--- a/Assets/Scripts/Library/BaseClasses/EntityObject/CombatantEntity.cs
+++ b/Assets/Scripts/Library/BaseClasses/EntityObject/CombatantEntity.cs
@@ -10,10 +10,11 @@ public class CombatantEntity : DamageableEntity, IArmed
     [SerializeField] protected Vector3 weaponLocation;
     public List<WeaponObject> weaponList = new();
     private int weaponIndex;
+    private WeaponObject weapon;
     
     // Set-Getters
     public List<WeaponObject> WeaponList => weaponList;
-    public WeaponObject Weapon => weaponList.Count > 0? weaponList[WeaponIndex] : null;
+    public WeaponObject Weapon => weapon;
     public Transform Orientation => transform;
     public Vector3 WeaponLocation => weaponLocation;
     public string AttackLayerCode => EnvironmentConfig.LAYER_ENVIRONMENT_ATTACK;
@@ -46,22 +47,20 @@ public class CombatantEntity : DamageableEntity, IArmed
         {
             return;
         }
-        Debug.Log($"Equipping weapon {WeaponIndex}");
+        Debug.Log($"Equipping weapon {index}");
 
         UnequipWeapon();
 
         WeaponIndex = index;
-
+        WeaponObject blueprint = WeaponList[WeaponIndex];
         WeaponObject weaponObject = ObjectFactory.CreateObject<WeaponObject>(
-            prefabPath: Weapon.prefabPath,
+            prefabPath: blueprint == null? NoWeapon.weaponPrefab : blueprint.prefabPath,
             parent: transform, 
-            position: WeaponLocation,
             objectName: EnvironmentConfig.OBJECT_WEAPON
         );
+        weaponObject.transform.localPosition = WeaponLocation;
         weaponObject.gameObject.layer = LayerMask.NameToLayer(AttackLayerCode);
-        WeaponList[weaponIndex] = weaponObject;
-
-        Debug.Log(Weapon == null);
+        weapon = weaponObject;
     }
 
     public void UnequipWeapon(){
diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
index 7f518119..0a5e8483 100644
--- a/Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
+++ b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
@@ -38,7 +38,7 @@ public class WorldEntity : WorldObject, IRigid
         groundDetectionSize = new Vector3(0.05f, 0.05f, 0.05f);      
         if(rigidbody == null)
         {
-            Debug.LogWarning("Rigid entity " + name + " does not have a rigidbody"); 
+            Debug.LogWarning($"Rigid entity {name} does not have a rigidbody"); 
         }
         groundLayers = LayerMask.GetMask(EnvironmentConfig.LAYER_DEFAULT);
     }
diff --git a/Assets/Scripts/Library/Util/ObjectFactory.cs b/Assets/Scripts/Library/Util/ObjectFactory.cs
index 8760e029..b4d3abaf 100644
--- a/Assets/Scripts/Library/Util/ObjectFactory.cs
+++ b/Assets/Scripts/Library/Util/ObjectFactory.cs
@@ -20,7 +20,7 @@ public static class ObjectFactory
         
         if(prefabObject == null)
         {
-            Debug.LogError("Prefab not found: " + prefabPath);
+            Debug.LogError($"Prefab not found: {prefabPath}");
         }
 
         return CreateObject(prefabObject, parent, position, scale, rotation, renderingOrder, objectName);
@@ -83,7 +83,7 @@ public static class ObjectFactory
 
         if(!prefabObject.TryGetComponent<T>(out var UnityObject))
         {
-            Debug.LogError("Loaded prefab is not a a valid type: " + prefabPath);
+            Debug.LogError($"Loaded prefab is not a a valid type: {prefabPath}");
         }
 
         return UnityObject;
@@ -111,7 +111,7 @@ public static class ObjectFactory
 
         if(!prefabObject.TryGetComponent<T>(out var UnityObject))
         {
-            Debug.LogError("Loaded gameobject is not a a valid type: " + gameObject.name);
+            Debug.LogError($"Loaded gameobject is not a a valid type: {gameObject.name}");
         }
 
         return UnityObject;
@@ -184,7 +184,7 @@ public static class ObjectFactory
 
         if(!attackObject.TryGetComponent<T>(out var UnityObject))
         {
-            Debug.LogError("Loaded prefab is not a a valid type: " + prefabPath);
+            Debug.LogError($"Loaded prefab is not a a valid type: {prefabPath}");
         }
 
         return UnityObject;
-- 
GitLab