From 99928e612b19969d0d2f1c62d0f99f0b512847c1 Mon Sep 17 00:00:00 2001
From: MuhamadAjiW <16521119@mahasiswa.itb.ac.id>
Date: Fri, 19 Apr 2024 08:59:51 +0700
Subject: [PATCH] feat: world object prefixes, object removal within manager

---
 .../Scripts/Core/Entities/Mobs/Dummy/Dummy.cs |  4 ++++
 .../TestCollectible/TestCollectible.cs        | 10 +++++++++
 Assets/Scripts/Core/Player/Player.cs          |  4 ++++
 .../Library/BaseClasses/BaseObjectManager.cs  |  9 ++++++++
 .../EntityObject/DamageableEntity.cs          |  2 +-
 .../{RigidEntity.cs => WorldEntity.cs}        |  2 +-
 ...igidEntity.cs.meta => WorldEntity.cs.meta} |  0
 .../BaseClasses/EntityObject/WorldObject.cs   | 16 ++++++++++++--
 Assets/Scripts/Library/Util/ObjectFactory.cs  | 22 +++++++++++++++----
 9 files changed, 61 insertions(+), 8 deletions(-)
 rename Assets/Scripts/Library/BaseClasses/EntityObject/{RigidEntity.cs => WorldEntity.cs} (96%)
 rename Assets/Scripts/Library/BaseClasses/EntityObject/{RigidEntity.cs.meta => WorldEntity.cs.meta} (100%)

