SpineboyBeginnerView.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using Spine.Unity;
  30. using System.Collections;
  31. using UnityEngine;
  32. namespace Spine.Unity.Examples {
  33. public class SpineboyBeginnerView : MonoBehaviour {
  34. #region Inspector
  35. [Header("Components")]
  36. public SpineboyBeginnerModel model;
  37. public SkeletonAnimation skeletonAnimation;
  38. public AnimationReferenceAsset run, idle, aim, shoot, jump;
  39. public EventDataReferenceAsset footstepEvent;
  40. [Header("Audio")]
  41. public float footstepPitchOffset = 0.2f;
  42. public float gunsoundPitchOffset = 0.13f;
  43. public AudioSource footstepSource, gunSource, jumpSource;
  44. [Header("Effects")]
  45. public ParticleSystem gunParticles;
  46. #endregion
  47. SpineBeginnerBodyState previousViewState;
  48. void Start () {
  49. if (skeletonAnimation == null) return;
  50. model.ShootEvent += PlayShoot;
  51. model.StartAimEvent += StartPlayingAim;
  52. model.StopAimEvent += StopPlayingAim;
  53. skeletonAnimation.AnimationState.Event += HandleEvent;
  54. }
  55. void HandleEvent (Spine.TrackEntry trackEntry, Spine.Event e) {
  56. if (e.Data == footstepEvent.EventData)
  57. PlayFootstepSound();
  58. }
  59. void Update () {
  60. if (skeletonAnimation == null) return;
  61. if (model == null) return;
  62. if ((skeletonAnimation.skeleton.ScaleX < 0) != model.facingLeft) { // Detect changes in model.facingLeft
  63. Turn(model.facingLeft);
  64. }
  65. // Detect changes in model.state
  66. var currentModelState = model.state;
  67. if (previousViewState != currentModelState) {
  68. PlayNewStableAnimation();
  69. }
  70. previousViewState = currentModelState;
  71. }
  72. void PlayNewStableAnimation () {
  73. var newModelState = model.state;
  74. Animation nextAnimation;
  75. // Add conditionals to not interrupt transient animations.
  76. if (previousViewState == SpineBeginnerBodyState.Jumping && newModelState != SpineBeginnerBodyState.Jumping) {
  77. PlayFootstepSound();
  78. }
  79. if (newModelState == SpineBeginnerBodyState.Jumping) {
  80. jumpSource.Play();
  81. nextAnimation = jump;
  82. } else {
  83. if (newModelState == SpineBeginnerBodyState.Running) {
  84. nextAnimation = run;
  85. } else {
  86. nextAnimation = idle;
  87. }
  88. }
  89. skeletonAnimation.AnimationState.SetAnimation(0, nextAnimation, true);
  90. }
  91. void PlayFootstepSound () {
  92. footstepSource.Play();
  93. footstepSource.pitch = GetRandomPitch(footstepPitchOffset);
  94. }
  95. [ContextMenu("Check Tracks")]
  96. void CheckTracks () {
  97. var state = skeletonAnimation.AnimationState;
  98. Debug.Log(state.GetCurrent(0));
  99. Debug.Log(state.GetCurrent(1));
  100. }
  101. #region Transient Actions
  102. public void PlayShoot () {
  103. // Play the shoot animation on track 1.
  104. var shootTrack = skeletonAnimation.AnimationState.SetAnimation(1, shoot, false);
  105. shootTrack.AttachmentThreshold = 1f;
  106. shootTrack.MixDuration = 0f;
  107. skeletonAnimation.state.AddEmptyAnimation(1, 0.5f, 0.1f);
  108. // Play the aim animation on track 2 to aim at the mouse target.
  109. var aimTrack = skeletonAnimation.AnimationState.SetAnimation(2, aim, false);
  110. aimTrack.AttachmentThreshold = 1f;
  111. aimTrack.MixDuration = 0f;
  112. skeletonAnimation.state.AddEmptyAnimation(2, 0.5f, 0.1f);
  113. gunSource.pitch = GetRandomPitch(gunsoundPitchOffset);
  114. gunSource.Play();
  115. //gunParticles.randomSeed = (uint)Random.Range(0, 100);
  116. gunParticles.Play();
  117. }
  118. public void StartPlayingAim () {
  119. // Play the aim animation on track 2 to aim at the mouse target.
  120. var aimTrack = skeletonAnimation.AnimationState.SetAnimation(2, aim, true);
  121. aimTrack.AttachmentThreshold = 1f;
  122. aimTrack.MixDuration = 0f;
  123. }
  124. public void StopPlayingAim () {
  125. skeletonAnimation.state.AddEmptyAnimation(2, 0.5f, 0.1f);
  126. }
  127. public void Turn (bool facingLeft) {
  128. skeletonAnimation.Skeleton.ScaleX = facingLeft ? -1f : 1f;
  129. // Maybe play a transient turning animation too, then call ChangeStableAnimation.
  130. }
  131. #endregion
  132. #region Utility
  133. public float GetRandomPitch (float maxPitchOffset) {
  134. return 1f + Random.Range(-maxPitchOffset, maxPitchOffset);
  135. }
  136. #endregion
  137. }
  138. }