Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6.  
  7. public class Shop : MonoBehaviour
  8. {
  9. public enum State
  10. {
  11. Lock,
  12. Sale,
  13. Bought
  14. }
  15.  
  16. public CarInfo Target
  17. {
  18. get
  19. {
  20. return m_Target;
  21. }
  22.  
  23. set
  24. {
  25. if(m_Target) Destroy(m_Target.gameObject);
  26. if(!value) return;
  27.  
  28. State state = (State) PlayerPrefsExt.IntValue[value.Id];
  29. //
  30. // if(state == State.Lock)
  31. // {
  32. // m_Target = GameObject.Instantiate(value.LockedCar, transform.position, transform.rotation) as CarInfo;
  33. // m_Target.Id = value.Id;
  34. //
  35. // Price.text = "LOCK";
  36. //
  37. // OnLockedCar.Invoke();
  38. //
  39. // return;
  40. // }
  41.  
  42. //OnUnlockedCar.Invoke();
  43.  
  44. m_Target = GameObject.Instantiate(value, transform.position, transform.rotation) as CarInfo;
  45. m_Target.Id = value.Id;
  46.  
  47. var controller = m_Target.GetComponent<RCCCarControllerV2>();
  48.  
  49. if(controller) controller.enabled = false;
  50.  
  51. if(Info) Info.Target = m_Target;
  52.  
  53. if(state == State.Bought)
  54. OnBoughtCar.Invoke();
  55. else
  56. OnSaleCar.Invoke();
  57. }
  58. }
  59. private CarInfo m_Target;
  60.  
  61. public CarList CarsList;
  62.  
  63. [Header("UI")]
  64. public Text Price;
  65.  
  66. [Header("Events")]
  67. public UnityEvent OnBoughtCar;
  68. public UnityEvent OnSaleCar;
  69. public UnityEvent OnNoMoney;
  70. // public UnityEvent OnUnlockedCar;
  71. // public UnityEvent OnLockedCar;
  72.  
  73. [Header("Other")]
  74. public CarInfoVisualizer Info;
  75. //public Garage GarageScript;
  76.  
  77. void Start()
  78. {
  79. //PlayerPrefs.DeleteAll();
  80. if(!Info) Info = GameObject.FindObjectOfType<CarInfoVisualizer>();
  81. if(!CarsList) CarsList = GameObject.FindObjectOfType<CarList>();
  82. if(!IsBought(CarsList[0])) PlayerPrefsExt.IntValue[CarsList[0].Id] = 2;
  83. Next();
  84. }
  85.  
  86. public void Next()
  87. {
  88. var index = -1;
  89.  
  90. if(Target)
  91. index = GetCurrentIndex();
  92.  
  93. index++;
  94.  
  95. if(index == CarsList.Cars.Count) index = 0;
  96.  
  97. Target = CarsList.Cars[index];
  98. }
  99.  
  100. public void Prev()
  101. {
  102. var index = -1;
  103.  
  104. if(Target)
  105. index = GetCurrentIndex();
  106.  
  107. index--;
  108. if(index < 0) index = CarsList.Cars.Count - 1;
  109.  
  110. Target = CarsList.Cars[index];
  111. }
  112.  
  113. public void Reload()
  114. {
  115. if(Target) Target = CarsList.Cars[GetCurrentIndex()];
  116. }
  117.  
  118. public void Buy()
  119. {
  120. Buy (Target.Id);
  121. }
  122.  
  123. public void Buy(CarInfo car)
  124. {
  125. Buy (car.Id);
  126. }
  127.  
  128. public void Buy(string carId)
  129. {
  130. if(Money.Remove(CarsList[carId].Price))
  131. {
  132. PlayerPrefsExt.IntValue[carId] = 2;
  133. Reload();
  134. }
  135. else
  136. {
  137. OnNoMoney.Invoke();
  138. }
  139. }
  140.  
  141. public void Sell()
  142. {
  143. Sell (Target.Id);
  144. }
  145.  
  146. public void Sell(string id)
  147. {
  148. // var car = CarsList[id];
  149. //
  150. // Money.Add(car.Price / 2);
  151. // PlayerPrefsExt.IntValue[car.Id] = 1;
  152. // Reload();
  153. // GarageScript.Remove(id);
  154. }
  155.  
  156. public void Unlock()
  157. {
  158. if(UnlockOperation())
  159. {
  160. PlayerPrefsExt.IntValue[Target.Id] = 1;
  161. Reload();
  162. }
  163. }
  164.  
  165. public void Select()
  166. {
  167. PlayerPrefsExt.StringValue["Current"] = Target.Id;
  168. }
  169.  
  170. public int GetCurrentIndex()
  171. {
  172. return CarsList.Cars.FindIndex(a => a.Id == Target.Id);
  173. }
  174.  
  175. public void Clear()
  176. {
  177. Target = null;
  178. }
  179.  
  180. public bool IsBought(CarInfo car)
  181. {
  182. return IsBought(car.Id);
  183. }
  184.  
  185. public bool IsBought(string carId)
  186. {
  187. return PlayerPrefsExt.IntValue[carId, 0] == 2;
  188. }
  189.  
  190. protected bool UnlockOperation()
  191. {
  192. return true;
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement