From 6ec075c63787a094da7ae6e4a9754e4cf373b7f2 Mon Sep 17 00:00:00 2001
From: MuhamadAjiW <16521119@mahasiswa.itb.ac.id>
Date: Thu, 18 Apr 2024 23:50:35 +0700
Subject: [PATCH] refactor: internal entity controllers

---
 .../Scripts/Config/GameEnvironmentConfig.cs   |  6 +++--
 .../Scripts/Core/Entities/Mobs/Dummy/Dummy.cs | 11 +++++++-
 .../Mobs/Dummy/DummyAnimationController.cs    | 25 +++++++++++++++++++
 .../Dummy/DummyAnimationController.cs.meta    | 11 ++++++++
 .../Mobs/Dummy/DummyStateController.cs        |  8 ++++--
 .../Scripts/Core/Entities/Mobs/EnemyEntity.cs |  2 +-
 Assets/Scripts/Core/Player/Player.cs          |  9 +++++--
 .../Core/Player/PlayerAnimationController.cs  | 15 ++++++-----
 ...tyStateController.meta => Controller.meta} |  0
 .../Controller/AnimationController.cs         |  8 ++++++
 .../Controller/AnimationController.cs.meta    | 11 ++++++++
 .../DamageableEntityStateController.cs        |  0
 .../DamageableEntityStateController.cs.meta   |  0
 .../EntityStateController.cs                  |  0
 .../EntityStateController.cs.meta             |  0
 .../Scripts/Library/Interfaces/IAnimated.cs   |  7 ++++++
 .../Library/Interfaces/IAnimated.cs.meta      | 11 ++++++++
 Assets/Scripts/Library/Util/ObjectFactory.cs  |  4 +--
 18 files changed, 110 insertions(+), 18 deletions(-)
 create mode 100644 Assets/Scripts/Core/Entities/Mobs/Dummy/DummyAnimationController.cs
 create mode 100644 Assets/Scripts/Core/Entities/Mobs/Dummy/DummyAnimationController.cs.meta
 rename Assets/Scripts/Library/BaseClasses/{EntityStateController.meta => Controller.meta} (100%)
 create mode 100644 Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs
 create mode 100644 Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs.meta
 rename Assets/Scripts/Library/BaseClasses/{EntityStateController => Controller}/DamageableEntityStateController.cs (100%)
 rename Assets/Scripts/Library/BaseClasses/{EntityStateController => Controller}/DamageableEntityStateController.cs.meta (100%)
 rename Assets/Scripts/Library/BaseClasses/{EntityStateController => Controller}/EntityStateController.cs (100%)
 rename Assets/Scripts/Library/BaseClasses/{EntityStateController => Controller}/EntityStateController.cs.meta (100%)
 create mode 100644 Assets/Scripts/Library/Interfaces/IAnimated.cs
 create mode 100644 Assets/Scripts/Library/Interfaces/IAnimated.cs.meta

diff --git a/Assets/Scripts/Config/GameEnvironmentConfig.cs b/Assets/Scripts/Config/GameEnvironmentConfig.cs
index 301e610e..4d4f6978 100644
--- a/Assets/Scripts/Config/GameEnvironmentConfig.cs
+++ b/Assets/Scripts/Config/GameEnvironmentConfig.cs
@@ -7,6 +7,8 @@ public static class GameEnvironmentConfig{
     public static string TAG_GROUND = "Ground";
 
     // Layers
-    public static string LAYER_ENEMY_HITBOX = "EnemyHitbox";
-    public static string LAYER_PLAYER_HITBOX = "PlayerHitbox";
+    public static string LAYER_ENEMY = "Enemy";
+    public static string LAYER_PLAYER = "Player";
+    public static string LAYER_ENEMY_ATTACK = "EnemyAttack";
+    public static string LAYER_PLAYER_ATTACK = "PlayerAttack";
 }
\ No newline at end of file
diff --git a/Assets/Scripts/Core/Entities/Mobs/Dummy/Dummy.cs b/Assets/Scripts/Core/Entities/Mobs/Dummy/Dummy.cs
index 84a354a2..43442605 100644
--- a/Assets/Scripts/Core/Entities/Mobs/Dummy/Dummy.cs
+++ b/Assets/Scripts/Core/Entities/Mobs/Dummy/Dummy.cs
@@ -1,3 +1,12 @@
 public class Dummy : EnemyEntity{
-    
+    // Attributes
+    private DummyAnimationController animationController;
+    public DummyStateController stateController;
+
+    // Constructor
+    new void Start(){
+        base.Start();
+        stateController = new DummyStateController(this);
+        animationController = new DummyAnimationController(this);
+    }
 }
