Sprite AddBoardCell (HexCoord c) { var texture = Resources.Textures.Tile; var textureHeight = texture.GetHeight(); float scale = 2f / textureHeight; var s = new Sprite { Texture = texture, Scale = new Vector2(scale, scale), ZIndex = (int)ZLayers.Background, Position = c.Position(), Modulate = BoardColor }; board.AddChild(s); var overlay = new Sprite { Texture = Resources.Textures.BoardGradient, Scale = new Vector2(scale, scale), ZIndex = (int)ZLayers.Background+1, Position = c.Position() }; board.AddChild(overlay); return s; }
public void ShowCursor(HexCoord coord) { if (!hexCursor.Visible) { hexCursor.Position = coord.Position(); hexCursor.Visible = true; } else { snapTween.InterpolateProperty(hexCursor, "position", null, coord.Position(), 0.1f, Tween.TransitionType.Cubic, Tween.EaseType.Out, 0); snapTween.Start(); } }
// Find the hex that overlaps a certain x,y point public void HighlightHex(HexCoord hex) { Vector2 hexPos = hex.Position(); GameObject hexDisp = null; //Brute force method to get hex display associated with a certain HexCoord int i = 0; float smallestDist = Mathf.Infinity; foreach (Transform child in HexDisplays.transform) { float dist = Vector2.SqrMagnitude(hexPos - (Vector2)child.position); if (dist < smallestDist) { smallestDist = dist; hexDisp = child.gameObject; } i++; } //Highlight the hex if (hexDisp != null) { hexDisp.GetComponent <HexDisplayControl>().Highlight(true); } }
//Moves piece to target hex. Used for undoing moves and moving piece back to start position. Checks for beetle stacking at target hex void SnapToHex(HexCoord hex) { Vector3 snapPos = hex.Position(); snapPos.z = 3.0f; //Move up high first to avoid collisions with other pieces this.transform.position = snapPos; //Snap to x,y position of target hex //this.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f)); //Snap to default rotation //For beetles the target hex might be occupied. In that case, fix the z distance and don't allow gravity. Also make sure it's a beetle that's coming from the board. If coming from the dugout then treat as a normal piece if (specificBehavior.type == BoardManager.UnitType.Beetle && specificBehavior.active == BoardManager.Active.Board) { if (boardManager.beetleDict[hex] > 0) //Check if there was a stacked beetle at target hex { Vector3 temp = this.transform.position; int numBeetles = boardManager.beetleDict[hex]; temp.z = 0.2f + 0.4f * (numBeetles); //Position will scale up if there are multiple beetles this.transform.position = temp; this.transform.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; } else { this.transform.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY; } } // If piece is not a beetle then treat it normally else { this.transform.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY; } currentHex = hexController.GetNearestHex(this.transform.position); }
public void SpawnTile () { var x = PuzzleTileHex.GetRandomTile(this); x.MakeDraggable(); x.Name = $"tile_{tileID++}"; x.Position = spawnCoord.Position(); board.AddChild(x); }
// Outline a single hex public void ShowLastMove(HexCoord strt, HexCoord curr) { ClearLastMove(); outlineObject = (GameObject)Instantiate(outlinePrefabTall); outlineObject.transform.position = curr.Position(); HighlightStart(strt); }
public Vector2 FindNearest(Vector2 pos) { // Find the hex that overlaps a certain x,y point //Debug.Log(pos); nearestHex = HexCoord.AtPosition(pos); Vector2 hexPos = nearestHex.Position(); return(hexPos); }
public void HighlightStart(HexCoord startHex) { if (startHighlight != null) { Destroy(startHighlight); } startHighlight = (GameObject)Instantiate(highlightPrefab); startHighlight.GetComponent <HighlightController>().StartHex(); startHighlight.transform.position = startHex.Position(); }
// Used when a piece moves to update the lists public void MovePiece(HexCoord prevHex, HexCoord newHex, UnitColor color, UnitType type, GameObject obj) { if (type == UnitType.Beetle) //If beetle then we need to update lists differently { if (occupiedList.Contains(newHex)) //If beetle is moving on top of another piece { beetleDict[newHex]++; //Add beetle on top CoverPieceBelow(obj.transform.position); //Don't add beetle to occupied list since hex is already occupied } else //If new hex is unoccupied then add beetle to occupied list { occupiedList.Add(newHex); colorDict[newHex] = color; typeDict[newHex] = type; } if (beetleDict[prevHex] > 0) //If moving a beetle from a stacked position { beetleDict[prevHex]--; //Decrement beetle count at previous hex UncoverPieceBelow(new Vector3(prevHex.Position().x, prevHex.Position().y, 5.0f)); //Define ray starting point far enough above prev position } else //Only remove prevHex from lists if no other piece is at previous position { occupiedList.Remove(prevHex); colorDict.Remove(prevHex); typeDict.Remove(prevHex); } } else //If any other move type then change lists and dict as normal; { occupiedList.Remove(prevHex); colorDict.Remove(prevHex); typeDict.Remove(prevHex); beetleDict.Remove(prevHex); occupiedList.Add(newHex); colorDict[newHex] = color; typeDict[newHex] = type; beetleDict[newHex] = 0; } }
public void Rescale () { var rect = GetViewportRect(); //scale such that the puzzle fits with a half tile margin var radius = rect.Size.y /((map.Size * 0.75f + 0.25f) + 1f) / 2f; Scale = new Vector2(radius, radius); var center = new HexCoord(map.EdgeSize - 1, map.EdgeSize - 1); var centerPos = center.Position() * Scale; var centerScreen = new Vector2(rect.Size.x / 2f, rect.Size.y / 2f); Position = centerScreen - centerPos; }
public void HighlightNearest(Vector2 pos) { nearestHex = HexCoord.AtPosition(pos); Vector2 hexPos = nearestHex.Position(); if (nearestHex != prevHighlightedHex) { if (prevHighlight != null) { Destroy(prevHighlight); } GameObject highlight = (GameObject)Instantiate(highlightPrefab); highlight.GetComponent <HighlightController>().IneligibleHex(); highlight.transform.position = hexPos; prevHighlight = highlight; prevHighlightedHex = nearestHex; } }
//Snap to the nearest eligible hex or the starting hex, whichever is closest void SnapToNearestEligible(List <HexCoord> eligibleList) { //Debug.Log(eligibleList.Count); //Brute force method to get nearest eligible hex float smallestDist = Mathf.Infinity; HexCoord closestHex = startHex; //Default to the starting hex List <HexCoord> eligibleListMod = new List <HexCoord>(eligibleList); eligibleListMod.Add(startHex); //Include the starting hex foreach (HexCoord hex in eligibleListMod) { float dist = Vector2.SqrMagnitude(hex.Position() - (Vector2)this.transform.position); if (dist < smallestDist) { smallestDist = dist; closestHex = hex; } } Vector2 snapPos = closestHex.Position(); this.transform.position = snapPos; //Snap to x,y position first //this.transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, 0f)); //Snap to default rotation //For beetles the eligible hex might be occupied. In that case, fix the z distance and don't allow gravity if (specificBehavior.type == BoardManager.UnitType.Beetle && boardManager.occupiedList.Contains(closestHex) && closestHex != startHex) { Vector3 temp = this.transform.position; int numBeetles = boardManager.beetleDict[closestHex]; temp.z = 0.2f + 0.4f * (numBeetles + 1); //Position will scale up if there are multiple beetles this.transform.position = temp; this.transform.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; } else { this.transform.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY; } currentHex = hexController.GetNearestHex(this.transform.position); }
//NOT USED public void HighlightNearestDeprecated(Vector2 pos) { // Find the hex that overlaps a certain x,y point //Debug.Log(pos); nearestHex = HexCoord.AtPosition(pos); Vector2 hexPos = nearestHex.Position(); GameObject hexDisp = null; //Brute force method to get hex display associated with a certain HexCoord int i = 0; //int ind = 0; float smallestDist = Mathf.Infinity; foreach (Transform child in HexDisplays.transform) { float dist = Vector2.SqrMagnitude(hexPos - (Vector2)child.position); if (dist < smallestDist) { smallestDist = dist; //ind = i; hexDisp = child.gameObject; } i++; } //Highlight the hex if (hexDisp != null && hexDisp != prevHexDisp) { if (prevHexDisp != null) { prevHexDisp.GetComponent <HexDisplayControl>().Highlight(false); } hexDisp.GetComponent <HexDisplayControl>().Highlight(true); prevHexDisp = hexDisp; } }
/// <summary> /// Converts HexCoord to world position according to this grid. /// </summary> /// <param name="hexCoord"></param> /// <returns></returns> public Vector3 GetWorldPositionOfHex(HexCoord hexCoord) { Vector2 hexPos = hexCoord.Position(); return(_startPos + new Vector3(hexPos.x, 0, hexPos.y) / _hexScaleFactor); }
//[PunRPC] public void FinalizeMove() { bool placement = false; if (specificBehavior.active != BoardManager.Active.Board) { placement = true; } //PUN can't serialize HexCoords by default. Need to translate to something else or setup a serialize/deserialize method (see PUN documentation - search serialize) this.GetComponent <PhotonView>().RPC("NetworkSyncMove", PhotonTargets.All, this.transform.position, placement, startHex.Position()); }
void LoadLevel (int number) { currentLevel = number; //GODOT: how on earth do I load a text file? //var resource = GD.Load($"res://Levels/{number}.txt"); //this took 30mins to figure out. every operation is different than the //C# BCL version and the docs are not helpful var f = new File(); f.Open($"res://Levels/{number}.txt", (int)File.ModeFlags.Read); var lines = new System.Collections.Generic.List<string>(); while(!f.EofReached()) { lines.Add(f.GetLine()); } //var lines = System.IO.File.ReadAllLines(System.IO.Path.Combine("Levels", $"{number}.txt")); var boardDef = lines[0].Split('|'); int boardSize = int.Parse(boardDef[0]); if (boardDef.Length == 6) { TileColor = new Color(boardDef[1]); if (string.IsNullOrEmpty(boardDef[2])) { StaticTileColor = TileColor.Darkened(0.2f); } else { StaticTileColor = new Color(boardDef[2]); } BoardColor = new Color(boardDef[3]); if (string.IsNullOrEmpty(boardDef[4])) { BackgroundColor = Colors.Black; } else { BackgroundColor = new Color(boardDef[4]); } LineHighlightColor = new Color(boardDef[5]); } else { TileColor = new Color("e017c2"); StaticTileColor = TileColor.Darkened(0.2f); BoardColor = new Color("bdf0ec"); BackgroundColor = Colors.Black; LineHighlightColor = Colors.Yellow; } VisualServer.SetDefaultClearColor(BackgroundColor); CreateLevel(boardSize); for (int i = 1; i < lines.Count; i++) { var line = lines[i]; if (string.IsNullOrWhiteSpace(line)) { continue; } int commaIdx = line.IndexOf(','); int q = int.Parse(line.Substring(0,commaIdx)); var pipeIdx = line.IndexOf('|'); int r = int.Parse(line.Substring(commaIdx + 1, pipeIdx - commaIdx - 1)); int[] desc = new int[6]; for (int j = 0; j < 6; j++) { desc[j] = line[pipeIdx+ 1+j] - '0'; } //offset due to the map we're using to figure out positions int offset = Math.Max(0, 4 - boardSize); var coord = new HexCoord(q, r - offset); var tile = new PuzzleTileHex { LineDescriptions = desc, Position = coord.Position() }; tile.IsStatic = true; map.SetCell(new CellInfo(coord, tile)); board.AddChild(tile); tile.CalculatePaths(this, coord); } var c = TileColor; c.a = 0.6f; hexCursor.Modulate = c; Rescale(); }
public void SnapTileToCell (PuzzleTileHex tile, HexCoord coord) { snapTween.InterpolateProperty(tile, "position", null, coord.Position(), 0.1f, Tween.TransitionType.Cubic, Tween.EaseType.Out, 0); snapTween.Start(); }
// Outline a single hex public void OutlineHexTall(HexCoord hex) { outlineObject = (GameObject)Instantiate(outlinePrefabTall); outlineObject.transform.position = hex.Position(); }