// Start is called before the first frame update protected void Start() { setUp(); getWayPoints(); boxWps = GameObject.Find("GameManager").GetComponent <GameManager>().boxWP; nextboxWp = boxWps[Random.Range(0, 4)]; }
// Use this for initialization void Start() { switch (mapVal) { case 1: { Array = new GameObject[Map1Pos.Length]; for (int i = 0; i < Map1Pos.Length; i++) { Array[i] = Instantiate(Object,this.transform.position,Quaternion.identity) as GameObject; //GameObject tempTransform = Instantiate(wayPoint1[i],wayPoint1[i].transform.position,Quaternion.identity) as GameObject; //tempTransform.name = "waypoint"; Array[i].SetActive(true); Array[i].transform.parent = this.transform; Array[i].transform.localScale = new Vector3(1,1,1); Array[i].transform.localPosition = Map1Pos[i]; wp = Array[i].GetComponent<wayPoint>(); //tempTransform.transform.parent = Array[i].transform; if(wp != null) wp.wpName = wayPoint1[i]; } break; } case 2: { Array = new GameObject[Map2Pos.Length]; for (int i = 0; i < Map2Pos.Length; i++) { Array[i] = Instantiate(Object,this.transform.position,Quaternion.identity) as GameObject; //GameObject tempTransform = Instantiate(wayPoint2[i],wayPoint1[i].transform.position,Quaternion.identity) as GameObject; //tempTransform.name = "waypoint"; Array[i].SetActive(true); Array[i].transform.parent = this.transform; Array[i].transform.localScale = new Vector3(1,1,1); Array[i].transform.localPosition = Map2Pos[i]; wp = Array[i].GetComponent<wayPoint>(); //tempTransform.transform.parent = Array[i].transform; if(wp != null) wp.wpName = wayPoint2[i]; } break; } case 3: { Array = new GameObject[Map3Pos.Length]; for (int i = 0; i < Map3Pos.Length; i++) { Array[i] = Instantiate(Object,this.transform.position,Quaternion.identity) as GameObject; //GameObject tempTransform = Instantiate(wayPoint3[i],wayPoint1[i].transform.position,Quaternion.identity) as GameObject; //tempTransform.name = "waypoint"; Array[i].SetActive(true); Array[i].transform.parent = this.transform; Array[i].transform.localScale = new Vector3(1,1,1); Array[i].transform.localPosition = Map3Pos[i]; wp = Array[i].GetComponent<wayPoint>(); //tempTransform.transform.parent = Array[i].transform; if(wp != null) wp.wpName = wayPoint3[i]; } break; } } }
private void Awake() { StartWP = GameObject.Find("wayPoint").GetComponent <wayPoint>(); setUpGame(); ScoreLable = GameObject.Find("Score").GetComponent <Text>(); TimeLable = GameObject.Find("Timer").GetComponent <Text>(); setUpAudio(); }
protected wayPoint naviTo(wayPoint aimWp)//Get the first Way point on the navigation path { if (curWp != aimWp) { return(getNavi(this.curWp, aimWp)[1]); } return(aimWp); }
protected void moveInBox()//Randomly move in the box before the monster wakes up { float distance = Vector3.Distance(transform.position, nextboxWp.transform.position); if (distance > 0.1f) { transform.position = Vector2.MoveTowards(transform.position, nextboxWp.transform.position, Time.deltaTime * speed); } else { nextboxWp = boxWps[Random.Range(0, 4)]; changeDirection(); } }
protected List <wayPoint> getNavi(wayPoint curWp, wayPoint aimWp) //Find the shortest path from cur way point to the target way point { List <wayPoint> markedWp = new List <wayPoint>(); List <wayPoint> ShortestPath = new List <wayPoint>(); List <wayPoint> WaitList = new List <wayPoint>(); List <int> fatherPoints = new List <int>(); wayPoint fp; int mark; WaitList.Add(curWp); markedWp.Add(curWp); fatherPoints.Add(-1);//If the parent of the current way point does not exist, it is marked as -1 for (int i = 0; i < WaitList.Count; i++) { for (int j = 0; j < WaitList[i].neighborWps.Count; j++) { if (!markedWp.Contains(WaitList[i].neighborWps[j])) { markedWp.Add(WaitList[i].neighborWps[j]); if (WaitList[i].neighborWps[j] == aimWp) { fatherPoints.Add(i); WaitList.Add(WaitList[i].neighborWps[j]); //Find the target way point ShortestPath.Add(aimWp); mark = fatherPoints[fatherPoints.Count - 1]; //Get the shortest path through the parent way point while (ShortestPath[0] != curWp) { fp = WaitList[mark]; ShortestPath.Insert(0, fp); mark = fatherPoints[mark]; } return(ShortestPath); } else { fatherPoints.Add(i); //Record the parent of each element in the Wait List WaitList.Add(WaitList[i].neighborWps[j]); //Add all adjacent way points of the current way point after waiting for the table } } } } return(ShortestPath); }
private wayPoint ranAway(GameObject aimObj) { wayPoint aimWp = null; float maxDistance = 0; //Choose a way point furthest away from Pac-Man and navigate for (int i = 0; i < Wps.Count; i++) { if (Vector3.Distance(aimObj.transform.position, Wps[i].transform.position) > maxDistance) { aimWp = Wps[i]; maxDistance = Vector3.Distance(aimObj.transform.position, Wps[i].transform.position); } } return(aimWp); }
private wayPoint chase(GameObject aimObj) { wayPoint aimWp = null; float minDistance = 999; //Choose a way point that is the shortest distance from Pac-Man and navigate for (int i = 0; i < Wps.Count; i++) { if (Vector3.Distance(aimObj.transform.position, Wps[i].transform.position) < minDistance) { aimWp = Wps[i]; minDistance = Vector3.Distance(aimObj.transform.position, Wps[i].transform.position); } } return(aimWp); }
void moveToNext() { //Determine whether the current position is equal to the way point, if not, continue moving float distance = Vector3.Distance(transform.position, nextWp.transform.position); if (distance > 0.1f) { transform.position = Vector2.MoveTowards(transform.position, nextWp.transform.position, Time.deltaTime * speed); } else { lastWp = curWp; curWp = nextWp; nextWp = findNextWp(); changeDirection(); } }
bool LinkWp()//link the cur waypoint to it's neighbor's waypoint, if the cur position already has a way point, then return false { float distance; for (int i = 0; i < WPs.Count; i++) { distance = Vector2.Distance(transform.position, WPs[i].transform.position); if (distance < 0.5f && WPs[i] != lastWp) { if (!WPs[i].neighborWps.Contains(lastWp)) { WPs[i].neighborWps.Add(lastWp); lastWp.neighborWps.Add(WPs[i]); } lastWp = WPs[i]; return(true); } } return(false); }
void makeWP() //Create a way point at the current location { if (!LinkWp()) { //if the cur position hasn't a way point, creat a new one GameObject newWP = new GameObject("wayPoint (" + WPs.Count + ")"); newWP.transform.position = transform.position; newWP.AddComponent <CircleCollider2D>(); newWP.GetComponent <CircleCollider2D>().radius = 1; newWP.AddComponent <wayPoint>(); newWP.GetComponent <wayPoint>().no = WPs.Count; newWP.tag = "wayPoints"; WPs.Add(newWP.GetComponent <wayPoint>()); GameObject.Find("GameManager").GetComponent <GameManager_Root>().WPs.Add(newWP.GetComponent <wayPoint>()); newWP.GetComponent <wayPoint>().neighborWps.Add(lastWp); lastWp.GetComponent <wayPoint>().neighborWps.Add(newWP.GetComponent <wayPoint>()); lastWp = newWP.GetComponent <wayPoint>(); } }
public void buildMaze() { GameObject.Find("GameManager").GetComponent <GameManager_Root>().noOfDots = 0; lastWp = WPs[3]; transform.position = (Vector2)WPs[1].transform.position + Vector2.up * 3; MoveAndBuilde(Vector2.right, 10); MoveAndBuilde(Vector2.down, 15); MoveAndBuilde(Vector2.left, 20); MoveAndBuilde(Vector2.up, 15); MoveAndBuilde(Vector2.right, 10); transform.position = WPs[1].transform.position; lastWp = WPs[1]; buildPath(20);//random build a path in random direction, repeit 20 times to build maze transform.position = WPs[2].transform.position; lastWp = WPs[2]; buildPath(15); makeWalls();//if there isn't a path, make walls full of the zone. Destroy(gameObject); }
void Awake() { instance = this; }