From 05f67524d4541a2063363b82e0ff149d25b5b1b8 Mon Sep 17 00:00:00 2001
From: MuhamadAjiW <16521119@mahasiswa.itb.ac.id>
Date: Wed, 24 Apr 2024 10:31:50 +0700
Subject: [PATCH] refactor: layer and damage modifier

---
 .../Characters/Basic1/Basic1Model.prefab      |  2 +-
 .../Characters/Basic2/Basic2Model.prefab      |  6 +++--
 .../{Basic3.prefab => Basic3Model.prefab}     |  2 +-
 ...c3.prefab.meta => Basic3Model.prefab.meta} |  0
 .../TestInteractableModel.prefab              |  6 +++--
 .../TestInteractable/TestInteractable.prefab  | 12 ++++-----
 .../Prefabs/Mobs/Dummy/DamagingHitbox.prefab  |  2 +-
 .../Resources/Prefabs/Mobs/Dummy/Dummy.prefab | 14 +++++------
 .../Core/Entities/Mobs/EnemyEntity.cs         |  9 +++----
 Assets/_Scripts/Core/Game/GameController.cs   |  2 +-
 .../Core/Objects/Collectibles/Collectible.cs  |  2 ++
 Assets/_Scripts/Core/Player/Player.cs         |  6 ++---
 .../EntityObject/CombatantEntity.cs           | 25 +++++++++++++++++--
 .../BaseClasses/EntityObject/WorldEntity.cs   |  7 ++++++
 .../BaseClasses/EntityObject/WorldObject.cs   | 17 ++++++++++++-
 Assets/_Scripts/Library/Interfaces/IArmed.cs  |  4 +--
 16 files changed, 81 insertions(+), 35 deletions(-)
 rename Assets/Resources/Models/Characters/Basic3/{Basic3.prefab => Basic3Model.prefab} (99%)
 rename Assets/Resources/Models/Characters/Basic3/{Basic3.prefab.meta => Basic3Model.prefab.meta} (100%)

diff --git a/Assets/Resources/Models/Characters/Basic1/Basic1Model.prefab b/Assets/Resources/Models/Characters/Basic1/Basic1Model.prefab
index 68a5a573..68beca2a 100644
--- a/Assets/Resources/Models/Characters/Basic1/Basic1Model.prefab
+++ b/Assets/Resources/Models/Characters/Basic1/Basic1Model.prefab
@@ -30,7 +30,7 @@ Transform:
   m_GameObject: {fileID: 125248941348886635}
   serializedVersion: 2
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: 0, y: 0.75, z: 0}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
   m_LocalScale: {x: 0.4, y: 1.5, z: 0.4}
   m_ConstrainProportionsScale: 0
   m_Children: []
diff --git a/Assets/Resources/Models/Characters/Basic2/Basic2Model.prefab b/Assets/Resources/Models/Characters/Basic2/Basic2Model.prefab
index 1ff50a5d..49c87719 100644
--- a/Assets/Resources/Models/Characters/Basic2/Basic2Model.prefab
+++ b/Assets/Resources/Models/Characters/Basic2/Basic2Model.prefab
@@ -30,7 +30,7 @@ Transform:
   m_GameObject: {fileID: 3596266059315004415}
   serializedVersion: 2
   m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
-  m_LocalPosition: {x: 0, y: 0.75, z: 0}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
   m_LocalScale: {x: 2, y: 1.5, z: 2}
   m_ConstrainProportionsScale: 0
   m_Children: []
@@ -143,4 +143,6 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: dc4d5359067cdfb47a45684d6cc1482a, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
-  WeaponLocation: {x: 0, y: 0, z: 0}
+  staticWeaponPivot: {x: 0.5, y: 0, z: 0.375}
+  renderer: {fileID: 0}
+  bottomOffset: 0
diff --git a/Assets/Resources/Models/Characters/Basic3/Basic3.prefab b/Assets/Resources/Models/Characters/Basic3/Basic3Model.prefab
similarity index 99%
rename from Assets/Resources/Models/Characters/Basic3/Basic3.prefab
rename to Assets/Resources/Models/Characters/Basic3/Basic3Model.prefab
index 5f3bdad7..6110bdb0 100644
--- a/Assets/Resources/Models/Characters/Basic3/Basic3.prefab
+++ b/Assets/Resources/Models/Characters/Basic3/Basic3Model.prefab
@@ -14,7 +14,7 @@ GameObject:
   - component: {fileID: 1523314302102385116}
   - component: {fileID: 138729632845352590}
   m_Layer: 0
-  m_Name: Basic3
+  m_Name: Basic3Model
   m_TagString: Untagged
   m_Icon: {fileID: 0}
   m_NavMeshLayer: 0
diff --git a/Assets/Resources/Models/Characters/Basic3/Basic3.prefab.meta b/Assets/Resources/Models/Characters/Basic3/Basic3Model.prefab.meta
similarity index 100%
rename from Assets/Resources/Models/Characters/Basic3/Basic3.prefab.meta
rename to Assets/Resources/Models/Characters/Basic3/Basic3Model.prefab.meta
diff --git a/Assets/Resources/Models/Environment/TestInteractable/TestInteractableModel.prefab b/Assets/Resources/Models/Environment/TestInteractable/TestInteractableModel.prefab
index 6ae69b6e..53dcc6de 100644
--- a/Assets/Resources/Models/Environment/TestInteractable/TestInteractableModel.prefab
+++ b/Assets/Resources/Models/Environment/TestInteractable/TestInteractableModel.prefab
@@ -30,7 +30,7 @@ Transform:
   m_GameObject: {fileID: 5071667469683539556}
   serializedVersion: 2
   m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
-  m_LocalPosition: {x: 0, y: 0.5, z: 0}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_ConstrainProportionsScale: 0
   m_Children: []
@@ -143,4 +143,6 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: dc4d5359067cdfb47a45684d6cc1482a, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
-  WeaponLocation: {x: 0, y: 0, z: 0}
+  staticWeaponPivot: {x: 0, y: 0, z: 0}
+  renderer: {fileID: 0}
+  bottomOffset: 0
diff --git a/Assets/Resources/Prefabs/Environment/TestInteractable/TestInteractable.prefab b/Assets/Resources/Prefabs/Environment/TestInteractable/TestInteractable.prefab
index 6c3119db..6dea883c 100644
--- a/Assets/Resources/Prefabs/Environment/TestInteractable/TestInteractable.prefab
+++ b/Assets/Resources/Prefabs/Environment/TestInteractable/TestInteractable.prefab
@@ -31,7 +31,7 @@ Transform:
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_ConstrainProportionsScale: 0
   m_Children:
-  - {fileID: 6312057550851766622}
+  - {fileID: 5263704032773805873}
   m_Father: {fileID: 0}
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
 --- !u!114 &344713511152250207
@@ -67,7 +67,7 @@ BoxCollider:
   serializedVersion: 3
   m_Size: {x: 1, y: 1, z: 1}
   m_Center: {x: -1, y: 0.5, z: 0}
---- !u!1001 &7576994708712382837
+--- !u!1001 &8625066742953119514
 PrefabInstance:
   m_ObjectHideFlags: 0
   serializedVersion: 2
@@ -81,7 +81,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 4521062537676429355, guid: ec549132bf117594e81e95cc4ae15b07, type: 3}
       propertyPath: m_LocalPosition.y
-      value: 0.5
+      value: 0
       objectReference: {fileID: 0}
     - target: {fileID: 4521062537676429355, guid: ec549132bf117594e81e95cc4ae15b07, type: 3}
       propertyPath: m_LocalPosition.z
@@ -117,15 +117,15 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 5071667469683539556, guid: ec549132bf117594e81e95cc4ae15b07, type: 3}
       propertyPath: m_Name
-      value: Model
+      value: TestInteractableModel
       objectReference: {fileID: 0}
     m_RemovedComponents: []
     m_RemovedGameObjects: []
     m_AddedGameObjects: []
     m_AddedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: ec549132bf117594e81e95cc4ae15b07, type: 3}
---- !u!4 &6312057550851766622 stripped
+--- !u!4 &5263704032773805873 stripped
 Transform:
   m_CorrespondingSourceObject: {fileID: 4521062537676429355, guid: ec549132bf117594e81e95cc4ae15b07, type: 3}
