MonsterSpawner.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Spine;
  5. using Spine.Unity;
  6. using UnityEngine.UI;
  7. public class MonsterSpawner : MonoBehaviour
  8. {
  9. List<GameObject> TheMonster = new List<GameObject>(); //The current created monster
  10. List<SkeletonAnimation> monsterAnimator = new List<SkeletonAnimation>(); //The animator script of the monster
  11. public Text monsterNameText;
  12. public List<Transform> TheFourPositions = new List<Transform>();
  13. public List<GameObject> AllMonsters = new List<GameObject>(); //a list of all monsters in the asset
  14. int CurrentMonster = 0;
  15. private void Start()
  16. {
  17. CurrentMonster = 0;
  18. for (int i = 0; i < 4; i++)
  19. {
  20. TheMonster.Add(null);
  21. monsterAnimator.Add(null);
  22. }
  23. SummonNewMonster();
  24. }
  25. public void SummonNextPrevMonster(int PrevOrNext) //Summon either next or previous monster
  26. {
  27. CurrentMonster += PrevOrNext;
  28. if (CurrentMonster >= AllMonsters.Count)
  29. CurrentMonster = 0;
  30. else if (CurrentMonster < 0)
  31. CurrentMonster = AllMonsters.Count - 1;
  32. //CurrentMonster += 3;
  33. //List<int> PlusOne = new List<int>() { 3, 56, 60 };
  34. //List<int> PlusTwo = new List<int>() { 10, 30, 97 };
  35. //List<int> PlusThree = new List<int>() { 44 };
  36. //Debug.Log(CurrentMonster);
  37. //if (PlusOne.Contains(CurrentMonster))
  38. //{
  39. // CurrentMonster++;
  40. //}
  41. //else if (PlusTwo.Contains(CurrentMonster))
  42. //{
  43. // CurrentMonster += 2;
  44. //}
  45. //else if (PlusThree.Contains(CurrentMonster))
  46. //{
  47. // CurrentMonster += 3;
  48. //}
  49. SummonNewMonster();
  50. }
  51. public void SummonNewMonster()
  52. {
  53. if (TheMonster.Count > 0)
  54. {
  55. for (int i = 0; i < 4; i++)
  56. {
  57. if (TheMonster[i] != null)
  58. Destroy(TheMonster[i]);
  59. }
  60. }
  61. //Create the selected monster
  62. for (int i = 0; i < 4; i++)
  63. {
  64. TheMonster[i] = Instantiate(AllMonsters[CurrentMonster], TheFourPositions[i]);
  65. }
  66. //Special dimensions for certain monsters
  67. string MonsterName = TheMonster[0].name;
  68. float additioanlYPos = 0;
  69. float scalingFactor = 1;
  70. if (MonsterName.Contains("Salamander"))
  71. {
  72. scalingFactor = 1.35f;
  73. }
  74. else if (MonsterName.Contains("Floating"))
  75. {
  76. scalingFactor = 0.67f;
  77. additioanlYPos = 12;
  78. }
  79. else if (MonsterName.Contains("Rabbit"))
  80. {
  81. scalingFactor = 0.75f;
  82. }
  83. else if (MonsterName.Contains("Mushroom"))
  84. {
  85. scalingFactor = 0.67f;
  86. }
  87. else if (MonsterName.Contains("Book"))
  88. {
  89. scalingFactor = 0.67f;
  90. additioanlYPos = 60;
  91. }
  92. else if (MonsterName.Contains("Corrupted"))
  93. {
  94. scalingFactor = 1.3f;
  95. additioanlYPos = 40;
  96. }
  97. else if (MonsterName.Contains("Ox"))
  98. {
  99. scalingFactor = 1.25f;
  100. }
  101. else if (MonsterName.Contains("Raptor"))
  102. {
  103. scalingFactor = 1.25f;
  104. }
  105. else if (MonsterName.Contains("Orc"))
  106. {
  107. scalingFactor = 1.1f;
  108. }
  109. else if (MonsterName.Contains("Snail"))
  110. {
  111. scalingFactor = 0.9f;
  112. }
  113. else if (MonsterName.Contains("Shell"))
  114. {
  115. scalingFactor = 1.2f;
  116. }
  117. else if (MonsterName.Contains("Lizard"))
  118. {
  119. scalingFactor = 1.55f;
  120. }
  121. else if (MonsterName.Contains("Hamy"))
  122. {
  123. scalingFactor = 1.3f;
  124. }
  125. //Change position and scale of the monster
  126. for (int i = 0; i < 4; i++)
  127. {
  128. TheMonster[i].transform.localPosition = new Vector2(0, additioanlYPos);
  129. TheMonster[i].transform.localScale = new Vector2(48, 48) * scalingFactor;
  130. monsterAnimator[i] = TheMonster[i].GetComponent<SkeletonAnimation>();
  131. }
  132. //string manipulation to get the name and id of the monster
  133. int idIndexStart = MonsterName.IndexOf('_', 1);
  134. int nameIndexStart = MonsterName.IndexOf('_', 9);
  135. int nameIndexEnd = MonsterName.IndexOf('(', 9);
  136. monsterNameText.text = "Monster "+ MonsterName.Substring (idIndexStart+1,nameIndexStart-idIndexStart-1)+ " : " +MonsterName.Substring(nameIndexStart + 1, nameIndexEnd - nameIndexStart -1);
  137. ChangeAnimation("Idle");
  138. }
  139. public void ChangeAnimation(string AnimationName) //Names are: Idle, Walk, Death, Hurt and Attack
  140. {
  141. if (monsterAnimator == null)
  142. return;
  143. bool IsLoop = AnimationName == "Death" ? false : true;
  144. //set the animation state to the selected one for each directional monster
  145. monsterAnimator[0].skeleton.SetSkin("Side");
  146. monsterAnimator[0].skeleton.SetSlotsToSetupPose();
  147. monsterAnimator[0].AnimationState.SetAnimation(0, "Side_" + AnimationName, IsLoop);
  148. monsterAnimator[1].skeleton.SetSkin("Side");
  149. monsterAnimator[1].skeleton.SetSlotsToSetupPose();
  150. monsterAnimator[1].AnimationState.SetAnimation(0, "Side_" + AnimationName, IsLoop);
  151. monsterAnimator[2].skeleton.SetSkin("Back");
  152. monsterAnimator[2].skeleton.SetSlotsToSetupPose();
  153. monsterAnimator[2].AnimationState.SetAnimation(0, "Back_" + AnimationName, IsLoop);
  154. monsterAnimator[3].skeleton.SetSkin("Front");
  155. monsterAnimator[3].skeleton.SetSlotsToSetupPose();
  156. monsterAnimator[3].AnimationState.SetAnimation(0, "Front_" + AnimationName, IsLoop);
  157. }
  158. public void RateUs()
  159. {
  160. System.Diagnostics.Process.Start("https://assetstore.unity.com/packages/slug/216312#reviews");
  161. }
  162. }