/Assets/Scripts/Fight Scripts/Weapon Scripts/DragThroughScript.cs

https://bitbucket.org/kKapalka/obscurity-game · C# · 417 lines · 356 code · 27 blank · 34 comment · 82 complexity · 078d976f2810e4bd1438dbf297846b63 MD5 · raw file

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. public class DragThroughScript : Weapon {
  6. public override void setHints ()
  7. {
  8. hints = new string[] {"Select a gem to continue",
  9. "Select another gem. First one will be pushed towards it, and all along the line will move to make place."
  10. };
  11. }
  12. public override bool CheckConditions (GameObject go1, GameObject go2)
  13. {
  14. return go1!=go2 && (go1.GetComponent<Shape> ().Column == go2.GetComponent<Shape> ().Column ||
  15. go1.GetComponent<Shape> ().Row == go2.GetComponent<Shape> ().Row);
  16. }
  17. public override IEnumerable<GameObject> GetAllMatches (GameObject go1, GameObject go2)
  18. {
  19. var shapes = sm.shapes;
  20. List<GameObject> matches = new List<GameObject>();
  21. List<GameObject> matchesV = new List<GameObject>();
  22. matches.Add(go1);
  23. var shape = go1.GetComponent<Shape>();
  24. //check left
  25. if (shape.Column != 0)
  26. for (int column = shape.Column - 1; column >= 0; column--)
  27. {
  28. if (shapes[shape.Row, column].GetComponent<Shape>().IsSameType(shape))
  29. {
  30. matches.Add(shapes[shape.Row, column]);
  31. }
  32. else
  33. break;
  34. }
  35. //check right
  36. if (shape.Column != Constants.Columns - 1)
  37. for (int column = shape.Column + 1; column < Constants.Columns; column++)
  38. {
  39. if (shapes[shape.Row, column].GetComponent<Shape>().IsSameType(shape))
  40. {
  41. matches.Add(shapes[shape.Row, column]);
  42. }
  43. else
  44. break;
  45. }
  46. //if sides dont have sufficient matches, clear and restart
  47. if (matches.Count < getMinimumMatches())
  48. matches.Clear();
  49. matchesV.Add(go1);
  50. //check bottom
  51. if (shape.Row != 0)
  52. for (int row = shape.Row - 1; row >= 0; row--)
  53. {
  54. if (shapes[row, shape.Column] != null &&
  55. shapes[row, shape.Column].GetComponent<Shape>().IsSameType(shape))
  56. {
  57. matchesV.Add(shapes[row, shape.Column]);
  58. }
  59. else
  60. break;
  61. }
  62. //check top
  63. if (shape.Row != Constants.Rows - 1)
  64. for (int row = shape.Row + 1; row < Constants.Rows; row++)
  65. {
  66. if (shapes[row, shape.Column] != null &&
  67. shapes[row, shape.Column].GetComponent<Shape>().IsSameType(shape))
  68. {
  69. matchesV.Add(shapes[row, shape.Column]);
  70. }
  71. else
  72. break;
  73. }
  74. if (matchesV.Count < getMinimumMatches())
  75. matchesV.Clear();
  76. return matches.Union(matchesV).Distinct();
  77. }
  78. public override GameObject[] GetCandidateAI ()
  79. {
  80. var shapes = sm.shapes;
  81. List<GameObject[]> potentialHits=new List<GameObject[]>();
  82. for (int j = 0; j < Constants.Rows - 2; j++) {
  83. for (int i = 0; i < Constants.Columns - 2; i++) {
  84. GameObject[] hits = new GameObject[4];
  85. //check horizontal
  86. Shape sh1 = shapes [i, j].GetComponent<Shape> ();
  87. Shape sh2 = shapes [i, j+1].GetComponent<Shape> ();
  88. Shape sh3 = shapes [i, j+2].GetComponent<Shape> ();
  89. List<int[]> validOptions = new List<int[]>();
  90. //Test #1:
  91. // * * ? *
  92. // * ? X ?
  93. // * * O *
  94. // * * O *
  95. // * ? X ?
  96. // * * ? *
  97. if (sh1.IsSameType (sh2)) {
  98. hits [2] = shapes[i,j];
  99. hits [3] = shapes[i,j+1];
  100. if (j > 0) {
  101. for (int k = 0; k < Constants.Columns; k++) {
  102. validOptions.Add (new int[]{ k, j - 1 });
  103. }
  104. }
  105. foreach (int[] opt in validOptions) {
  106. if (shapes [opt[0],opt[1]].GetComponent<Shape> ().IsSameType (sh1)) {
  107. hits [1] = shapes [i, j - 1];
  108. hits [0] = shapes [opt[0],opt[1]];
  109. potentialHits.Add (hits);
  110. }
  111. }
  112. validOptions.Clear ();
  113. if (j <Constants.Rows-2) {
  114. for (int k = 0; k < Constants.Columns; k++) {
  115. validOptions.Add (new int[]{ k, j + 2 });
  116. }
  117. }
  118. foreach (int[] opt in validOptions) {
  119. if (shapes [opt[0],opt[1]].GetComponent<Shape> ().IsSameType (sh1)) {
  120. hits [1] = shapes [i, j +2];
  121. hits [0] = shapes [opt[0],opt[1]];
  122. potentialHits.Add (hits);
  123. }
  124. }
  125. validOptions.Clear ();
  126. }
  127. // * * * *
  128. // * * O *
  129. // * ? X ?
  130. // * * O *
  131. // * * * *
  132. else if(sh1.IsSameType(sh3)){
  133. hits [2] = shapes[i,j];
  134. hits [3] = shapes[i,j+2];
  135. for (int k = 0; k < Constants.Columns; k++) {
  136. validOptions.Add (new int[]{ k, j +1 });
  137. }
  138. foreach (int[] opt in validOptions) {
  139. if (shapes [opt[0],opt[1]].GetComponent<Shape> ().IsSameType (sh1)) {
  140. hits [1] = shapes [i, j + 1];
  141. hits [0] = shapes [opt[0],opt[1]];
  142. potentialHits.Add (hits);
  143. }
  144. }
  145. validOptions.Clear ();
  146. }
  147. //vertical
  148. sh1 = shapes [i, j].GetComponent<Shape> ();
  149. sh2 = shapes [i+1, j].GetComponent<Shape> ();
  150. sh3 = shapes [i+2, j].GetComponent<Shape> ();
  151. if (sh1.IsSameType (sh2)) {
  152. hits [2] = shapes[i,j];
  153. hits [3] = shapes[i+1,j];
  154. if (i > 0) {
  155. for (int k = 0; k < Constants.Rows; k++) {
  156. validOptions.Add (new int[]{ i - 1, k });
  157. }
  158. }
  159. foreach (int[] opt in validOptions) {
  160. if (shapes [opt[0],opt[1]].GetComponent<Shape> ().IsSameType (sh1)) {
  161. hits [1] = shapes [i-1, j];
  162. hits [0] = shapes [opt[0],opt[1]];
  163. potentialHits.Add (hits);
  164. }
  165. }
  166. validOptions.Clear ();
  167. if (i < Constants.Columns - 2) {
  168. for (int k = 0; k < Constants.Rows; k++) {
  169. validOptions.Add (new int[]{ i + 2, k });
  170. }
  171. }
  172. foreach (int[] opt in validOptions) {
  173. if (shapes [opt[0],opt[1]].GetComponent<Shape> ().IsSameType (sh1)) {
  174. hits [1] = shapes [i+2, j];
  175. hits [0] = shapes [opt[0],opt[1]];
  176. potentialHits.Add (hits);
  177. }
  178. }
  179. validOptions.Clear ();
  180. }
  181. // * * * *
  182. // * * O *
  183. // * ? X ?
  184. // * * O *
  185. // * * * *
  186. else if(sh1.IsSameType(sh3)){
  187. hits [2] = shapes[i,j];
  188. hits [3] = shapes[i+2,j];
  189. for (int k = 0; k < Constants.Rows; k++) {
  190. validOptions.Add (new int[]{ i+1, k });
  191. }
  192. foreach (int[] opt in validOptions) {
  193. if (shapes [opt[0],opt[1]].GetComponent<Shape> ().IsSameType (sh1)) {
  194. hits [1] = shapes [i+1, j];
  195. hits [0] = shapes [opt[0],opt[1]];
  196. potentialHits.Add (hits);
  197. }
  198. }
  199. validOptions.Clear ();
  200. }
  201. }
  202. }
  203. return potentialHits.ElementAt(Random.Range(0,potentialHits.Count()));
  204. }
  205. public override float getDamage (float matchedGems)
  206. {
  207. return 8 * Mathf.Pow (1.35f, (matchedGems - 2f)) - 1;
  208. }
  209. public override void HighlightSelection (GameObject go)
  210. {
  211. List<Vector3> positions = new List<Vector3> ();
  212. int posX = go.GetComponent<Shape> ().Column;
  213. int posY = go.GetComponent<Shape> ().Row;
  214. GameObject marker;
  215. for (int i = 0; i < Constants.Rows; i++)
  216. positions.Add (sm.shapes [i, posX].transform.position);
  217. for (int i = 0; i < Constants.Columns; i++)
  218. positions.Add (sm.shapes [posY, i].transform.position);
  219. foreach (Vector3 position in positions) {
  220. marker = Instantiate (sm.MarkerPrefabs [0], position, Quaternion.identity) as GameObject;
  221. markers.Add (marker);
  222. marker.transform.localScale = Constants.boardLossyScale;
  223. }
  224. Color c = go.GetComponent<SpriteRenderer> ().color;
  225. c.a = 0.6f;
  226. go.GetComponent<SpriteRenderer> ().color = c;
  227. }
  228. public override IEnumerator PerformAttack (GameObject go1, GameObject go2)
  229. {
  230. var shapes = sm.shapes;
  231. List<MatchesInfo> allMatches = new List<MatchesInfo> ();
  232. var totalMatches = new List<GameObject> ();
  233. bool validMove = false;
  234. if (go1.GetComponent<Shape> ().Column == go2.GetComponent<Shape> ().Column) {
  235. int column = go1.GetComponent<Shape> ().Column;
  236. int beginning = go1.GetComponent<Shape> ().Row;
  237. int end = go2.GetComponent<Shape> ().Row;
  238. Vector3[] positions = new Vector3[Constants.Rows];
  239. //z dolu do gory
  240. if (beginning < end) {
  241. positions [beginning] = shapes [end, column].transform.position;
  242. for (int i = beginning + 1; i <= end; i++) {
  243. positions [i] = shapes [i - 1, column].transform.position;
  244. }
  245. for (int i = beginning; i < end; i++) {
  246. shapes.Swap (shapes [i, column], shapes [i + 1, column]);
  247. shapes [i, column].transform.positionTo (Constants.AnimationDuration, positions [i + 1]);
  248. allMatches.Add (getMatches (shapes [i, column],shapes [i, column]));
  249. }
  250. go1.transform.positionTo (Constants.AnimationDuration, positions [beginning]);
  251. allMatches.Add (getMatches (go1,go1));
  252. yield return new WaitForSeconds (Constants.AnimationDuration);
  253. //sprawdza czy sa linie
  254. foreach (MatchesInfo match in allMatches) {
  255. if (match.MatchedCandy.Count () >= getMinimumMatches ()) {
  256. validMove = true;
  257. totalMatches.AddRange (match.MatchedCandy);
  258. }
  259. }
  260. //jesli nie to wroc na miejsce swoje
  261. if (!validMove) {
  262. positions [end] = shapes [beginning, column].transform.position;
  263. for (int i = end - 1; i >= beginning; i--) {
  264. positions [i] = shapes [i + 1, column].transform.position;
  265. }
  266. for (int i = end; i > beginning; i--) {
  267. shapes.Swap (shapes [i, column], shapes [i - 1, column]);
  268. shapes [i, column].transform.positionTo (Constants.AnimationDuration, positions [i - 1]);
  269. }
  270. go1.transform.positionTo (Constants.AnimationDuration, positions [end]);
  271. yield return new WaitForSeconds (Constants.AnimationDuration);
  272. sm.setState(GameState.None);
  273. }
  274. //z gory na dol
  275. } else {
  276. positions [beginning] = shapes [end, column].transform.position;
  277. for (int i = beginning - 1; i >= end; i--) {
  278. positions [i] = shapes [i + 1, column].transform.position;
  279. }
  280. for (int i = beginning; i > end; i--) {
  281. shapes.Swap (shapes [i, column], shapes [i - 1, column]);
  282. shapes [i, column].transform.positionTo (Constants.AnimationDuration, positions [i - 1]);
  283. allMatches.Add (getMatches (shapes [i, column],shapes [i, column]));
  284. }
  285. go1.transform.positionTo (Constants.AnimationDuration, positions [beginning]);
  286. allMatches.Add (getMatches (go1,go1));
  287. yield return new WaitForSeconds (Constants.AnimationDuration);
  288. //sprawdza czy sa linie
  289. foreach (MatchesInfo match in allMatches) {
  290. if (match.MatchedCandy.Count () >= getMinimumMatches ()) {
  291. validMove = true;
  292. totalMatches.AddRange (match.MatchedCandy);
  293. }
  294. }
  295. if (!validMove) {
  296. positions [end] = shapes [beginning, column].transform.position;
  297. for (int i = end + 1; i <= beginning; i++) {
  298. positions [i] = shapes [i - 1, column].transform.position;
  299. }
  300. for (int i = end; i < beginning; i++) {
  301. shapes.Swap (shapes [i, column], shapes [i + 1, column]);
  302. shapes [i, column].transform.positionTo (Constants.AnimationDuration, positions [i + 1]);
  303. }
  304. go1.transform.positionTo (Constants.AnimationDuration, positions [end]);
  305. yield return new WaitForSeconds (Constants.AnimationDuration);
  306. sm.setState(GameState.None);
  307. }
  308. }
  309. } else {
  310. int row = go1.GetComponent<Shape> ().Row;
  311. int beginning = go1.GetComponent<Shape> ().Column;
  312. int end = go2.GetComponent<Shape> ().Column;
  313. Vector3[] positions = new Vector3[Constants.Columns];
  314. //z lewo na prawo
  315. if (beginning < end) {
  316. positions [beginning] = shapes [row, end].transform.position;
  317. for (int i = beginning+1; i <= end; i++) {
  318. positions [i] = shapes [row,i-1].transform.position;
  319. }
  320. for (int i = beginning; i < end; i++) {
  321. shapes.Swap (shapes [row,i], shapes [row,i+1]);
  322. shapes [row,i].transform.positionTo (Constants.AnimationDuration, positions [i+1]);
  323. allMatches.Add (getMatches (shapes [row,i],shapes [row,i]));
  324. }
  325. go1.transform.positionTo(Constants.AnimationDuration,positions[beginning]);
  326. allMatches.Add (getMatches (go1,go1));
  327. yield return new WaitForSeconds(Constants.AnimationDuration);
  328. //sprawdza czy sa linie
  329. foreach (MatchesInfo match in allMatches) {
  330. if (match.MatchedCandy.Count () >= getMinimumMatches ()) {
  331. validMove = true;
  332. totalMatches.AddRange (match.MatchedCandy);
  333. }
  334. }
  335. //jesli nie to wroc na miejsce swoje
  336. if (!validMove) {
  337. positions [end] = shapes [row,beginning].transform.position;
  338. for (int i = end-1; i >= beginning; i--) {
  339. positions [i] = shapes [row,i+1].transform.position;
  340. }
  341. for (int i = end; i > beginning; i--) {
  342. shapes.Swap (shapes [row,i], shapes [row,i-1]);
  343. shapes [row,i].transform.positionTo (Constants.AnimationDuration, positions [i-1]);
  344. }
  345. go1.transform.positionTo(Constants.AnimationDuration,positions[end]);
  346. yield return new WaitForSeconds(Constants.AnimationDuration);
  347. sm.setState(GameState.None);
  348. }
  349. //z prawo na lewo
  350. } else {
  351. positions [beginning] = shapes [row,end].transform.position;
  352. for (int i = beginning-1; i >= end; i--) {
  353. positions [i] = shapes [row,i+1].transform.position;
  354. }
  355. for (int i = beginning; i > end; i--) {
  356. shapes.Swap (shapes [row,i], shapes [row,i-1]);
  357. shapes [row,i].transform.positionTo (Constants.AnimationDuration, positions [i-1]);
  358. allMatches.Add (getMatches (shapes [row,i],shapes [row,i]));
  359. }
  360. go1.transform.positionTo(Constants.AnimationDuration,positions[beginning]);
  361. allMatches.Add (getMatches (go1,go1));
  362. yield return new WaitForSeconds(Constants.AnimationDuration);
  363. //sprawdza czy sa linie
  364. foreach (MatchesInfo match in allMatches) {
  365. if (match.MatchedCandy.Count () >= getMinimumMatches ()) {
  366. validMove = true;
  367. totalMatches.AddRange (match.MatchedCandy);
  368. }
  369. }
  370. if (!validMove) {
  371. positions [end] = shapes [row,beginning].transform.position;
  372. for (int i = end+1; i <= beginning; i++) {
  373. positions [i] = shapes [row,i-1].transform.position;
  374. }
  375. for (int i = end; i < beginning; i++) {
  376. shapes.Swap (shapes [row,i], shapes [row,i+1]);
  377. shapes [row,i].transform.positionTo (Constants.AnimationDuration, positions [i+1]);
  378. }
  379. go1.transform.positionTo(Constants.AnimationDuration,positions[end]);
  380. yield return new WaitForSeconds(Constants.AnimationDuration);
  381. sm.setState (GameState.None);
  382. }
  383. }
  384. }
  385. if(validMove) {
  386. yield return sm.BreakMatchesAndGravity (totalMatches.Distinct());
  387. sm.processEndOfTurn ();
  388. }
  389. }
  390. }