private void FillPreconfigDiagnostic() { int count = GemObjectList.Count; Debug.Log("FillEmptyDiagnostic"); HexManager.Instance.SetScanSetting(0); GameObject go = HexManager.Instance.QueryScanNextHex(); int index = 0; while (go != null) { HexObject objectScript = go.GetComponent <HexObject> (); if (objectScript._Type == HexObject.eType.Main && objectScript.NoGemAttached()) { GameObject gem = GemManager.Instance.QueryGetAvailableObject(); if (gem != null) { int colorType = PreConfigBoard3[index++]; GemObject gemScript = gem.GetComponent <GemObject> (); gemScript.SetGemSprite(GemObjectList[colorType], (GemObject.eColorType)colorType); objectScript.AttachGem(gem); } } go = HexManager.Instance.QueryScanNextHex(); } }
///<summary> ///Called when a GameObject has been clicked ///</summary> private void ClickHex(GameObject hex) { //The HexObject class of the clicked object HexObject clickedHex = hex.GetComponent <HexObject>(); //If the clicked object is not a HexObject, return if (clickedHex == null) { return; } //if the clicked hex is already highlighted, a unit is selected and the unit can move to it, or attack the unit on there if (clickedHex.isHighlighted) { //Check if the clicked hex has a unit on it if (clickedHex.unit != null) { //If the unit on the hex is a player (our own team) the unit cannot move there, so do not highlight it if (clickedHex.unit.unitTeam == Unit.UnitTeamType.PLAYER) { SelectHex(clickedHex); } else { //the clicked hex has an ememy on it, and we want to attack it bool isDead = clickedHex.unit.Damage(grid.currentSelectedHex.unit.attack); if (isDead) { //Unit died, so we need to update the Units in the grid grid.UpdateUnits(); //Bowmen do not move after they kill someone, but others do if (grid.currentSelectedHex.unit.unitType != Unit.UnitType.BOWMAN) { MoveUnit(clickedHex); } } } //Attacked an enemy, end turn handler.NextTurn(); } else { //We want to move the selected unit MoveUnit(clickedHex); //Turn off the highlights foreach (HexObject hexObject in grid.hexes) { hexObject.SetHighlighted(false); } //We moved handler.NextTurn(); return; } } SelectHex(clickedHex); }
private void FillEmptyDiagnostic() { int count = GemObjectList.Count; Debug.Log("FillEmptyDiagnostic"); HexManager.Instance.SetScanSetting(0); GameObject go = HexManager.Instance.QueryScanNextHex(); while (go != null) { HexObject objectScript = go.GetComponent <HexObject> (); if (objectScript.NoGemAttached()) { GameObject gem = GemManager.Instance.QueryGetAvailableObject(); if (gem != null) { int colorType = (int)Random.Range(0, count); GemObject gemScript = gem.GetComponent <GemObject> (); gemScript.SetGemSprite(GemObjectList[colorType], (GemObject.eColorType)colorType); objectScript.AttachGem(gem); } } go = HexManager.Instance.QueryScanNextHex(); } }
///<summary> ///Select the clicked hex ///</summary> private void SelectHex(HexObject clickedHex) { //The clicked hex was not highlighted, which means it's a fresh click grid.currentSelectedHex = clickedHex; //Remove every highlight foreach (HexObject hexObject in grid.hexes) { hexObject.SetHighlighted(false); } //If the clicked hex does not contain a Unit, or the unit is an enemy, return if (clickedHex.unit == null || clickedHex.unit.unitTeam == Unit.UnitTeamType.ENEMY) { return; } int radius = clickedHex.unit.unitType == Unit.UnitType.KNIGHT ? 1 : 2; List <HexObject> hexList = grid.GetNeighbours(clickedHex.Coords, radius); hexList = grid.FilterHexes(clickedHex.Coords, hexList, clickedHex.unit.unitType); foreach (HexObject hexObject in hexList) { hexObject.SetHighlighted(true); } }
private void TriangulateCornerTerraces(Vector3 begin, HexObject beginCell, Vector3 left, HexObject leftCell, Vector3 right, HexObject rightCell) { Vector3 v3 = HexMetrics.TerraceLerp(begin, left, 1); Vector3 v4 = HexMetrics.TerraceLerp(begin, right, 1); Color c3 = HexMetrics.TerraceLerp(beginCell.Color, leftCell.Color, 1); Color c4 = HexMetrics.TerraceLerp(beginCell.Color, rightCell.Color, 1); terrain.AddTriangle(begin, v3, v4); terrain.AddTriangleColor(beginCell.Color, c3, c4); for (int i = 2; i < HexMetrics.Instance.terraceSteps; i++) { Vector3 v1 = v3; Vector3 v2 = v4; Color c1 = c3; Color c2 = c4; v3 = HexMetrics.TerraceLerp(begin, left, i); v4 = HexMetrics.TerraceLerp(begin, right, i); c3 = HexMetrics.TerraceLerp(beginCell.Color, leftCell.Color, i); c4 = HexMetrics.TerraceLerp(beginCell.Color, rightCell.Color, i); terrain.AddQuad(v1, v2, v3, v4); terrain.AddQuadColor(c1, c2, c3, c4); } terrain.AddQuad(v3, v4, left, right); terrain.AddQuadColor(c3, c4, leftCell.Color, rightCell.Color); }
private bool EvaluateLinkedList(List <GameObject> linkList, GemObject.eColorType colorType) { bool eval = false; foreach (GameObject linkObj in linkList) { HexObject objectScript = linkObj.GetComponent <HexObject> (); if (objectScript._Type == HexObject.eType.Main) { if (objectScript.ScanColor == (int)GemObject.eColorType.Black && objectScript.MarkedColor == (int)GemObject.eColorType.Black) { GemObject.eColorType cType = objectScript.GetGemRefColorType(); if (cType == colorType) { objectScript.ScanColor = (int)colorType; ScannedLinkedList.Add(linkObj); eval = true; } } } } return(eval); }
private void TriangulateAdjacentToRiver(HexDirection direction, HexObject cell, Vector3 center, EdgeVertices e) { if (cell.HasRoads) { TriangulateRoadAdjacentToRiver(direction, cell, center, e); } if (cell.HasRiverThroughEdge(direction.Next())) { if (cell.HasRiverThroughEdge(direction.Previous())) { center += HexMetrics.GetSolidEdgeMiddle(direction) * (HexMetrics.Instance.innerToOuter * 0.5f); } else if (cell.HasRiverThroughEdge(direction.Previous2())) { center += HexMetrics.GetFirstSolidCorner(direction) * 0.25f; } } else if (cell.HasRiverThroughEdge(direction.Previous()) && cell.HasRiverThroughEdge(direction.Next2())) { center += HexMetrics.GetSecondSolidCorner(direction) * 0.25f; } EdgeVertices m = new EdgeVertices(Vector3.Lerp(center, e.v1, 0.5f), Vector3.Lerp(center, e.v5, 0.5f)); TriangulateEdgeStrip(m, cell.Color, e, cell.Color); TriangulateEdgeFan(center, m, cell.Color); if (!cell.IsUnderwater && !cell.HasRoadThroughEdge(direction)) { features.AddFeature(cell, (center + e.v1 + e.v5) * (1f / 3f)); } }
private void HandleHoldInput() { Ray inputRay = cameraRig.Camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(inputRay, out hit)) { HexObject currentCell = HexGrid.Instance.FindHexObject(GetHexAt(hit.point)); if (previousCell && previousCell != currentCell) { ValidateDrag(currentCell); } else { isDragging = false; } UpdateHex(currentCell); previousCell = currentCell; } else { previousCell = null; } }
/** * Swap selected elements in cycle according to the given direction. */ private void CycleSwapSelected(bool clockwise) { // We dont have to swap elements if there are none. if (selectedGroup == null || selectedGroup.Count == 0) { return; } Hex selected1 = grid[selectedGroup[0].x][selectedGroup[0].y]; Hex selected2 = grid[selectedGroup[1].x][selectedGroup[1].y]; Hex selected3 = grid[selectedGroup[2].x][selectedGroup[2].y]; HexObject temp = new HexObject(selected1); if (clockwise) { SwapHexes(new HexObject(selected1), new HexObject(selected2)); SwapHexes(new HexObject(selected2), new HexObject(selected3)); SwapHexes(new HexObject(selected3), temp); } else { SwapHexes(new HexObject(selected1), new HexObject(selected3)); SwapHexes(new HexObject(selected3), new HexObject(selected2)); SwapHexes(new HexObject(selected2), temp); } }
private void SpawnChunk(HexChunk chunk) { //chunk.InitializeChunk() List <Hex> hexKeys = new List <Hex>(chunk.grid.Keys); foreach (Hex key in hexKeys) { Hex hex = key; if (hex != null) { if (chunk.grid.ContainsKey(key)) { HexObject hexObject = chunk.grid[key]; if (hexObject != null) { hexObject.meshFilter = hexObject.gameObject.AddComponent <MeshFilter>(); hexObject.meshFilter.sharedMesh = GridManager.I.pointyHexagonMesh.mesh; hexObject.meshCollider = hexObject.gameObject.AddComponent <MeshCollider>(); hexObject.meshRenderer = hexObject.gameObject.AddComponent <MeshRenderer>(); hexObject.GetComponent <Renderer>().sharedMaterial = LoadFromHexMaterials(hexObject.hex.hexType); } } } } chunk.chunkState = HexChunk.ChunkState_e.SPAWNED; }
private void LoadHexObjects() { for (int t = 0; t < objectPoolSize; t++) { GameObject _sfObj = Instantiate(Resources.Load("Prefabs/HexObject", typeof(GameObject))) as GameObject; if (_sfObj != null) { if (HexObjectContainer != null) { _sfObj.transform.parent = HexObjectContainer.transform; } _sfObj.name = "hexObj" + t.ToString(); //default storage location _sfObj.transform.position = new Vector2(StoragePosition.transform.position.x, StoragePosition.transform.position.y); HexObject objectScript = _sfObj.GetComponent <HexObject> (); objectScript.ID = t; HexObjectList.Add(_sfObj); } else { Debug.Log("Couldn't load hex object prefab"); } } _nullObj = Instantiate(Resources.Load("Prefabs/HexObject", typeof(GameObject))) as GameObject; _nullObj.transform.position = new Vector2(StoragePosition.transform.position.x, StoragePosition.transform.position.y); HexObject nullObjectScript = _nullObj.GetComponent <HexObject> (); nullObjectScript.ID = -1; nullObjectScript.isNullObject = true; }
private void TriangulateOpenWater(HexDirection direction, HexObject cell, HexObject neighbour, Vector3 center) { Vector3 c1 = center + HexMetrics.GetFirstWaterCorner(direction); Vector3 c2 = center + HexMetrics.GetSecondWaterCorner(direction); water.AddTriangle(center, c1, c2); if (direction <= HexDirection.SE && neighbour != null) { Vector3 bridge = HexMetrics.GetWaterBridge(direction); Vector3 e1 = c1 + bridge; Vector3 e2 = c2 + bridge; water.AddQuad(c1, c2, e1, e2); if (direction <= HexDirection.E) { HexObject nextNeighbour = cell.GetNeighbour(direction.Next()); if (nextNeighbour == null || !nextNeighbour.IsUnderwater) { return; } water.AddTriangle(c2, e2, c2 + HexMetrics.GetWaterBridge(direction.Next())); } } }
private void TriangulateWithRiverBeginOrEnd(HexDirection direction, HexObject cell, Vector3 center, EdgeVertices e) { EdgeVertices m = new EdgeVertices(Vector3.Lerp(center, e.v1, 0.5f), Vector3.Lerp(center, e.v5, 0.5f)); m.v3.y = e.v3.y; TriangulateEdgeStrip(m, cell.Color, e, cell.Color); TriangulateEdgeFan(center, m, cell.Color); if (!cell.IsUnderwater) { bool reversed = cell.HasIncomingRiver; TriangulateRiverQuad(m.v2, m.v4, e.v2, e.v4, cell.RiverSurfaceY, 0.6f, reversed); center.y = m.v2.y = m.v4.y = cell.RiverSurfaceY; rivers.AddTriangle(center, m.v2, m.v4); if (reversed) { rivers.AddTriangleUV(new Vector2(0.5f, 0.4f), new Vector2(1f, 0.2f), new Vector2(0f, 0.2f)); } else { rivers.AddTriangleUV(new Vector2(0.5f, 0.4f), new Vector2(0f, 0.6f), new Vector2(1f, 0.6f)); } } }
public void SetOutgoingRiver(HexDirection direction) { if (HasOutgoingRiver && OutgoingRiver == direction) { return; } HexObject neighbour = GetNeighbour(direction); if (!IsValidRiverDestination(neighbour)) { return; } RemoveOutgoingRiver(); if (HasIncomingRiver && IncomingRiver == direction) { RemoveIncomingRiver(); } _hasOutgoingRiver = true; _outgoingRiver = direction; neighbour.RemoveIncomingRiver(); neighbour._hasIncomingRiver = true; neighbour._incomingRiver = direction.Opposite(); SetRoad((int)direction, false); }
public void InitHexObject(int x, int z, float xPos, float zPos, Hex.HexType_e hexType) { //GameObject goHex = new GameObject("Hex " + x + "_" + z); //goHex.transform.parent = chunkObject.transform; //goHex.transform.localPosition = new Vector3(xPos, zPos); GameObject goHex = new GameObject("Hex " + x + "_" + z); goHex.transform.SetParent(goChunk.transform); goHex.transform.localPosition = new Vector3(xPos, 0, zPos); goHex.transform.rotation = Quaternion.identity; goHex.tag = "Hex"; goHex.AddComponent <BoxCollider>(); HexObject hexObject = goHex.AddComponent <HexObject>(); hexObject.InitializeHexObject(new Hex(x, z, xPos, zPos, hexType)); if (!grid.ContainsKey(hexObject.hex)) { grid.Add(hexObject.hex, hexObject); } else { grid[hexObject.hex] = hexObject; } }
///<summary> ///Physically move the unit ///</summary> ///<param name="currentHex">The hex the unit is standing on</param> ///<param name="newHex">The new hex to move to</param> public void MoveUnit(HexObject currentHex, HexObject newHex) { grid.currentSelectedHex = newHex; newHex.unit = currentHex.unit; currentHex.unit = null; grid.currentSelectedHex.unit.gameObject.transform.SetParent(newHex.transform); newHex.unit.transform.localPosition = newHex.transform.localPosition; }
/* * public void SetOutlinerPositionWithDelay(HexObject aHexObjectManager, float aDelayTime) { * touchFocusedOnThisObject = aHexObjectManager; * Invoke("SetOutlinerPosition", aDelayTime); * } * * * public void SetOutlinerPosition() { * if (!InputManager.im.isDragging) { * outliner.position = touchFocusedOnThisObject.transform.position; * outliner.rotation = Quaternion.Euler(0, 0, touchFocusedOnThisObject.ExecuteRotatorAngle()); * outliner.localScale = new Vector3(touchFocusedOnThisObject.GetSideLength(), touchFocusedOnThisObject.GetSideLength(), touchFocusedOnThisObject.GetSideLength()); * outliner.gameObject.SetActive(true); * Debug.Log("Object selection triggered in GameController"); * } * } */ /* * public void SelectObject(HexObject aHexObjectManager) { * if (!isSelectionCoroutineRunning) { * StartCoroutine(SetOutlinerPosition(aHexObjectManager)); * } * } * private IEnumerator SetOutlinerPosition(HexObject aHexObjectManager) { * isSelectionCoroutineRunning = true; * yield return new WaitForSeconds(0.3f); * if (!InputManager.im.isDragging) { * touchFocusedOnThisObject = aHexObjectManager; * outliner.position = touchFocusedOnThisObject.transform.position; * outliner.rotation = Quaternion.Euler(0, 0, touchFocusedOnThisObject.ExecuteRotatorAngle()); * outliner.localScale = new Vector3(touchFocusedOnThisObject.GetSideLength(), touchFocusedOnThisObject.GetSideLength(), touchFocusedOnThisObject.GetSideLength()); * outliner.gameObject.SetActive(true); * Debug.Log("Object selection triggered in GameController"); * } * isSelectionCoroutineRunning = false; * } */ public void SetOutlinerPosition(HexObject aHexObjectManager) { touchFocusedOnThisObject = aHexObjectManager; outliner.position = touchFocusedOnThisObject.transform.position; outliner.rotation = Quaternion.Euler(0, 0, touchFocusedOnThisObject.ExecuteRotatorAngle()); outliner.localScale = new Vector3(touchFocusedOnThisObject.GetSideLength(), touchFocusedOnThisObject.GetSideLength(), touchFocusedOnThisObject.GetSideLength()); outliner.gameObject.SetActive(true); //Debug.Log("Object selection triggered in GameController"); }
public HexObject SpawnNormalObject(int objectIndex, HexCell cellToSpawnOn) { GameObject objToSpawn = objects[objectIndex].gameObject; HexObject spawnedObject = Instantiate(objToSpawn).GetComponent <HexObject>(); spawnedObject.transform.SetParent(objectParent); spawnedObject.Location = cellToSpawnOn; return(spawnedObject); }
public void UpdateObjectNeighbors(HexObject aHexObject) { aHexObject.neighbor30 = gridsAndContents.ContainsKey(aHexObject.neighborIndex30) ? gridsAndContents[aHexObject.neighborIndex30].hex.transform : null; aHexObject.neighbor90 = gridsAndContents.ContainsKey(aHexObject.neighborIndex90) ? gridsAndContents[aHexObject.neighborIndex90].hex.transform : null; aHexObject.neighbor120 = gridsAndContents.ContainsKey(aHexObject.neighborIndex120) ? gridsAndContents[aHexObject.neighborIndex120].hex.transform : null; aHexObject.neighbor210 = gridsAndContents.ContainsKey(aHexObject.neighborIndex210) ? gridsAndContents[aHexObject.neighborIndex210].hex.transform : null; aHexObject.neighbor270 = gridsAndContents.ContainsKey(aHexObject.neighborIndex270) ? gridsAndContents[aHexObject.neighborIndex270].hex.transform : null; aHexObject.neighbor330 = gridsAndContents.ContainsKey(aHexObject.neighborIndex330) ? gridsAndContents[aHexObject.neighborIndex330].hex.transform : null; }
public void QueryScanAndMark() { QueryClearMarked(); foreach (GameObject tObj in HexObjectList) { HexObject objectScript = tObj.GetComponent <HexObject> (); if (objectScript._Type == HexObject.eType.Main && objectScript.MarkedColor == (int)GemObject.eColorType.Black) { //new target so clear scan colors QueryClearScan(); ScannedLinkedList.Clear(); //do link walk GemObject.eColorType colorType = objectScript.GetGemRefColorType(); objectScript.ScanColor = (int)colorType; //get link list for this target object List <GameObject> linkList = objectScript.HexLinkList; bool eval = EvaluateLinkedList(linkList, colorType); while (eval == true) { List <GameObject> evalList = new List <GameObject>(ScannedLinkedList); ScannedLinkedList.Clear(); if (evalList.Count == 0) { eval = false; } bool innerEval = false; foreach (GameObject linkObj in evalList) { HexObject objScript = linkObj.GetComponent <HexObject> (); List <GameObject> sublinkList = objScript.HexLinkList; if (EvaluateLinkedList(sublinkList, colorType)) { innerEval = true; } } eval = innerEval; } int count = QueryCountScanColors((int)colorType); if (count >= 4) { //if >= 5 -> add powerup QueryScanToMarked(); Debug.Log("########### QueryScanToMarked for color " + colorType.ToString() + " count = " + count); } } } }
public void QueryAttachGemToHex(GameObject go) { foreach (GameObject tObj in HexObjectList) { HexObject objectScript = tObj.GetComponent <HexObject> (); if (objectScript._Type == HexObject.eType.Main) { } } }
void QuerySetObjectsLoaded() { foreach (GameObject tObj in HexObjectList) { HexObject objectScript = tObj.GetComponent <HexObject> (); objectScript._State = HexObject.eState.Loaded; objectScript.InitHexLinkList(); } }
public static HexObject[] FindHexObjects(this HexGrid Instance, Hex[] hexes) { HexObject[] retVal = new HexObject[hexes.Length]; for (int i = 0; i < retVal.Length; i++) { retVal[i] = Instance.FindHexObject(hexes[i].cubeCoords); } return(retVal); }
private void TriangulateWithoutRiver(HexDirection direction, HexObject cell, Vector3 center, EdgeVertices e) { TriangulateEdgeFan(center, e, cell.Color); if (cell.HasRoads) { Vector2 interpolators = GetRoadInterpolators(direction, cell); TriangulateRoad(center, Vector3.Lerp(center, e.v1, interpolators.x), Vector3.Lerp(center, e.v5, interpolators.y), e, cell.HasRoadThroughEdge(direction)); } }
private void SyncCurrentMaterial(HexObject hexObject) { Material material = hexObject.GetComponent <Renderer>().material; if (material != null) { string sMaterialName = material.name; hexObject.hex.hexType = GetHexMaterialType(sMaterialName); } }
private void AddHexToChunk(int x, int z, HexObject hex) { int chunkX = x / HexMetrics.Instance.chunkSizeX; int chunkZ = z / HexMetrics.Instance.chunkSizeZ; HexGridChunk chunk = chunks[chunkX + chunkZ * ChunkCountX]; int localX = x - chunkX * HexMetrics.Instance.chunkSizeX; int localZ = z - chunkZ * HexMetrics.Instance.chunkSizeZ; chunk.AddHex(localX + localZ * HexMetrics.Instance.chunkSizeX, hex); }
private void CreateCells() { Hexes = new HexObject[Width * Height]; for (int z = 0, i = 0; z < Height; z++) { for (int x = 0; x < Width; x++) { CreateCell(x, z, i++); } } }
private void TriangulateWithRiver(HexDirection direction, HexObject cell, Vector3 center, EdgeVertices e) { Vector3 centerL; Vector3 centerR; if (cell.HasRiverThroughEdge(direction.Opposite())) { centerL = center + HexMetrics.GetFirstSolidCorner(direction.Previous()) * 0.25f; centerR = center + HexMetrics.GetSecondSolidCorner(direction.Next()) * 0.25f; } else if (cell.HasRiverThroughEdge(direction.Next())) { centerL = center; centerR = Vector3.Lerp(center, e.v5, 2f / 3f); } else if (cell.HasRiverThroughEdge(direction.Previous())) { centerL = Vector3.Lerp(center, e.v1, 2f / 3f); centerR = center; } else if (cell.HasRiverThroughEdge(direction.Next2())) { centerL = center; centerR = center + HexMetrics.GetSolidEdgeMiddle(direction.Next()) * (0.5f * HexMetrics.Instance.innerToOuter); } else { centerL = center + HexMetrics.GetSolidEdgeMiddle(direction.Previous()) * (0.5f * HexMetrics.Instance.innerToOuter); centerR = center; } center = Vector3.Lerp(centerL, centerR, 0.5f); EdgeVertices m = new EdgeVertices(Vector3.Lerp(centerL, e.v1, 0.5f), Vector3.Lerp(centerR, e.v5, 0.5f), 1f / 6f); m.v3.y = center.y = e.v3.y; TriangulateEdgeStrip(m, cell.Color, e, cell.Color); terrain.AddTriangle(centerL, m.v1, m.v2); terrain.AddTriangleColor(cell.Color); terrain.AddQuad(centerL, center, m.v2, m.v3); terrain.AddQuadColor(cell.Color); terrain.AddQuad(center, centerR, m.v3, m.v4); terrain.AddQuadColor(cell.Color); terrain.AddTriangle(centerR, m.v4, m.v5); terrain.AddTriangleColor(cell.Color); if (!cell.IsUnderwater) { bool reversed = cell.IncomingRiver == direction; TriangulateRiverQuad(centerL, centerR, m.v2, m.v4, cell.RiverSurfaceY, 0.4f, reversed); TriangulateRiverQuad(m.v2, m.v4, e.v2, e.v4, cell.RiverSurfaceY, 0.6f, reversed); } }
private IEnumerator ExecuteSelectionWithDelay(HexObject aHexObject) { yield return(new WaitForSeconds(0.3f)); if (!isDragging) { GameController.gc.SetOutlinerPosition(aHexObject); } StopCoroutine(ExecuteSelectionWithDelay(aHexObject)); isSelectionBeingDelayed = false; }
public void QueryClearScan() { foreach (GameObject tObj in HexObjectList) { HexObject objectScript = tObj.GetComponent <HexObject> (); if (objectScript._Type == HexObject.eType.Main) { objectScript.ScanColor = (int)GemObject.eColorType.Black; } } }