-  m_PrefabInstance: {fileID: 7576994708712382837}
+  m_PrefabInstance: {fileID: 8625066742953119514}
   m_PrefabAsset: {fileID: 0}
diff --git a/Assets/Resources/Prefabs/Mobs/Dummy/DamagingHitbox.prefab b/Assets/Resources/Prefabs/Mobs/Dummy/DamagingHitbox.prefab
index 14c10109..b26df103 100644
--- a/Assets/Resources/Prefabs/Mobs/Dummy/DamagingHitbox.prefab
+++ b/Assets/Resources/Prefabs/Mobs/Dummy/DamagingHitbox.prefab
@@ -53,7 +53,7 @@ BoxCollider:
   m_Enabled: 1
   serializedVersion: 3
   m_Size: {x: 2, y: 1.5, z: 2}
-  m_Center: {x: 0, y: 0.75, z: 0}
+  m_Center: {x: 0, y: 0, z: 0}
 --- !u!114 &7768068847418568192
 MonoBehaviour:
   m_ObjectHideFlags: 0
diff --git a/Assets/Resources/Prefabs/Mobs/Dummy/Dummy.prefab b/Assets/Resources/Prefabs/Mobs/Dummy/Dummy.prefab
index 57daa199..52a88af0 100644
--- a/Assets/Resources/Prefabs/Mobs/Dummy/Dummy.prefab
+++ b/Assets/Resources/Prefabs/Mobs/Dummy/Dummy.prefab
@@ -31,7 +31,7 @@ Transform:
   m_LocalScale: {x: 1, y: 1, z: 1}
   m_ConstrainProportionsScale: 0
   m_Children:
-  - {fileID: 2941944313554370296}
+  - {fileID: 1239839213894966083}
   - {fileID: 8287830469197447668}
   m_Father: {fileID: 0}
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -84,10 +84,10 @@ MonoBehaviour:
   maxHealth: 100000
   health: 100000
   damagedDelay: 0.5
+  baseDamage: 0
   weaponList:
   - {fileID: 2798277747015640978, guid: 967e1612f343be242bd63968a7374248, type: 3}
-  baseDamage: 0
---- !u!1001 &1851493345103208170
+--- !u!1001 &2330325376918371153
 PrefabInstance:
   m_ObjectHideFlags: 0
   serializedVersion: 2
@@ -101,7 +101,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 3558461250457509906, guid: 12a4aadfaedb1df42bbe4222406bb2ad, type: 3}
       propertyPath: m_LocalPosition.y
-      value: 0.75
+      value: 0
       objectReference: {fileID: 0}
     - target: {fileID: 3558461250457509906, guid: 12a4aadfaedb1df42bbe4222406bb2ad, type: 3}
       propertyPath: m_LocalPosition.z
@@ -137,17 +137,17 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 3596266059315004415, guid: 12a4aadfaedb1df42bbe4222406bb2ad, type: 3}
       propertyPath: m_Name
-      value: Model
+      value: Basic2Model
       objectReference: {fileID: 0}
     m_RemovedComponents: []
     m_RemovedGameObjects: []
     m_AddedGameObjects: []
     m_AddedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: 12a4aadfaedb1df42bbe4222406bb2ad, type: 3}
---- !u!4 &2941944313554370296 stripped
+--- !u!4 &1239839213894966083 stripped
 Transform:
   m_CorrespondingSourceObject: {fileID: 3558461250457509906, guid: 12a4aadfaedb1df42bbe4222406bb2ad, type: 3}
-  m_PrefabInstance: {fileID: 1851493345103208170}
+  m_PrefabInstance: {fileID: 2330325376918371153}
   m_PrefabAsset: {fileID: 0}
 --- !u!1001 &4258106615694226828
 PrefabInstance:
