public bool NavToRandom(ShipRuntime targetShip) { this.targetShip = targetShip; // Enter this state from a cell pos, no previous path exists (idle roam from absolute cell pos) curPath = null; bool success = false; Vector2Int pos = targetShip.GetRandomNavigateableCellPos(out success, WallState.Down); if (!success) { return(false); } if (!Navigate( targetShip, new Vector2Int(Mathf.RoundToInt(transform.localPosition.x / 3.2f), Mathf.RoundToInt(transform.localPosition.y / 3.2f)), pos)) { return(false); } return(true); }
public void Init(ShipRuntime targetShip) { this.targetShip = targetShip; transform.SetParent(targetShip.transform); bool success = false; Vector2Int pos = targetShip.GetRandomNavigateableCellPos(out success, WallState.Down); if (success) { transform.localPosition = new Vector3(pos.x, pos.y, 0) * 3.2f; } // Idle job jobController.SetIdleJob(new JobPlayerCrewIdle("Idle", new object[] { jobController, navigationController, targetShip })); }
public void NavToRandom(ShipRuntime targetShip) { bool success = false; Vector2Int pos = targetShip.GetRandomNavigateableCellPos(out success); if (success) { Navigate( targetShip, new Vector2Int(Mathf.RoundToInt(transform.localPosition.x / 3.2f), Mathf.RoundToInt(transform.localPosition.y / 3.2f)), pos); } else { StartCoroutine("DelaySearch"); } }
public void Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos) { Init(); this.targetShip = targetShip; if (globalStartPos == globalEndPos) { StartCoroutine("DelaySearch"); return; } nextIndex = 1; CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos); CellTemplate endCell = targetShip.GetCellByGlobalPos(globalEndPos); // Invalid path supplied if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0) { return; } ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos); ShipPiece endPiece = targetShip.GetPieceByGlobalCellPos(globalEndPos); NavGrid startGrid = new NavGrid(startPiece); startGrid.Generate(); NavGrid goalGrid = startGrid; if (startPiece != endPiece) { goalGrid = new NavGrid(endPiece); goalGrid.Generate(); } AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos); int newTTmp = 0; curPath = aStarAlgorithm.AStarSearch(ref newTTmp); wiggleTimer = Random.Range(0, 6.28318f); }
public JobPlayerCrewNavigateToCellFromMousePos(string name, params object[] requiredObjects) : base(name, requiredObjects) { characterNav = requiredObjects[0] as CharacterNavigation; targetShip = requiredObjects[1] as ShipRuntime; }
public bool Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos) { if (globalStartPos == globalEndPos) { return(false); } NavCell[] overrideStart = null; // Cur, prev and T evaluation if (curPath != null) { if (nextIndex < curPath.Length) { overrideStart = new NavCell[2]; // Store C0 and C1 overrideStart[0] = curPath[nextIndex - 1]; overrideStart[1] = curPath[nextIndex]; } } nextIndex = 1; CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos); CellTemplate endCell = targetShip.GetCellByGlobalPos(globalEndPos); // Invalid path supplied if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0) { return(false); } ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos); ShipPiece endPiece = targetShip.GetPieceByGlobalCellPos(globalEndPos); NavGrid startGrid = new NavGrid(startPiece); startGrid.Generate(); NavGrid goalGrid = startGrid; if (startPiece != endPiece) { goalGrid = new NavGrid(endPiece); goalGrid.Generate(); } AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos); int newTState = -1; curPath = aStarAlgorithm.AStarSearch(ref newTState, overrideStart); // Either reset, invert or leave move timer switch (newTState) { case -1: moveTimer = 0; break; case 1: moveTimer = 1 - moveTimer; break; } pathTracer.SetPositions(GetNavArray()); return(curPath != null); }
public JobPlayerCrewIdle(string name, params object[] requiredObjects) : base(name, requiredObjects) { jobController = requiredObjects[0] as JobController; characterNav = requiredObjects[1] as CharacterNavigation; targetShip = requiredObjects[2] as ShipRuntime; }
// Start is called before the first frame update void Start() { /* * * CREATE A NEW SHIP, POPULATE WITH PIECES AND CONNECTIONS * */ GameObject newShipGO = new GameObject("newShipObject"); // DemoShip ship = newShipGO.AddComponent <ShipRuntime>(); ship.InitShip(database, true); // Add demo pieces ship.AddNewPiece(5, new Vector2Int(0, 0)); if (ship.QueryAddNewPiece(1, new Vector2Int(1, 0))) { ship.AddNewPiece(1, new Vector2Int(1, 0)); } if (ship.QueryAddNewPiece(4, new Vector2Int(3, 0))) { ship.AddNewPiece(4, new Vector2Int(3, 0)); } if (ship.QueryAddNewPiece(3, new Vector2Int(4, -1))) { ship.AddNewPiece(3, new Vector2Int(4, -1)); } if (ship.QueryAddNewPiece(1, new Vector2Int(6, -1))) { ship.AddNewPiece(1, new Vector2Int(6, -1)); } // Set up fwd and bkwd connections on pieces ship.ConnectPiecesByIndex(0, 1, new Vector2Int(0, 0), new Vector2Int(0, 0)); ship.ConnectPiecesByIndex(1, 2, new Vector2Int(1, 0), new Vector2Int(0, 0)); ship.ConnectPiecesByIndex(2, 3, new Vector2Int(0, 0), new Vector2Int(0, 1)); ship.ConnectPiecesByIndex(3, 4, new Vector2Int(1, 0), new Vector2Int(0, 0)); GameObject stationGO = new GameObject("newShipObject"); // DemoShip station = stationGO.AddComponent <ShipRuntime>(); station.InitShip(database); station.AddNewPiece(6, new Vector2Int(0, 0), true); //station.AddNewPiece(5, new Vector2Int(0, 0)); /* * * ASSIGN DEMO CHARACTER TO SHIP AND TEST NAVIGATION FUNCTIONALITY * */ // Do worms GameObject[] chars = GameObject.FindGameObjectsWithTag("Characters"); foreach (GameObject character in chars) { PlayerCrewAI charAI = character.GetComponent <PlayerCrewAI>(); charAI.Init(ship); } // Do worms GameObject[] worms = GameObject.FindGameObjectsWithTag("Worm"); for (int i = 0; i < worms.Length; ++i) { Worm wormComponent = worms[i].GetComponent <Worm>(); wormComponent.NavToRandom(station); wormComponent.transform.SetParent(station.transform); } stationGO.transform.position = new Vector3(-15, 0, 0); ship.transform.position = new Vector3(15, 0, 0); }