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 GenerateMap() { if (chunkPrefab == null) { Debug.LogError("ChunkPrefab is null for WorldHandler"); return; } foreach (Transform t in transform) { Destroy(t.gameObject); } chunks = new HexChunk[chunksX, chunksY]; for (int x = 0; x < chunksX; x++) { for (int y = 0; y < chunksY; y++) { GameObject obj = Instantiate(chunkPrefab, Vector3.zero, Quaternion.identity); HexChunk chunk = obj.GetComponent <HexChunk>(); if (ReferenceEquals(chunk, null)) { Debug.LogError("HexChunk not found on ChunkPrefab"); return; } obj.transform.name = "Chunk (" + x + "x" + y + ")"; obj.transform.parent = transform; chunk.chunkPos = new Vector2Int(x, y); chunks[x, y] = chunk; } } }
public void MoveChildren() { bool shouldDestroy = false; for (int c = transform.childCount - 1; c >= 0; c--) { Transform child = transform.GetChild(c); HexChunk chunk = child.GetComponent <HexChunk>(); if (chunk != null) { chunk.MoveChildren(); } else { Hexagon hex = child.GetComponent <Hexagon>(); hex.gameObject.SetActive(true); Vector3 hexPos = hex.transform.position; HexGrid grid = GameObject.FindWithTag("Hexgrid").GetComponent <HexGrid>(); hexPos.y = grid.GetYPos(hex.xValue, hex.zValue); hex.SetPositions(hexPos); hex.transform.parent = transform.parent; shouldDestroy = true; } } if (shouldDestroy) { Destroy(gameObject); } }
void HighlightHoveredCell() { Rect screenRect = new Rect(0, 0, Screen.width, Screen.height); if (!screenRect.Contains(Input.mousePosition)) { return; } Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(inputRay, out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer("Chunks"))) { HexChunk chunk = hit.collider.gameObject.GetComponent <HexChunk>(); HexCell cell = chunk.subtestCollision(inputRay); if (cell != null) { cell.setHighlighted(); if (chunk != highlightedChunk) { highlightedChunk.RerenderCells(); highlightedChunk = chunk; } Debug.Log(cell.name); chunk.RerenderCells(); } } }
HexChunk GetChunkWithHexCoords(Vector3 coords) { int chunk_x = Mathf.FloorToInt(coords.x / HexChunksManager.instance.chunkSize.x); int chunk_z = Mathf.FloorToInt(coords.z / HexChunksManager.instance.chunkSize.z); string s = chunk_x.ToString() + ":" + chunk_z.ToString(); HexChunk hc = GameObject.Find(s).GetComponent <HexChunk>(); return(hc); }
public void CreateMap() { mFilter = GetComponent <MeshFilter>(); mRenderer = GetComponent <MeshRenderer>(); hexChunk = GetComponent <HexChunk>(); Mesh mesh = new Mesh(); hexChunk.ApplyToMesh(mesh); mFilter.sharedMesh = mesh; }
public void RemoveHex() { if (AimHex.isHexAimedAt) { HexChunk hc = GetChunkWithHexCoords(AimHex.hexAimedAt); Vector3Int v = HexCoordsWithinChunk(AimHex.hexAimedAt); hc.SetBlocks(v.x, v.y, v.z, 0); hc.ReCreateMeshes(); } }
IEnumerator ChangeHeightMap(string name) { heightMap = Resources.Load <Texture2D>("Images/" + name); foreach (GameObject obj in chunks) { HexChunk hc = obj.GetComponent <HexChunk>(); hc.MoveChildren(); StartCoroutine("ReCombine", hc.gameObject); yield return(null); } }
public void GenerateChunk(HexCords chunk_id) { if (!chunks.ContainsKey(chunk_id.hex_crds)) { HexChunk chunk = new HexChunk(chunk_id); HexChunkObject chunk_obj = Instantiate(chunkprefab); chunks.Add(chunk_id.hex_crds, chunk_obj); chunk_obj.transform.SetParent(transform, false); chunk_obj.data = chunk; chunk_obj.hex_grid_ref = this; chunk_obj.GenerateCells(); } }
public void RenderMap() { foreach (Transform t in transform) { HexChunk chunk = t.gameObject.GetComponent <HexChunk>(); if (ReferenceEquals(chunk, null)) { Debug.LogError("HexChunk not found on chunk in world"); continue; } chunk.RenderChunk(); } }
void GenChunks() { chunks = new HexChunk[chunkCount_Z * chunkCount_X]; for (int z = 0, i = 0; z < chunkCount_Z; z++) { for (int x = 0; x < chunkCount_X; x++, i++) { HexChunk chunk = Instantiate(chunkPrefab, transform); chunk.name = "chunk" + i.ToString(); chunks[i] = chunk; } } }
void MakeChunks() { int x, y; for (x = 0; x < widthInChunks; x++) { for (y = 0; y < heightInChunks; y++) { chunks[x, y] = Instantiate <HexChunk>(hexChunkPrefab); chunks[x, y].name = "Chunk " + x + "," + y; MakeChunk(x, y); } } highlightedChunk = chunks[0, 0]; }
public void AddNeighborChunk(bool isAbove, HexChunk neighbor) { if (this == neighbor) { //this never gets tripped apparently Debug.Log("Error: Chunk given self as neighbor"); return; } if (isAbove) { for (int i = 0; i < HexMetrics.chunkSize; ++i) { HexMesh from = hexMeshes[i, 0]; HexMesh to = neighbor.hexMeshes[i, 5]; Debug.Log("From hex: " + from.xOff + ", " + from.zOff); Debug.Log("To hex: " + to.xOff + ", " + to.zOff); AddBridgeOutside(HexDirection.SE, from, to, translationVector, neighbor.translationVector); if (i > 0) { HexMesh toSW = neighbor.hexMeshes[i - 1, 5]; AddBridgeOutside(HexDirection.SW, from, toSW, translationVector, neighbor.translationVector); } } } else { for (int i = 0; i < HexMetrics.chunkSize; ++i) { HexMesh from = hexMeshes[0, i]; HexMesh to = neighbor.hexMeshes[neighbor.hexMeshes.GetLength(0) - 1, i]; Debug.Log("From hex: " + from.xOff + ", " + from.zOff); Debug.Log("To hex: " + to.xOff + ", " + to.zOff); AddBridgeOutside(HexDirection.W, from, to, translationVector, neighbor.translationVector); if (i % 2 == 0) { to = neighbor.hexMeshes[neighbor.hexMeshes.GetLength(0) - 1, i + 1]; AddBridgeOutside(HexDirection.NW, from, to, translationVector, neighbor.translationVector); if (i != 0) { to = neighbor.hexMeshes[neighbor.hexMeshes.GetLength(0) - 1, i - 1]; AddBridgeOutside(HexDirection.SW, from, to, translationVector, neighbor.translationVector); } } } } }
void HandleInput() { Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(inputRay, out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer("Chunks"))) { Debug.Log(hit.collider.gameObject.name); HexChunk chunk = hit.collider.gameObject.GetComponent <HexChunk>(); HexCell cell = chunk.subtestCollision(inputRay); if (cell != null) { cell.recursivelyAdjustHeight(20, 5, 1); chunk.RerenderCells(); } } }
private void CreateChunk(int x, int z) { bool hasNeighborBelow = (z > 0); bool hasNeighborToLeft = (x > 0); GameObject[] allChunks = GameObject.FindGameObjectsWithTag("HexChunk"); numChunks = allChunks.Length; //Debug.Log(numChunks + " existing chunks"); if ((x * zChunks) + z < numChunks) //don't make a chunk if it already exists { return; } GameObject newChunkObject = Instantiate(chunk); newChunkObject.tag = "HexChunk"; HexChunkDisplay newDisplay = newChunkObject.GetComponent <HexChunkDisplay>(); HexChunk newChunk = newDisplay.Chunk; chunkDisplays.Add(newDisplay); chunks.Add(newChunk); newDisplay.OffsetX = x; newDisplay.OffsetZ = z; chunkMap[x, z] = newChunk; Vector3 tVector = new Vector3(x * HexMetrics.chunkWidth, 0f, z * HexMetrics.chunkHeight); //Debug.Log("Translation Vector: " + tVector.x + ", " + tVector.y + ", " + tVector.z); tVector += newChunkObject.transform.position; //newChunk.Translate(tVector); //newChunkObject.transform.position = tVector; newChunk.CreateGrid(HexMetrics.chunkSize, HexMetrics.chunkSize); //newChunk.CompleteGrid(); newChunk.Translate(tVector); if (x > 0) { newChunk.AddNeighborChunk(false, chunkMap[x - 1, z]); } if (z > 0) { newChunk.AddNeighborChunk(true, chunkMap[x, z - 1]); } //don't do this until after the edges are stitched newDisplay.CreateMap(); }
public void Combine(HexChunk other) { long start = Math.Min(Offset, other.Offset); long end = Math.Max(Offset + Data.Length, other.Offset + other.Data.Length); int length = (int)(end - start); byte[] buffer = new byte[length]; int ss = (int)(Offset - start); for (int i = 0; i < Data.Length; i++) { buffer[ss + i] = Data[i]; } ss = (int)(other.Offset - start); for (int i = 0; i < other.Data.Length; i++) { buffer[ss + i] = other.Data[i]; } Offset = start; Data = buffer; }
private void DespawnChunk(HexChunk chunk) { List <Hex> hexKeys = new List <Hex>(chunk.grid.Keys); foreach (Hex key in hexKeys) { Hex hex = key; HexObject hexObject = chunk.grid[key]; if (hex != null && hexObject != null) { SyncCurrentMaterial(hexObject); GameObject.Destroy(hexObject.transform.gameObject); } } GameObject.Destroy(chunk.chunkObject.transform.gameObject); chunk.goChunk = null; chunk.chunkState = HexChunk.ChunkState_e.DESPAWNED; }
private void AppendData(long offset, byte[] data) { HexChunk nchunk = new HexChunk(offset, data); if (Chunks.Count > 0) { HexChunk lchunk = Chunks[Chunks.Count - 1]; if (lchunk.Intersect(nchunk)) { lchunk.Combine(nchunk); return; } for (int i = 0; i < Chunks.Count; i++) { if (Chunks[i].Intersect(nchunk)) { Chunks[i].Combine(nchunk); return; } } } Chunks.Add(nchunk); }
void Update() { if (player == null) { return; } int[] playerChunk = new int[2]; playerChunk[0] = Mathf.FloorToInt(player.transform.position.x / HexChunk.size); playerChunk[1] = Mathf.FloorToInt(player.transform.position.z / HexChunk.size); List <Chunk> chunksKeys; for (int x = playerChunk[0] - viewDistance; x <= playerChunk[0] + viewDistance; x++) { for (int z = playerChunk[1] - viewDistance; z <= playerChunk[1] + viewDistance; z++) { bool spawn = true; chunksKeys = new List <Chunk>(chunks.Keys); foreach (Chunk key in chunksKeys) { Chunk chunk = key; HexChunk hexChunk = chunks[key]; if (hexChunk == null) { continue; } if (hexChunk.chunkObject.chunk.x == x && hexChunk.chunkObject.chunk.z == z) { if (hexChunk.chunkState == HexChunk.ChunkState_e.DESPAWNED) { hexChunk.InitializeChunk(x, z); hexChunk.GenerateGrid(); SpawnChunk(hexChunk); } spawn = false; } } if (spawn) { HexChunk newHexChunk = new HexChunk(); newHexChunk.InitializeChunk(x, z); newHexChunk.GenerateGrid(); SpawnChunk(newHexChunk); chunks.Add(newHexChunk.chunkObject.chunk, newHexChunk); } } } chunksKeys = new List <Chunk>(chunks.Keys); foreach (Chunk key in chunksKeys) { Chunk chunk = key; HexChunk hexChunk = chunks[key]; if ((hexChunk.chunkObject.chunk.x < playerChunk[0] - viewDistance || hexChunk.chunkObject.chunk.x > playerChunk[0] + viewDistance) || (hexChunk.chunkObject.chunk.z <playerChunk[1] - viewDistance || hexChunk.chunkObject.chunk.z> playerChunk[1] + viewDistance)) { if (hexChunk.chunkState == HexChunk.ChunkState_e.SPAWNED) { DespawnChunk(hexChunk); } } } }
/// <summary> /// Record that a chunk was added. /// </summary> private void RecordChunkAddedUndo(HexChunk chunk) { Undo.RegisterCreatedObjectUndo(chunk.gameObject, undoMessage); }
// ------------------------------------------------------- UV Modifying Functions public Vector2 SetHexUVs(Vector3 hitPoint, int i) { // check if initialized if (!isInitialized) { return(-Vector2.one); } // where is this hexagon? float _z = hitPoint.z; _z /= Mathf.Sin(30f * Mathf.Deg2Rad); _z /= 1.5f; _z /= tileRadius; int z = Mathf.FloorToInt(_z); float _x = hitPoint.x; _x /= Mathf.Cos(30f * Mathf.Deg2Rad); _x /= tileRadius; if (z % 2 == 1) { _x -= 0.5f; } int x = Mathf.FloorToInt(_x); // what chunk is it in? int cX = Mathf.FloorToInt(x / chunkSize); int cZ = Mathf.FloorToInt(z / chunkSize); // where is it relative to the chunk? int rX = x - (chunkSize * cX); int rZ = z - (chunkSize * cZ); // debug for testing if (Application.isEditor) { Debug.Log("world pos " + x + " " + z + " : chunk " + cX + " " + cZ + " : chunk pos " + rX + " " + rZ); } // check if in range if (x < 0 || x >= worldSize.x || z < 0 || z >= worldSize.y) { return(-Vector2.one); } // if the value for i is -1, set the hexagon back to the last texture index value assigned if (i == -1) { i = hexWorldData[x, z]; } // update hexWorldData with new value if not value for highlighted if (i != 1) { hexWorldData[x, z] = i; } // tell the chunk to update UVs for selected tile HexChunk currChunk = hexChunks[cX, cZ]; currChunk.SetHexUVs(rX, rZ, i); // return the data coordinates of this hexagon return(new Vector2(x, z)); }
public bool Intersect(HexChunk other) { return(Offset <= other.Offset + other.Data.Length && other.Offset <= Offset + Data.Length); }
/// <summary> /// Record an undo action for a chunk. /// </summary> private void RecordChunkModifiedUndo(HexChunk chunk) { Undo.RegisterCompleteObjectUndo(chunk, undoMessage); Undo.RegisterCompleteObjectUndo(chunk.MeshFilter, undoMessage); Undo.RegisterCompleteObjectUndo(chunk.MeshCollider, undoMessage); }
void ShootTerraformer(string objectHit, Vector3 point, float charge, int dir) { bool hasHit = false; float chargedRadius = radius * (Mathf.Floor(/*Mathf.Clamp(*/ charge /*, 1, maxChargeTime)*/) + 1); Collider[] objectsHit = Physics.OverlapSphere(point, chargedRadius); for (int n = 0; n < objectsHit.Length; n++) { HexChunk hexChunk = objectsHit [n].GetComponent <HexChunk>(); if (hexChunk != null) { //Resettime x 2 uit hexagon prefab hexChunk.StopAllCoroutines(); hexChunk.StartCoroutine("SplitChunk", 20); hexChunk.MoveChildren(point, chargedRadius, dir); hasHit = true; continue; } Hexagon hex = objectsHit [n].GetComponent <Hexagon>(); if (hex != null) { hex.MoveHexagon(point, chargedRadius, dir); hasHit = true; continue; } if (dir == 1) { if (objectsHit [n].tag == "Player") { Vector3 launchDir = Vector3.Normalize(objectsHit [n].transform.position - point); launchDir.y = 1; objectsHit [n].GetComponent <Player_Force>().AddImpact(launchDir, (50 / objectsHit [n].transform.localScale.x) * chargedRadius); } else if (objectsHit [n].tag == "PhysicsObject" || objectsHit [n].tag == "Flag" || objectsHit [n].tag == "Ball") { /*float force = 0; * switch ((int)chargedRadius) * { * case 3: * { * force = 4000; * break; * } * case 6: * { * force = 6000; * break; * } * case 9: * { * force = 8000; * break; * } * }*/ objectsHit [n].GetComponent <Rigidbody>().AddForce(Vector3.up * (1500 + 2000 * (chargedRadius / 3)) /*force*/); if (objectsHit [n].tag == "Ball") { objectsHit [n].GetComponent <GM_Ball>().lastHitBy = transform.name; } } } } if (hasHit) { StartCoroutine(ShootTimer(reloadTime)); StartCoroutine(PlayRubbleSound()); } }
public ModifiedTileInfo(HexChunk chunk, ChunkOperation operation) { Chunk = chunk; Operation = operation; }
public void AddHex() { bool isHexAimedAt = AimHex.isHexAimedAt; int dir = 0; if (isHexAimedAt) { Vector3 hexAimedAt = AimHex.hexAimedAt; Vector3 aimNormal = AimHex.aimNormal; if (aimNormal.y == 0) { if (aimNormal.x > 0 && aimNormal.z > 0) { dir = 1; } if (aimNormal.x > 0 && aimNormal.z == 0) { dir = 2; } if (aimNormal.x > 0 && aimNormal.z < 0) { dir = 3; } if (aimNormal.x < 0 && aimNormal.z < 0) { dir = 4; } if (aimNormal.x < 0 && aimNormal.z == 0) { dir = 5; } if (aimNormal.x < 0 && aimNormal.z > 0) { dir = 6; } } else if (aimNormal.y > 0) { dir = 7; } else if (aimNormal.y < 0) { dir = 8; } Vector3 hexToAdd = hexAimedAt; if (hexAimedAt.z % 2 == 0) { switch (dir) { case 1: hexToAdd = hexAimedAt + new Vector3(0, 0, 1); break; case 2: hexToAdd = hexAimedAt + new Vector3(1, 0, 0); break; case 3: hexToAdd = hexAimedAt + new Vector3(0, 0, -1); break; case 4: hexToAdd = hexAimedAt + new Vector3(-1, 0, -1); break; case 5: hexToAdd = hexAimedAt + new Vector3(-1, 0, 0); break; case 6: hexToAdd = hexAimedAt + new Vector3(-1, 0, 1); break; case 7: hexToAdd = hexAimedAt + new Vector3(0, 1, 0); break; case 8: hexToAdd = hexAimedAt + new Vector3(0, -1, 0); break; } } else { switch (dir) { case 1: hexToAdd = hexAimedAt + new Vector3(1, 0, 1); break; case 2: hexToAdd = hexAimedAt + new Vector3(1, 0, 0); break; case 3: hexToAdd = hexAimedAt + new Vector3(1, 0, -1); break; case 4: hexToAdd = hexAimedAt + new Vector3(0, 0, -1); break; case 5: hexToAdd = hexAimedAt + new Vector3(-1, 0, 0); break; case 6: hexToAdd = hexAimedAt + new Vector3(0, 0, 1); break; case 7: hexToAdd = hexAimedAt + new Vector3(0, 1, 0); break; case 8: hexToAdd = hexAimedAt + new Vector3(0, -1, 0); break; } } HexChunk hc = GetChunkWithHexCoords(hexToAdd); Vector3Int v = HexCoordsWithinChunk(hexToAdd); hc.SetBlocks(v.x, v.y, v.z, current); hc.ReCreateMeshes(); } }