Guest User

Untitled

a guest
Apr 5th, 2016
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.EventSystems;
  5.  
  6. public class ElementControl : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler,
  7. IPointerExitHandler
  8. {
  9.  
  10. public List<GameObject> Directions = new List<GameObject>(); //to define the line's directions
  11. //for different shapes of objects
  12. public LayerMask ElementMask;
  13. [Tooltip("bigger than 1 distance, less than 2 distances, distance configged in gamemanager")]
  14. public float RayLength = 2.1f;
  15. public float WaitingSeconds = .5f;
  16.  
  17. public GameObject ShootingParticls;
  18.  
  19. // [Tooltip("Set the amount as the same as the directions, and keep the order")]
  20. public List<GameObject> DifferentNumbers = new List<GameObject>();
  21. public GameObject BlankElement;
  22. public GameObject MineElement;
  23. public Material FlagMat;
  24. public Material DefaultMat;
  25.  
  26.  
  27. private bool _isAMine = false;
  28. private bool _isABlank = false;
  29. private bool _isSweepered = false;
  30. private bool _isFlagged = false;
  31. private bool _rightClick = false;
  32.  
  33. private bool _isPointerOnTheObject;
  34.  
  35.  
  36. private int _ownNumber;
  37.  
  38. public bool IsABlank
  39. {
  40.  
  41. get { return _isABlank; }
  42.  
  43.  
  44. }
  45.  
  46.  
  47. public bool IsAMine
  48. {
  49.  
  50. get { return _isAMine; }
  51. set { _isAMine = value; }
  52.  
  53.  
  54. }
  55.  
  56. public bool IsSweepered
  57. {
  58.  
  59. get { return _isSweepered; }
  60. }
  61.  
  62. public bool IsFlagged
  63. {
  64.  
  65. get { return _isFlagged; }
  66. }
  67.  
  68. void Start()
  69. {
  70. _ownNumber = GetHowManyMinesNear();
  71. InitThisElement();
  72. _isSweepered = false;
  73. _isFlagged = false;
  74.  
  75. _isPointerOnTheObject = false;
  76.  
  77. }
  78.  
  79.  
  80. void InitThisElement()
  81. {
  82.  
  83.  
  84. if (_ownNumber == 0)
  85. {
  86. _isABlank = true;
  87.  
  88.  
  89. }
  90.  
  91.  
  92. }
  93.  
  94. void Update()
  95. {
  96. #if UNITY_ANDROID
  97. // long press
  98.  
  99.  
  100. #else
  101. //the mouse input
  102.  
  103.  
  104. if (_isPointerOnTheObject)
  105. { //pointer on the object
  106.  
  107.  
  108. if (Input.GetMouseButtonDown(1)) //right click
  109. {
  110.  
  111. StartCoroutine(PlayClickAudio());
  112.  
  113. _rightClick = true;
  114.  
  115. if (!_isFlagged)
  116. {
  117.  
  118. GetComponent<Renderer>().material = FlagMat;
  119. _isFlagged = true;
  120.  
  121. GameManager.instance.FlagOne();
  122.  
  123. if (_isAMine)
  124. {
  125.  
  126. GameManager.instance.FlagRightOne();
  127. }
  128.  
  129.  
  130.  
  131. StartCoroutine(SetRightClickFalse()); //use this corountine to avoid to cancle flag to sweeper
  132.  
  133. }
  134. else
  135. {
  136. GetComponent<Renderer>().material = DefaultMat;
  137. _isFlagged = false;
  138.  
  139. GameManager.instance.UnFlagOne();
  140.  
  141.  
  142. StartCoroutine(SetRightClickFalse());
  143.  
  144. }
  145.  
  146.  
  147.  
  148. }
  149.  
  150.  
  151.  
  152. }
  153.  
  154. #endif
  155.  
  156. }
  157.  
  158. IEnumerator SetRightClickFalse()
  159. {
  160. yield return new WaitForSeconds(WaitingSeconds);
  161.  
  162. _rightClick = false;
  163.  
  164. }
  165.  
  166.  
  167. IEnumerator PlayClickAudio()
  168. {
  169. if (!((_rightClick) || (_isFlagged)))
  170. {
  171.  
  172. //////////////////////////////////////////////////////
  173. AudioSource audio = GetComponent<AudioSource>();
  174. audio.Play(44100); //play
  175.  
  176. //Wait for Audio to finish playing
  177. while (audio.isPlaying)
  178. {
  179. yield return null; //Don't freeze
  180. }
  181.  
  182. _isSweepered = true;
  183.  
  184.  
  185.  
  186. Debug.Log("mouse pointer click on" + gameObject.name);
  187.  
  188. if (_isAMine) //keep check mine firstly
  189. {
  190.  
  191.  
  192.  
  193. //you falied, and game over
  194. Debug.Log(gameObject.name + " is a mine, you failed!");
  195. GameObject go = Instantiate(MineElement, transform.position,
  196. transform.rotation) as GameObject;
  197.  
  198. go.transform.parent = this.gameObject.transform.parent;
  199. }
  200. else if (_isABlank)
  201. {
  202. ClickOnABlank();
  203. //this is a blank element, then send line to sweeper elements near
  204.  
  205. //////////////////////////////////////////////////////
  206. audio.Play(44100); //play
  207.  
  208. //Wait for Audio to finish playing
  209. while (audio.isPlaying)
  210. {
  211. yield return null; //Don't freeze
  212. }
  213.  
  214. }
  215. else
  216. { //this is an element with mines near
  217.  
  218.  
  219.  
  220. SweeperThisElement();
  221. //////////////////////////////////////////////////////
  222. audio.Play(44100); //play
  223.  
  224. //Wait for Audio to finish playing
  225. while (audio.isPlaying)
  226. {
  227. yield return null; //Don't freeze
  228. }
  229.  
  230. }
  231. }
  232.  
  233. //Now Destroy GameObject after audio has finished
  234. Destroy(this.gameObject);
  235.  
  236. }
  237.  
  238. void SweeperToSendLine(Vector3 direction)
  239. {
  240. //wait
  241.  
  242.  
  243. // cast a ray along the directions
  244. //to dig the elements near
  245.  
  246. //particle system
  247.  
  248.  
  249.  
  250.  
  251. //below is use the ray
  252.  
  253. Ray ray = new Ray(transform.position, direction);
  254. RaycastHit hit;
  255.  
  256.  
  257. if (Physics.Raycast(ray, out hit, RayLength, ElementMask))
  258. {
  259. GameObject hitObject = hit.collider.gameObject;
  260. ElementControl hitObjectElement = hitObject.GetComponent<ElementControl>();
  261.  
  262.  
  263. if ((hitObjectElement != null)
  264. && (!(hitObjectElement.IsAMine))
  265. && (!hitObjectElement.IsSweepered)
  266. && (!hitObjectElement.IsFlagged))
  267. {
  268.  
  269.  
  270.  
  271. // not a mine and not be sweepered
  272.  
  273. // sweeper the elements near
  274. if (hitObjectElement.IsABlank)
  275. {
  276.  
  277. hitObjectElement.ClickOnABlank();
  278.  
  279.  
  280. }
  281. else
  282. {
  283. hitObjectElement.SweeperThisElement();
  284.  
  285.  
  286. }
  287.  
  288. }
  289.  
  290. }
  291.  
  292.  
  293.  
  294.  
  295. }
  296.  
  297. void SweeperToSendParticle(GameObject direction)
  298. {
  299.  
  300.  
  301. Vector3 startPosition = transform.position +
  302. (direction.transform.position - transform.position) * transform.localScale.x / 2;
  303. Quaternion startRotation = transform.rotation;
  304.  
  305. GameObject ps = Instantiate
  306. (ShootingParticls, startPosition,
  307.  
  308.  
  309.  
  310. startRotation
  311. )
  312. as GameObject;
  313.  
  314. ps.transform.LookAt(direction.transform.position);
  315.  
  316. ps.transform.parent = transform.parent.transform;
  317.  
  318. }
  319.  
  320.  
  321. int GetHowManyMinesNear()
  322. { // to calculate how many mines near this element
  323. int ownNumber = 0;
  324.  
  325. foreach (GameObject direction in Directions)
  326. {
  327.  
  328.  
  329. Ray ray = new Ray(transform.position, direction.transform.position - transform.position);
  330. RaycastHit hit;
  331.  
  332.  
  333. if (Physics.Raycast(ray, out hit, RayLength, ElementMask))
  334. {
  335.  
  336.  
  337. if (hit.collider.gameObject.GetComponent<ElementControl>().IsAMine)
  338. {
  339. ownNumber++;
  340. }
  341.  
  342. }
  343. }
  344. return ownNumber;
  345.  
  346. }
  347.  
  348. #region public
  349.  
  350. public void SweeperThisElement()
  351. {
  352. Debug.Log(gameObject.name + "has " + _ownNumber + " mines near");
  353. //set the number texture
  354.  
  355.  
  356. GameObject go = Instantiate(DifferentNumbers[_ownNumber - 1], transform.position,
  357. transform.rotation * Quaternion.AngleAxis(90f, Vector3.left)) //rotation for the texture
  358. as GameObject;
  359.  
  360. go.transform.parent = this.gameObject.transform.parent;
  361. }
  362.  
  363. public void ClickOnABlank()
  364. {
  365.  
  366. Debug.Log(gameObject.name + "is a blank element");
  367.  
  368.  
  369. GameObject go = Instantiate(BlankElement, transform.position,
  370. this.transform.rotation) as GameObject;
  371.  
  372. go.transform.parent = this.gameObject.transform.parent;
  373.  
  374.  
  375. //to sweeper the near
  376. for (int i = 0; i < Directions.Count; i++)
  377. {
  378. //SweeperToSendLine(Directions [i]);
  379.  
  380. SweeperToSendParticle(Directions[i]);
  381.  
  382. }
  383.  
  384. }
  385.  
  386.  
  387.  
  388. public void OnPointerClick(PointerEventData eventData)
  389. { // add a if for whether it is a flag
  390. StartCoroutine(PlayClickAudio());
  391. }
  392.  
  393. public void OnPointerEnter(PointerEventData eventData)
  394. {
  395. //Debug.Log("mouse pointer enter on" + gameObject.name);
  396.  
  397.  
  398. _isPointerOnTheObject = true;
  399.  
  400. }
  401.  
  402.  
  403. public void OnPointerExit(PointerEventData eventData)
  404. {
  405. _isPointerOnTheObject = false;
  406.  
  407.  
  408.  
  409. }
  410.  
  411. #endregion
  412. }
Advertisement
Add Comment
Please, Sign In to add comment