SpineboyBeginnerModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 System.Collections;
  30. using UnityEngine;
  31. namespace Spine.Unity.Examples {
  32. [SelectionBase]
  33. public class SpineboyBeginnerModel : MonoBehaviour {
  34. #region Inspector
  35. [Header("Current State")]
  36. public SpineBeginnerBodyState state;
  37. public bool facingLeft;
  38. [Range(-1f, 1f)]
  39. public float currentSpeed;
  40. [Header("Balance")]
  41. public float shootInterval = 0.12f;
  42. #endregion
  43. float lastShootTime;
  44. public event System.Action ShootEvent; // Lets other scripts know when Spineboy is shooting. Check C# Documentation to learn more about events and delegates.
  45. public event System.Action StartAimEvent; // Lets other scripts know when Spineboy is aiming.
  46. public event System.Action StopAimEvent; // Lets other scripts know when Spineboy is no longer aiming.
  47. #region API
  48. public void TryJump () {
  49. StartCoroutine(JumpRoutine());
  50. }
  51. public void TryShoot () {
  52. float currentTime = Time.time;
  53. if (currentTime - lastShootTime > shootInterval) {
  54. lastShootTime = currentTime;
  55. if (ShootEvent != null) ShootEvent(); // Fire the "ShootEvent" event.
  56. }
  57. }
  58. public void StartAim () {
  59. if (StartAimEvent != null) StartAimEvent(); // Fire the "StartAimEvent" event.
  60. }
  61. public void StopAim () {
  62. if (StopAimEvent != null) StopAimEvent(); // Fire the "StopAimEvent" event.
  63. }
  64. public void TryMove (float speed) {
  65. currentSpeed = speed; // show the "speed" in the Inspector.
  66. if (speed != 0) {
  67. bool speedIsNegative = (speed < 0f);
  68. facingLeft = speedIsNegative; // Change facing direction whenever speed is not 0.
  69. }
  70. if (state != SpineBeginnerBodyState.Jumping) {
  71. state = (speed == 0) ? SpineBeginnerBodyState.Idle : SpineBeginnerBodyState.Running;
  72. }
  73. }
  74. #endregion
  75. IEnumerator JumpRoutine () {
  76. if (state == SpineBeginnerBodyState.Jumping) yield break; // Don't jump when already jumping.
  77. state = SpineBeginnerBodyState.Jumping;
  78. // Fake jumping.
  79. {
  80. var pos = transform.localPosition;
  81. const float jumpTime = 1.2f;
  82. const float half = jumpTime * 0.5f;
  83. const float jumpPower = 20f;
  84. for (float t = 0; t < half; t += Time.deltaTime) {
  85. float d = jumpPower * (half - t);
  86. transform.Translate((d * Time.deltaTime) * Vector3.up);
  87. yield return null;
  88. }
  89. for (float t = 0; t < half; t += Time.deltaTime) {
  90. float d = jumpPower * t;
  91. transform.Translate((d * Time.deltaTime) * Vector3.down);
  92. yield return null;
  93. }
  94. transform.localPosition = pos;
  95. }
  96. state = SpineBeginnerBodyState.Idle;
  97. }
  98. }
  99. public enum SpineBeginnerBodyState {
  100. Idle,
  101. Running,
  102. Jumping
  103. }
  104. }