private void Awake() { rigidbody = GetComponent <Rigidbody2D>(); animator = GetComponent <Animator>(); renderer = GetComponent <SpriteRenderer>(); environment = GameObject.FindObjectOfType <Environment>(); player = GameObject.FindObjectOfType <PlayerMovement>(); health = GetComponent <GhostHealth>(); eyes = transform.GetChild(0); eyePosition = eyes.localPosition; regularPattern = regularMovement.GetMovementPattern(); vulnerablePattern = vulnerableMovement.GetMovementPattern(); }
protected Vector2 FindNextPathLocation(GhostMovementPattern pattern) { Vector2 targetLocation = pattern.CalculateTarget(transform, environment, player.gameObject); //TODO: optimize to reduce garbage colleciton MapPathNode start = new MapPathNode(environment.GetClosetNode(transform.position)); MapPathNode target = new MapPathNode(environment.GetClosetNode(targetLocation)); start.score = 0; SortedList <Vector2, MapPathNode> closedSet = new SortedList <Vector2, MapPathNode>(new Vector2Comparer()); SortedList <float, MapPathNode> openSet = new SortedList <float, MapPathNode>(new MapPathNodeComparer()); openSet.Add(start.fScore, start); while (openSet.Count > 0) { MapPathNode current = openSet.Values[0]; if (current == target) { lastPath = current; MapPathNode check = current; MapPathNode lastBefore = current; while (check.cameFrom != null) { lastBefore = check; check = lastBefore.cameFrom; } Vector2 result = lastBefore.node.Value.location; if (result.x <= environment.min.x || result.x >= environment.max.x || result.y <= environment.min.y || result.y >= environment.max.y) { result = (Vector2)transform.position + (lastBefore.direction * speed); } return(result); } openSet.RemoveAt(0); closedSet.Add(current.node.Value.location, current); for (int i = 0; i < 4; ++i) { Node?child = current.node.Value.connections[i]; if (child == null || closedSet.ContainsKey(child.Value.location)) { continue; } MapPathNode addChild = new MapPathNode(child.Value); float tenitiveScore = 1.0f + current.score; int index = openSet.IndexOfValue(addChild); if (index >= 0) { if (tenitiveScore >= openSet.Values[index].score) { continue; } else { addChild = openSet.Values[index]; openSet.RemoveAt(index); } } addChild.score = tenitiveScore; addChild.cameFrom = current; addChild.fScore = pattern.CalculateFScore(targetLocation, addChild.node.Value.location, addChild.score); addChild.direction = movementDirections[i]; openSet.Add(addChild.fScore, addChild); } } return(start.node.Value.location); }