\ No newline at end of file
diff --git a/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyAnimationController.cs b/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyAnimationController.cs
new file mode 100644
index 00000000..6aad4717
--- /dev/null
+++ b/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyAnimationController.cs
@@ -0,0 +1,25 @@
+using UnityEngine;
+
+public class DummyAnimationController : AnimationController{
+    // Consts
+    private const string HIT_TRIGGER = "hit_param";
+
+    // Attributes
+    private readonly Dummy dummy;
+
+    // Constructor
+    public DummyAnimationController(Dummy dummy){
+        this.dummy = dummy;
+        model = dummy.transform.Find("Model");
+        animator = model.GetComponent<Animator>();
+        meshRenderer = model.GetComponent<MeshRenderer>();
+
+        dummy.OnDamagedEvent += IndicateDamaged;
+    }
+
+    // Functions
+    private void IndicateDamaged(){
+        Debug.Log("Dummy is damaged");
+        meshRenderer.material.color = Color.red;
+    }
+}
\ No newline at end of file
diff --git a/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyAnimationController.cs.meta b/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyAnimationController.cs.meta
new file mode 100644
index 00000000..b9107983
--- /dev/null
+++ b/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyAnimationController.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 437890b11336c944586038c8f20176bb
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyStateController.cs b/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyStateController.cs
index e66250dd..60c91fb4 100644
--- a/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyStateController.cs
+++ b/Assets/Scripts/Core/Entities/Mobs/Dummy/DummyStateController.cs
@@ -1,5 +1,9 @@
-public class DummyStateController : EntityStateController{
+public class DummyStateController : DamageableEntityStateController{
+    // Constructor
+    public DummyStateController(Dummy dummy) : base(dummy){}
+    
+    // Functions
     public override int UpdateState(){
-        throw new System.NotImplementedException();
+        return EntityState.IDLE;
     }
 }
\ No newline at end of file
diff --git a/Assets/Scripts/Core/Entities/Mobs/EnemyEntity.cs b/Assets/Scripts/Core/Entities/Mobs/EnemyEntity.cs
index b3727e74..88a464b0 100644
--- a/Assets/Scripts/Core/Entities/Mobs/EnemyEntity.cs
+++ b/Assets/Scripts/Core/Entities/Mobs/EnemyEntity.cs
@@ -4,6 +4,6 @@ public abstract class EnemyEntity : Combatant {
         base.Start();
         Health *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].EnemyHealthMultiplier;
         BaseDamage *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].EnemyDamageMultiplier;
-        tag = GameEnvironmentConfig.ENEMY_TAG;
+        tag = GameEnvironmentConfig.TAG_ENEMY;
     }
 }
