From 6d00cf7cf327ed64a6949ac580263fbe0e80919d Mon Sep 17 00:00:00 2001 From: MuhamadAjiW <16521119@mahasiswa.itb.ac.id> Date: Fri, 19 Apr 2024 08:30:30 +0700 Subject: [PATCH] feat: improved object managers --- Assets/Scripts/Core/Game/Data/GameControls.cs | 1 + .../Core/Game/Managers/EntityManager.cs | 3 +- .../Core/Game/Managers/EnvironmentManager.cs | 3 +- .../Core/Game/Managers/ObjectManager.cs | 3 +- .../Core/Objects/Collectibles/Collectible.cs | 2 +- Assets/Scripts/Core/Player/Player.cs | 6 ++-- .../Core/Player/PlayerAttackController.cs | 19 ------------ .../Core/Player/PlayerInputController.cs | 31 +++++++++++++++++++ ....cs.meta => PlayerInputController.cs.meta} | 0 .../Core/Player/PlayerMovementController.cs | 6 ++-- .../Library/BaseClasses/BaseObjectManager.cs | 26 ++++++++++++++++ .../BaseClasses/BaseObjectManager.cs.meta | 11 +++++++ .../BaseClasses/EntityObject/AttackEntity.cs | 2 +- .../BaseClasses/EntityObject/AttackObject.cs | 2 +- .../EntityObject/DamageableObject.cs | 2 +- .../BaseClasses/EntityObject/RigidEntity.cs | 2 +- .../BaseClasses/EntityObject/WorldObject.cs | 11 +++++++ .../EntityObject/WorldObject.cs.meta | 11 +++++++ 18 files changed, 108 insertions(+), 33 deletions(-) delete mode 100644 Assets/Scripts/Core/Player/PlayerAttackController.cs create mode 100644 Assets/Scripts/Core/Player/PlayerInputController.cs rename Assets/Scripts/Core/Player/{PlayerAttackController.cs.meta => PlayerInputController.cs.meta} (100%) create mode 100644 Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs create mode 100644 Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs.meta create mode 100644 Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs create mode 100644 Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs.meta diff --git a/Assets/Scripts/Core/Game/Data/GameControls.cs b/Assets/Scripts/Core/Game/Data/GameControls.cs index acd8630a..3dd7e06a 100644 --- a/Assets/Scripts/Core/Game/Data/GameControls.cs +++ b/Assets/Scripts/Core/Game/Data/GameControls.cs @@ -7,6 +7,7 @@ public class GameControls : MonoBehaviour { // Attributes public KeyCode backButton = KeyCode.Escape; public KeyCode attackButton = KeyCode.Z; + public KeyCode interactButton = KeyCode.X; // Constructor protected void Awake(){ diff --git a/Assets/Scripts/Core/Game/Managers/EntityManager.cs b/Assets/Scripts/Core/Game/Managers/EntityManager.cs index aec186b7..0bf304d6 100644 --- a/Assets/Scripts/Core/Game/Managers/EntityManager.cs +++ b/Assets/Scripts/Core/Game/Managers/EntityManager.cs @@ -2,12 +2,13 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class EntityManager : MonoBehaviour{ +public class EntityManager : BaseObjectManager{ // Static instance public static EntityManager instance; // Constructor protected void Awake(){ instance = this; + ManagerName = "Entity Manager"; } } diff --git a/Assets/Scripts/Core/Game/Managers/EnvironmentManager.cs b/Assets/Scripts/Core/Game/Managers/EnvironmentManager.cs index 1d7c8260..7f962396 100644 --- a/Assets/Scripts/Core/Game/Managers/EnvironmentManager.cs +++ b/Assets/Scripts/Core/Game/Managers/EnvironmentManager.cs @@ -2,12 +2,13 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class EnvironmentManager : MonoBehaviour{ +public class EnvironmentManager : BaseObjectManager{ // Static instance public static EnvironmentManager instance; // Constructor protected void Awake(){ instance = this; + ManagerName = "Environment Manager"; } } diff --git a/Assets/Scripts/Core/Game/Managers/ObjectManager.cs b/Assets/Scripts/Core/Game/Managers/ObjectManager.cs index 38a3f90a..0882dd12 100644 --- a/Assets/Scripts/Core/Game/Managers/ObjectManager.cs +++ b/Assets/Scripts/Core/Game/Managers/ObjectManager.cs @@ -1,11 +1,12 @@ using UnityEngine; -public class ObjectManager : MonoBehaviour{ +public class ObjectManager : BaseObjectManager{ // Static Instance public static ObjectManager instance; // Constructor protected void Awake(){ instance = this; + ManagerName = "Object Manager"; } } \ No newline at end of file diff --git a/Assets/Scripts/Core/Objects/Collectibles/Collectible.cs b/Assets/Scripts/Core/Objects/Collectibles/Collectible.cs index 6985de0e..3c59fe01 100644 --- a/Assets/Scripts/Core/Objects/Collectibles/Collectible.cs +++ b/Assets/Scripts/Core/Objects/Collectibles/Collectible.cs @@ -2,7 +2,7 @@ using System; using System.Collections; using UnityEngine; -public abstract class Collectible : MonoBehaviour { +public abstract class Collectible : WorldObject { // Attributes [SerializeField] float TimeToLive; private event Action OnCollectEvent; diff --git a/Assets/Scripts/Core/Player/Player.cs b/Assets/Scripts/Core/Player/Player.cs index 8f40213f..dc6bcb1f 100644 --- a/Assets/Scripts/Core/Player/Player.cs +++ b/Assets/Scripts/Core/Player/Player.cs @@ -7,7 +7,7 @@ public class Player : AccompaniableCombatant { // Attributes private PlayerAnimationController animationController; private PlayerMovementController movementController; - private PlayerAttackController attackController; + public PlayerInputController inputController; public PlayerStateController stateController; public PlayerStats stats; @@ -23,7 +23,7 @@ public class Player : AccompaniableCombatant { stateController = new PlayerStateController(this); movementController = new PlayerMovementController(this); animationController = new PlayerAnimationController(this); - attackController = new PlayerAttackController(this); + inputController = new PlayerInputController(this); stats = new PlayerStats(this); GameController.instance.player = this; @@ -33,7 +33,7 @@ public class Player : AccompaniableCombatant { new void Update(){ base.Update(); - attackController.HandleInputs(); + inputController.HandleInputs(); } new void FixedUpdate(){ diff --git a/Assets/Scripts/Core/Player/PlayerAttackController.cs b/Assets/Scripts/Core/Player/PlayerAttackController.cs deleted file mode 100644 index 157a93d1..00000000 --- a/Assets/Scripts/Core/Player/PlayerAttackController.cs +++ /dev/null @@ -1,19 +0,0 @@ -using UnityEngine; - -public class PlayerAttackController{ - private readonly Player player; - - public PlayerAttackController(Player player){ - this.player = player; - } - - public void HandleInputs(){ - if(Input.GetKeyDown(GameControls.instance.attackButton)){ - Debug.Log("Player is attacking"); - - if(player.Weapon == null) return; - - player.Weapon.Attack(); - } - } -} \ No newline at end of file diff --git a/Assets/Scripts/Core/Player/PlayerInputController.cs b/Assets/Scripts/Core/Player/PlayerInputController.cs new file mode 100644 index 00000000..09084365 --- /dev/null +++ b/Assets/Scripts/Core/Player/PlayerInputController.cs @@ -0,0 +1,31 @@ +using UnityEngine; + +public class PlayerInputController{ + private readonly Player player; + + public float movementInputX; + public float movementInputZ; + + public PlayerInputController(Player player){ + this.player = player; + } + + public void HandleInputs(){ + movementInputX = Input.GetAxisRaw("Horizontal"); + movementInputZ = Input.GetAxisRaw("Vertical"); + + if(Input.GetKeyDown(GameControls.instance.attackButton)){ + Debug.Log("Player is attacking"); + + if(player.Weapon == null) return; + + player.Weapon.Attack(); + } + else if(Input.GetKeyDown(GameControls.instance.interactButton)){ + Debug.Log("Player is interacting"); + + ObjectManager.instance.LogObjects(); + EntityManager.instance.LogObjects(); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Core/Player/PlayerAttackController.cs.meta b/Assets/Scripts/Core/Player/PlayerInputController.cs.meta similarity index 100% rename from Assets/Scripts/Core/Player/PlayerAttackController.cs.meta rename to Assets/Scripts/Core/Player/PlayerInputController.cs.meta diff --git a/Assets/Scripts/Core/Player/PlayerMovementController.cs b/Assets/Scripts/Core/Player/PlayerMovementController.cs index 9cd5490f..58681cee 100644 --- a/Assets/Scripts/Core/Player/PlayerMovementController.cs +++ b/Assets/Scripts/Core/Player/PlayerMovementController.cs @@ -19,12 +19,12 @@ public class PlayerMovementController{ } public void HandleMovement(){ - float keyPressX = Input.GetAxisRaw("Horizontal"); - float keyPressZ = Input.GetAxisRaw("Vertical"); + float inputX = player.inputController.movementInputX; + float inputZ = player.inputController.movementInputZ; Vector3 velocity = new(player.Rigidbody.velocity.x, player.Rigidbody.velocity.y, player.Rigidbody.velocity.z); Vector3 dampVelocity = Vector3.zero; - Vector3 inputVector = new(keyPressX, 0, keyPressZ); + Vector3 inputVector = new(inputX, 0, inputZ); Vector3 modifierVector = inputVector.normalized * player.stats.MaxSpeed; velocity.x = modifierVector.x; velocity.z = modifierVector.z; diff --git a/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs b/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs new file mode 100644 index 00000000..5c1854d7 --- /dev/null +++ b/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using UnityEngine; + +public class BaseObjectManager : MonoBehaviour{ + protected string ManagerName = "Manager"; + // Functions + public virtual void LogObjects(){ + WorldObject[] worldObjects = GetComponentsInChildren<WorldObject>(); + string idArray = "["; + for (int i = 0; i < worldObjects.Length; i++){ + idArray += worldObjects[i].Id; + if(i != worldObjects.Length - 1) idArray += ","; + } + idArray += "]"; + + Debug.Log(string.Format("Object ids in {0}: {1}", ManagerName, idArray)); + } + + public virtual WorldObject GetWorldObject(string id){ + WorldObject[] worldObjects = GetComponentsInChildren<WorldObject>(); + for (int i = 0; i < worldObjects.Length; i++){ + if(worldObjects[i].Id == id) return worldObjects[i]; + } + return null; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs.meta b/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs.meta new file mode 100644 index 00000000..99f2c182 --- /dev/null +++ b/Assets/Scripts/Library/BaseClasses/BaseObjectManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e021d77bd3c097341bf8774207a6ab50 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/AttackEntity.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/AttackEntity.cs index 7cdde843..5b77ced0 100644 --- a/Assets/Scripts/Library/BaseClasses/EntityObject/AttackEntity.cs +++ b/Assets/Scripts/Library/BaseClasses/EntityObject/AttackEntity.cs @@ -1,7 +1,7 @@ using System; using UnityEngine; -public class AttackEntity : RigidEntity, IAttack{ +public class AttackEntity : WorldObject, IAttack{ // Attributes [SerializeField] private float damage; [SerializeField] private float knockbackPower; diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/AttackObject.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/AttackObject.cs index 41c5ea2c..940b3447 100644 --- a/Assets/Scripts/Library/BaseClasses/EntityObject/AttackObject.cs +++ b/Assets/Scripts/Library/BaseClasses/EntityObject/AttackObject.cs @@ -1,7 +1,7 @@ using System; using UnityEngine; -public class AttackObject : MonoBehaviour, IAttack{ +public class AttackObject : WorldObject, IAttack{ // Attributes [SerializeField] private float damage; [SerializeField] private float knockbackPower; diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/DamageableObject.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/DamageableObject.cs index 84356f9e..927c4e8c 100644 --- a/Assets/Scripts/Library/BaseClasses/EntityObject/DamageableObject.cs +++ b/Assets/Scripts/Library/BaseClasses/EntityObject/DamageableObject.cs @@ -1,7 +1,7 @@ using System; using UnityEngine; -public class DamageableObject : MonoBehaviour, IDamageable{ +public class DamageableObject : WorldObject, 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/RigidEntity.cs index 2b27abc8..105e3b70 100644 --- a/Assets/Scripts/Library/BaseClasses/EntityObject/RigidEntity.cs +++ b/Assets/Scripts/Library/BaseClasses/EntityObject/RigidEntity.cs @@ -1,6 +1,6 @@ using UnityEngine; -public class RigidEntity : MonoBehaviour, IRigid { +public class RigidEntity : WorldObject, IRigid { // Attributes [SerializeField] private float knockbackResistance; [SerializeField] private float baseSpeed; diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs new file mode 100644 index 00000000..cae45231 --- /dev/null +++ b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs @@ -0,0 +1,11 @@ +using UnityEngine; + +public class WorldObject : MonoBehaviour{ + private static int autoIncrement = 0; + public string Id; + + protected void Awake(){ + Id = autoIncrement.ToString(); + autoIncrement++; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs.meta b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs.meta new file mode 100644 index 00000000..f4ebe400 --- /dev/null +++ b/Assets/Scripts/Library/BaseClasses/EntityObject/WorldObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fcd557e81d5a5f145874252a42723dcc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- GitLab