RewardBase.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DataTools;
  5. using WebTools.Currencies.Behaviours;
  6. namespace WebTools.Reward
  7. {
  8. public enum RewardType
  9. {
  10. Dollar,
  11. Motivation,
  12. Glam,
  13. Experience
  14. }
  15. public class RewardBase : MonoBehaviour
  16. {
  17. [Header("DATAUSER")]
  18. [SerializeField]
  19. private DataUser dataUser;
  20. [SerializeField]
  21. private Rigidbody2D mRigidBody;
  22. [SerializeField]
  23. private SpriteRenderer sprite;
  24. [SerializeField]
  25. private List<int> spriteChangeValues;
  26. [SerializeField]
  27. private List<Sprite> glam;
  28. [SerializeField]
  29. protected int glamValue = 1;
  30. [SerializeField]
  31. private List<Sprite> dollar;
  32. [SerializeField]
  33. protected int dollarValue = 2;
  34. [SerializeField]
  35. private List<Sprite> motivation;
  36. [SerializeField]
  37. protected int motivationValue = 2;
  38. [SerializeField]
  39. private Sprite experience;
  40. [SerializeField]
  41. protected int experienceValue = 2;
  42. [SerializeField]
  43. private float launchPower = 1;
  44. [SerializeField]
  45. private float lifeSpan = 3;
  46. [SerializeField]
  47. private RewardText rewardText;
  48. [SerializeField]
  49. private AudioSource touchSound;
  50. [SerializeField]
  51. private GameObject explosionPrefab;
  52. private DataTools.Currency currency;
  53. private bool isRewardClaimed;
  54. private Vector3 scale;
  55. private RewardType rewardType;
  56. private int rewardValue;
  57. private int GetSpriteIndexByValue(int value)
  58. {
  59. if(value <= spriteChangeValues[0])
  60. return 0;
  61. else if(value <= spriteChangeValues[1])
  62. return 1;
  63. else if(value <= spriteChangeValues[2])
  64. return 2;
  65. else
  66. return 3;
  67. }
  68. protected virtual void Start()
  69. {
  70. GameObject canvas = GameObject.Find("Canvas");
  71. transform.SetParent(canvas.transform, true);
  72. transform.localScale = Vector3.one * 50.0f;
  73. scale = transform.localScale;
  74. Launch();
  75. }
  76. protected void Launch()
  77. {
  78. //mRigidBody.AddForce(new Vector2(Random.Range(-10f * launchPower, 10f * launchPower), launchPower));
  79. mRigidBody.AddForce(new Vector2(Random.Range(-3.0f * launchPower, 3.0f * launchPower), launchPower * 5.0f));
  80. }
  81. protected void SetGlam()
  82. {
  83. currency = new DataTools.Currency(glamValue, 0, 0, 0);
  84. sprite.sprite = glam[GetSpriteIndexByValue(glamValue)];
  85. rewardType = RewardType.Glam;
  86. rewardValue = glamValue;
  87. }
  88. protected void SetDollar()
  89. {
  90. currency = new DataTools.Currency(0, 0, dollarValue, 0);
  91. sprite.sprite = dollar[GetSpriteIndexByValue(dollarValue)];
  92. rewardType = RewardType.Dollar;
  93. rewardValue = dollarValue;
  94. }
  95. protected void SetMotivation()
  96. {
  97. currency = new DataTools.Currency(0, 0, 0, motivationValue);
  98. sprite.sprite = motivation[GetSpriteIndexByValue(motivationValue)];
  99. rewardType = RewardType.Motivation;
  100. rewardValue = motivationValue;
  101. }
  102. protected void SetExperience()
  103. {
  104. currency = new DataTools.Currency(0, experienceValue, 0, 0);
  105. sprite.sprite = experience;
  106. rewardType = RewardType.Experience;
  107. rewardValue = experienceValue;
  108. }
  109. protected virtual void Update()
  110. {
  111. if (!isRewardClaimed)
  112. {
  113. lifeSpan -= Time.deltaTime;
  114. if (lifeSpan <= 0)
  115. GetReward();
  116. }
  117. }
  118. private void GetReward()
  119. {
  120. if (!isRewardClaimed)
  121. {
  122. dataUser.UserData.CurrencyData.AddReward(currency.Glam, currency.Experience, currency.Dollars, currency.Motivation);
  123. dataUser.UpdateUserCurrencyData();
  124. PanelCurrenciesBehaviour.Instance.UpdateTimer();
  125. }
  126. isRewardClaimed = true;
  127. GameObject explosion = Instantiate(explosionPrefab, transform.position, transform.rotation);
  128. rewardText.Show(rewardValue, rewardType);
  129. touchSound.Play();
  130. Destroy(explosion,0.5f);
  131. Destroy(gameObject);
  132. }
  133. private void OnMouseUp()
  134. {
  135. GetReward();
  136. }
  137. }
  138. }