diff --git a/Assets/Scripts/Core/Entities/Mobs/Dummy/Dummy.cs b/Assets/Scripts/Core/Entities/Mobs/Dummy/Dummy.cs
index bc52d4d9..b0db12c7 100644
--- a/Assets/Scripts/Core/Entities/Mobs/Dummy/Dummy.cs
+++ b/Assets/Scripts/Core/Entities/Mobs/Dummy/Dummy.cs
@@ -1,6 +1,9 @@
 using UnityEngine;
 
 public class Dummy : EnemyEntity{
+    // Static attributes
+    public static string ObjectIdPrefix = "Dummy";
+
     // Attributes
     private DummyAnimationController animationController;
     public DummyStateController stateController;
@@ -8,6 +11,7 @@ public class Dummy : EnemyEntity{
     // Constructor
     new protected void Start(){
         base.Start();
+        SetIdPrefix(ObjectIdPrefix);
         stateController = new DummyStateController(this);
         animationController = new DummyAnimationController(this);
     }
diff --git a/Assets/Scripts/Core/Objects/Collectibles/TestCollectible/TestCollectible.cs b/Assets/Scripts/Core/Objects/Collectibles/TestCollectible/TestCollectible.cs
index 4fa1dcf9..86ae86d3 100644
--- a/Assets/Scripts/Core/Objects/Collectibles/TestCollectible/TestCollectible.cs
+++ b/Assets/Scripts/Core/Objects/Collectibles/TestCollectible/TestCollectible.cs
@@ -1,6 +1,16 @@
 using UnityEngine;
 
 public class TestCollectible : Collectible{
+    // Static attributes
+    public static string ObjectIdPrefix = "Player";
+
+    // Constructor
+    protected new void Start(){
+        base.Start();
+        SetIdPrefix(ObjectIdPrefix);
+    }
+
+    // Functions
     protected override void OnCollect(){
         Debug.Log("Test collectible collected");
     }
diff --git a/Assets/Scripts/Core/Player/Player.cs b/Assets/Scripts/Core/Player/Player.cs
index dc6bcb1f..24b9f694 100644
--- a/Assets/Scripts/Core/Player/Player.cs
+++ b/Assets/Scripts/Core/Player/Player.cs
@@ -4,6 +4,9 @@ using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : AccompaniableCombatant {
+    // Static attributes
+    public static string ObjectIdPrefix = "Player"; 
+
     // Attributes
     private PlayerAnimationController animationController;
     private PlayerMovementController movementController;
@@ -14,6 +17,7 @@ public class Player : AccompaniableCombatant {
     // Constructor
     new void Start(){
         base.Start();
+        SetIdPrefix(ObjectIdPrefix);
         Health *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].PlayerHealthMultiplier;
         
         // TODO: Review, base damage is currently done in the ObjectFactory. Might need to decide which is best
diff --git a/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs b/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs
index 5c1854d7..b495b2ea 100644
--- a/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs
+++ b/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs
@@ -16,6 +16,10 @@ public class BaseObjectManager : MonoBehaviour{
         Debug.Log(string.Format("Object ids in {0}: {1}", ManagerName, idArray));
     }
 
+    public virtual WorldObject[] GetWorldObjects(){
+        return GetComponentsInChildren<WorldObject>();
+    }
+
     public virtual WorldObject GetWorldObject(string id){
         WorldObject[] worldObjects = GetComponentsInChildren<WorldObject>();
         for (int i = 0; i < worldObjects.Length; i++){
@@ -23,4 +27,9 @@ public class BaseObjectManager : MonoBehaviour{
         }
         return null;
     }
+
+    public virtual void RemoveWorldObject(string id){
+        WorldObject worldObject = GetWorldObject(id);
+        Destroy(worldObject.gameObject);
+    }
 }
\ No newline at end of file
diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/DamageableEntity.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/DamageableEntity.cs
index 128b94fa..544e3516 100644
--- a/Assets/Scripts/Library/BaseClasses/EntityObject/DamageableEntity.cs
+++ b/Assets/Scripts/Library/BaseClasses/EntityObject/DamageableEntity.cs
@@ -1,7 +1,7 @@
 using System;
 using UnityEngine;
 
-public class DamageableEntity : RigidEntity, IDamageable{
+public class DamageableEntity : WorldEntity, IDamageable{
     // Attributes
     private bool damageable = true;
     [SerializeField] private float maxHealth;
diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/RigidEntity.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
similarity index 96%
rename from Assets/Scripts/Library/BaseClasses/EntityObject/RigidEntity.cs
rename to Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
index 105e3b70..9e5369b4 100644
--- a/Assets/Scripts/Library/BaseClasses/EntityObject/RigidEntity.cs
+++ b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs
@@ -1,6 +1,6 @@
 using UnityEngine;
 
-public class RigidEntity : WorldObject, IRigid {
+public class WorldEntity : WorldObject, IRigid {
     // Attributes
     [SerializeField] private float knockbackResistance;
     [SerializeField] private float baseSpeed;
diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/RigidEntity.cs.meta b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs.meta
similarity index 100%
rename from Assets/Scripts/Library/BaseClasses/EntityObject/RigidEntity.cs.meta
rename to Assets/Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs.meta
diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs
index cae45231..7495451c 100644
--- a/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs
+++ b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs
@@ -1,11 +1,23 @@
 using UnityEngine;
 
 public class WorldObject : MonoBehaviour{
+    // Attributes
     private static int autoIncrement = 0;
+    private int NumberId; 
+    private string Prefix;
     public string Id;
 
+    // Constructor
     protected void Awake(){
-        Id = autoIncrement.ToString();
+        NumberId = autoIncrement;
+        Id = NumberId.ToString();
         autoIncrement++;
     }
-}
\ No newline at end of file
+
+    // Functions
+    protected void SetIdPrefix(string prefix){
+        Prefix = prefix;
+        if(Prefix == "") Id = NumberId.ToString();
+        else Id = Prefix + "_" + Id;
+    }
+}
diff --git a/Assets/Scripts/Library/Util/ObjectFactory.cs b/Assets/Scripts/Library/Util/ObjectFactory.cs
index e0d8a44c..bbe48929 100644
--- a/Assets/Scripts/Library/Util/ObjectFactory.cs
+++ b/Assets/Scripts/Library/Util/ObjectFactory.cs
@@ -12,7 +12,7 @@ public static class ObjectFactory{
         int renderingOrder = 0,
         string objectName = "Unnamed Object"
     ){
-        GameObject returnObject = parent == null? GameObject.Instantiate(gameObject) : GameObject.Instantiate(gameObject, parent);
+        GameObject returnObject = parent == null? GameObject.Instantiate(gameObject, ObjectManager.instance.transform) : GameObject.Instantiate(gameObject, parent);
         if(position != null) returnObject.transform.position = position.Value;
         if(rotation != null) returnObject.transform.rotation = rotation.Value;
         if(scale != null) returnObject.transform.localScale = Vector3.Scale(returnObject.transform.localScale, scale.Value);
@@ -73,7 +73,7 @@ public static class ObjectFactory{
         return prefabObject;
     }
 
-    public static GameObject CreateCollectibleObject(
+    public static Collectible CreateCollectibleObject(
         string prefabPath,
         Transform parent = null,
         Vector3? position = null,
@@ -83,10 +83,24 @@ public static class ObjectFactory{
         string objectName = "Unnamed Object"
     ){
         GameObject prefabObject = CreateObject(prefabPath, parent == null? ObjectManager.instance.transform : parent, position, scale, rotation, renderingOrder, objectName);
-        if(!prefabObject.TryGetComponent<Collectible>(out var collectibleObject)) Debug.LogError("Loaded prefab is not a Collectible: " + prefabPath);
+        if(!prefabObject.TryGetComponent<Collectible>(out var collectible)) Debug.LogError("Loaded prefab is not a Collectible: " + prefabPath);
         prefabObject.layer = LayerMask.NameToLayer(GameEnvironmentConfig.LAYER_COLLECTIBLE);
 
-        return prefabObject;
+        return collectible;
+    }
+
+    public static WorldEntity CreateEntity(
+        string prefabPath,
+        Transform parent = null,
+        Vector3? position = null,
+        Vector3? scale = null,
+        Quaternion? rotation = null,
+        int renderingOrder = 0,
+        string objectName = "Unnamed Object"
+    ){
+        GameObject prefabObject = CreateObject(prefabPath, parent == null? EntityManager.instance.transform : parent, position, scale, rotation, renderingOrder, objectName);
+        if(!prefabObject.TryGetComponent<WorldEntity>(out var entity)) Debug.LogError("Loaded prefab is not an entity: " + prefabPath);
+        return entity;
     }
 
     public static void Destroy(GameObject gameObject, float delay = 0){
-- 
GitLab