WeaponsCharacterBrain.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using Animancer.FSM;
  4. using Animancer.Units;
  5. using System;
  6. using UnityEngine;
  7. namespace Animancer.Samples.StateMachines
  8. {
  9. /// <summary>Uses player input to control a <see cref="Character"/>.</summary>
  10. ///
  11. /// <remarks>
  12. /// <strong>Sample:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/samples/fsm/weapons">
  14. /// Weapons</see>
  15. /// </remarks>
  16. ///
  17. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.StateMachines/WeaponsCharacterBrain
  18. ///
  19. [AddComponentMenu(Strings.SamplesMenuPrefix + "Weapons - Weapons Character Brain")]
  20. [AnimancerHelpUrl(typeof(WeaponsCharacterBrain))]
  21. public class WeaponsCharacterBrain : MonoBehaviour
  22. {
  23. /************************************************************************************************************************/
  24. [SerializeField] private Character _Character;
  25. [SerializeField] private CharacterState _Move;
  26. [SerializeField] private CharacterState _Attack;
  27. [SerializeField, Seconds] private float _InputTimeOut = 0.5f;
  28. [SerializeField] private EquipState _Equip;
  29. [SerializeField] private Weapon[] _Weapons;
  30. private StateMachine<CharacterState>.InputBuffer _InputBuffer;
  31. /************************************************************************************************************************/
  32. protected virtual void Awake()
  33. {
  34. _InputBuffer = new StateMachine<CharacterState>.InputBuffer(_Character.StateMachine);
  35. }
  36. /************************************************************************************************************************/
  37. protected virtual void Update()
  38. {
  39. UpdateMovement();
  40. UpdateEquip();
  41. UpdateAction();
  42. _InputBuffer.Update();
  43. }
  44. /************************************************************************************************************************/
  45. private void UpdateMovement()// This method is identical to the one in MovingCharacterBrain.
  46. {
  47. Vector2 input = SampleInput.WASD;
  48. if (input != Vector2.zero)
  49. {
  50. // Convert the input to 3D in the XZ plane.
  51. Vector3 movementDirection = new Vector3(input.x, 0, input.y);
  52. // Apply the camera's rotation and set the parameter.
  53. Transform camera = Camera.main.transform;
  54. movementDirection = camera.TransformDirection(movementDirection);
  55. _Character.Parameters.MovementDirection = movementDirection;
  56. // Enter the locomotion state if we aren't already in it.
  57. _Character.StateMachine.TrySetState(_Move);
  58. }
  59. else
  60. {
  61. _Character.Parameters.MovementDirection = Vector3.zero;
  62. _Character.StateMachine.TrySetDefaultState();
  63. }
  64. // Indicate whether the character wants to run or not.
  65. _Character.Parameters.WantsToRun = SampleInput.LeftShiftHold;
  66. }
  67. /************************************************************************************************************************/
  68. private void UpdateEquip()
  69. {
  70. if (SampleInput.RightMouseDown)
  71. {
  72. int equippedWeaponIndex = Array.IndexOf(_Weapons, _Character.Equipment.Weapon);
  73. equippedWeaponIndex++;
  74. if (equippedWeaponIndex >= _Weapons.Length)
  75. equippedWeaponIndex = 0;
  76. _Equip.NextWeapon = _Weapons[equippedWeaponIndex];
  77. _InputBuffer.Buffer(_Equip, _InputTimeOut);
  78. }
  79. }
  80. /************************************************************************************************************************/
  81. private void UpdateAction()
  82. {
  83. if (SampleInput.LeftMouseDown)
  84. {
  85. _InputBuffer.Buffer(_Attack, _InputTimeOut);
  86. }
  87. }
  88. /************************************************************************************************************************/
  89. }
  90. }