diff --git a/Assets/Scripts/Core/Player/Player.cs b/Assets/Scripts/Core/Player/Player.cs
index 980be6f2..87e7e5a2 100644
--- a/Assets/Scripts/Core/Player/Player.cs
+++ b/Assets/Scripts/Core/Player/Player.cs
@@ -3,17 +3,21 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
-public class Player : AccompaniableCombatant {
+public class Player : AccompaniableCombatant{
+    // Attributes
     private PlayerAnimationController animationController;
     private PlayerMovementController movementController;
     private PlayerAttackController attackController;
     public PlayerStateController stateController;
     public PlayerStats stats;
 
+    // Constructor
     new void Start(){
         base.Start();
         Health *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].PlayerHealthMultiplier;
-        BaseDamage *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].PlayerDamageMultiplier;
+        
+        // TODO: Review, base damage is currently done in the ObjectFactory. Might need to decide which is best
+        // BaseDamage *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].PlayerDamageMultiplier;
 
         Weapon = GetComponentInChildren<WeaponObject>();
         stateController = new PlayerStateController(this);
@@ -25,6 +29,7 @@ public class Player : AccompaniableCombatant {
         GameController.instance.player = this;
     }
 
+    // Functions
     new void Update(){
         base.Update();
 
diff --git a/Assets/Scripts/Core/Player/PlayerAnimationController.cs b/Assets/Scripts/Core/Player/PlayerAnimationController.cs
index 29a37e14..aefafe9b 100644
--- a/Assets/Scripts/Core/Player/PlayerAnimationController.cs
+++ b/Assets/Scripts/Core/Player/PlayerAnimationController.cs
@@ -1,6 +1,6 @@
 using UnityEngine;
 
-public class PlayerAnimationController{
+public class PlayerAnimationController : AnimationController{
     // Consts
     private const string IDLE_TRIGGER = "idle_param"; 
     private const string WALK_TRIGGER = "walk_param"; 
@@ -8,21 +8,20 @@ public class PlayerAnimationController{
     
     // Attributes
     private readonly Player player;
-    private PlayerStateController playerStateController;
-    private Transform modelTransform;
-    private Animator animator;
+    private readonly PlayerStateController playerStateController;
 
     // Constructor
     public PlayerAnimationController(Player player){
         this.player = player;
-        modelTransform = player.transform.Find("Model");
-        animator = modelTransform.GetComponent<Animator>();
+        model = player.transform.Find("Model");
+        animator = model.GetComponent<Animator>();
+        meshRenderer = model.GetComponent<MeshRenderer>();
 
-        player.stateController.OnStateChangeEvent += Animate;
+        player.stateController.OnStateChangeEvent += AnimateStates;
     }
 
     // Functions
-    public void Animate(){
+    public void AnimateStates(){
         switch (player.stateController.state){
             case PlayerState.IDLE:
                 animator.SetBool(IDLE_TRIGGER, true);
diff --git a/Assets/Scripts/Library/BaseClasses/EntityStateController.meta b/Assets/Scripts/Library/BaseClasses/Controller.meta
similarity index 100%
rename from Assets/Scripts/Library/BaseClasses/EntityStateController.meta
rename to Assets/Scripts/Library/BaseClasses/Controller.meta
diff --git a/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs b/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs
new file mode 100644
index 00000000..78afaaf4
--- /dev/null
+++ b/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs
@@ -0,0 +1,8 @@
+using UnityEngine;
+
+public abstract class AnimationController {
+    // Attributes
+    public Transform model;
+    public MeshRenderer meshRenderer;
+    public Animator animator;
+}
\ No newline at end of file
diff --git a/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs.meta b/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs.meta
new file mode 100644
index 00000000..42116e56
--- /dev/null
+++ b/Assets/Scripts/Library/BaseClasses/Controller/AnimationController.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 43272e763af7f3e46823072c47463f14
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Scripts/Library/BaseClasses/EntityStateController/DamageableEntityStateController.cs b/Assets/Scripts/Library/BaseClasses/Controller/DamageableEntityStateController.cs
similarity index 100%
rename from Assets/Scripts/Library/BaseClasses/EntityStateController/DamageableEntityStateController.cs
rename to Assets/Scripts/Library/BaseClasses/Controller/DamageableEntityStateController.cs
diff --git a/Assets/Scripts/Library/BaseClasses/EntityStateController/DamageableEntityStateController.cs.meta b/Assets/Scripts/Library/BaseClasses/Controller/DamageableEntityStateController.cs.meta
similarity index 100%
rename from Assets/Scripts/Library/BaseClasses/EntityStateController/DamageableEntityStateController.cs.meta
rename to Assets/Scripts/Library/BaseClasses/Controller/DamageableEntityStateController.cs.meta
diff --git a/Assets/Scripts/Library/BaseClasses/EntityStateController/EntityStateController.cs b/Assets/Scripts/Library/BaseClasses/Controller/EntityStateController.cs
similarity index 100%
rename from Assets/Scripts/Library/BaseClasses/EntityStateController/EntityStateController.cs
rename to Assets/Scripts/Library/BaseClasses/Controller/EntityStateController.cs
diff --git a/Assets/Scripts/Library/BaseClasses/EntityStateController/EntityStateController.cs.meta b/Assets/Scripts/Library/BaseClasses/Controller/EntityStateController.cs.meta
similarity index 100%
rename from Assets/Scripts/Library/BaseClasses/EntityStateController/EntityStateController.cs.meta
rename to Assets/Scripts/Library/BaseClasses/Controller/EntityStateController.cs.meta
diff --git a/Assets/Scripts/Library/Interfaces/IAnimated.cs b/Assets/Scripts/Library/Interfaces/IAnimated.cs
new file mode 100644
index 00000000..57a11d7b
--- /dev/null
+++ b/Assets/Scripts/Library/Interfaces/IAnimated.cs
@@ -0,0 +1,7 @@
+using UnityEngine;
+
+public interface IAnimated{
+    Transform Model {get;}
+    MeshRenderer MeshRenderer {get;}
+    Animator Animator {get;}
+}
\ No newline at end of file
diff --git a/Assets/Scripts/Library/Interfaces/IAnimated.cs.meta b/Assets/Scripts/Library/Interfaces/IAnimated.cs.meta
new file mode 100644
index 00000000..21a8c508
--- /dev/null
+++ b/Assets/Scripts/Library/Interfaces/IAnimated.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 0bd632269ee7a1140b5d8621a3675e8a
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 
diff --git a/Assets/Scripts/Library/Util/ObjectFactory.cs b/Assets/Scripts/Library/Util/ObjectFactory.cs
index f63ec360..a16af0e4 100644
--- a/Assets/Scripts/Library/Util/ObjectFactory.cs
+++ b/Assets/Scripts/Library/Util/ObjectFactory.cs
@@ -57,11 +57,11 @@ public static class ObjectFactory{
 
         switch (type){
             case AttackObjectType.PLAYER:
-                prefabObject.layer = LayerMask.NameToLayer(GameEnvironmentConfig.LAYER_PLAYER_HITBOX);
+                prefabObject.layer = LayerMask.NameToLayer(GameEnvironmentConfig.LAYER_PLAYER_ATTACK);
                 attackObject.Damage *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].PlayerDamageMultiplier;
                 break;
             case AttackObjectType.ENEMY:
-                prefabObject.layer = LayerMask.NameToLayer(GameEnvironmentConfig.LAYER_ENEMY_HITBOX);
+                prefabObject.layer = LayerMask.NameToLayer(GameEnvironmentConfig.LAYER_ENEMY_ATTACK);
                 attackObject.Damage *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].EnemyDamageMultiplier;
                 break;
             default:
-- 
GitLab