Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma strict
- import System.Collections.Generic; //Using unity lists //Syntax highlighting comes from copy/ paste
- //var structs : GameObject[];
- //structs = GameObject.FindGameObjectsWithTag("Static");
- var player : Transform; //Set in inspector to player
- var playGoal : boolean; //A boolean checking if the player's position is the goal
- var mon : Transform; //Set in inspector to monster
- var currentNode : node; //A dynamic coordinate object, used for whenever I need to access the node the monster is standing on
- var nextNode : node; //A dynamic coordinate object, used for whenever I need to access the next node the monster will be standing on
- var targetNode : node; //Used when monster is actually moving, the sub-main goal
- var target : node; //Used when monster is actually moving, a goal in a series of goals between two sub-main goals
- var nodeLeft : node;
- var nodeRight : node;
- var nodeFor : node;
- var nodeBack : node;
- var nodeForLeft : node;
- var nodeForRight : node;
- var nodeBackLeft : node;
- var nodeBackRight : node;
- var bestScore : node;
- var tempScore : node;
- //var currentPoly : Transform; //I was (and still might) navmap it to an extent, this will allow me to access the polygon the monster is standing on
- var goals : List.<node> = new List.<node>(); //A list of the main position goals of the monster
- var nodeList : List.<node> = new List.<node>(); //A list of all the nodes in an area
- var neighborNodes : List.<node> = new List.<node>(); //A list later containing nodes surrounding monster's current position
- var openSet : List.<node> = new List.<node>(); //Open set of nodes (explored, viable)
- var closedSet : List.<node> = new List.<node>(); //Closed set of nodes (explored, not viable)
- var lockedSet : List.<node> = new List.<node>(); //Explored, unusable (blocked)
- var maxEyeRange : int; //How far the monster can see with no obstructions
- var rightEye : boolean; //A placeholder in the code to make sure the monster can see out of his right eye
- var leftEye : boolean; //A placeholder in the code to make sure the monster can see out of his left eye
- var lineOfSight : int; //How far the monster can currently see
- var subCounter : int; //A counter to keep track of sub nodes
- var mainCounter : int; //A counter to keep track of overhead nodes
- function Start(){ //Assign all the previously created variables
- subCounter = 0;
- mainCounter = 0;
- leftEye = true;
- rightEye = true;
- lineOfSight = 5;
- maxEyeRange = 5;
- playGoal = false;
- currentNode = new node(player.position);
- InvokeRepeating("checkSight", 0, 1); //Start checking sightline immediately every second
- yield layerMap(); //And begin to create the main goals
- }
- function checkSight(){
- if(leftEye && rightEye){ //If both of the monster's eyes are functioning
- var sight : Vector3 = Vector3(mon.position.x + maxEyeRange, mon.position.y, mon.position.z); //Check the spot 5 meters directly ahead of him
- if(Physics.Linecast(mon.position, sight)){ //If the spot hits
- lineOfSight = Mathf.Abs(sight.x - mon.position.x); //Reduce his line of sight
- }
- }
- }
- function layerMap() : IEnumerator{
- while(Vector3.Distance(mon.position, currentNode.getPos()) > maxEyeRange){ //If he can't see the end goal
- //print(currentNode.getPos());
- goals.Add(currentNode); //Put that goal in the goal list
- currentNode.setPos(Vector3(((mon.position.x + currentNode.getPos().x) / 2), currentNode.getPos().y, ((mon.position.z + currentNode.getPos().z) / 2))); //Next goal is halfway between them
- //Note: I was using recursion, but according to research on Unity forums, function calls cost more memory than loops - each call must be placed onto the stack
- }
- goals.Reverse(); //Reverse the list's order so to not have to access them backwards (before reverse - last goal--->third goal--->second goal--->first goal)
- refineMap(new node(mon.position), goals[0]); //After the large goals have been set, fill in the smaller spaces
- }
- function refineMap(currentNode : node, goal : node) : IEnumerator{ //Params are the node the monster is standing on and where he wants to get to next
- //while(Mathf.RoundToInt(currentNode.getPos().x) != Mathf.RoundToInt(goal.getPos().x) && Mathf.RoundToInt(currentNode.getPos().z) != Mathf.RoundToInt(goal.getPos().z)){ //if the mapping has reached the edge of the monster's vision, begin movement along pathways
- while(Vector3.Distance(currentNode.getPos(), goal.getPos()) >= 0.1){
- //print("loop" + " " + Vector3.Distance(currentNode.getPos(), goal.getPos()));
- //print(new Vector3(Mathf.RoundToInt(currentNode.getPos().x), 0, Mathf.RoundToInt(currentNode.getPos().z)) + " " + new Vector3(Mathf.RoundToInt(goal.getPos().x), 0, Mathf.RoundToInt(goal.getPos().z)));
- nodeLeft.setPos(Vector3(currentNode.getPos().x, currentNode.getPos().y, currentNode.getPos().z + 0.1)); //]
- nodeLeft.setCost(Vector3.Distance(goal.getPos(), nodeLeft.getPos()) + Vector3.Distance(nodeLeft.getPos(), currentNode.getPos()));//Heuristic - Distance to goal + distance between nodes
- //print("Left: " + nodeLeft.getPos() + " " + nodeLeft.getCost());
- nodeRight.setPos(Vector3(currentNode.getPos().x, currentNode.getPos().y, currentNode.getPos().z - 0.1)); //]
- nodeRight.setCost(Vector3.Distance(goal.getPos(), nodeRight.getPos()) + Vector3.Distance(nodeRight.getPos(), currentNode.getPos()));
- //print("Right: " + nodeRight.getPos() + " " + nodeRight.getCost());
- nodeFor.setPos(Vector3(currentNode.getPos().x + 0.1, currentNode.getPos().y, currentNode.getPos().z)); //]
- nodeFor.setCost(Vector3.Distance(goal.getPos(), nodeLeft.getPos()) + Vector3.Distance(nodeFor.getPos(), currentNode.getPos()));
- //print("For: " + nodeFor.getPos() + " " + nodeFor.getCost());
- nodeBack.setPos(Vector3(currentNode.getPos().x - 0.1, currentNode.getPos().y, currentNode.getPos().z)); //] Grab the next 8 nodes
- nodeBack.setCost(Vector3.Distance(goal.getPos(), nodeBack.getPos()) + Vector3.Distance(nodeBack.getPos(), currentNode.getPos()));
- //print("Back: " + nodeBack.getPos() + " " + nodeBack.getCost());
- nodeForLeft.setPos(Vector3(currentNode.getPos().x + 0.1, currentNode.getPos().y, currentNode.getPos().z + 0.1)); //] In a decagon of directions
- nodeForLeft.setCost(Vector3.Distance(goal.getPos(), nodeForLeft.getPos()) + Vector3.Distance(nodeForLeft.getPos(), currentNode.getPos()));
- //print("ForLeft: " + nodeForLeft.getPos() + " " + nodeForLeft.getCost());
- nodeForRight.setPos(Vector3(currentNode.getPos().x + 0.1, currentNode.getPos().y, currentNode.getPos().z - 0.1)); //] Around the node just traveled to
- nodeForRight.setCost(Vector3.Distance(goal.getPos(), nodeForRight.getPos()) + Vector3.Distance(nodeForRight.getPos(), currentNode.getPos()));
- //print("ForRight: " + nodeForRight.getPos() + " " + nodeForRight.getCost());
- nodeBackLeft.setPos(Vector3(currentNode.getPos().x - 0.1, currentNode.getPos().y, currentNode.getPos().z + 0.1)); //]
- nodeBackLeft.setCost(Vector3.Distance(goal.getPos(), nodeBackLeft.getPos()) + Vector3.Distance(nodeBackLeft.getPos(), currentNode.getPos()));
- //print("BackLeft: " + nodeBackLeft.getPos() + " " + nodeBackLeft.getCost());
- nodeBackRight.setPos(Vector3(currentNode.getPos().x - 0.1, currentNode.getPos().y, currentNode.getPos().z - 0.1)); //]
- nodeBackRight.setCost(Vector3.Distance(goal.getPos(), nodeBackRight.getPos()) + Vector3.Distance(nodeBackRight.getPos(), currentNode.getPos()));
- //print("BackRight: " + nodeBackRight.getPos() + " " + nodeBackRight.getCost());
- neighborNodes.Add(nodeLeft);
- neighborNodes.Add(nodeRight);
- neighborNodes.Add(nodeFor); //] Put all the possible next nodes
- neighborNodes.Add(nodeBack);
- neighborNodes.Add(nodeForLeft);
- neighborNodes.Add(nodeForRight); //] Into the "neighborNodes" list
- neighborNodes.Add(nodeBackLeft);
- neighborNodes.Add(nodeBackRight);
- //for(var iter : int = neighborNodes.Count-1; iter >= 0; iter--){
- // if(openSet.Contains(neighborNodes[iter]) || closedSet.Contains(neighborNodes[iter])){
- // neighborNodes[iter].setCost(-1);
- // }
- //}
- //var neighborCopy : List.<node> = neighborNodes;
- for(var iter = neighborNodes.Count-1; iter >= 0; iter--){
- for(var loop1 = openSet.Count-1; loop1 >= 0; loop1--){
- if(openSet[loop1].getPos() == neighborNodes[iter].getPos()){
- //print("REMOVE111: " + neighborNodes[iter].getPos() + " " + neighborNodes[iter].getCost());
- neighborNodes.Remove(neighborNodes[iter]);
- iter -= 0.5;
- }
- }
- if(iter != 0){
- if(1 % iter > 1){
- iter += 0.5;
- }
- }
- for(var loop2 = closedSet.Count-1; loop2 >= 0; loop2--){
- if(closedSet[loop2].getPos() == neighborNodes[iter].getPos()){
- //print("REMOVE222: " + neighborNodes[iter].getPos() + " " + closedSet[loop2].getPos());
- neighborNodes.Remove(neighborNodes[iter]);
- iter -= 0.5;
- }
- }
- if(iter != 0){
- if(1 % iter > 1){
- iter += 0.5;
- }
- }
- for(var loop3 = lockedSet.Count-1; loop3 >= 0; loop3--){
- if(lockedSet[loop3].getPos() == neighborNodes[loop3].getPos()){
- print("REMOVE333: " + neighborNodes[iter].getPos() + " " + neighborNodes[iter].getCost());
- neighborNodes.Remove(neighborNodes[iter]);
- }
- }
- }
- bestScore.setCost(Mathf.Infinity); //Set the compare score to the maximum value
- for(iter = 0; iter < neighborNodes.Count; iter++){ //For every element left in the "neighborNodes" list
- if(neighborNodes[iter].getCost() < bestScore.getCost()){ //Check for the best score
- bestScore.setPos(neighborNodes[iter].getPos());
- bestScore.setCost(neighborNodes[iter].getCost());
- }
- }
- //print("LIST CONTENTS111111: ");
- //for(var item : int = 0; item < openSet.Count; item++){
- // print(openSet[item].getPos());
- //}
- //print("END111111");
- openSet.Add(new node(bestScore)); //Add the best scoring node
- //print("BEST SCORE POS: " + bestScore.getPos() + " BEST SCORE COST: " + bestScore.getCost());
- //print("LIST CONTENTS222222: ");
- //for(var item2 : int = 0; item2 < openSet.Count; item2++){
- // print(openSet[item2].getPos());
- //}
- //print("END222222");
- neighborNodes.Remove(bestScore);
- for(iter = 0; iter < neighborNodes.Count; iter++){
- closedSet.Add(new node(neighborNodes[iter])); //Add all the non-best score nodes to the closed set
- }
- neighborNodes.Clear(); //Clear the list for the next loop
- currentNode = bestScore;
- //print(currentNode.getPos() + " " + currentNode.getCost() + "CURRENTNODE");
- if(playGoal){
- goal.setPos(player.position);
- }
- yield;
- }
- travelPath();
- }
- function travelPath(){
- print("CONTENTS: ");
- for(var item : int = 0; item < openSet.Count; item++){
- print(openSet[item].getPos());
- }
- targetNode = goals[mainCounter];
- if(openSet[openSet.Count-1].getPos().x != targetNode.getPos().x){
- openSet[openSet.Count-1].setPos(targetNode.getPos());
- }
- print("TARGETTT: " + targetNode.getPos());
- while(Vector3.Distance(mon.position, targetNode.getPos()) > 0.1){
- target = openSet[subCounter];
- mon.LookAt(targetNode.getPos());
- //mon.Rotate(-90);
- //mon.localRotation.eulerAngles = Vector3(0,rotation,0);
- //while(mon. <= Mathf.Atan((target.getPos().z - mon.position.z) / (target.getPos().x - mon.position.x)) * (180 / Mathf.PI)){
- print(Vector3.Distance(mon.position, target.getPos()));
- while(Vector3.Distance(mon.position, target.getPos()) > 0.1){
- mon.Translate(0, 0, Time.deltaTime * 6);
- print(mon.position + " " + target.getPos());
- yield;
- }
- print("LOOP MAIN");
- subCounter++;
- yield;
- }
- print("BREAK BREAK BREAK BREAK BREAK");
- mainCounter++;
- refineMap(targetNode, goals[mainCounter]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement