private void Start() { var center = Vector3.zero; hexCalc = new HexCalculator(); hexCoordinate = new HexCoordinate(RingSize - 1, (RingSize - 1) * 2); var proguression = hexCalc.CalcProgression(0, 6, RingSize); var hexNum = proguression[proguression.Length - 1] + 1; hices = new Hex[hexNum]; perform = new HexPerform(this); shake = GetComponent <HexShake>(); for (int i = 0; i < hexNum; i++) { var obj = Instantiate(HexObject, transform); obj.name = "obj" + i; obj.transform.localPosition = hexCalc.PositionFromIndex(i, center, 87, proguression); // obj.transform.SetParent(transform); hices[i] = obj.GetComponent <Hex>(); hices[i].Point = hexCalc.CalcCoordinate(i, proguression); hices[i].SetHexMaster(this); hices[i].ID = i; hexCoordinate.SetHex(hices[i]); } for (int i = 0; i < hexNum; i++) { hices[i].SetContacted(hexCoordinate.GetContactedHex(hices[i].Point)); } deck = GetComponent <DeckManager>(); }
private HexCellData CreateCell(int row, int col) { Vector3 position; if (row % 2 == 0) { position.x = HexConst.innerRadius + col * HexConst.innerRadius * 2f; } else { position.x = col * HexConst.innerRadius * 2f; } position.y = 0f; position.z = -row * HexConst.outerRadius * 1.5f; HexCell cell = Instantiate <HexCell>(cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; HexCoordinate pos = new HexCoordinate(row, col); HexCellData data = new HexCellData(); data.SetPos(pos); cell.SetData(data); Cells[row, col] = cell; return(data); }
//instantiates a hex cell with parent and location and coordinates void CreateCell(int x, int z, int i) { //position Vector3 position; position.x = (x + z * 0.5f - (z / 2)) * (HexMetrics.innerRadius * 2f); position.y = 0f; position.z = z * (HexMetrics.outerRadius * 1.5f); //gives position a cell with the needed parent and position HexCell cell = cells[i] = Instantiate <HexCell>(cellPrefab); cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.coordinates = HexCoordinate.FromOffsetCoordinates(x, z); cell.color = defaultColor; //gives coordinates Text label = Instantiate <Text>(cellLabelPrefab); label.rectTransform.SetParent(gridCanvas.transform, false); label.rectTransform.anchoredPosition = new Vector2(position.x, position.z); label.text = cell.coordinates.ToStringSeperateLines(); }
public void RemoveFogOfWar(HexCoordinate worldCoordinate) { if (_db.Hexes.HexExists(worldCoordinate)) { _db.Hexes.UpdateFogOfWar(worldCoordinate, TileId.NoFogOfWar); } }
static HexCoordinate Parse(string line) { Queue <char> tokens = new Queue <char>(line); HexCoordinate hex = new HexCoordinate(0, 0, 0); while (tokens.Count > 0) { char token = tokens.Dequeue(); switch (token) { case 'e': hex = hex.Add(East); break; case 'w': hex = hex.Add(West); break; case 'n': token = tokens.Dequeue(); hex = hex.Add(token == 'w' ? NorthWest : NorthEast); break; case 's': token = tokens.Dequeue(); hex = hex.Add(token == 'w' ? SouthWest : SouthEast); break; default: throw new FormatException("Invalid hex string"); } } return(hex); }
public int CompareTo(object obj) { HexCoordinate other = obj as HexCoordinate; Vector2 cOther = other.coordinate; if (coordinate.x == cOther.x) { return(0); } else if (cOther.y == coordinate.y) { if (coordinate.x % 2 == 0) { return(1); } else { return(-1); } } else if (coordinate.y > cOther.y) { return(-1); } else { return(1); } }
//TopLeftCoordinate must be divisible by 3 or other calculations may screw up public static HexCoordinate ConvertToValidTopLeftCoordinate(HexCoordinate oldCoordinate) { var x = ClosestNumber(oldCoordinate.X, 2); var y = ClosestNumber(oldCoordinate.Y, 2); return(new HexCoordinate(x, y)); }
private void ClearPlayerIcons(HexCoordinate worldCoordinate) { if (_db.Hexes.HexExists(worldCoordinate)) { _db.Hexes.ClearPlayerIcons(worldCoordinate); } }
public void ShouldGetAndSetIndexer() { HexMap <HexCoordinate> map; HexCoordinate coordinate; map = new HexMap <HexCoordinate>(5); for (int r = 0; r <= 5; r++) { for (int i = 0; i < HexMap.GetCount(r); i++) { coordinate = new HexCoordinate(r, i); map[coordinate] = coordinate; } } for (int r = 0; r <= 5; r++) { for (int i = 0; i < HexMap.GetCount(r); i++) { coordinate = new HexCoordinate(r, i); Assert.AreEqual(coordinate, map[coordinate]); Assert.AreEqual(coordinate, map[coordinate.Index]); } } }
public void ShouldReturnNeighbours() { HexCoordinate[] ng; // Radius = 0 ng = new HexCoordinate(0, 0).GetNeighbours().ToArray(); Assert.AreEqual(6, ng.Length); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 0))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 1))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 2))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 3))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 4))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 5))); // Axis Aligned cell ng = new HexCoordinate(2, 8).GetNeighbours().ToArray(); Assert.AreEqual(6, ng.Length); Assert.AreEqual(true, ng.Contains(new HexCoordinate(2, 7))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(2, 9))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 4))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(3, 12))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(3, 11))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(3, 13))); //*/ // Not aligned cell ng = new HexCoordinate(2, 9).GetNeighbours().ToArray(); Assert.AreEqual(6, ng.Length); Assert.AreEqual(true, ng.Contains(new HexCoordinate(2, 8))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(2, 10))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 4))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(1, 5))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(3, 13))); Assert.AreEqual(true, ng.Contains(new HexCoordinate(3, 14))); //*/ }
public IEnumerator <object> PlayerMove() { Debug.Log("Move has been clicked"); HexCoordinate Phex = (HexCoordinate)player.GetLocation(); Debug.Log(player.MoveRemaining()); while (player.MoveRemaining() > 0) { while (Input.GetMouseButtonDown(0) == false) { Debug.Log("Please Work" + Input.GetMouseButtonDown(0)); yield return(null); } Debug.Log("MouseHasBeenClicked"); HexCoordinate hex = MousePostion(); if (!GameManager._instance.gameState.gameBoard.CanCreateRoom(hex)) //Checks if room is there { if (GameManager._instance.gameState.gameBoard.canMove(Phex, hex)) //Room is there, check if there is a valid door { Debug.Log("I'm Moving from " + Phex.ToString() + " To " + hex.ToString()); player.MovePlayer(hex); } } else //create tile { if (GameManager._instance.gameState.gameBoard.hasNeighbor(hex)) //Checks if tile has valid neighbor { Debug.Log("NewTilePlz"); GameManager._instance.gameState.gameBoard.CreateRoom(hex); player.MoveToZero(); } } yield return(new WaitForEndOfFrame()); } }
public void ShouldHaveCorrectDirectionIndex() { HexCoordinate coord; for (int t = 0; t < 6; t++) { coord = new HexCoordinate(1, t); Assert.AreEqual(t, coord.DirectionIndex); } coord = new HexCoordinate(0, 5); Assert.AreEqual(0, coord.DirectionIndex); coord = new HexCoordinate(2, 0); Assert.AreEqual(0, coord.DirectionIndex); coord = new HexCoordinate(2, 1); Assert.AreEqual(0, coord.DirectionIndex); coord = new HexCoordinate(2, 2); Assert.AreEqual(1, coord.DirectionIndex); coord = new HexCoordinate(2, 3); Assert.AreEqual(1, coord.DirectionIndex); coord = new HexCoordinate(2, 4); Assert.AreEqual(2, coord.DirectionIndex); coord = new HexCoordinate(2, 5); Assert.AreEqual(2, coord.DirectionIndex); coord = new HexCoordinate(2, 6); Assert.AreEqual(3, coord.DirectionIndex); coord = new HexCoordinate(2, 7); Assert.AreEqual(3, coord.DirectionIndex); coord = new HexCoordinate(2, 8); Assert.AreEqual(4, coord.DirectionIndex); coord = new HexCoordinate(2, 9); Assert.AreEqual(4, coord.DirectionIndex); coord = new HexCoordinate(2, 10); Assert.AreEqual(5, coord.DirectionIndex); coord = new HexCoordinate(2, 11); Assert.AreEqual(5, coord.DirectionIndex); coord = new HexCoordinate(2, -1); Assert.AreEqual(5, coord.DirectionIndex); coord = new HexCoordinate(2, -2); Assert.AreEqual(5, coord.DirectionIndex); coord = new HexCoordinate(2, -3); Assert.AreEqual(4, coord.DirectionIndex); coord = new HexCoordinate(2, -4); Assert.AreEqual(4, coord.DirectionIndex); }
public void ShouldJumpUpTransform() { HexCoordinate[] coords; coords = new HexCoordinate(0, 0).JumpUpTransform().ToArray(); Assert.AreEqual(6, coords.Length); Assert.IsTrue(coords.Contains(new HexCoordinate(1, 0))); Assert.IsTrue(coords.Contains(new HexCoordinate(1, 1))); Assert.IsTrue(coords.Contains(new HexCoordinate(1, 2))); Assert.IsTrue(coords.Contains(new HexCoordinate(1, 3))); Assert.IsTrue(coords.Contains(new HexCoordinate(1, 4))); Assert.IsTrue(coords.Contains(new HexCoordinate(1, 5))); coords = new HexCoordinate(2, 9).JumpUpTransform().ToArray(); Assert.AreEqual(2, coords.Length); Assert.IsTrue(coords.Contains(new HexCoordinate(3, 13))); Assert.IsTrue(coords.Contains(new HexCoordinate(3, 14))); coords = new HexCoordinate(3, 12).JumpUpTransform().ToArray(); Assert.AreEqual(3, coords.Length); Assert.IsTrue(coords.Contains(new HexCoordinate(4, 15))); Assert.IsTrue(coords.Contains(new HexCoordinate(4, 16))); Assert.IsTrue(coords.Contains(new HexCoordinate(4, 17))); }
public void UpdateLocation(HexCoordinate partyLocation) { Db.Update(TableName, new[] { CurrentMapCornerX, CurrentMapCornerY }, new object[] { partyLocation.X, partyLocation.Y }, new[] { Id }, new object[] { 1 }); }
public List <List <HexCoordinate> > GetRingsSurroundingHex(HexCoordinate coordinate, int maxRings) { // reference: http://gamedev.stackexchange.com/questions/51264/get-ring-of-tiles-in-hexagon-grid // int ring = 1 // Travel around the ring by traversing N,SE,S,SW,W,NW,N,NE multiplied by the ring number // ring++ // Travel Around ring again // cont until desired ring... var hexRings = new List <List <HexCoordinate> >(); // Add in the current hex to the list var currentHex = new List <HexCoordinate>(); currentHex.Add(coordinate); hexRings.Add(currentHex); // Now go through and add the other rings while (hexRings.Count <= maxRings) { hexRings.Add(coordinate.GetRingAroundHex(hexRings.Count)); } return(hexRings); }
private void MoveWindow(Direction direction, int distance) { if (_lockControls) { return; } _selectedCoordinate = null; switch (direction) { case Direction.North: imgHexMap.UpdateVerticalOffset(-distance); break; case Direction.East: imgHexMap.UpdateHorizontalOffset(distance); break; case Direction.South: imgHexMap.UpdateVerticalOffset(distance); break; case Direction.West: imgHexMap.UpdateHorizontalOffset(-distance); break; } //Save the coordinate, so we will be here next time we open the program _db.Session.UpdateLocation(imgHexMap.TopLeftCoordinate); //Draw the new map DrawMap(); }
private void SelectHex(HexCoordinate hexWorldCoordinate) { if (_selectedCoordinate != null) //Redraw the previously selected hex (deselect) if there was a previously selected hex { var oldSelectedCoordinate = _selectedCoordinate; _selectedCoordinate = null; //We set the SelectedCoordinate to null, so that hexMapFactory knows it is not selected _drawingHandler.RedrawSelectedHex(oldSelectedCoordinate); } _selectedCoordinate = hexWorldCoordinate; //Mark the new hex as selected _drawingHandler.RedrawSelectedHex(_selectedCoordinate); //Update the textfield with the hex's detail (if any) Hex hex = _db.Hexes.GetForCoordinate(hexWorldCoordinate); if (hex != null) { txtDetail.Text = hex.Detail; } else { txtDetail.Text = ""; } lblCoordinates.Text = _selectedCoordinate.ToText(); }
public void Remove(HexCoordinate c) { var position = GetPosition(c); for (var i = 0; i < positions.Count; i += 2) { if (position == positions[i]) // Check if we the removed position is at the beginning of the chain { if (positions[i + 1] == positions[i]) // We found a chain of length 1 { positions.RemoveRange(i, 2); } else { positions[i] = position + 1; } } else if (position == positions[i + 1]) // Check if the removed position is at the end of the chain { positions[i + 1] = position - 1; } else if (positions[i] < position && position < positions[i + 1]) // The removed position is in the middle of the chain { positions.Insert(i + 1, position + 1); positions.Insert(i + 1, position - 1); } else { continue; } break; } }
private void ClearTerrain(HexCoordinate worldCoordinate) { if (_db.Hexes.HexExists(worldCoordinate)) { _db.Hexes.ClearTerrain(worldCoordinate); } }
private void InitializeGrids() { float positionX; float positionY; Vector2 coordinate; Vector2 position; for (var x = 0; x < matrix.x; x++) { positionX = x * 0.7f; for (var y = 0; y < matrix.y; y++) { if (x % 2 == 0) { positionY = -0.8f * y; } else { positionY = -0.4f + (-0.8f * y); } coordinate = new Vector2(x, y); position = new Vector2(positionX, positionY); var hexCoordinate = new HexCoordinate(coordinate, position); Hexagon hexagon = CreateHexagon(hexCoordinate); hexagons.Add(hexCoordinate, hexagon); } } }
public int DistanceTo(HexCoordinate other) { return (((x < other.x ? other.x - x : x - other.x) + (Y < other.Y ? other.Y - Y : Y - other.Y) + (z < other.z ? other.z - z : z - other.z)) / 2); }
public override bool Equals(object obj) { if (obj == null) return false; HexCoordinate o = (HexCoordinate)obj; return ((FirstCoord == o.FirstCoord) && (SecondCoord == o.SecondCoord)); }
public HexCell GetCellFromPosition(Vector3 point) { HexCoordinate hexCoord = HexCoordinate.FromPosition(transform.InverseTransformPoint(point)); int index = hexCoord.X + hexCoord.Z * mapWidth + hexCoord.Z / 2; return(cells[index]); }
protected override void DrawHex(Graphics graphics, Hex hex, int alpha = 0) { HexCoordinate positionOnVisibleMap = hex.Coordinate.Minus(UiInterface.GetMapBox().TopLeftCoordinate); Point positionOnScreen = PositionManager.HexToScreen(positionOnVisibleMap); var pictureLocationAndSize = new Rectangle(positionOnScreen, new Size(50, 44)); if (hex.RiverSections.Count == 0) { return; } else if (hex.RiverSections.Count == 1) { var river = hex.RiverSections[0]; DrawSection(graphics, river, pictureLocationAndSize); } //else if (hex.RiverSections.Count == 2 && hex.RiverSections[0].Type == hex.RiverSections[1].Type) //{ // graphics.DrawArc(); //} else { foreach (var riverSection in hex.RiverSections) { DrawSection(graphics, riverSection, pictureLocationAndSize); } } }
public static HexCoordinate GetTopLeftCoordinateToCenter(HexCoordinate centerCoordinate, Rectangle mapArea) { var newTopLeftCorner = centerCoordinate.Minus(new HexCoordinate(mapArea.Width / 2, mapArea.Height / 2)); var adjustedTopLeftCorner = ConvertToValidTopLeftCoordinate(newTopLeftCorner); return(adjustedTopLeftCorner); }
static HexagonBoard() { randomNumbers = new Dictionary <Player, long[, ]>(); neighborLookup = new HexCoordinate[BOARD_WIDTH, BOARD_WIDTH][]; var blackRandomNumbers = new long[BOARD_WIDTH, BOARD_WIDTH]; var whiteRandomNumbers = new long[BOARD_WIDTH, BOARD_WIDTH]; var random = new Random(1); // always use the same seed for deterministic results var byteBuffer = new byte[64]; for (var r = -BOARD_RADIUS; r <= BOARD_RADIUS; r++) { var q_start = Math.Max(-BOARD_RADIUS - r, -BOARD_RADIUS); var q_end = Math.Min(BOARD_RADIUS - r, BOARD_RADIUS); for (var q = q_start; q <= q_end; q++) { // Generate random numbers for zobris hashing var arrayPos = new HexCoordinate(r + BOARD_RADIUS, q + BOARD_RADIUS); random.NextBytes(byteBuffer); blackRandomNumbers[arrayPos.R, arrayPos.Q] = Math.Abs(BitConverter.ToInt64(byteBuffer, 0)); random.NextBytes(byteBuffer); whiteRandomNumbers[arrayPos.R, arrayPos.Q] = Math.Abs(BitConverter.ToInt64(byteBuffer, 0)); // Generate valid neighbors for faster access var pos = new HexCoordinate(r, q); neighborLookup[arrayPos.R, arrayPos.Q] = pos.GetNeighborsClockwise().Where(x => x.Distance(new HexCoordinate(0, 0)) <= BOARD_RADIUS).ToArray(); } } randomNumbers.Add(Player.Black, blackRandomNumbers); randomNumbers.Add(Player.White, whiteRandomNumbers); }
public static Rectangle ScreenAreaToHexArea(HexCoordinate globalTopLeft, Size screenSize) { var localBottomRight = ScreenToHex(screenSize.Width, screenSize.Height); //We want global for the location, and local for the width/height return(new Rectangle((int)globalTopLeft.X, (int)globalTopLeft.Y, (int)localBottomRight.X, (int)localBottomRight.Y)); }
/// <summary> /// returns the hfexcoordinate the mouse is at /// </summary> /// <returns></returns> public HexCoordinate MousePostion() { Vector2 pos = Input.mousePosition; Ray ray = Camera.main.ScreenPointToRay(pos); //Create a plane to represent our game grid Plane plane = new Plane(Vector3.forward, 0.0f); //Do a raycast to find on the ray where we intersect with our game board float d; plane.Raycast(ray, out d); //Check if we collided if (d > 0) { //Get the position along the ray that we collided Vector3 hitPoint = ray.GetPoint(d); //Return the HexCoordinate Conversion HexCoordinate hex = HexCoordinate.GetHexPositionFromWorld(hitPoint); Debug.Log(hex); return(hex); } else { Debug.LogError("Unable to decect mouse click on game board."); return(new HexCoordinate(0, 0)); } }
private List <HexCoordinate> GetEnclosedStones(HexCoordinate stoneCord, Player targetPlayer) { var opponent = targetPlayer.GetOpponent(); // Use Stack instead of Queue because depth first is MUCH faster, as we first try to move directly to the border var openList = new Stack <HexCoordinate>(); var closedList = new HashSet <HexCoordinate>(); openList.Push(stoneCord); closedList.Add(stoneCord); while (openList.Count > 0) { var next = openList.Pop(); if (next.TwiceLength() == HexagonBoard.BOARD_RADIUS * 2) { return(null); } var neighbors = Board.GetNeighbors(next); for (var i = 0; i < neighbors.Length; i++) { var neighbor = neighbors[i]; if (Board[neighbor] == opponent || closedList.Contains(neighbor)) { continue; } openList.Push(neighbor); closedList.Add(neighbor); } } return(closedList.ToList()); }
public void UpdatePartyLocation(HexCoordinate partyLocation) { _partyLocation = partyLocation; _db.Party.UpdateLocation(partyLocation); RedrawPartyLocation(); }
/// <summary> /// Get a specific neighbor /// </summary> /// <param name="direction">The direction the neighbor is in</param> /// <returns>Hextile in the specified direction, if it exists</returns> public HexTile GetNeighbor(HexCoordinate direction) { string hex_name = string.Format("{0}/{1},{2}", transform.parent.name, HexCoord.Q + direction.Q, HexCoord.R + direction.R); if (GameObject.Find(hex_name)) return GameObject.Find(hex_name).GetComponent<HexTile>(); else return null; }
public void GenerateBattleField() { //foreach (Transform t in HexPrefab.transform) // t.localScale = new Vector3(1 / sqrt3div2, 1, 1); HexPrefab.GetComponent<Transform>().localScale = new Vector3(columnWidth, 1, rowWidth); //var halfWay = (columns - 1) * 0.5f; var halfField = columnsPer * 0.5f; var halfRows = -(rows - .5f) * 0.5f; var middleField = 1; PlayerTarget.Translate(halfField, 0, halfRows); MiddleTarget.Translate(halfField * 2 + middleField, 0, halfRows); EnemyTarget.Translate(halfField * 2 + middleField * 2 + halfField, 0, halfRows); for (int c = 0; c < columns; c++) { for (int r = 0; r < rows; r++) { var go = Instantiate(HexPrefab); go.transform.Translate(columnWidth * c, 0, -rowWidth * r); if (c % 2 == 1) go.transform.Translate(0, 0, -rowWidth * 0.5f); if (c < columnsPer) go.transform.SetParent(Player.transform, true); else if (c == columnsPer) go.transform.SetParent(Middle.transform, true); else if (c > columnsPer) go.transform.SetParent(Enemy.transform, true); var script = go.GetComponent<HexScript>().SetCoord(HexCoord.AtOffset(r, c)); Hexes.Add(script.Coordinate, script); } } var firstHex = new HexCoordinate(Hexes.Keys.First()); var lastHex = new HexCoordinate(Hexes.Keys.Last()); foreach (var h in Hexes.Values) h.SetNeighbours(firstHex, lastHex); }
/// <summary> /// Sets direction coordinates /// </summary> /// <param name="is_even">Is the tile an "even" tile?</param> public void SetDirections(bool is_even) { Directions[0] = new HexCoordinate(0, 1); Directions[1] = new HexCoordinate(1, 1); Directions[2] = new HexCoordinate(1, 0); Directions[3] = new HexCoordinate(0, -1); Directions[4] = new HexCoordinate(-1, 0); Directions[5] = new HexCoordinate(-1, 1); if(is_even) { Directions[1].R--; Directions[2].R--; Directions[4].R--; Directions[5].R--; } }
public void SetNeighbours(HexCoordinate firstHex, HexCoordinate lastHex) { m_Neighbours = new List<HexScript>(); foreach (var n in m_HexCoord.Neighbors().Where(x => x.IsWithinRectangle(firstHex.HexCoord, lastHex.HexCoord))) m_Neighbours.Add(BattleFieldManager.Hexes[n]); }
public MeshRenderer MeshRenderer; //Reference to tile's mesh #endregion Fields #region Methods /// <summary> /// Copy coordinates and neighbor coordinates of another tile /// </summary> /// <param name="other">Tile to copy</param> public void CopyTile(HexTile other) { HexCoord = new HexCoordinate(other.HexCoord.Q, other.HexCoord.R); for(int i = 0; i < other.Directions.Length; i++) Directions[i] = new HexCoordinate(other.Directions[i].Q, other.Directions[i].R); }