diff --git a/Assets/_Scripts/Core/Entities/Mobs/EnemyEntity.cs b/Assets/_Scripts/Core/Entities/Mobs/EnemyEntity.cs
index 582d0e7e..a2109b21 100644
--- a/Assets/_Scripts/Core/Entities/Mobs/EnemyEntity.cs
+++ b/Assets/_Scripts/Core/Entities/Mobs/EnemyEntity.cs
@@ -1,15 +1,14 @@
+using UnityEngine;
+
 public abstract class EnemyEntity : CombatantEntity 
 {
-    // Set-Getters
-    public new string AttackLayerCode => EnvironmentConfig.LAYER_ENEMY_ATTACK;
-    public new float AttackMultiplier => GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.Instance.difficulty].enemyDamageMultiplier;
-
     // Functions
     new protected void Start()
     {
         base.Start();
+        SetLayer(EnvironmentConfig.LAYER_ENEMY);
+        SetAttackLayer(EnvironmentConfig.LAYER_ENEMY_ATTACK);
         Health *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.Instance.difficulty].enemyHealthMultiplier;
-        BaseDamage *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.Instance.difficulty].enemyDamageMultiplier;
         tag = EnvironmentConfig.TAG_ENEMY;
     }
 }
diff --git a/Assets/_Scripts/Core/Game/GameController.cs b/Assets/_Scripts/Core/Game/GameController.cs
index a64ee1e5..07e72f81 100644
--- a/Assets/_Scripts/Core/Game/GameController.cs
+++ b/Assets/_Scripts/Core/Game/GameController.cs
@@ -11,7 +11,7 @@ public class GameController : MonoBehaviour
     public GameCameraController mainCamera;
     public GameStateController stateController;
 
-    // Set-getters
+    // Set-Getters
     public bool IsPaused => Time.timeScale == 0;
 
     // Constructor
diff --git a/Assets/_Scripts/Core/Objects/Collectibles/Collectible.cs b/Assets/_Scripts/Core/Objects/Collectibles/Collectible.cs
index 68e6f331..94b33560 100644
--- a/Assets/_Scripts/Core/Objects/Collectibles/Collectible.cs
+++ b/Assets/_Scripts/Core/Objects/Collectibles/Collectible.cs
@@ -15,6 +15,8 @@ public abstract class Collectible : WorldObject
     protected new void Start()
     {
         base.Start();
+        SetLayer(EnvironmentConfig.LAYER_COLLECTIBLE);
+        
         OnCollectEvent += OnCollect;
         OnCollectEvent += OnCollectEnd;
         OnTimeoutEvent += OnTimeout;
diff --git a/Assets/_Scripts/Core/Player/Player.cs b/Assets/_Scripts/Core/Player/Player.cs
index fcd8e252..dcc42f46 100644
--- a/Assets/_Scripts/Core/Player/Player.cs
+++ b/Assets/_Scripts/Core/Player/Player.cs
@@ -15,10 +15,6 @@ public class Player : PlayerEntity
     public PlayerStateController stateController;
     public PlayerStats stats;
 
-    // Set-Getters
-    public new string AttackLayerCode => EnvironmentConfig.LAYER_PLAYER;
-    public new float AttackMultiplier => GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.Instance.difficulty].playerDamageMultiplier;
-
     // Constructor
     new void Start()
     {
@@ -30,6 +26,8 @@ public class Player : PlayerEntity
         inputController = new PlayerInputController(this);
         movementController = new PlayerMovementController(this);
         animationController = new PlayerAnimationController(this);
+        SetLayer(EnvironmentConfig.LAYER_PLAYER);
+        SetAttackLayer(EnvironmentConfig.LAYER_PLAYER_ATTACK);
         GameController.Instance.player = this;
 
 
diff --git a/Assets/_Scripts/Library/BaseClasses/EntityObject/CombatantEntity.cs b/Assets/_Scripts/Library/BaseClasses/EntityObject/CombatantEntity.cs
index abf63889..602552ce 100644
--- a/Assets/_Scripts/Library/BaseClasses/EntityObject/CombatantEntity.cs
+++ b/Assets/_Scripts/Library/BaseClasses/EntityObject/CombatantEntity.cs
@@ -11,14 +11,24 @@ public class CombatantEntity : DamageableEntity, IArmed
     public List<WeaponObject> weaponList = new();
     private int weaponIndex;
     private WeaponObject weapon;
+    private float attackMultiplier = 1f;
+    private string attackLayerCode = EnvironmentConfig.LAYER_ENVIRONMENT_ATTACK;
     
     // Set-Getters
     public List<WeaponObject> WeaponList => weaponList;
     public WeaponObject Weapon => weapon;
     public Transform Orientation => transform;
     public Vector3 WeaponLocation => model.WeaponPivot;
-    public string AttackLayerCode => EnvironmentConfig.LAYER_ENVIRONMENT_ATTACK;
-    public float AttackMultiplier => 1f;
+    public float AttackMultiplier
+    {
+        get => attackMultiplier;
+        set => attackMultiplier = value;
+    }
+    public string AttackLayerCode
+    {
+        get => attackLayerCode;
+        set => attackLayerCode = value;
+    }
     public float BaseDamage 
     {
         get => baseDamage;
@@ -51,6 +61,17 @@ public class CombatantEntity : DamageableEntity, IArmed
 
 
     // Functions
+    public void SetAttackLayer(string attackLayerCode)
+    {
+        AttackLayerCode = attackLayerCode;
+        AttackMultiplier = attackLayerCode switch 
+        {
+            EnvironmentConfig.LAYER_ENEMY_ATTACK => GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.Instance.difficulty].enemyDamageMultiplier,
+            EnvironmentConfig.LAYER_PLAYER_ATTACK => GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.Instance.difficulty].playerDamageMultiplier,
+            _ => 1f
+        };
+    }
+    
     public void EquipWeapon(int index)
     {
         if(weaponList.Count == 0)
diff --git a/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs b/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
index 0b955224..2245c671 100644
--- a/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
+++ b/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
@@ -52,9 +52,16 @@ public class WorldEntity : WorldObject, IRigid
 
         groundDetectionSize = new Vector3(0.05f, 0.05f, 0.05f);      
         groundLayers = LayerMask.GetMask(EnvironmentConfig.LAYER_DEFAULT);
+        model.gameObject.layer = LayerMask.NameToLayer(LayerCode);
     }
 
     // Functions
