public void PlayerMovement() { if (Input.GetKey("up")) { if (Node_PlayerIsOn.GetComponent <Script_Node>().NodeUp != null) { Player_Movment = true; Node_MovingTo = Node_PlayerIsOn.GetComponent <Script_Node>().NodeUp; } } if (Input.GetKey("down")) { if (Node_PlayerIsOn.GetComponent <Script_Node>().NodeDown != null) { Player_Movment = true; Node_MovingTo = Node_PlayerIsOn.GetComponent <Script_Node>().NodeDown; } } if (Input.GetKey("left")) { if (Node_PlayerIsOn.GetComponent <Script_Node>().NodeLeft != null) { Player_Movment = true; Node_MovingTo = Node_PlayerIsOn.GetComponent <Script_Node>().NodeLeft; } } if (Input.GetKey("right")) { if (Node_PlayerIsOn.GetComponent <Script_Node>().NodeRight != null) { Player_Movment = true; Node_MovingTo = Node_PlayerIsOn.GetComponent <Script_Node>().NodeRight; } } }
// Update is called once per frame void Update() { Player_Speed_Delta = Player_Speed * Time.deltaTime; if (transform.position == Node_MovingTo.transform.position) { Player_Movment = false; Node_PlayerIsOn = Node_MovingTo; } if (Node_PlayerIsOn.Enum_NodeType == Script_Node.NodeTypes.EncounterNode) { GameManager.SwitchToBattle(); Node_PlayerIsOn.SetNodeType(Script_Node.NodeTypes.BasicNode); } if (Node_PlayerIsOn.Enum_NodeType == Script_Node.NodeTypes.ShopNode) { SceneManager.LoadScene(1); } if (Player_Movment == true) { OverworldMovement(); } if (Player_Movment == false) { PlayerMovement(); } }
public List <Script_Node> GetLocationOfPath(Script_Node p_endNode) { List <Script_Node> _pathList = new List <Script_Node>(); GetCurrentPathRecursive(p_endNode, _pathList); _pathList.Reverse(); return(_pathList); }
private Script_Node GetCurrentPathRecursive(Script_Node p_node, List <Script_Node> p_pathList) { p_pathList.Add(p_node); if (p_node.GetParent() != null) { return(GetCurrentPathRecursive(p_node.GetParent(), p_pathList)); } else { return(p_node); } }
public Script_Node(Vector3Int p_position, Vector3Int p_destination, Script_Node p_parent) { _position = p_position; _destination = p_destination; _parent = p_parent; if (p_parent != null) { _gScore = p_parent.GetGScore(); } _gScore = 0; _hScore = 0; CalculateHScore(); _fScore = 0; CalculateFScore(); }
public void FindPath(Vector3Int p_startPos, Vector3Int p_endPos) { Script_Node start = new Script_Node(p_startPos, p_endPos, null); Script_Node _destination = _pathfinding.CalculatePath(start); if (_destination != null) { _pathList = _pathfinding.GetLocationOfPath(_destination); if (FoundPath()) { foreach (Script_Node node in _pathList) { GameObject pathObject = CreatePathObject(node.GetNodePosition()); _pathObjects.Add(pathObject); } } } }
public Script_StarChaser(Script_GameManager p_manager, Script_Grid p_grid, Vector3Int p_position) { _meshObjectName = "ObjectStarChaser"; _meshObjectColor = new Color(1.0f, 1.0f, 1.0f); _meshObjectScale = new Vector3(1.0f, 1.0f, 1.0f); _nodeCollisionRange = 0.1f; _targetNode = null; _pathList = new List <Script_Node> (); _gridPosition = p_position; _manager = p_manager; _grid = p_grid; _speed = 1.0f; _position = (Vector3)p_position; _startLocation = p_position; _chasingState = new Script_StateChasingStar(_grid, _manager, this); _sellingState = new Script_StateSellStar(_grid, _manager, this); _restingState = new Script_StateRest(_grid, _manager, this); _doNothingState = new Script_StateDoNothing(_manager, this); _stateManager = new Script_Statemachine(); _doNothingState.SetNextState(StateIDs.CHASINGFALLENSTAR); _chasingState.SetNextState(StateIDs.SELLINGFALLENSTAR); _sellingState.SetNextState(StateIDs.RESTING); _restingState.SetNextState(StateIDs.DOINGNOTHING); _stateManager.AddState(StateIDs.CHASINGFALLENSTAR, _chasingState); _stateManager.AddState(StateIDs.SELLINGFALLENSTAR, _sellingState); _stateManager.AddState(StateIDs.RESTING, _restingState); _stateManager.AddState(StateIDs.DOINGNOTHING, _doNothingState); _stateManager.SetState(StateIDs.DOINGNOTHING); _nextState = StateIDs.DOINGNOTHING; CreateGameObject(); }
public Script_Node GetLowestFScoreOpenDictionary() { int fScore = int.MaxValue; Script_Node lowestFScoreNode = null; foreach (Script_Node node in _openDictionary.Values) { if (fScore > node.GetFScore()) { lowestFScoreNode = node; fScore = node.GetFScore(); } } if (lowestFScoreNode != null) { return(lowestFScoreNode); } return(null); }
public void WalkAlongPath() { Vector3 targetPosition = (Vector3)_targetNode.GetNodePosition(); if (Vector3.Distance(_position, targetPosition) < _nodeCollisionRange) { if (_pathList.IndexOf(_targetNode) != _pathList.Count - 1) { _targetNode = _pathList [_pathList.IndexOf(_targetNode) + 1]; } else { _meshObject.transform.position = targetPosition; } } else { Vector3 nodePosition = _targetNode.GetNodePosition(); _meshObject.transform.position += (nodePosition - _meshObject.transform.position).normalized * _speed * Time.deltaTime; } }
public Script_Node CalculatePath(Script_Node p_initialNode) { //Based on pseudoCode found here: https://www.raywenderlich.com/3016-introduction-to-a-pathfinding AddOpenListElementAtPosition(p_initialNode.GetNodePosition().x, p_initialNode.GetNodePosition().z, p_initialNode); while (_openDictionary.Values.Count > 0) { Script_Node currentNode = GetLowestFScoreOpenDictionary(); AddClosedListElementAtPosition(currentNode.GetNodePosition().x, currentNode.GetNodePosition().z, currentNode); RemoveOpenListElementAtPosition(currentNode.GetNodePosition().x, currentNode.GetNodePosition().z); foreach (Script_Node node in _closedDictionary.Values) { if (node.GetNodePosition().x == node.GetNodeDestination().x&& node.GetNodePosition().z == node.GetNodeDestination().z) { return(node); } } CalculateNeighbours(currentNode); } return(null); }
public void SetParent(Script_Node p_parent) { _parent = p_parent; }
private void CalculateNeighbours(Script_Node p_node) { int xCurrent = p_node.GetNodePosition().x; int zCurrent = p_node.GetNodePosition().z; int xMin = xCurrent; int xMax = xCurrent; int zMin = zCurrent; int zMax = zCurrent; for (int x = xCurrent - 1; x <= xCurrent; x++) { if (x < 0) { continue; } xMin = x; break; } for (int x = xCurrent + 1; x >= xCurrent; x--) { if (x >= _grid.GetWidth()) { continue; } xMax = x; break; } for (int z = zCurrent - 1; z <= zCurrent; z++) { if (z < 0) { continue; } zMin = z; break; } for (int z = zCurrent + 1; z >= zCurrent; z--) { if (z >= _grid.GetHeight()) { continue; } zMax = z; break; } for (int z = zMin; z <= zMax; z++) { for (int x = xMin; x <= xMax; x++) { if (_grid.AccessGridTile(x, z).GetWalkable() == true && !_closedDictionary.ContainsKey(z * _grid.GetWidth() + x)) { if ((x != xCurrent && z != zCurrent)) { if (_grid.AccessGridTile(x, z + (zCurrent - z)) != null && _grid.AccessGridTile(x, z + (zCurrent - z)).GetWalkable() == false) { continue; } if (_grid.AccessGridTile(x + (xCurrent - x), z) != null && _grid.AccessGridTile(x + (xCurrent - x), z).GetWalkable() == false) { continue; } } int tempGScore = 0; if (x == xCurrent || z == zCurrent) { tempGScore = Constants.linearMovementCost; } else { tempGScore = Constants.diagonalMovementCost; } if (_openDictionary.ContainsKey(z * _grid.GetWidth() + x)) { Script_Node node = GetOpenListElement(x, z); if (node.GetParent() != null) { tempGScore += p_node.GetGScore(); } if (tempGScore < node.GetGScore()) { node.SetGScore(tempGScore); node.CalculateFScore(); node.SetParent(p_node); } } if (!_openDictionary.ContainsKey(z * _grid.GetWidth() + x)) { Vector3Int location = new Vector3Int(x, 0, z); Script_Node newNode = new Script_Node(location, p_node.GetNodeDestination(), p_node); tempGScore += newNode.GetParent().GetGScore(); newNode.SetGScore(tempGScore); newNode.CalculateHScore(); newNode.CalculateFScore(); AddOpenListElementAtPosition(x, z, newNode); } } } } }
private void AddClosedListElementAtPosition(int p_x, int p_z, Script_Node p_elementToAdd) { _closedDictionary.Add(p_z * _grid.GetWidth() + p_x, p_elementToAdd); }
public void SetPathBeginning(Script_Node p_path) { _targetNode = p_path; }