// Use this for initialization void Awake() { iController = this.GetComponent <InteractableController>(); nodeGrid = new NodeGrid[gridWidth, gridHeight]; gridActualWidth = gridWidth * nodeSize; gridActualHeight = gridHeight * nodeSize; // Generates nodeGrid array based on the height and width of the grid for (int i = 0; i < gridWidth; i++) { for (int j = 0; j < gridHeight; j++) { nodeGrid[i, j] = new NodeGrid(); nodeGrid[i, j].parentGrid = this; // Puts the node at the center of the square. nodeGrid[i, j].loc.x = (i * nodeSize) + (nodeSize / 2); nodeGrid[i, j].loc.y = (j * nodeSize) + (nodeSize / 2); } } currentPath = new List <NodeGrid>(); doneSearching = false; currentHighlightSquares = new List <GameObject>(); currentHighlightTimer = 0.0f; // iController.InitializeInteractables(); gridInitialized = true; DebugDrawGrid(); }
public bool DirectionIsOpen(Coord direction, bool cutCorners = false) { if (direction.x == 0 && direction.y == 0 && direction.z == 0) { return(true); } if (direction.IsDirection) { return(directionOpen[direction.ToDirectionIndex]); } if (DirectionIsOpen(new Coord(direction.x, 0, 0)) && DirectionIsOpen(new Coord(0, direction.y, 0)) && DirectionIsOpen(new Coord(0, 0, direction.z))) { return(true); } if (cutCorners) { Coord x = new Coord(direction.x, 0, 0); Coord y = new Coord(0, direction.y, 0); Coord z = new Coord(0, 0, direction.z); return(DirectionIsOpen(x) && NodeGrid.GetNode(position + x).DirectionIsOpen(y + z) || DirectionIsOpen(y) && NodeGrid.GetNode(position + y).DirectionIsOpen(x + z) || DirectionIsOpen(z) && NodeGrid.GetNode(position + z).DirectionIsOpen(x + y)); } return(false); }
private void InitGridGraph() { for (int x = 0; x <= NodeGrid.GetUpperBound(0); x++) { for (int y = 0; y <= NodeGrid.GetUpperBound(1); y++) { var upIndex = y + 1; var downIndex = y - 1; var rightIndex = x + 1; var leftIndex = x - 1; if (upIndex >= 0 && upIndex <= NodeGrid.GetUpperBound(1)) { NodeGrid[x, y].Up = NodeGrid[x, upIndex]; } if (downIndex >= 0 && downIndex <= NodeGrid.GetUpperBound(1)) { NodeGrid[x, y].Down = NodeGrid[x, downIndex]; } if (rightIndex >= 0 && rightIndex <= NodeGrid.GetUpperBound(0)) { NodeGrid[x, y].Right = NodeGrid[rightIndex, y]; } if (leftIndex >= 0 && leftIndex <= NodeGrid.GetUpperBound(0)) { NodeGrid[x, y].Left = NodeGrid[leftIndex, y]; } } } }
/// <summary> /// Used for setting up the data used for the pathfinding. /// </summary> /// <param name="firstPoint"></param> /// <param name="secondPoint"></param> /// <param name="nodeGrid"></param> public DijkstraPathFinding(double[] firstPoint, double[] secondPoint, NodeGrid nodeGrid) { //making the begin situasions of data used for finding a path: int nodesCount = nodeGrid.nodes.Count; prev = new List <Node>(); dist = new double[nodesCount]; for (int i = 0; i < dist.Count(); i++) { prev.Add(null); dist[i] = double.MaxValue; } unvisited = new List <Node>(); foreach (Node node in nodeGrid.nodes) { unvisited.Add(node); } this.nodeGrid = nodeGrid; //finding the nodes by the begin- and endpoint: for (int i = 0; i < nodesCount; i++) { if (nodeGrid.nodes[i].position.SequenceEqual(firstPoint)) { currentNode = unvisited[i]; dist[i] = 0; } if (nodeGrid.nodes[i].position.SequenceEqual(secondPoint)) { endNode = nodeGrid.nodes[i]; } } }
public Node(Node org) { ID = ALL; ++ALL; parent = org.parent; org.inps.ForEach(i => { Connector n = new Connector(i); n.index = inps.Count; n.onClick.AddListener(me => { connectionManagement(me); }); inps.Add(n); }); org.outs.ForEach(i => { Connector n = new Connector(i); n.index = outs.Count; n.onClick.AddListener(me => { connectionManagement(me); }); outs.Add(n); }); snapTo = org.snapTo; pos = org.pos; size = org.size; title = org.title; }
/* * Used to pass the grid data from AGrid to here */ public void SetGrid(NodeGrid nodeGrid) { grid = nodeGrid.Grid; gridWorldSize = nodeGrid.GridWorldSize; gridSizeX = nodeGrid.GridSizeX; gridSizeY = nodeGrid.GridSizeY; }
public void FillNodeMapWithWorldCoordinates(NodeGrid nodeGrid, MeshSettings meshSettings) { Vector2 bottomLeft; if (meshSettings.isCentered) { bottomLeft = new Vector2(-nodeGrid.width, -nodeGrid.height) * meshSettings.cellScale * 0.5f + new Vector2(meshSettings.mapOffsetX, -meshSettings.mapOffsetZ); } else { bottomLeft = new Vector2(meshSettings.mapOffsetX, -meshSettings.mapOffsetZ); } float vertexOffset = meshSettings.cellScale; Vector3 halfCell = new Vector3(vertexOffset, 0.0f, vertexOffset) * 0.5f; for (int y = 0; y < nodeGrid.height; y++) { for (int x = 0; x < nodeGrid.width; x++) { Vector3 cellOffset = new Vector3(x * meshSettings.cellScale, 0.0f, y * meshSettings.cellScale) + new Vector3(bottomLeft.x, 0.0f, bottomLeft.y); //! Cell center nodeGrid.values[x, y].pos = halfCell + cellOffset; } } }
Vector3[] RetracePath(Node startNode, Node endNode, NodeGrid grid) { List <Node> path = new List <Node>(); Node currentNode = endNode; if (startNode == endNode) { Vector3[] vec = new Vector3[1]; vec[0] = endNode.worldPos; return(vec); } while (currentNode != startNode) { path.Add(currentNode); currentNode = currentNode.parent; } grid.pathA = path; Vector3[] waypoints = SimplifyPath(path); grid.pathS = waypoints; Array.Reverse(waypoints); return(waypoints); }
private void Awake() { _rigidbody2D = GetComponent <Rigidbody2D>(); AngleDefinition = GetComponent <AngleDefinition>(); KillHandler = GetComponent <UnitKillHandler>(); _gridNode = FindObjectOfType <NodeGrid>(); }
public PathRequest(Vector3 _pathStart, Vector3 _pathEnd, NodeGrid _grid, Action <Vector3[], bool> _callback) { pathStart = _pathStart; pathEnd = _pathEnd; grid = _grid; callback = _callback; }
private void Awake() { nodeGrid = FindObjectOfType <NodeGrid>(); animator = GetComponent <Animator>(); animWait = new WaitForSeconds(animInterval); animPlay = new WaitForSeconds(animPlayTime); }
public void Load(NodeGrid grid) { if (File.Exists(Application.persistentDataPath + "/playerInfo.dat") && loading) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open); PlayerData data = (PlayerData)bf.Deserialize(file); file.Close(); grid.globalValues.defaultValues = data.resourceValues; grid.startingNodeValue = data.currentNodeValue; for (int i = 0; i < grid.nodes.Count; i++) { grid.nodes[i].animator.SetBool(DialogParameters.visitedString, data.visitedNodes[i]); } Item[] itemResources = Resources.LoadAll <Item>("") as Item[]; for (int i = 0; i < data.inventoryItemsNames.Length; i++) { for (int j = 0; j < itemResources.Length; j++) { if (itemResources[j].name == data.inventoryItemsNames[i]) { grid.inventory.AddItem(itemResources[j]); } } } } }
public void StartPath(NodeGrid destination, Func <bool, int> callback = null) { currentDestination = destination; currentCallback = callback; NodeGrid currentNode = grid.FindClosestNode(transform.position.x, transform.position.y); nodes = grid.FindPath( new Location(currentNode.loc.x, currentNode.loc.y), new Location(destination.loc.x, destination.loc.y)); if (nodes.Count > 1) { currentlyWalking = true; animator.Play("Walking"); nodeIndex = 0; grid.EmptyOutNode(currentNode.loc.x, currentNode.loc.y); // For now, choose the end node for which direction to face // Eventually, would be better to find next horizontal node that // forces a change in direction FaceTheRightWay(nodes[nodes.Count - 1].x); } else // Assuming already at destination { if (null != currentCallback) { currentCallback(true); } } }
public Node(NodeGrid parentGrid) { ID = ALL; ++ALL; parent = parentGrid; }
public Stack <Vector3Int> Algorithm(Vector3Int start, Vector3Int goal, NodeGrid grid) { _CurrentNode = grid.GetNode(start); _OpenList = new HashSet <Node>(); _ClosedList = new HashSet <Node>(); _OpenList.Add(_CurrentNode); Stack <Vector3Int> path = null; while (_OpenList.Count > 0 && path == null) { List <Node> neighbors = grid.FindNeighbors(_CurrentNode.Position); ExamineNeighbors(neighbors, _CurrentNode, goal, grid); UpdateCurrentTile(ref _CurrentNode); path = GeneratePath(_CurrentNode, start, goal); } AStarDebug.Instance?.CreateTiles(_OpenList, _ClosedList, grid, start, goal, path); return(path); }
void Start() { // A* dijkstra = new Dijkstra(width, height, depth, nodeSize, origin); // Create main camera Vector3 position = new Vector3(width * -6, height * 18, depth * -6); Instantiate(cam, position, cam.transform.rotation); grid = dijkstra.GetGrid(); // Add walls manually dijkstra.GetNode(2, 2, 2).SetWalkable(!dijkstra.GetNode(2, 2, 2).isWalkable); // Draw walls DrawWalls(); // Create visual nodes stepVisual.Setup(grid); // Show goal node Object.Instantiate(goalObject, grid.GetPosition(goalX, goalY, goalZ) + new Vector3(5, 5, 5), new Quaternion()); Object.Instantiate(goalObject, grid.GetPosition(startX, startY, startZ) + new Vector3(5, 5, 5), new Quaternion()); }
/***** Helper Functions *****/ public void updatePower(Vector3 worldPosition) { NodeGrid NodeGrid = NodeFromWorldPoint(worldPosition); NodeGrid.power += 1.0f; NodeGrid.hasEnemies = true; }
private void Awake() { _nodeGrid = FindObjectOfType <NodeGrid>(); AngleDefinition = GetComponent <AngleDefinition>(); KillHandler = GetComponent <UnitKillHandler>(); Transform = transform; }
public IEnumerator HarvestTest() { // load the test scene EditorSceneManager.LoadSceneInPlayMode ( "Assets/Resources/Scenes/Test_Scene.unity", new LoadSceneParameters(LoadSceneMode.Single, LocalPhysicsMode.Physics2D) ); yield return(TestUtilities.AssertSceneLoaded("Assets/Resources/Scenes/Test_Scene.unity")); Inventory inventory = Object.FindObjectOfType <Inventory>(); NodeGrid grid = Object.FindObjectOfType <NodeGrid>(); LoadingOrder loadingOrder = Object.FindObjectOfType <LoadingOrder>(); ItemType itemType = Resources.Load("SO/Potato") as ItemType; // wait till grid is loaded while (!grid.LoadedSection) { yield return(null); } // wait till all data is loaded while (!loadingOrder.LoadedAll) { yield return(null); } if (itemType == null) { Debug.LogError("There is no ItemType scriptable object at path: SO/Potato"); } // get the node we are going to plant on Node nodeToPlant = grid.GetNodeFromXY(0, 0); // plant the prefab bool succesfulPlanting = false; nodeToPlant.Interactable.OnInteract(ToolTypes.Hoe); nodeToPlant.Interactable.OnInteract(ToolTypes.Other, prefab, () => succesfulPlanting = true); Assert.IsTrue(succesfulPlanting); // get the single plant we made in this scene Plantable plant = Object.FindObjectOfType <Plantable>(); // Harvest the plant plant.Grow(completeGrowth); plant.OnHarvest(ToolTypes.Sickle, null); // check if the items dropped WorldItem[] worldItems = Object.FindObjectsOfType <WorldItem>(); Assert.IsTrue(worldItems.Length >= 2 && worldItems.Length <= 5); yield return(null); // plant object should be destroyed Assert.IsTrue(plant == null); }
void Start() { path = gameObject.GetComponent <Pathfinding>(); grid = GameObject.Find("node grid").GetComponent <NodeGrid>(); currentHealth = maxHealth; delay = 0; pathingDelay = 0; }
public Node(NodeGrid <Node> grid, int x, int y, int z) { this.grid = grid; this.x = x; this.y = y; this.z = z; isWalkable = true; }
void Awake() { instance = this; nodeDiameter = nodeRadius * 2; gridSizeX = Mathf.RoundToInt(gridWorldSize.x / nodeDiameter); gridSizeY = Mathf.RoundToInt(gridWorldSize.y / nodeDiameter); CreateGrid(); }
void Start() { if (!grid) { grid = FindObjectOfType <NodeGrid>(); } _path = new List <Node>(); }
void Start() { currentRoom = GameInfo.GetNewRoom(currentRoom); //transform.position = currentRoom.position; grid = FindObjectOfType <NodeGrid>(); path = new List <Node>(); FindPath(oldPosition, currentRoom.position); }
public void SetUp() { currNodeGrid = new GameObject().AddComponent <NodeGrid>(); // all tests should work even if the location of the section is changed currNodeGrid.transform.position = new Vector2(Random.Range(-100, 100), Random.Range(-100, 100)); currNodeGrid.LoadSectionTest(); }
private void Awake() { nodeGrid = FindObjectOfType <NodeGrid>(); canvas = FindObjectOfType <Canvas>(); audioManager = FindObjectOfType <AudioManager>(); InitTools(); EquippedTool = tools[ToolTypes.Hand]; }
public void Build() { BuildVertices(); BuildIndices(); BuildNormals(); CalculateTextureWeights(); CopyToBuffers(); nodeGrid = new NodeGrid(this); }
public void SetUp() { currNodeGrid = new GameObject().AddComponent <NodeGrid>(); currNodeGrid.transform.position = Vector3.zero; currNodeGrid.LoadSectionTest(); currRequestManager = currNodeGrid.gameObject.AddComponent <PathRequestManager>(); }
private void Start() { _Grid = GetComponent <NodeGrid>(); if (_Grid == null) { Debug.LogError("Grid is null. Cannot make paths."); } }
public Vector3 spawnPosition; // the position where the ghost started in this scene private void Start() { nodeGridReference = GetComponent <NodeGrid>(); spriteRenderer = GetComponent <SpriteRenderer>(); animator = GetComponent <Animator>(); boxCollider2D = GetComponent <BoxCollider2D>(); spawnPosition = transform.position; }