public void PlacingSettlement_Update() { Vector3?mousePos = RaycastMouse(); if (mousePos.HasValue) { PlaceSettlementAction action = new PlaceSettlementAction(HexCorner.GetNearestCorner(mousePos.Value), Color); if (action.IsValid(Board)) { Board.SetCurrentAction(action); if (Input.GetMouseButtonDown(0)) { Board.ApplyCurrentAction(); if (Coordinator.GameState.State == GameStates.Setup) { ChangeState(PlayerStates.PlacingRoad); } else if (Coordinator.GameState.State == GameStates.InProgress) { ChangeState(PlayerStates.Idle); } } } } }
/// <summary> /// Returns a given the world position of a given hex Cell /// </summary> /// <param name="corner">the corner wanted</param> /// <returns>The world position of the corner</returns> public Vector2 GetCorner(HexCorner corner) { // angle for corner in degree: 60° * corner + 30° float angleRad = Mathf.PI / 180.0f * (60.0f * (float)corner + 30.0f); Vector2 center = Map.HexCellToWorldPosition(this); return(new Vector2(center.x + HEXCELL_SIZE * Mathf.Cos(angleRad), center.y + HEXCELL_SIZE * Mathf.Sin(angleRad))); }
public bool IsValidCity(HexCorner corner, PlayerColor color) { if (Units.ContainsKey(corner) && Units[corner].Type == UnitTypes.Settlement && Units[corner].Color == color) { return(true); } return(false); }
public Unit InstantiateUnit(HexCorner location, PlayerColor color, UnitTypes type) { Unit unit = Instantiate(unitPrefabs[type], transform.TransformPoint(location.ToLocalCoords()), location.ToLocalRot(), transform).GetComponent <Unit>(); unit.Type = type; unit.Color = color; return(unit); }
public void AddUnit(HexCorner intersection, Unit unit) { //if something's already there, delete it if (Units.ContainsKey(intersection)) { GameObject.Destroy(Units[intersection]); Units.Remove(intersection); } Units.Add(intersection, unit); }
public HexTile(TileType pTileType, Coord pCPos, float pSize, Vector3 pWPos) : base(pTileType, pCPos, pSize, pWPos) { height = size * 2; width = Mathf.Sqrt(3f) / 2 * height; corners[0] = new HexCorner(this, 0, new Vector3(wPos.x, wPos.y, wPos.z + height / 2)); corners[1] = new HexCorner(this, 1, new Vector3(wPos.x + width / 2, wPos.y, wPos.z + height / 4)); corners[2] = new HexCorner(this, 2, new Vector3(wPos.x + width / 2, wPos.y, wPos.z - height / 4)); corners[3] = new HexCorner(this, 3, new Vector3(wPos.x, wPos.y, wPos.z - height / 2)); corners[4] = new HexCorner(this, 4, new Vector3(wPos.x - width / 2, wPos.y, wPos.z - height / 4)); corners[5] = new HexCorner(this, 5, new Vector3(wPos.x - width / 2, wPos.y, wPos.z + height / 4)); }
public void UndoMove() { bombList.Clear(); for (int x = 0; x < tileSpace.tileNumberX; x++) { for (int y = 0; y < tileSpace.tileNumberY; y++) { GameObject tmpObj; Destroy(hexList[x, y].gameObject); if (undoList[x, y].y != 2) { tmpObj = Instantiate(hexagonPrefab, tilePositionList[x, y], Quaternion.identity); } else { tmpObj = Instantiate(bombPrefab, tilePositionList[x, y], Quaternion.identity); } tmpObj.GetComponent <SpriteRenderer>().color = tileColors[undoList[x, y].x]; tmpObj.transform.localScale = new Vector3(tileSpace.hexScale, tileSpace.hexScale, 1f); Hex tmpHex = tmpObj.GetComponent <Hex>(); tmpHex.tileNumberX = x; tmpHex.tileNumberY = y; tmpHex.colorNo = undoList[x, y].x; //tmpHex.coordinate = new Vector2Int(x, y); if (undoList[x, y].y == 1) { tmpHex.gameObject.GetComponent <SpriteRenderer>().sprite = starHexSprite; tmpHex.isBombStar = 1; } if (undoList[x, y].y == 2) { //tmpHex.gameObject.GetComponent<SpriteRenderer>().sprite = starHexSprite; tmpHex.isBombStar = 2; tmpHex.bombTimer = undoList[x, y].z; tmpHex.gameObject.GetComponentInChildren <TextMesh>().text = tmpHex.bombTimer.ToString(); bombList.Add(tmpHex); } hexList[x, y] = tmpHex; } } score = undoScore; scoreText.text = "Score: " + score; selectedCorner = null; selector.transform.position = new Vector3(6f, 0f, 0f); undoButton.SetActive(false); }
public void PlacingCity_Update() { Vector3?mousePos = RaycastMouse(); if (mousePos.HasValue) { PlaceCityAction action = new PlaceCityAction(HexCorner.GetNearestCorner(mousePos.Value), Color); if (action.IsValid(Board)) { Board.SetCurrentAction(action); if (Input.GetMouseButtonDown(0)) { Board.ApplyCurrentAction(); ChangeState(PlayerStates.Idle); } } } }
public static Vector3 BridgePoint(HexMetrics metrics, HexCorner fromCorner, HexCorner towardCorner, float ringPercentage, float bridgePercentage, Vector3 centerVertex, float[] cornerHeights) { Vector3 cornerVertex = HexUtils.CornerPosition((int)fromCorner) * metrics.tileSize; cornerVertex = cornerVertex + Vector3.up * cornerHeights[fromCorner.GetInt()] * metrics.mapHeight; Vector3 nextCornerPoint = HexUtils.CornerPosition(towardCorner.GetInt()) * metrics.tileSize; nextCornerPoint = nextCornerPoint + Vector3.up * cornerHeights[towardCorner.GetInt()] * metrics.mapHeight; Vector3 vector = nextCornerPoint - cornerVertex; float bridgeMod = 0.5f * (1f - bridgePercentage); Vector3 bridgePoint = cornerVertex + vector * bridgeMod; vector = centerVertex - bridgePoint; return(bridgePoint + vector * (1f - ringPercentage)); }
public bool IsValidSettlement(PlayerColor color, HexCorner corner) { var unitLocations = Units.Keys; //true if this placement is connected by road to a settlement of the same color bool isConnected = false; foreach (var location in unitLocations) { if ((Units[location].Type == UnitTypes.Settlement || Units[location].Type == UnitTypes.City) && CornerGraph.GraphSearch(location, corner).Length < 2) { return(false); } if (Units[location].Color == color && CornerGraph.GraphRoadSearch(corner, location) != null) { isConnected = true; } } //the road restriction only applies if the game isn't in setup mode if (coordinator.GameState.State != GameStates.Setup && isConnected == false) { return(false); } return(true); }
/// <summary> /// Returns the next corner CCW /// </summary> /// <param name="direction"></param> /// <returns></returns> public static HexCorner Next(this HexCorner corner) { return(corner == HexCorner.S ? HexCorner.SE : (corner + 1)); }
public void SetUnitVisible(HexCorner location, bool visible) { Board.Units[location].GetComponent <Renderer>().enabled = visible; }
public static HexCorner Opposite(this HexCorner corner) { return((int)corner < 3 ? (corner + 3) : (corner - 3)); }
public bool IsValidUnitPlacement(HexCorner corner, UnitTypes type, PlayerColor color) { return((type == UnitTypes.Settlement && IsValidSettlement(color, corner)) || (type == UnitTypes.City && IsValidCity(corner, color))); }
public static int GetInt(this HexCorner corner) { return((int)corner); }
/// <summary> /// Returns one of the sides that this corner touches. /// The side returned is the most CCW one relative to the hexagon. /// </summary> /// <param name="direction"></param> /// <returns></returns> public static HexDirection GetDirection(this HexCorner corner) { return((HexDirection)(int)corner); }
public PlaceSettlementAction(HexCorner location, PlayerColor color) { Location = location; Color = color; Settlement = null; }
private float GetHeightAtCorner(int x, int y, HexCorner corner) { HexPoint pt = new HexPoint(x, y); switch (corner) { case HexCorner.SouthEast: { HexPoint se = pt.Neighbor(HexDirection.SouthEast); HexPoint ea = pt.Neighbor(HexDirection.East); if (IsValid(se) && IsValid(ea)) return (this[se].Height + this[ea].Height + Height) / 3.0f; if (IsValid(se)) return (this[se].Height + Height) / 2.0f; if (IsValid(ea)) return (this[ea].Height + Height) / 2.0f; return Height; } case HexCorner.NorthEast: { HexPoint ne = pt.Neighbor(HexDirection.NorthEast); HexPoint ea = pt.Neighbor(HexDirection.East); if (IsValid(ne) && IsValid(ea)) return (this[ne].Height + this[ea].Height + Height) / 3.0f; if (IsValid(ne)) return (this[ne].Height + Height) / 2.0f; if (IsValid(ea)) return (this[ea].Height + Height) / 2.0f; return Height; } case HexCorner.North: { HexPoint ne = pt.Neighbor(HexDirection.NorthEast); HexPoint nw = pt.Neighbor(HexDirection.NorthWest); if (IsValid(ne) && IsValid(nw)) return (this[ne].Height + this[nw].Height + Height) / 3.0f; if (IsValid(ne)) return (this[ne].Height + Height) / 2.0f; if (IsValid(nw)) return (this[nw].Height + Height) / 2.0f; return Height; } case HexCorner.SouthWest: { HexPoint sw = pt.Neighbor(HexDirection.SouthWest); HexPoint we = pt.Neighbor(HexDirection.West); if (IsValid(sw) && IsValid(we)) return (this[sw].Height + this[we].Height + Height) / 3.0f; if (IsValid(sw)) return (this[sw].Height + Height) / 2.0f; if (IsValid(we)) return (this[we].Height + Height) / 2.0f; return Height; } case HexCorner.NorthWest: { HexPoint nw = pt.Neighbor(HexDirection.NorthWest); HexPoint we = pt.Neighbor(HexDirection.West); if (IsValid(nw) && IsValid(we)) return (this[nw].Height + this[we].Height + Height) / 3.0f; if (IsValid(nw)) return (this[nw].Height + Height) / 2.0f; if (IsValid(we)) return (this[we].Height + Height) / 2.0f; return Height; } case HexCorner.South: { HexPoint se = pt.Neighbor(HexDirection.SouthEast); HexPoint sw = pt.Neighbor(HexDirection.SouthWest); if (IsValid(se) && IsValid(sw)) return (this[se].Height + this[sw].Height + Height) / 3.0f; if (IsValid(se)) return (this[se].Height + Height) / 2.0f; if (IsValid(sw)) return (this[sw].Height + Height) / 2.0f; return Height; } } return 0f; }
/// <summary> /// Returns the last corner CW /// </summary> /// <param name="direction"></param> /// <returns></returns> public static HexCorner Last(this HexCorner corner) { return(corner == HexCorner.SE ? HexCorner.S : (corner - 1)); }