private void FixedUpdate() { Vector2Int pos = new Vector2Int(Mathf.FloorToInt(transform.position.x), Mathf.FloorToInt(transform.position.z)); if (grid.CheckIsInGrid(pos)) { _rigidbody.AddForce(grid.GetCell(pos).FlowFieldOrientation *Acceleration); } _rigidbody.velocity = Vector3.ClampMagnitude(_rigidbody.velocity, MaxSpeed); }
private void Update() { if (Move) { Vector2Int posOnGrid = new Vector2Int(Mathf.FloorToInt(transform.position.x), Mathf.FloorToInt(transform.position.z)); if (Grid.CheckIsInGrid(posOnGrid)) { Rigidbody.AddForce(Grid.GetCell(posOnGrid).FlowFieldOrientation); } } Rigidbody.velocity = Vector3.ClampMagnitude(Rigidbody.velocity, MaxSpeed); }
private void Update() { RaycastHit hit; if (Physics.Raycast(_camera.ScreenPointToRay(Input.mousePosition), out hit)) { Vector2Int selectedCellT = Grid.GetCellByWorldPos(hit.point); Vector2Int selectedCell; if (selectedCellT != new Vector2Int(-1, -1)) { selectedCell = selectedCellT; _cursorPos = Grid.GetCellCenterWorldPosByCell(selectedCell); if (Input.GetButtonDown("Fire1")) { Grid.Cells[selectedCell.x, selectedCell.y].AddMoveValue(10); } if (Input.GetButtonDown("Fire2")) { Grid.GetCell(selectedCell).Iswall = true; Grid.Cells[selectedCell.x, selectedCell.y].AddMoveValue(500); } if (Input.GetButtonDown("Jump")) { Grid.originePos = selectedCell; Grid.CalculateFlowField(); } if (Input.GetKey("a")) { MoveActor MA = Instantiate(prefabCube, hit.point + new Vector3(0, 0.5f, 0), quaternion.identity); MA.grid = Grid; } } } _cursor.position = Vector3.Lerp(_cursor.position, _cursorPos, cursorSmoothFactor * Time.deltaTime); }
public static void CalculatFlowField(PlayGrid grid, Vector2Int origin) { List <Vector2Int> OpenList = new List <Vector2Int>(); List <Vector2Int> temporalToAdd = new List <Vector2Int>(); foreach (Cell cell in grid.Cells) { cell.SetMoveValue(int.MaxValue); } OpenList.Add(origin); grid.GetCell(origin).SetMoveValue(0); while (OpenList.Count > 0) { foreach (Vector2Int cell in OpenList) { foreach (Vector2Int neibors in grid.GetNeibors(cell)) { if ((neibors - cell).magnitude > 1) { if (grid.GetCell(neibors).MoveValue > grid.GetCell(cell).MoveValue + 14 + grid.GetCell(neibors).IndividualMoveValue) { grid.GetCell(neibors).SetMoveValue(grid.GetCell(cell).MoveValue + 14 + grid.GetCell(neibors).IndividualMoveValue); temporalToAdd.Add(neibors); Vector2Int oriantation = cell - neibors; grid.GetCell(neibors).SetFlowFieldVector(new Vector3(oriantation.x, 0, oriantation.y)); } } else { if (grid.GetCell(neibors).MoveValue > grid.GetCell(cell).MoveValue + 10 + grid.GetCell(neibors).IndividualMoveValue) { grid.GetCell(neibors).SetMoveValue(grid.GetCell(cell).MoveValue + 10 + grid.GetCell(neibors).IndividualMoveValue); temporalToAdd.Add(neibors); Vector2Int oriantation = cell - neibors; grid.GetCell(neibors).SetFlowFieldVector(new Vector3(oriantation.x, 0, oriantation.y)); } } } } OpenList.Clear(); OpenList.AddRange(temporalToAdd); temporalToAdd.Clear(); } /* OpenList.Add(origin); * do * { * List<Vector2Int> temporalToRemouve = new List<Vector2Int>(); * List<Vector2Int> temporalTAdd = new List<Vector2Int>(); * foreach (Vector2Int pos in OpenList) * { * Vector2Int testPos = pos + new Vector2Int(1, 0); * if (CheckCell(grid, testPos, closeList)) grid.GetCell(testPos).calculatMoveValue(10); temporalTAdd.Add(testPos); * testPos = pos + new Vector2Int(-1, 0); * if (CheckCell(grid, testPos, closeList)) grid.GetCell(testPos).calculatMoveValue(10); temporalTAdd.Add(testPos); * testPos = pos + new Vector2Int(0, 1); * if (CheckCell(grid, testPos, closeList)) grid.GetCell(testPos).calculatMoveValue(10); temporalTAdd.Add(testPos); * testPos = pos + new Vector2Int(0, -1); * if (CheckCell(grid, testPos, closeList)) grid.GetCell(testPos).calculatMoveValue(10); temporalTAdd.Add(testPos); * testPos = pos + new Vector2Int(1, 1); * if (CheckCell(grid, testPos, closeList)) grid.GetCell(testPos).calculatMoveValue(14); temporalTAdd.Add(testPos); * testPos = pos + new Vector2Int(-1, 1); * if (CheckCell(grid, testPos, closeList)) grid.GetCell(testPos).calculatMoveValue(14); temporalTAdd.Add(testPos); * testPos = pos + new Vector2Int(1, -1); * if (CheckCell(grid, testPos, closeList)) grid.GetCell(testPos).calculatMoveValue(14); temporalTAdd.Add(testPos); * testPos = pos + new Vector2Int(-1, -1); * if (CheckCell(grid, testPos, closeList)) grid.GetCell(testPos).calculatMoveValue(14); temporalTAdd.Add(testPos); * temporalToRemouve.Add(pos); * } * * foreach (Vector2Int pos in temporalToRemouve) { * closeList.Add(pos); * OpenList.Remove(pos); * } * OpenList.AddRange(temporalTAdd); * * * } while (OpenList.Count>0);*/ }