/// <summary> /// Constructor /// </summary> /// <param name="q">column</param> /// <param name="r">row</param> /// <param name="h">height</param> public HexCell(int q, int r, int h) { this.q = q; this.r = r; this.h = h; this.centerPos = HexConst.HexToWorldCoord(q, r, h); }
/// <summary> /// Step towards the player along a path /// Assumes pathToPlayer is not null /// </summary> /// <returns>Returns true when move action has finished</returns> public bool MoveToPlayer() { //If the monster is already adjacent to the player if (curPInd == pathToPlayer.Count - 2) { //don't move more return(true); } //Get the move destination by converting it's hex coordinates to a world position Vector3 movementDest = HexConst.HexToWorldCoord(pathToPlayer[movementSpeed][0], pathToPlayer[movementSpeed][1], pathToPlayer[movementSpeed][2]) + new Vector3(0, 0.8f, 0); //If this monster has reached the target destination if ((transform.position - movementDest).magnitude < 0.2f) { //Set the transform position to equal the destination transform.position = movementDest; //Return true since movement has ended return(true); } //Get the 'next waypoint' to move towards. This is simply the next cell in the path's world coordinates Vector3 nextWaypoint = HexConst.HexToWorldCoord(pathToPlayer[curPInd + 1][0], pathToPlayer[curPInd + 1][1], pathToPlayer[curPInd + 1][2]) + new Vector3(0, 0.8f, 0); //Step towards the waypoint transform.position = Vector3.MoveTowards(transform.position, nextWaypoint, realtimeSpeed * Time.deltaTime); //If next waypoint has been reached, move on to the next one if ((transform.position - nextWaypoint).magnitude < 0.2f) { curPInd++; } //If logic has made it this far, movement is not over. return(false); }
/// <summary> /// Create player figure if there isnt already one /// </summary> void spawnPlayerFigure() { if (!playerFigure) { playerFigure = (GameObject)Instantiate(playerFigurePrefab, transform.position, transform.rotation); playerFigure.transform.SetParent(gameObject.transform); //Subtract the player position figurePositionHex = HexConst.CoordToHexIndex(new Vector3(playerFigure.transform.position.x - transform.parent.position.x, playerFigure.transform.position.y - transform.parent.position.y, playerFigure.transform.position.z - transform.parent.position.z)); } else { playerFigure.transform.position = HexConst.HexToWorldCoord(player.q, player.r, player.h) + uiControllerCenterPos; } }
/// <summary> /// Unity draw gizmos function. This is just a debug to draw pathfinding data in the world /// </summary> void OnDrawGizmos() { //If the path exists if (pathToPlayer != null) { Gizmos.color = Color.cyan; //Loop through the path and draw a line between all cells to form a path. for (int i = 1; i < pathToPlayer.Count; i++) { Vector3 curCoords = HexConst.HexToWorldCoord(pathToPlayer[i][0], pathToPlayer[i][1], pathToPlayer[i][2]) + new Vector3(0, 1, 0); Vector3 prevCoords = HexConst.HexToWorldCoord(pathToPlayer[i - 1][0], pathToPlayer[i - 1][1], pathToPlayer[i - 1][2]) + new Vector3(0, 1, 0); Gizmos.DrawLine(curCoords, prevCoords); } } }
private static void MenuSnapToGrid() { //Loop through all gameobjects (this could be really clunky) //TODO: Find a better way of getting hex cells foreach (Transform transform in Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable)) { //If the object's transform is named "HexCell", it is a snap-able hex cell if (transform.name.Contains("HexCell")) { float modelHeight = transform.gameObject.GetComponent <Renderer>().bounds.size.y; //Get the hex coordinates the object is overlapping with int[] hexCoords = HexConst.CoordToHexIndex(transform.position + new Vector3(0, modelHeight / 2, 0)); //Move this object to the specific grid-aligned coordinates. transform.position = HexConst.HexToWorldCoord(hexCoords[0], hexCoords[1], hexCoords[2]); transform.position = transform.position - new Vector3(0, modelHeight / 2, 0); } } }
/// <summary> /// Scales the controller position to world UI coordinates ans snaps to a grid /// </summary> /// <param></param> /// <returns></returns> void convertControllerPosition() { Vector3 scaledControllerPosition = new Vector3(controller.transform.parent.transform.localPosition.x, controller.transform.parent.transform.localPosition.y - 1, controller.transform.parent.transform.localPosition.z); scaledControllerPosition = scaledControllerPosition * 50; int[] figurePositionHex = HexConst.CoordToHexIndex(scaledControllerPosition); Vector3 newPosition = HexConst.HexToWorldCoord(figurePositionHex [0], figurePositionHex [1], figurePositionHex [2]); if (levelController.levelGrid.GetHex(figurePositionHex [0], figurePositionHex [1], figurePositionHex [2]) != null) { drawLine(newPosition * 0.02f, Color.green); createDestinationObject(newPosition); } else { //Destroy (instantiatedHolographicFigure); drawLine(newPosition * 0.02f, Color.red); } }
/// <summary> /// Enable/Disable the renderer this will eventually become on/off for the minimap /// </summary> public void setVisibility(bool visible) { //Show UI if (visible) { //Get cells in a radius UICell[] toDisplay = uiGrid.TopDownRadius(player.q, player.r, player.h, 8, true); //Display them all foreach (UICell c in toDisplay) { c.Display(true); } //Center the minimap Vector3 translationVec = HexConst.HexToWorldCoord(player.q, player.r, player.h) + uiControllerCenterPos - uiGrid[player.q, player.r, player.h].gameObject.transform.position; gameObject.transform.position += translationVec; //Show player figure spawnPlayerFigure(); foreach (MeshRenderer r in playerFigure.GetComponentsInChildren <MeshRenderer>()) { r.enabled = true; } //Hide UI } else { //Hide all UICells foreach (UICell c in uiGrid) { c.Display(false); } //Hide player figure foreach (MeshRenderer r in playerFigure.GetComponentsInChildren <MeshRenderer>()) { r.enabled = false; } } }
/// <summary> /// Called by a canvas button when minimap is open, Moves the player to the hex corresponding to the player figure /// </summary> public void doMove() { //Subtract the player position Vector3 scaledFigurePosition = new Vector3(playerFigure.transform.position.x - transform.parent.position.x, playerFigure.transform.position.y - transform.parent.position.y, playerFigure.transform.position.z - transform.parent.position.z); //Scale up the position from the miniature to full scale and reset the y axis scaledFigurePosition = scaledFigurePosition * 50; scaledFigurePosition.y = scaledFigurePosition.y - 50; //Convert the position into hex coordinates and move the player int[] figurePositionHex = HexConst.CoordToHexIndex(scaledFigurePosition); if (levelController.levelGrid.GetHex(figurePositionHex[0], figurePositionHex[1], figurePositionHex[2]) != null) { print("There is a Hex Here"); Vector3 newPosition = HexConst.HexToWorldCoord(figurePositionHex[0], figurePositionHex[1], figurePositionHex[2]); GameObject.FindGameObjectWithTag("Player").transform.position = newPosition; ClearCells(); } else { Debug.LogError("OH NO!! THERE IS NO HEX WHERE THE PLAYER FIGURE IS!!" + "q: " + figurePositionHex[0] + "r: " + figurePositionHex[1] + "h: " + figurePositionHex[2]); } }