SpineboyBeginnerView.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using UnityEngine;
  31. using System.Collections;
  32. using Spine.Unity;
  33. namespace Spine.Unity.Examples {
  34. public class SpineboyBeginnerView : MonoBehaviour {
  35. #region Inspector
  36. [Header("Components")]
  37. public SpineboyBeginnerModel model;
  38. public SkeletonAnimation skeletonAnimation;
  39. [SpineAnimation] public string run, idle, shoot, jump;
  40. [SpineEvent] public string footstepEventName;
  41. [Header("Audio")]
  42. public float footstepPitchOffset = 0.2f;
  43. public float gunsoundPitchOffset = 0.13f;
  44. public AudioSource footstepSource, gunSource, jumpSource;
  45. [Header("Effects")]
  46. public ParticleSystem gunParticles;
  47. #endregion
  48. SpineBeginnerBodyState previousViewState;
  49. void Start () {
  50. if (skeletonAnimation == null) return;
  51. model.ShootEvent += PlayShoot;
  52. skeletonAnimation.AnimationState.Event += HandleEvent;
  53. }
  54. void HandleEvent (Spine.TrackEntry trackEntry, Spine.Event e) {
  55. if (e.Data.Name == footstepEventName)
  56. PlayFootstepSound();
  57. }
  58. void Update () {
  59. if (skeletonAnimation == null) return;
  60. if (model == null) return;
  61. if (skeletonAnimation.skeleton.FlipX != model.facingLeft) { // Detect changes in model.facingLeft
  62. Turn(model.facingLeft);
  63. }
  64. // Detect changes in model.state
  65. var currentModelState = model.state;
  66. if (previousViewState != currentModelState) {
  67. PlayNewStableAnimation();
  68. }
  69. previousViewState = currentModelState;
  70. }
  71. void PlayNewStableAnimation () {
  72. var newModelState = model.state;
  73. string nextAnimation;
  74. // Add conditionals to not interrupt transient animations.
  75. if (previousViewState == SpineBeginnerBodyState.Jumping && newModelState != SpineBeginnerBodyState.Jumping) {
  76. PlayFootstepSound();
  77. }
  78. if (newModelState == SpineBeginnerBodyState.Jumping) {
  79. jumpSource.Play();
  80. nextAnimation = jump;
  81. } else {
  82. if (newModelState == SpineBeginnerBodyState.Running) {
  83. nextAnimation = run;
  84. } else {
  85. nextAnimation = idle;
  86. }
  87. }
  88. skeletonAnimation.AnimationState.SetAnimation(0, nextAnimation, true);
  89. }
  90. void PlayFootstepSound () {
  91. footstepSource.Play();
  92. footstepSource.pitch = GetRandomPitch(footstepPitchOffset);
  93. }
  94. [ContextMenu("Check Tracks")]
  95. void CheckTracks () {
  96. var state = skeletonAnimation.AnimationState;
  97. Debug.Log(state.GetCurrent(0));
  98. Debug.Log(state.GetCurrent(1));
  99. }
  100. #region Transient Actions
  101. public void PlayShoot () {
  102. // Play the shoot animation on track 1.
  103. var track = skeletonAnimation.AnimationState.SetAnimation(1, shoot, false);
  104. track.AttachmentThreshold = 1f;
  105. track.MixDuration = 0f;
  106. var empty = skeletonAnimation.state.AddEmptyAnimation(1, 0.5f, 0.1f);
  107. empty.AttachmentThreshold = 1f;
  108. gunSource.pitch = GetRandomPitch(gunsoundPitchOffset);
  109. gunSource.Play();
  110. //gunParticles.randomSeed = (uint)Random.Range(0, 100);
  111. gunParticles.Play();
  112. }
  113. public void Turn (bool facingLeft) {
  114. skeletonAnimation.Skeleton.FlipX = facingLeft;
  115. // Maybe play a transient turning animation too, then call ChangeStableAnimation.
  116. }
  117. #endregion
  118. #region Utility
  119. public float GetRandomPitch (float maxPitchOffset) {
  120. return 1f + Random.Range(-maxPitchOffset, maxPitchOffset);
  121. }
  122. #endregion
  123. }
  124. }