Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.IO;
- using UnityEngine.UI;
- using System.Collections.Generic;
- public class MusuhMukull : MonoBehaviour {
- Color campur;
- Color []campuran = new Color[4];
- int darah=100;
- public static int tempMusuh = 0;
- Animator anim;
- public GameObject [,]floorrr = new GameObject[15,15];
- public Material material;
- Collision coll,cal,cal1;
- public float timeRemeaning = 5f;
- public float tempTime =0f;
- float speed = 5;
- int tampungX,tampungZ;
- public Button retry,cancel;
- public GameObject musuh,player;
- Vector3 posArr = new Vector3(0,0,0);
- Vector3 possAwalArr = new Vector3 (0,0,0);
- Renderer render;
- Vector3 posPlayer = new Vector3(0,0,0);
- int posPlayerX,posPlayerZ;
- int tempPosPlayerNowX,tempPosPlayerNowz;
- Vector3 des,desti;
- Quaternion look;
- int tempGantian;
- int tempDarahBeneran=0;
- void campurWarnaRandom()
- {
- for(int i=0;i<4;i++)
- {
- campur.r = Random.Range (0.0f,1.0f);
- campur.g = Random.Range (0.0f,1.0f);
- campur.b = Random.Range (0.0f,1.0f);
- campuran[i] = campur;
- }
- }
- void Start () {
- campurWarnaRandom ();
- tempGantian = 0;
- //TempIdd.idGantian = 0;
- tempGantian = TempIdd.idGantian++;
- print (tempGantian);
- find = true;
- stack.Clear();
- floor.Clear();
- defaultt();
- berseiinWarna();
- Djixtra();
- //warna();
- buatJalan();
- /*for (int i=0; i<15; i++) {
- for (int j=0; j<15; j++) {
- //if (jalur [i, j] == true) {
- //DefaultMap.mapDefault [j, i] = 5;
- if(DefaultMap.mapDefault[j,i] != null){
- render = DefaultMap.tempFloor[j,i].gameObject.GetComponent<Renderer>();
- render.material.SetColor("_Color",Color.white);
- }
- //print("warna");
- //ren.material.SetColor("_Color",Color.black);
- //print (DefaultMap.mapDefault[i,j]);
- //print (i+" "+j);
- //}
- //else
- //{
- // DefaultMap.mapDefault[j,i] = 1;
- //}
- //swt.Write(DefaultMap.mapDefault[i,j]);
- //}
- //swt.WriteLine("");
- //print("\n");
- }
- //swt.Close ();
- }*/
- possAwalArr.x = transform.position.x;
- possAwalArr.z = transform.position.z;
- tempPosPlayerNowX = (int)Mathf.Round (possAwalArr.x);
- tempPosPlayerNowz = (int)Mathf.Round (possAwalArr.z);
- playerX = (int)Mathf.Round(transform.position.x);
- playerY = (int)Mathf.Round(transform.position.z);
- // print (playerX+" "+playerY);
- render = GetComponent<Renderer> ();
- anim = GetComponent<Animator> ();
- Data tempData = new Data ();
- floor.Add (tempData);
- stack.Add (tempData);
- }
- bool find = true;
- bool[,] flag = new bool[15,15];
- bool[,] jalur = new bool[15,15];
- int playerX,playerY;
- public class Data{
- public int x,y,cost;
- public Data parent;
- }
- //public ArrayList stack = new ArrayList();
- //public ArrayList floor = new ArrayList();
- List<Data> floor = new List<Data>();
- List <Data> stack= new List<Data>();
- public Data begin = new Data();
- Data finish;
- void Djixtra()
- {
- possAwalArr.x = transform.position.x;
- possAwalArr.z = transform.position.z;
- tempPosPlayerNowX = (int)Mathf.Round (possAwalArr.x);
- tempPosPlayerNowz = (int)Mathf.Round (possAwalArr.z);
- posPlayer.x = player.transform.position.x;
- posPlayer.y = player.transform.position.z;
- //print("usuh"+DefaultMap.mapDefault[8,8]);
- posArr = transform.position;
- posArr.x = transform.position.x;
- posArr.z= transform.position.z;
- //print ("musuh"+(int)Mathf.Round(posArr.x) + (int)Mathf.Round (posArr.z));
- defaultt ();
- begin.x = (int)Mathf.Round(posArr.x);
- begin.y = (int)Mathf.Round (posArr.z);
- //print (begin.x + " "+begin.y);
- begin.parent = null;
- begin.cost = 0;
- stack.Add (begin);
- int i = 1;
- //Debug.Log (DefaultMap.mapDefault[1,1]);
- while(find&&stack.Count!=0)
- {
- //i++;
- stack.Sort((x,y)=>x.cost.CompareTo(y.cost));
- Data temp_curr = (Data)stack[0];
- atas(i,temp_curr);
- kiri (i,temp_curr);
- kanan(i,temp_curr);
- bawah(i,temp_curr);
- stack.RemoveAt(0);
- }
- backTrack ();
- }
- void warna()
- {
- //StreamWriter swt = new StreamWriter (@"djixtra.txt");
- for (int i=0; i<15; i++) {
- for (int j=0; j<15; j++) {
- if (jalur [i, j] == true && DefaultMap.tempFloor[j,i] != null)
- {
- render = DefaultMap.tempFloor[j,i].gameObject.GetComponent<Renderer>();
- if(render.material.GetColor("_Color").Equals(Color.white))
- {
- if(tempGantian%7==0)
- {
- render.material.SetColor("_Color",Color.red);
- }
- else if(tempGantian %7==1)
- {
- render.material.SetColor("_Color",Color.blue);
- }
- else if(tempGantian %7==2)
- {
- render.material.SetColor("_Color",Color.green);
- }
- else if(tempGantian %7==3)
- {
- render.material.SetColor("_Color",campuran[0]);
- }
- else if(tempGantian %7==4)
- {
- render.material.SetColor("_Color",campuran[1]);
- }
- else if(tempGantian %7==5)
- {
- render.material.SetColor("_Color",campuran[2]);
- }
- else if(tempGantian %7==6)
- {
- render.material.SetColor("_Color",campuran[3]);
- }
- }
- else
- {
- if(tempGantian%7==0)
- {
- render.material.SetColor("_Color",(Color.red+render.material.GetColor("_Color")));
- }
- else if(tempGantian %7==1)
- {
- render.material.SetColor("_Color",(Color.blue+render.material.GetColor("_Color")));
- }
- else if(tempGantian %7==2)
- {
- render.material.SetColor("_Color",(Color.green+render.material.GetColor("_Color")));
- }
- else if(tempGantian %7==3)
- {
- render.material.SetColor("_Color",(campuran[0]+render.material.GetColor("_Color")));
- }
- else if(tempGantian %7==4)
- {
- render.material.SetColor("_Color",(campuran[1]+render.material.GetColor("_Color")));
- }
- else if(tempGantian %7==5)
- {
- render.material.SetColor("_Color",(campuran[2]+render.material.GetColor("_Color")));
- }
- else if(tempGantian %7==6)
- {
- render.material.SetColor("_Color",(campuran[3]+render.material.GetColor("_Color")));
- }
- }
- }
- }
- }
- }
- void defaultt()
- {
- posPlayer = player.transform.position;
- posPlayerX = (int)Mathf.Round(player.transform.position.x);
- posPlayerZ = (int)Mathf.Round(player.transform.position.z);
- //DefaultMap.mapDefault [posPlayerX,posPlayerZ] = 3;
- //print (" ini "+posPlayerX+posPlayerZ);
- //DefaultMap.mapDefault [(int)Mathf.Round(posArr.x),(int)Mathf.Round(posArr.z)] = 9;
- //print (" tuh "+posPlayerX+" "+posPlayerZ);
- for(int i=0;i<15;i++)
- {
- for(int j=0;j<15;j++)
- {
- flag[i,j] = false;
- jalur[i,j] = false;
- //floorrr[i,j] = 0;
- }
- }
- }
- void backTrack()
- {
- posPlayer = player.transform.position;
- posPlayerX = (int)Mathf.Round(player.transform.position.x);
- posPlayerZ = (int)Mathf.Round(player.transform.position.z);
- Data curr = finish;
- while(curr.parent!=null)
- {
- //print("back");
- floor.Add(curr);
- curr = curr.parent;
- }
- for(int i=0;i<floor.Count;i++)
- {
- //print ("backWarna");
- // que.Sort((x, y) => x.label.CompareTo(y.label));
- //Data tempX = (Data)floor[i];
- int a = floor[i].x;
- //Data tempY = (Data)floor[i];
- int b = floor[i].y;
- jalur[a,b]=true;
- //floorrr[x,y] = 1;
- warna();
- }
- //floor.Reverse ();
- /*for(int i=0;i<floor.Count;i++)
- {
- Data posX = (Data)floor[i];
- int x = posX.x;
- Data posZ = (Data)floor[i];
- int z = posZ.y;
- if(x<posPlayerZ && z==posPlayerZ)
- {
- }
- }*/
- }
- void kanan(int i,Data parent)
- {
- if(find)
- {
- //print ("kanan3");
- //Data tempY = (Data)stack[0];
- int y = stack[0].y;
- if(y<15)
- {
- y++;
- }
- //Data tempX = (Data)stack[0];
- int x = stack[0].x;
- //Data tempCost = (Data)stack[0];
- int cost = stack[0].cost;
- //print(x +"kanan"+ y);
- if(x>=15 || y>=15)
- return;
- if(x<=0 || y<=0)
- return;
- if(DefaultMap.mapDefault[x,y] == 1)
- {
- //print ("kanan");
- return;
- }
- else if(flag[x,y] == true)
- {
- //print ("kanan1");
- return;
- }
- else
- {//print ("kanan2");
- Data temp = new Data();
- temp.x = x;
- temp.y = y;
- //print(x+"kanan"+y);
- if(DefaultMap.mapDefault[x,y] == 2)
- {
- i+=5;
- }
- temp.cost = cost + i;
- temp.parent = parent;
- stack.Add (temp);
- flag[temp.x,temp.y] = true;
- if(temp.x == posPlayerX && temp.y == posPlayerZ)
- {
- //print ("berhasil");
- finish = temp;
- find = false;
- }
- }
- }
- else
- {
- //print ("ketemu");
- return;
- }
- }
- void kiri(int i,Data parent)
- {
- if(find)
- {
- //Data tempY = (Data)stack[0];
- int y = stack[0].y;
- if(y>0)
- {
- y--;
- }
- //Data tempX = (Data)stack[0];
- int x = stack[0].x;
- //Data tempCost = (Data)stack[0];
- int cost = stack[0].cost;
- if(x>=15 || y>=15)
- return;
- if(x<=0 || y<=0)
- return;
- if(DefaultMap.mapDefault[x,y] == 1)
- {
- return;
- }
- else if(flag[x,y] == true)
- {
- return;
- }
- else
- {
- Data temp = new Data();
- temp.x = x;
- temp.y = y;
- //print(x+"kiri"+y);
- if(DefaultMap.mapDefault[x,y] == 2)
- {
- i+=5;
- }
- temp.cost = cost+i;
- temp.parent = parent;
- stack.Add (temp);
- flag[temp.x,temp.y] = true;
- if(temp.x == posPlayerX && temp.y == posPlayerZ)
- {
- finish = temp;
- find = false;
- }
- }
- }
- else
- {
- return;
- }
- }
- void buatJalan()
- {
- desti = new Vector3(floor[floor.Count - 1].x, transform.position.y, floor[floor.Count - 1].y);
- //print (desti.x +"Jarak"+ desti.z);
- gameObject.transform.position = Vector3.MoveTowards(transform.position, desti, 1f * Time.deltaTime);
- if (transform.position == desti)
- {
- floor.RemoveAt(floor.Count - 1);
- }
- des = (desti - transform.position).normalized;
- look = Quaternion.LookRotation(des);
- transform.rotation = Quaternion.Slerp(transform.rotation, look, Time.deltaTime * 1.0f);
- }
- void atas(int i,Data parent)
- {
- if(find)
- {
- //Data tempY = (Data)stack[0];
- int y = stack[0].y;
- //Data tempX = (Data)stack[0];
- int x = stack[0].x;
- //Data tempCost = (Data)stack[0];
- int cost = stack[0].cost;
- if(x>0)
- {
- x--;
- }
- if(x>=15 || y>=15)
- return;
- if(x<=0 || y<=0)
- return;
- if(DefaultMap.mapDefault[x,y] == 1)
- {
- return;
- }
- else if(flag[x,y] == true)
- {
- return;
- }
- else
- {
- Data temp = new Data();
- temp.x = x;
- temp.y = y;
- //print(x+"atas"+y);
- if(DefaultMap.mapDefault[x,y] == 2)
- {
- i+=4;
- }
- temp.cost = cost+i;
- temp.parent = parent;
- stack.Add (temp);
- flag[temp.x,temp.y] = true;
- if(temp.x == posPlayerX && temp.y == posPlayerZ)
- {
- finish = temp;
- find = false;
- }
- }
- }
- else
- {
- return;
- }
- }
- void bawah(int i,Data parent)
- {
- if(find)
- {
- //Data tempY = (Data)stack[0];
- int y = stack[0].y;
- //Data tempX = (Data)stack[0];
- int x = stack[0].x;
- //Data tempCost = (Data)stack[0];
- int cost = stack[0].cost;
- if(x<15)
- {
- x++;
- }
- if(x>=15 || y>=15)
- return;
- if(x<=0 || y<=0)
- return;
- if(DefaultMap.mapDefault[x,y] == 1)
- {
- return;
- }
- else if(flag[x,y] == true)
- {
- return;
- }
- else
- {
- Data temp = new Data();
- temp.x = x;
- temp.y = y;
- if(DefaultMap.mapDefault[x,y] == 2)
- {
- i+=4;
- }
- temp.cost = cost+i;
- //print(x+"bawah"+y);
- //print(temp.cost);
- temp.parent = parent;
- stack.Add (temp);
- flag[temp.x,temp.y] = true;
- if(temp.x == posPlayerX && temp.y == posPlayerZ)
- {
- finish = temp;
- find = false;
- }
- }
- }
- else
- {
- return;
- }
- }
- /*void OnCollisionStay(Collision collision)
- {
- foreach(ContactPoint cp in collision){
- if(cp.otherCollider.gameObject.name.Equals("NormalBrick(Clone)"))
- {
- tempNormal tn = cp.otherCollider.gameObject.GetComponent<tempNormal>();
- tn.hitted(gameObject);
- }
- }
- }*/
- void OnCollisionEnter(Collision col)
- {
- //Debug.Log (col.gameObject.name);
- if (col.gameObject.name.Equals ("NormalBrick(Clone)")) {
- if(tempGantian%3==0)
- {
- //print ("Punya si 1");
- //TempIdd.darah--;
- //darah--;
- //darah=1;
- tempDarahBeneran =1;
- coll = col;
- }
- if(tempGantian%3==1)
- {
- // print ("Punya si 2");
- //TempIdd.darah--;
- //darah--;
- //darah = 1;
- tempDarahBeneran =1;
- coll = col;
- }
- if(tempGantian%3==2)
- {
- //print ("Punya si 3");
- //TempIdd.darah--;
- //darah--;
- //darah = 1;
- tempDarahBeneran =1;
- coll = col;
- }
- }
- if(col.gameObject.name.Equals ("ST_Finish"))
- {
- cal1 = col;
- }
- if(col.gameObject.name.Equals("HardBrick(Clone)"))
- {
- //Debug.Log("berhasil");
- cal = col;
- //if(transform.position == Vector3.right)
- //{
- //}
- //else if(transform.position == Vector3.left)
- //{
- // transform.position+=transform.TransformDirection(Vector3.back)*Time.deltaTime*1;
- //}
- ///else if(transform.position == Vector3.forward)
- //{
- //transform.position+=transform.TransformDirection(Vector3.right)*Time.deltaTime*1;
- //Debug.Log("berhasil");
- //}
- ///else if(transform.position == Vector3.back)
- //{
- /// transform.position+=transform.TransformDirection(Vector3.left)*Time.deltaTime*1;
- //}
- }
- }
- // Update is called once per frame
- void Update () {
- //print (TempIdd.darah);
- if(coll!=null)
- {
- DefaultMap.mapDefault [tampungX, tampungZ] = 0;
- if(tempDarahBeneran == 1)
- {
- darah--;
- if(darah <0)
- {
- //if(tempGantian %3==0)
- //{
- tampungX = (int)transform.position.x;
- tampungZ = (int)transform.position.z;
- Destroy(coll.gameObject);
- tempDarahBeneran = 0;
- darah = 100;
- //}
- //if(tempGantian %3==1)
- //{
- //Destroy(coll.gameObject);
- ///darah = 0;
- //TempIdd.darah = 500;
- //}
- //if(tempGantian %3==2)
- //{
- // Destroy(coll.gameObject);
- // darah = 0;
- // TempIdd.darah = 500;
- //}
- }
- }
- if(coll!=null && darah>0 && darah<100)
- {
- anim.SetBool("tonjok",true);
- }
- else if(darah==100){
- anim.SetBool("tonjok",false);
- }
- /*if(tempGantian %3==0)
- {
- Destroy (coll.gameObject);
- tampungX = (int)transform.position.x;
- tampungZ = (int)transform.position.z;
- DefaultMap.mapDefault [tampungX, tampungZ] = 0;
- darah = 10;
- }
- if(tempGantian%3==1)
- {
- Destroy (coll.gameObject);
- tampungX = (int)transform.position.x;
- tampungZ = (int)transform.position.z;
- DefaultMap.mapDefault [tampungX, tampungZ] = 0;
- darah = 10;
- }
- if(tempGantian%3==2)
- {
- Destroy (coll.gameObject);
- tampungX = (int)transform.position.x;
- tampungZ = (int)transform.position.z;
- DefaultMap.mapDefault [tampungX, tampungZ] = 0;
- darah = 10;
- }*/
- }
- if(tempPosPlayerNowX!=(int)Mathf.Round(posPlayer.x) || tempPosPlayerNowz!=(int)Mathf.Round(posPlayer.z))
- {
- find = true;
- stack.Clear();
- floor.Clear();
- defaultt();
- berseiinWarna();
- Djixtra();
- //warna();
- buatJalan();
- /*StreamWriter swt = new StreamWriter (@"djixtra.txt");
- for(int k=0;k<15;k++)
- {
- for(int j=0;j<15;j++)
- {
- //if(jalur[i,j] == true)
- //{
- //DefaultMap.mapDefault[j,i] = 5;
- //print("warna");
- //ren.material.SetColor("_Color",Color.black);
- //print (DefaultMap.mapDefault[i,j]);
- //print (i+" "+j);
- //}
- //else
- //{
- //DefaultMap.mapDefault[j,i] = 1;
- //}
- swt.Write(DefaultMap.mapDefault[k,j]);
- }
- swt.WriteLine("");
- print("\n");
- }
- swt.Close ();*/
- }
- }
- void berseiinWarna()
- {
- for(int i=0;i<15;i++)
- {
- for(int j=0;j<15;j++)
- {
- if(tempGantian%7==0 && DefaultMap.tempFloor[j,i] != null)
- {
- render = DefaultMap.tempFloor[j,i].gameObject.GetComponent<Renderer>();
- render.material.SetColor("_Color",Color.white);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement