SpineboyBeginnerView.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using System.Collections;
  3. using Spine.Unity;
  4. public class SpineboyBeginnerView : MonoBehaviour {
  5. #region Inspector
  6. [Header("Components")]
  7. public SpineboyBeginnerModel model;
  8. public SkeletonAnimation skeletonAnimation;
  9. //public ParticleSystem gunParticles;
  10. [SpineAnimation] public string run, idle, shoot, jump;
  11. [SpineEvent] public string footstepEventName;
  12. [Header("Audio")]
  13. public float footstepPitchOffset = 0.2f;
  14. public float gunsoundPitchOffset = 0.13f;
  15. public AudioSource footstepSource, gunSource, jumpSource;
  16. [Header("Effects")]
  17. public ParticleSystem gunParticles;
  18. #endregion
  19. SpineBeginnerBodyState previousViewState;
  20. void Start () {
  21. if (skeletonAnimation == null) return;
  22. model.ShootEvent += PlayShoot;
  23. skeletonAnimation.state.Event += HandleEvent;
  24. }
  25. void HandleEvent (Spine.AnimationState state, int trackIndex, Spine.Event e) {
  26. if (e.Data.Name == footstepEventName) {
  27. PlayFootstepSound();
  28. }
  29. }
  30. void Update () {
  31. if (skeletonAnimation == null) return;
  32. if (model == null) return;
  33. if (skeletonAnimation.skeleton.FlipX != model.facingLeft) { // Detect changes in model.facingLeft
  34. Turn(model.facingLeft);
  35. }
  36. // Detect changes in model.state
  37. var currentModelState = model.state;
  38. if (previousViewState != currentModelState) {
  39. PlayNewStableAnimation();
  40. }
  41. previousViewState = currentModelState;
  42. }
  43. void PlayNewStableAnimation () {
  44. var newModelState = model.state;
  45. string nextAnimation;
  46. // Add conditionals to not interrupt transient animations.
  47. if (previousViewState == SpineBeginnerBodyState.Jumping && newModelState != SpineBeginnerBodyState.Jumping) {
  48. PlayFootstepSound();
  49. }
  50. if (newModelState == SpineBeginnerBodyState.Jumping) {
  51. jumpSource.Play();
  52. nextAnimation = jump;
  53. } else {
  54. if (newModelState == SpineBeginnerBodyState.Running) {
  55. nextAnimation = run;
  56. } else {
  57. nextAnimation = idle;
  58. }
  59. }
  60. skeletonAnimation.state.SetAnimation(0, nextAnimation, true);
  61. }
  62. void PlayFootstepSound () {
  63. footstepSource.Play();
  64. footstepSource.pitch = GetRandomPitch(footstepPitchOffset);
  65. }
  66. #region Transient Actions
  67. public void PlayShoot () {
  68. // Play the shoot animation on track 1.
  69. skeletonAnimation.state.SetAnimation(1, shoot, false);
  70. gunSource.pitch = GetRandomPitch(gunsoundPitchOffset);
  71. gunSource.Play();
  72. gunParticles.Play();
  73. }
  74. public void Turn (bool facingLeft) {
  75. skeletonAnimation.skeleton.FlipX = facingLeft;
  76. // Maybe play a transient turning animation too, then call ChangeStableAnimation.
  77. }
  78. #endregion
  79. #region Utility
  80. public float GetRandomPitch (float maxPitchOffset) {
  81. return 1f + Random.Range(-maxPitchOffset, maxPitchOffset);
  82. }
  83. #endregion
  84. }