From c616d95f233c730bebb8d97903e16f6738ef95eb Mon Sep 17 00:00:00 2001 From: MuhamadAjiW <16521119@mahasiswa.itb.ac.id> Date: Sun, 28 Apr 2024 11:31:43 +0700 Subject: [PATCH] feat: audio controller --- .../Characters/Basic1/Basic1Model.prefab | 11 ++--- .../Characters/Basic2/Basic2Model.prefab | 11 ++--- .../Characters/Basic3/Basic3Model.prefab | 9 ++-- .../Models/Characters/Jean/JeanModel.prefab | 8 ++-- Assets/_Scripts/Core/Player/Player.cs | 16 ++++++++ .../Core/Player/PlayerAnimationController.cs | 8 +++- .../Core/Player/PlayerAudioController.cs | 10 +++++ .../Core/Player/PlayerAudioController.cs.meta | 2 + .../Core/Player/PlayerInputController.cs | 1 + .../Core/Player/PlayerMovementController.cs | 14 ++++--- .../Core/Player/PlayerStateController.cs | 4 +- Assets/_Scripts/Core/Player/PlayerStats.cs | 3 +- Assets/_Scripts/Library/BaseClasses/Audio.cs | 13 ++++++ .../Library/BaseClasses/Audio.cs.meta | 2 + .../Library/BaseClasses/CharacterModel.cs | 14 +++++++ .../BaseClasses/CharacterModel.cs.meta | 2 + .../BaseClasses/Controller/AudioController.cs | 41 +++++++++++++++++++ .../Controller/AudioController.cs.meta | 2 + .../BaseClasses/EntityObject/WorldEntity.cs | 6 +-- Assets/_Scripts/Library/BaseClasses/Model.cs | 6 --- 20 files changed, 146 insertions(+), 37 deletions(-) create mode 100644 Assets/_Scripts/Core/Player/PlayerAudioController.cs create mode 100644 Assets/_Scripts/Core/Player/PlayerAudioController.cs.meta create mode 100644 Assets/_Scripts/Library/BaseClasses/Audio.cs create mode 100644 Assets/_Scripts/Library/BaseClasses/Audio.cs.meta create mode 100644 Assets/_Scripts/Library/BaseClasses/CharacterModel.cs create mode 100644 Assets/_Scripts/Library/BaseClasses/CharacterModel.cs.meta create mode 100644 Assets/_Scripts/Library/BaseClasses/Controller/AudioController.cs create mode 100644 Assets/_Scripts/Library/BaseClasses/Controller/AudioController.cs.meta diff --git a/Assets/_Resources/Models/Characters/Basic1/Basic1Model.prefab b/Assets/_Resources/Models/Characters/Basic1/Basic1Model.prefab index 794cab26..5e7abccb 100644 --- a/Assets/_Resources/Models/Characters/Basic1/Basic1Model.prefab +++ b/Assets/_Resources/Models/Characters/Basic1/Basic1Model.prefab @@ -13,7 +13,7 @@ GameObject: - component: {fileID: 9221680813695291731} - component: {fileID: 211686610887331952} - component: {fileID: 7854868560450781420} - - component: {fileID: 4321336250984689561} + - component: {fileID: 1345125961690847755} m_Layer: 6 m_Name: Basic1Model m_TagString: Player @@ -131,7 +131,7 @@ BoxCollider: serializedVersion: 3 m_Size: {x: 1, y: 1, z: 1} m_Center: {x: 0, y: 0, z: 0} ---- !u!114 &4321336250984689561 +--- !u!114 &1345125961690847755 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -140,11 +140,12 @@ MonoBehaviour: m_GameObject: {fileID: 125248941348886635} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: dc4d5359067cdfb47a45684d6cc1482a, type: 3} + m_Script: {fileID: 11500000, guid: 26f6a876496a9a44f96ef4d53bab5144, type: 3} m_Name: m_EditorClassIdentifier: + dynamicBottomPoint: {fileID: 0} meleeAnimationDelay: 0 rangedAnimationDelay: 0 - staticWeaponPivot: {x: 0.2, y: 0, z: 0.375} - dynamicBottomPoint: {fileID: 0} + skillAnimationDelay: 0 + staticWeaponPivot: {x: 0, y: 0, z: 0} dynamicWeaponPivot: {fileID: 0} diff --git a/Assets/_Resources/Models/Characters/Basic2/Basic2Model.prefab b/Assets/_Resources/Models/Characters/Basic2/Basic2Model.prefab index 35b8d80e..6d86d87d 100644 --- a/Assets/_Resources/Models/Characters/Basic2/Basic2Model.prefab +++ b/Assets/_Resources/Models/Characters/Basic2/Basic2Model.prefab @@ -13,7 +13,7 @@ GameObject: - component: {fileID: 4666754847935405941} - component: {fileID: 7320871978689485787} - component: {fileID: 4205797128800968765} - - component: {fileID: 5114590188839600129} + - component: {fileID: 4843744555028261649} m_Layer: 7 m_Name: Basic2Model m_TagString: Untagged @@ -131,7 +131,7 @@ BoxCollider: serializedVersion: 3 m_Size: {x: 1, y: 1, z: 1} m_Center: {x: 0.000000059604645, y: 0, z: -0.000000059604645} ---- !u!114 &5114590188839600129 +--- !u!114 &4843744555028261649 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -140,11 +140,12 @@ MonoBehaviour: m_GameObject: {fileID: 3596266059315004415} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: dc4d5359067cdfb47a45684d6cc1482a, type: 3} + m_Script: {fileID: 11500000, guid: 26f6a876496a9a44f96ef4d53bab5144, type: 3} m_Name: m_EditorClassIdentifier: + dynamicBottomPoint: {fileID: 0} meleeAnimationDelay: 0 rangedAnimationDelay: 0 - staticWeaponPivot: {x: 0.5, y: 0, z: 0.375} - dynamicBottomPoint: {fileID: 0} + skillAnimationDelay: 0 + staticWeaponPivot: {x: 0, y: 0, z: 0} dynamicWeaponPivot: {fileID: 0} diff --git a/Assets/_Resources/Models/Characters/Basic3/Basic3Model.prefab b/Assets/_Resources/Models/Characters/Basic3/Basic3Model.prefab index c6a2165f..5b05cac8 100644 --- a/Assets/_Resources/Models/Characters/Basic3/Basic3Model.prefab +++ b/Assets/_Resources/Models/Characters/Basic3/Basic3Model.prefab @@ -12,7 +12,7 @@ GameObject: - component: {fileID: 2785673166861171252} - component: {fileID: 230920367868259381} - component: {fileID: 1523314302102385116} - - component: {fileID: 138729632845352590} + - component: {fileID: 8314328193744417061} m_Layer: 0 m_Name: Basic3Model m_TagString: Untagged @@ -108,7 +108,7 @@ BoxCollider: serializedVersion: 3 m_Size: {x: 1, y: 2.0000005, z: 1.0000002} m_Center: {x: 0, y: 0, z: 0} ---- !u!114 &138729632845352590 +--- !u!114 &8314328193744417061 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -117,11 +117,12 @@ MonoBehaviour: m_GameObject: {fileID: 3802837052780452132} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: dc4d5359067cdfb47a45684d6cc1482a, type: 3} + m_Script: {fileID: 11500000, guid: 26f6a876496a9a44f96ef4d53bab5144, type: 3} m_Name: m_EditorClassIdentifier: + dynamicBottomPoint: {fileID: 0} meleeAnimationDelay: 0 rangedAnimationDelay: 0 + skillAnimationDelay: 0 staticWeaponPivot: {x: 0, y: 0, z: 0} - dynamicBottomPoint: {fileID: 0} dynamicWeaponPivot: {fileID: 0} diff --git a/Assets/_Resources/Models/Characters/Jean/JeanModel.prefab b/Assets/_Resources/Models/Characters/Jean/JeanModel.prefab index a068bcf8..181e0c23 100644 --- a/Assets/_Resources/Models/Characters/Jean/JeanModel.prefab +++ b/Assets/_Resources/Models/Characters/Jean/JeanModel.prefab @@ -12703,7 +12703,7 @@ GameObject: - component: {fileID: 7453883038108435637} - component: {fileID: 2422882661156513751} - component: {fileID: 5961469541952119665} - - component: {fileID: 8709419276929931250} + - component: {fileID: 6642347362383756050} - component: {fileID: 1174415839910585320} m_Layer: 0 m_Name: JeanModel @@ -13311,7 +13311,7 @@ BoxCollider: serializedVersion: 3 m_Size: {x: 0.3, y: 0.2, z: 0.2202658} m_Center: {x: -0.000014201666, y: 0.1, z: 0.006592878} ---- !u!114 &8709419276929931250 +--- !u!114 &6642347362383756050 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -13320,14 +13320,14 @@ MonoBehaviour: m_GameObject: {fileID: 6497556749472931103} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: dc4d5359067cdfb47a45684d6cc1482a, type: 3} + m_Script: {fileID: 11500000, guid: 26f6a876496a9a44f96ef4d53bab5144, type: 3} m_Name: m_EditorClassIdentifier: + dynamicBottomPoint: {fileID: 3738926971660565114} meleeAnimationDelay: 0 rangedAnimationDelay: 0.1 skillAnimationDelay: 0.8 staticWeaponPivot: {x: 0, y: 0, z: 0} - dynamicBottomPoint: {fileID: 3738926971660565114} dynamicWeaponPivot: {fileID: 324142494282531500} --- !u!95 &1174415839910585320 Animator: diff --git a/Assets/_Scripts/Core/Player/Player.cs b/Assets/_Scripts/Core/Player/Player.cs index 500cc6ee..2bdfd538 100644 --- a/Assets/_Scripts/Core/Player/Player.cs +++ b/Assets/_Scripts/Core/Player/Player.cs @@ -13,6 +13,7 @@ public class Player : PlayerEntity public PlayerAnimationController animationController; public PlayerInputController inputController; public PlayerStateController stateController; + public PlayerAudioController audioController; public PlayerStats stats; // Constructor @@ -26,6 +27,7 @@ public class Player : PlayerEntity inputController = new PlayerInputController(this); movementController = new PlayerMovementController(this); animationController = new PlayerAnimationController(this); + audioController = new PlayerAudioController(this); SetLayer(EnvironmentConfig.LAYER_PLAYER); SetAttackLayer(EnvironmentConfig.LAYER_PLAYER_ATTACK); GameController.Instance.player = this; @@ -107,4 +109,18 @@ public class Player : PlayerEntity interactable.InvokeOnInteractAreaExitEvent(); stateController.currentInteractables.Remove(interactable); } + + + protected void OnDrawGizmosSelected() + { + // Visualize stair detection + Gizmos.color = Color.red; + + if(model != null) + { + Vector3 location = model.Bottom; + location.y += movementController.stairMaxHeight / 2 + movementController.stairDetectionBottomOffset; + Gizmos.DrawWireCube(location, new(movementController.stairDetectionDistance, movementController.stairMaxHeight, movementController.stairDetectionDistance)); + } + } } diff --git a/Assets/_Scripts/Core/Player/PlayerAnimationController.cs b/Assets/_Scripts/Core/Player/PlayerAnimationController.cs index 804d46a7..f5ec3e42 100644 --- a/Assets/_Scripts/Core/Player/PlayerAnimationController.cs +++ b/Assets/_Scripts/Core/Player/PlayerAnimationController.cs @@ -1,5 +1,7 @@ +using System; using UnityEngine; +[Serializable] public class PlayerAnimationController : AnimationController { // Consts @@ -50,6 +52,9 @@ public class PlayerAnimationController : AnimationController case PlayerState.SPRINTING: animator.SetInteger(MOVEMENT_PARAM, PlayerState.SPRINTING); break; + case PlayerState.JUMPING: + animator.SetInteger(MOVEMENT_PARAM, PlayerState.JUMPING); + break; } } @@ -79,7 +84,7 @@ public class PlayerAnimationController : AnimationController private int GetMovementState(int state) { - return state & (PlayerState.IDLE | PlayerState.WALKING | PlayerState.SPRINTING); + return state & (PlayerState.IDLE | PlayerState.WALKING | PlayerState.SPRINTING | PlayerState.JUMPING); } private void ClearJumping() @@ -95,5 +100,6 @@ public class PlayerAnimationController : AnimationController private void OnDeath() { animator.SetBool(DEAD_Trigger, true); + animator.SetInteger(MOVEMENT_PARAM, PlayerState.DEAD); } } \ No newline at end of file diff --git a/Assets/_Scripts/Core/Player/PlayerAudioController.cs b/Assets/_Scripts/Core/Player/PlayerAudioController.cs new file mode 100644 index 00000000..409532d2 --- /dev/null +++ b/Assets/_Scripts/Core/Player/PlayerAudioController.cs @@ -0,0 +1,10 @@ +using System; + +[Serializable] +public class PlayerAudioController : AudioController +{ + // Constructor + public PlayerAudioController(Player player) : base(player.gameObject) + { + } +} \ No newline at end of file diff --git a/Assets/_Scripts/Core/Player/PlayerAudioController.cs.meta b/Assets/_Scripts/Core/Player/PlayerAudioController.cs.meta new file mode 100644 index 00000000..f063bddc --- /dev/null +++ b/Assets/_Scripts/Core/Player/PlayerAudioController.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 8fe9f0dac9bd7344192c96215ba38ae9 \ No newline at end of file diff --git a/Assets/_Scripts/Core/Player/PlayerInputController.cs b/Assets/_Scripts/Core/Player/PlayerInputController.cs index b556d2e6..9c34e354 100644 --- a/Assets/_Scripts/Core/Player/PlayerInputController.cs +++ b/Assets/_Scripts/Core/Player/PlayerInputController.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using UnityEngine; +[Serializable] public class PlayerInputController { // Attributes diff --git a/Assets/_Scripts/Core/Player/PlayerMovementController.cs b/Assets/_Scripts/Core/Player/PlayerMovementController.cs index 5c24cec8..b86333d7 100644 --- a/Assets/_Scripts/Core/Player/PlayerMovementController.cs +++ b/Assets/_Scripts/Core/Player/PlayerMovementController.cs @@ -1,7 +1,7 @@ using System; -using Unity.VisualScripting; using UnityEngine; +[Serializable] public class PlayerMovementController { // Attributes @@ -9,7 +9,9 @@ public class PlayerMovementController private Vector3 axisX; private Vector3 axisZ; private bool aim = false; - private readonly float maxStairHeight = 0.5f; + public readonly float stairMaxHeight = 0.3f; + public readonly float stairDetectionDistance = 0.25f; + public readonly float stairDetectionBottomOffset = -0.07f; // Constructor public PlayerMovementController(Player player) @@ -75,13 +77,13 @@ public class PlayerMovementController private void HandleStairs(Vector3 movementVector) { - float raycastHeight = -0.07f; + float raycastHeight = stairDetectionBottomOffset; bool stairFront = false; - while (raycastHeight < maxStairHeight) + while (raycastHeight < stairMaxHeight) { Vector3 stairDetectorRear = player.model.Bottom + (Vector3.up * raycastHeight); - Vector3 stairDetectorFront = player.model.Bottom + (movementVector * 0.25f) + (Vector3.up * raycastHeight); + Vector3 stairDetectorFront = player.model.Bottom + (movementVector * stairDetectionDistance) + (Vector3.up * raycastHeight); bool hit = Physics.Linecast(stairDetectorRear, stairDetectorFront, 1); if(hit) @@ -100,7 +102,7 @@ public class PlayerMovementController } // TODO: Could use some smoothing - if(stairFront && raycastHeight < maxStairHeight) + if(stairFront && raycastHeight < stairMaxHeight) { Debug.Log($"Height: {raycastHeight}"); { diff --git a/Assets/_Scripts/Core/Player/PlayerStateController.cs b/Assets/_Scripts/Core/Player/PlayerStateController.cs index bae8d09d..86143074 100644 --- a/Assets/_Scripts/Core/Player/PlayerStateController.cs +++ b/Assets/_Scripts/Core/Player/PlayerStateController.cs @@ -1,14 +1,14 @@ -using System.Collections; +using System; using System.Collections.Generic; using UnityEngine; +[Serializable] public class PlayerStateController : EntityStateController { // Attributes private readonly Player player; public List<IInteractable> currentInteractables = new(); public WeaponState weaponState = WeaponState.IDLE; - public bool inAttackWindow = false; // Contstructor public PlayerStateController(Player player) diff --git a/Assets/_Scripts/Core/Player/PlayerStats.cs b/Assets/_Scripts/Core/Player/PlayerStats.cs index e1d5abe4..b7499f27 100644 --- a/Assets/_Scripts/Core/Player/PlayerStats.cs +++ b/Assets/_Scripts/Core/Player/PlayerStats.cs @@ -1,5 +1,6 @@ -using UnityEngine; +using System; +[Serializable] public class PlayerStats { // Attributes diff --git a/Assets/_Scripts/Library/BaseClasses/Audio.cs b/Assets/_Scripts/Library/BaseClasses/Audio.cs new file mode 100644 index 00000000..b30fc9ec --- /dev/null +++ b/Assets/_Scripts/Library/BaseClasses/Audio.cs @@ -0,0 +1,13 @@ +using System; +using UnityEngine; + +[Serializable] +public class Audio +{ + // Attributes + [HideInInspector] public AudioSource source; + public string name; + public AudioClip clip; + public float volume; + public float pitch; +} \ No newline at end of file diff --git a/Assets/_Scripts/Library/BaseClasses/Audio.cs.meta b/Assets/_Scripts/Library/BaseClasses/Audio.cs.meta new file mode 100644 index 00000000..54251217 --- /dev/null +++ b/Assets/_Scripts/Library/BaseClasses/Audio.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b3e77b19919716d418dc1c2899cda0bb \ No newline at end of file diff --git a/Assets/_Scripts/Library/BaseClasses/CharacterModel.cs b/Assets/_Scripts/Library/BaseClasses/CharacterModel.cs new file mode 100644 index 00000000..0a66f7ea --- /dev/null +++ b/Assets/_Scripts/Library/BaseClasses/CharacterModel.cs @@ -0,0 +1,14 @@ +using UnityEngine; + +public class CharacterModel : Model +{ + // Attributes + public float meleeAnimationDelay; + public float rangedAnimationDelay; + public float skillAnimationDelay; + public Vector3 staticWeaponPivot; + public Transform dynamicWeaponPivot; + + // Set-Getters + public Vector3 WeaponPivot => dynamicWeaponPivot == null ? staticWeaponPivot : dynamicWeaponPivot.position; +} \ No newline at end of file diff --git a/Assets/_Scripts/Library/BaseClasses/CharacterModel.cs.meta b/Assets/_Scripts/Library/BaseClasses/CharacterModel.cs.meta new file mode 100644 index 00000000..0529e57f --- /dev/null +++ b/Assets/_Scripts/Library/BaseClasses/CharacterModel.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 26f6a876496a9a44f96ef4d53bab5144 \ No newline at end of file diff --git a/Assets/_Scripts/Library/BaseClasses/Controller/AudioController.cs b/Assets/_Scripts/Library/BaseClasses/Controller/AudioController.cs new file mode 100644 index 00000000..828611b8 --- /dev/null +++ b/Assets/_Scripts/Library/BaseClasses/Controller/AudioController.cs @@ -0,0 +1,41 @@ +using System; +using UnityEngine; + +[Serializable] +public class AudioController +{ + // Attributes + public Audio[] audios; + + // Constructors + public AudioController(GameObject gameObject) + { + if(audios == null) + { + audios = new Audio[0]; + } + + foreach (Audio audio in audios) + { + audio.source = gameObject.AddComponent<AudioSource>(); + audio.source.clip = audio.clip; + + audio.source.volume = audio.volume; + audio.source.pitch = audio.pitch; + } + } + + // Functions + public void Play (string name) + { + Audio audio = Array.Find(audios, audio => audio.name == name); + + if(audio == null) + { + Debug.LogWarning($"Audio not found: {name}"); + return; + } + + audio.source.Play(); + } +} \ No newline at end of file diff --git a/Assets/_Scripts/Library/BaseClasses/Controller/AudioController.cs.meta b/Assets/_Scripts/Library/BaseClasses/Controller/AudioController.cs.meta new file mode 100644 index 00000000..209f42d1 --- /dev/null +++ b/Assets/_Scripts/Library/BaseClasses/Controller/AudioController.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: f982fb1fac9ca6540bd35b869fd5dc27 \ No newline at end of file diff --git a/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs b/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs index 9577631e..37674c7e 100644 --- a/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs +++ b/Assets/_Scripts/Library/BaseClasses/EntityObject/WorldEntity.cs @@ -4,7 +4,7 @@ using UnityEngine; public class WorldEntity : WorldObject, IRigid { // Attributes - [NonSerialized] public Model model; + [NonSerialized] public CharacterModel model; [SerializeField] private float knockbackResistance; [SerializeField] private float baseSpeed; [SerializeField] private float jumpForce; @@ -41,7 +41,7 @@ public class WorldEntity : WorldObject, IRigid { base.Start(); rigidbody = GetComponent<Rigidbody>(); - model = GetComponentInChildren<Model>(); + model = GetComponentInChildren<CharacterModel>(); #if STRICT if(rigidbody == null) @@ -50,7 +50,7 @@ public class WorldEntity : WorldObject, IRigid } if(model == null) { - Debug.LogError($"Rigid entity {name} does not have a model. How to resolve: Create a gameObject with a model.cs script as its child"); + Debug.LogError($"Rigid entity {name} does not have a CharacterModel. How to resolve: Create a gameObject with a CharacterModel.cs script as its child"); } #endif diff --git a/Assets/_Scripts/Library/BaseClasses/Model.cs b/Assets/_Scripts/Library/BaseClasses/Model.cs index 74f7ade3..5e0cbcaa 100644 --- a/Assets/_Scripts/Library/BaseClasses/Model.cs +++ b/Assets/_Scripts/Library/BaseClasses/Model.cs @@ -4,17 +4,11 @@ using UnityEngine; public class Model : MonoBehaviour { // Attributes - public float meleeAnimationDelay; - public float rangedAnimationDelay; - public float skillAnimationDelay; - public Vector3 staticWeaponPivot; public Transform dynamicBottomPoint; - public Transform dynamicWeaponPivot; [NonSerialized] public new Renderer renderer; [NonSerialized] public float bottomOffset; // Set-getters - public Vector3 WeaponPivot => dynamicWeaponPivot == null ? staticWeaponPivot : dynamicWeaponPivot.position; public Vector3 Bottom => dynamicBottomPoint == null? new(transform.position.x, transform.position.y - bottomOffset, transform.position.z) : dynamicBottomPoint.position; // Constructors -- GitLab