+    public new void SetLayer(string layerCode)
+    {
+        base.SetLayer(layerCode);
+        model.gameObject.layer = LayerMask.NameToLayer(LayerCode);
+    }
+
     protected void Refresh()
     {
         Rigidbody.AddForce(Vector2.zero);
diff --git a/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldObject.cs b/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldObject.cs
index 1faf5797..02f2f909 100644
--- a/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldObject.cs
+++ b/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldObject.cs
@@ -3,14 +3,20 @@ using UnityEngine;
 public class WorldObject : MonoBehaviour
 {
     // Attributes
+    public string id;
     private static int autoIncrement = 0;
     private int numberId; 
     private string prefix;
-    public string id;
+    private string layerCode = EnvironmentConfig.LAYER_DEFAULT;
 
     // Set-Getters
     public Vector3 Front => transform.rotation * Vector3.forward;
     public Quaternion Rotation => transform.rotation;
+    public string LayerCode
+    {
+        get => layerCode;
+        set => layerCode = value;
+    }
 
     // Constructor
     protected void Start()
@@ -29,8 +35,17 @@ public class WorldObject : MonoBehaviour
         numberId = autoIncrement;
         id = numberId.ToString();
         autoIncrement++;
+
+        gameObject.layer = LayerMask.NameToLayer(LayerCode);
     }
 
+    public void SetLayer(string layerCode)
+    {
+        LayerCode = layerCode;
+        gameObject.layer = LayerMask.NameToLayer(LayerCode);
+    }
+
+
     // Functions
     protected void SetIdPrefix(string prefix)
     {
diff --git a/Assets/_Scripts/Library/Interfaces/IArmed.cs b/Assets/_Scripts/Library/Interfaces/IArmed.cs
index 5282e6a7..905d51a7 100644
--- a/Assets/_Scripts/Library/Interfaces/IArmed.cs
+++ b/Assets/_Scripts/Library/Interfaces/IArmed.cs
@@ -5,8 +5,8 @@ public interface IArmed
 {
     // Set-Getters
     public float BaseDamage { get; set; }
-    public string AttackLayerCode { get; }
-    public float AttackMultiplier { get; }
+    public string AttackLayerCode { get; set; }
+    public float AttackMultiplier { get; set; }
     public Vector3 WeaponLocation { get; }
     public WeaponObject Weapon { get; }
     public Transform Orientation { get; }
-- 
GitLab