Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.15 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using UnityEngine.UI;
  5. using System.Collections.Generic;
  6. public class MusuhMukull : MonoBehaviour {
  7. Color campur;
  8. Color []campuran = new Color[4];
  9. int darah=100;
  10. public static int tempMusuh = 0;
  11. Animator anim;
  12. public GameObject [,]floorrr = new GameObject[15,15];
  13. public Material material;
  14. Collision coll,cal,cal1;
  15. public float timeRemeaning = 5f;
  16. public float tempTime =0f;
  17. float speed = 5;
  18. int tampungX,tampungZ;
  19. public Button retry,cancel;
  20. public GameObject musuh,player;
  21. Vector3 posArr = new Vector3(0,0,0);
  22. Vector3 possAwalArr = new Vector3 (0,0,0);
  23. Renderer render;
  24. Vector3 posPlayer = new Vector3(0,0,0);
  25. int posPlayerX,posPlayerZ;
  26. int tempPosPlayerNowX,tempPosPlayerNowz;
  27. Vector3 des,desti;
  28. Quaternion look;
  29. int tempGantian;
  30. int tempDarahBeneran=0;
  31. void campurWarnaRandom()
  32. {
  33. for(int i=0;i<4;i++)
  34. {
  35. campur.r = Random.Range (0.0f,1.0f);
  36. campur.g = Random.Range (0.0f,1.0f);
  37. campur.b = Random.Range (0.0f,1.0f);
  38. campuran[i] = campur;
  39. }
  40. }
  41. void Start () {
  42.  
  43. campurWarnaRandom ();
  44. tempGantian = 0;
  45. //TempIdd.idGantian = 0;
  46. tempGantian = TempIdd.idGantian++;
  47. print (tempGantian);
  48. find = true;
  49. stack.Clear();
  50. floor.Clear();
  51. defaultt();
  52. berseiinWarna();
  53. Djixtra();
  54. //warna();
  55. buatJalan();
  56. /*for (int i=0; i<15; i++) {
  57. for (int j=0; j<15; j++) {
  58. //if (jalur [i, j] == true) {
  59. //DefaultMap.mapDefault [j, i] = 5;
  60. if(DefaultMap.mapDefault[j,i] != null){
  61. render = DefaultMap.tempFloor[j,i].gameObject.GetComponent<Renderer>();
  62. render.material.SetColor("_Color",Color.white);
  63. }
  64. //print("warna");
  65. //ren.material.SetColor("_Color",Color.black);
  66. //print (DefaultMap.mapDefault[i,j]);
  67. //print (i+" "+j);
  68. //}
  69. //else
  70. //{
  71. // DefaultMap.mapDefault[j,i] = 1;
  72. //}
  73. //swt.Write(DefaultMap.mapDefault[i,j]);
  74. //}
  75. //swt.WriteLine("");
  76. //print("\n");
  77. }
  78. //swt.Close ();
  79. }*/
  80. possAwalArr.x = transform.position.x;
  81. possAwalArr.z = transform.position.z;
  82. tempPosPlayerNowX = (int)Mathf.Round (possAwalArr.x);
  83. tempPosPlayerNowz = (int)Mathf.Round (possAwalArr.z);
  84.  
  85.  
  86.  
  87.  
  88. playerX = (int)Mathf.Round(transform.position.x);
  89. playerY = (int)Mathf.Round(transform.position.z);
  90. // print (playerX+" "+playerY);
  91. render = GetComponent<Renderer> ();
  92. anim = GetComponent<Animator> ();
  93. Data tempData = new Data ();
  94. floor.Add (tempData);
  95. stack.Add (tempData);
  96. }
  97. bool find = true;
  98. bool[,] flag = new bool[15,15];
  99. bool[,] jalur = new bool[15,15];
  100. int playerX,playerY;
  101. public class Data{
  102. public int x,y,cost;
  103. public Data parent;
  104. }
  105. //public ArrayList stack = new ArrayList();
  106. //public ArrayList floor = new ArrayList();
  107. List<Data> floor = new List<Data>();
  108. List <Data> stack= new List<Data>();
  109. public Data begin = new Data();
  110. Data finish;
  111.  
  112. void Djixtra()
  113. {
  114. possAwalArr.x = transform.position.x;
  115. possAwalArr.z = transform.position.z;
  116. tempPosPlayerNowX = (int)Mathf.Round (possAwalArr.x);
  117. tempPosPlayerNowz = (int)Mathf.Round (possAwalArr.z);
  118. posPlayer.x = player.transform.position.x;
  119. posPlayer.y = player.transform.position.z;
  120. //print("usuh"+DefaultMap.mapDefault[8,8]);
  121. posArr = transform.position;
  122. posArr.x = transform.position.x;
  123. posArr.z= transform.position.z;
  124. //print ("musuh"+(int)Mathf.Round(posArr.x) + (int)Mathf.Round (posArr.z));
  125. defaultt ();
  126. begin.x = (int)Mathf.Round(posArr.x);
  127. begin.y = (int)Mathf.Round (posArr.z);
  128.  
  129. //print (begin.x + " "+begin.y);
  130. begin.parent = null;
  131. begin.cost = 0;
  132. stack.Add (begin);
  133. int i = 1;
  134. //Debug.Log (DefaultMap.mapDefault[1,1]);
  135. while(find&&stack.Count!=0)
  136. {
  137.  
  138. //i++;
  139. stack.Sort((x,y)=>x.cost.CompareTo(y.cost));
  140. Data temp_curr = (Data)stack[0];
  141.  
  142. atas(i,temp_curr);
  143. kiri (i,temp_curr);
  144. kanan(i,temp_curr);
  145. bawah(i,temp_curr);
  146. stack.RemoveAt(0);
  147. }
  148. backTrack ();
  149. }
  150.  
  151. void warna()
  152. {
  153. //StreamWriter swt = new StreamWriter (@"djixtra.txt");
  154. for (int i=0; i<15; i++) {
  155. for (int j=0; j<15; j++) {
  156. if (jalur [i, j] == true && DefaultMap.tempFloor[j,i] != null)
  157. {
  158. render = DefaultMap.tempFloor[j,i].gameObject.GetComponent<Renderer>();
  159. if(render.material.GetColor("_Color").Equals(Color.white))
  160. {
  161. if(tempGantian%7==0)
  162. {
  163. render.material.SetColor("_Color",Color.red);
  164. }
  165. else if(tempGantian %7==1)
  166. {
  167. render.material.SetColor("_Color",Color.blue);
  168. }
  169. else if(tempGantian %7==2)
  170. {
  171. render.material.SetColor("_Color",Color.green);
  172. }
  173. else if(tempGantian %7==3)
  174. {
  175. render.material.SetColor("_Color",campuran[0]);
  176. }
  177.  
  178. else if(tempGantian %7==4)
  179. {
  180. render.material.SetColor("_Color",campuran[1]);
  181. }
  182.  
  183. else if(tempGantian %7==5)
  184. {
  185. render.material.SetColor("_Color",campuran[2]);
  186. }
  187.  
  188. else if(tempGantian %7==6)
  189. {
  190. render.material.SetColor("_Color",campuran[3]);
  191. }
  192. }
  193. else
  194. {
  195. if(tempGantian%7==0)
  196. {
  197. render.material.SetColor("_Color",(Color.red+render.material.GetColor("_Color")));
  198. }
  199. else if(tempGantian %7==1)
  200. {
  201. render.material.SetColor("_Color",(Color.blue+render.material.GetColor("_Color")));
  202. }
  203. else if(tempGantian %7==2)
  204. {
  205. render.material.SetColor("_Color",(Color.green+render.material.GetColor("_Color")));
  206. }
  207. else if(tempGantian %7==3)
  208. {
  209. render.material.SetColor("_Color",(campuran[0]+render.material.GetColor("_Color")));
  210. }
  211.  
  212. else if(tempGantian %7==4)
  213. {
  214. render.material.SetColor("_Color",(campuran[1]+render.material.GetColor("_Color")));
  215. }
  216.  
  217. else if(tempGantian %7==5)
  218. {
  219. render.material.SetColor("_Color",(campuran[2]+render.material.GetColor("_Color")));
  220. }
  221.  
  222. else if(tempGantian %7==6)
  223. {
  224. render.material.SetColor("_Color",(campuran[3]+render.material.GetColor("_Color")));
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. void defaultt()
  232. {
  233. posPlayer = player.transform.position;
  234. posPlayerX = (int)Mathf.Round(player.transform.position.x);
  235. posPlayerZ = (int)Mathf.Round(player.transform.position.z);
  236. //DefaultMap.mapDefault [posPlayerX,posPlayerZ] = 3;
  237. //print (" ini "+posPlayerX+posPlayerZ);
  238. //DefaultMap.mapDefault [(int)Mathf.Round(posArr.x),(int)Mathf.Round(posArr.z)] = 9;
  239. //print (" tuh "+posPlayerX+" "+posPlayerZ);
  240. for(int i=0;i<15;i++)
  241. {
  242. for(int j=0;j<15;j++)
  243. {
  244. flag[i,j] = false;
  245. jalur[i,j] = false;
  246. //floorrr[i,j] = 0;
  247. }
  248. }
  249. }
  250.  
  251. void backTrack()
  252. {
  253. posPlayer = player.transform.position;
  254. posPlayerX = (int)Mathf.Round(player.transform.position.x);
  255. posPlayerZ = (int)Mathf.Round(player.transform.position.z);
  256. Data curr = finish;
  257. while(curr.parent!=null)
  258. {
  259. //print("back");
  260.  
  261. floor.Add(curr);
  262. curr = curr.parent;
  263. }
  264.  
  265. for(int i=0;i<floor.Count;i++)
  266. {
  267. //print ("backWarna");
  268. // que.Sort((x, y) => x.label.CompareTo(y.label));
  269. //Data tempX = (Data)floor[i];
  270. int a = floor[i].x;
  271. //Data tempY = (Data)floor[i];
  272. int b = floor[i].y;
  273. jalur[a,b]=true;
  274. //floorrr[x,y] = 1;
  275. warna();
  276. }
  277. //floor.Reverse ();
  278. /*for(int i=0;i<floor.Count;i++)
  279. {
  280. Data posX = (Data)floor[i];
  281. int x = posX.x;
  282. Data posZ = (Data)floor[i];
  283. int z = posZ.y;
  284.  
  285. if(x<posPlayerZ && z==posPlayerZ)
  286. {
  287.  
  288. }
  289.  
  290. }*/
  291.  
  292. }
  293.  
  294. void kanan(int i,Data parent)
  295. {
  296. if(find)
  297. {
  298. //print ("kanan3");
  299. //Data tempY = (Data)stack[0];
  300. int y = stack[0].y;
  301. if(y<15)
  302. {
  303. y++;
  304. }
  305.  
  306. //Data tempX = (Data)stack[0];
  307. int x = stack[0].x;
  308. //Data tempCost = (Data)stack[0];
  309. int cost = stack[0].cost;
  310. //print(x +"kanan"+ y);
  311. if(x>=15 || y>=15)
  312. return;
  313. if(x<=0 || y<=0)
  314. return;
  315. if(DefaultMap.mapDefault[x,y] == 1)
  316. {
  317. //print ("kanan");
  318. return;
  319. }
  320. else if(flag[x,y] == true)
  321. {
  322. //print ("kanan1");
  323. return;
  324. }
  325. else
  326. {//print ("kanan2");
  327.  
  328. Data temp = new Data();
  329. temp.x = x;
  330. temp.y = y;
  331. //print(x+"kanan"+y);
  332. if(DefaultMap.mapDefault[x,y] == 2)
  333. {
  334. i+=5;
  335. }
  336. temp.cost = cost + i;
  337. temp.parent = parent;
  338. stack.Add (temp);
  339. flag[temp.x,temp.y] = true;
  340. if(temp.x == posPlayerX && temp.y == posPlayerZ)
  341. {
  342. //print ("berhasil");
  343. finish = temp;
  344. find = false;
  345. }
  346. }
  347. }
  348. else
  349. {
  350. //print ("ketemu");
  351. return;
  352. }
  353. }
  354.  
  355. void kiri(int i,Data parent)
  356. {
  357. if(find)
  358. {
  359. //Data tempY = (Data)stack[0];
  360. int y = stack[0].y;
  361. if(y>0)
  362. {
  363. y--;
  364. }
  365. //Data tempX = (Data)stack[0];
  366. int x = stack[0].x;
  367. //Data tempCost = (Data)stack[0];
  368. int cost = stack[0].cost;
  369. if(x>=15 || y>=15)
  370. return;
  371. if(x<=0 || y<=0)
  372. return;
  373. if(DefaultMap.mapDefault[x,y] == 1)
  374. {
  375. return;
  376. }
  377. else if(flag[x,y] == true)
  378. {
  379. return;
  380. }
  381. else
  382. {
  383. Data temp = new Data();
  384. temp.x = x;
  385. temp.y = y;
  386. //print(x+"kiri"+y);
  387. if(DefaultMap.mapDefault[x,y] == 2)
  388. {
  389. i+=5;
  390. }
  391. temp.cost = cost+i;
  392. temp.parent = parent;
  393. stack.Add (temp);
  394. flag[temp.x,temp.y] = true;
  395. if(temp.x == posPlayerX && temp.y == posPlayerZ)
  396. {
  397. finish = temp;
  398. find = false;
  399. }
  400. }
  401. }
  402. else
  403. {
  404. return;
  405. }
  406. }
  407. void buatJalan()
  408. {
  409. desti = new Vector3(floor[floor.Count - 1].x, transform.position.y, floor[floor.Count - 1].y);
  410. //print (desti.x +"Jarak"+ desti.z);
  411. gameObject.transform.position = Vector3.MoveTowards(transform.position, desti, 1f * Time.deltaTime);
  412. if (transform.position == desti)
  413. {
  414. floor.RemoveAt(floor.Count - 1);
  415. }
  416. des = (desti - transform.position).normalized;
  417. look = Quaternion.LookRotation(des);
  418.  
  419. transform.rotation = Quaternion.Slerp(transform.rotation, look, Time.deltaTime * 1.0f);
  420. }
  421. void atas(int i,Data parent)
  422. {
  423. if(find)
  424. {
  425.  
  426. //Data tempY = (Data)stack[0];
  427. int y = stack[0].y;
  428. //Data tempX = (Data)stack[0];
  429. int x = stack[0].x;
  430. //Data tempCost = (Data)stack[0];
  431. int cost = stack[0].cost;
  432. if(x>0)
  433. {
  434. x--;
  435. }
  436. if(x>=15 || y>=15)
  437. return;
  438. if(x<=0 || y<=0)
  439. return;
  440. if(DefaultMap.mapDefault[x,y] == 1)
  441. {
  442. return;
  443. }
  444. else if(flag[x,y] == true)
  445. {
  446. return;
  447. }
  448. else
  449. {
  450. Data temp = new Data();
  451. temp.x = x;
  452. temp.y = y;
  453. //print(x+"atas"+y);
  454. if(DefaultMap.mapDefault[x,y] == 2)
  455. {
  456. i+=4;
  457. }
  458. temp.cost = cost+i;
  459. temp.parent = parent;
  460. stack.Add (temp);
  461. flag[temp.x,temp.y] = true;
  462. if(temp.x == posPlayerX && temp.y == posPlayerZ)
  463. {
  464. finish = temp;
  465. find = false;
  466. }
  467. }
  468. }
  469. else
  470. {
  471. return;
  472. }
  473. }
  474.  
  475. void bawah(int i,Data parent)
  476. {
  477. if(find)
  478. {
  479. //Data tempY = (Data)stack[0];
  480. int y = stack[0].y;
  481. //Data tempX = (Data)stack[0];
  482. int x = stack[0].x;
  483. //Data tempCost = (Data)stack[0];
  484. int cost = stack[0].cost;
  485. if(x<15)
  486. {
  487. x++;
  488. }
  489.  
  490. if(x>=15 || y>=15)
  491. return;
  492. if(x<=0 || y<=0)
  493. return;
  494. if(DefaultMap.mapDefault[x,y] == 1)
  495. {
  496. return;
  497. }
  498. else if(flag[x,y] == true)
  499. {
  500. return;
  501. }
  502. else
  503. {
  504. Data temp = new Data();
  505. temp.x = x;
  506. temp.y = y;
  507. if(DefaultMap.mapDefault[x,y] == 2)
  508. {
  509. i+=4;
  510. }
  511. temp.cost = cost+i;
  512. //print(x+"bawah"+y);
  513. //print(temp.cost);
  514. temp.parent = parent;
  515. stack.Add (temp);
  516. flag[temp.x,temp.y] = true;
  517. if(temp.x == posPlayerX && temp.y == posPlayerZ)
  518. {
  519. finish = temp;
  520. find = false;
  521. }
  522. }
  523. }
  524. else
  525. {
  526. return;
  527. }
  528. }
  529. /*void OnCollisionStay(Collision collision)
  530. {
  531. foreach(ContactPoint cp in collision){
  532. if(cp.otherCollider.gameObject.name.Equals("NormalBrick(Clone)"))
  533. {
  534. tempNormal tn = cp.otherCollider.gameObject.GetComponent<tempNormal>();
  535. tn.hitted(gameObject);
  536. }
  537. }
  538. }*/
  539. void OnCollisionEnter(Collision col)
  540. {
  541. //Debug.Log (col.gameObject.name);
  542. if (col.gameObject.name.Equals ("NormalBrick(Clone)")) {
  543. if(tempGantian%3==0)
  544. {
  545. //print ("Punya si 1");
  546. //TempIdd.darah--;
  547. //darah--;
  548. //darah=1;
  549. tempDarahBeneran =1;
  550. coll = col;
  551. }
  552. if(tempGantian%3==1)
  553. {
  554. // print ("Punya si 2");
  555. //TempIdd.darah--;
  556. //darah--;
  557. //darah = 1;
  558. tempDarahBeneran =1;
  559. coll = col;
  560. }
  561. if(tempGantian%3==2)
  562. {
  563. //print ("Punya si 3");
  564. //TempIdd.darah--;
  565. //darah--;
  566. //darah = 1;
  567. tempDarahBeneran =1;
  568. coll = col;
  569. }
  570. }
  571. if(col.gameObject.name.Equals ("ST_Finish"))
  572. {
  573. cal1 = col;
  574. }
  575. if(col.gameObject.name.Equals("HardBrick(Clone)"))
  576. {
  577.  
  578.  
  579. //Debug.Log("berhasil");
  580. cal = col;
  581. //if(transform.position == Vector3.right)
  582. //{
  583.  
  584. //}
  585. //else if(transform.position == Vector3.left)
  586. //{
  587. // transform.position+=transform.TransformDirection(Vector3.back)*Time.deltaTime*1;
  588. //}
  589. ///else if(transform.position == Vector3.forward)
  590. //{
  591. //transform.position+=transform.TransformDirection(Vector3.right)*Time.deltaTime*1;
  592. //Debug.Log("berhasil");
  593. //}
  594. ///else if(transform.position == Vector3.back)
  595. //{
  596. /// transform.position+=transform.TransformDirection(Vector3.left)*Time.deltaTime*1;
  597. //}
  598. }
  599. }
  600.  
  601. // Update is called once per frame
  602. void Update () {
  603. //print (TempIdd.darah);
  604. if(coll!=null)
  605. {
  606. DefaultMap.mapDefault [tampungX, tampungZ] = 0;
  607. if(tempDarahBeneran == 1)
  608. {
  609. darah--;
  610. if(darah <0)
  611. {
  612. //if(tempGantian %3==0)
  613. //{
  614.  
  615. tampungX = (int)transform.position.x;
  616. tampungZ = (int)transform.position.z;
  617. Destroy(coll.gameObject);
  618. tempDarahBeneran = 0;
  619. darah = 100;
  620.  
  621.  
  622. //}
  623. //if(tempGantian %3==1)
  624. //{
  625. //Destroy(coll.gameObject);
  626. ///darah = 0;
  627. //TempIdd.darah = 500;
  628. //}
  629. //if(tempGantian %3==2)
  630. //{
  631. // Destroy(coll.gameObject);
  632. // darah = 0;
  633. // TempIdd.darah = 500;
  634. //}
  635. }
  636. }
  637. if(coll!=null && darah>0 && darah<100)
  638. {
  639. anim.SetBool("tonjok",true);
  640. }
  641. else if(darah==100){
  642. anim.SetBool("tonjok",false);
  643. }
  644. /*if(tempGantian %3==0)
  645. {
  646. Destroy (coll.gameObject);
  647. tampungX = (int)transform.position.x;
  648. tampungZ = (int)transform.position.z;
  649. DefaultMap.mapDefault [tampungX, tampungZ] = 0;
  650. darah = 10;
  651. }
  652. if(tempGantian%3==1)
  653. {
  654. Destroy (coll.gameObject);
  655. tampungX = (int)transform.position.x;
  656. tampungZ = (int)transform.position.z;
  657. DefaultMap.mapDefault [tampungX, tampungZ] = 0;
  658. darah = 10;
  659. }
  660. if(tempGantian%3==2)
  661. {
  662. Destroy (coll.gameObject);
  663. tampungX = (int)transform.position.x;
  664. tampungZ = (int)transform.position.z;
  665. DefaultMap.mapDefault [tampungX, tampungZ] = 0;
  666. darah = 10;
  667. }*/
  668. }
  669. if(tempPosPlayerNowX!=(int)Mathf.Round(posPlayer.x) || tempPosPlayerNowz!=(int)Mathf.Round(posPlayer.z))
  670. {
  671. find = true;
  672. stack.Clear();
  673. floor.Clear();
  674. defaultt();
  675. berseiinWarna();
  676. Djixtra();
  677. //warna();
  678. buatJalan();
  679. /*StreamWriter swt = new StreamWriter (@"djixtra.txt");
  680. for(int k=0;k<15;k++)
  681. {
  682. for(int j=0;j<15;j++)
  683. {
  684. //if(jalur[i,j] == true)
  685. //{
  686. //DefaultMap.mapDefault[j,i] = 5;
  687.  
  688. //print("warna");
  689. //ren.material.SetColor("_Color",Color.black);
  690. //print (DefaultMap.mapDefault[i,j]);
  691. //print (i+" "+j);
  692. //}
  693. //else
  694. //{
  695. //DefaultMap.mapDefault[j,i] = 1;
  696. //}
  697. swt.Write(DefaultMap.mapDefault[k,j]);
  698. }
  699. swt.WriteLine("");
  700. print("\n");
  701. }
  702. swt.Close ();*/
  703.  
  704. }
  705.  
  706. }
  707. void berseiinWarna()
  708. {
  709. for(int i=0;i<15;i++)
  710. {
  711. for(int j=0;j<15;j++)
  712. {
  713. if(tempGantian%7==0 && DefaultMap.tempFloor[j,i] != null)
  714. {
  715. render = DefaultMap.tempFloor[j,i].gameObject.GetComponent<Renderer>();
  716. render.material.SetColor("_Color",Color.white);
  717. }
  718. }
  719. }
  720. }
  721. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement