public virtual void Stop(HexXY p) { }
static void PerformInterfaceMove(Interfacing.EntityHandle objHandle, HexXY pos, float timeToGetThere) { GameObject obj = entities[objHandle]; obj.GetComponent <EntityGraphics>().Move(pos, timeToGetThere); }
public void CreateMapMeshes() { meshGenerator = new HexMeshGenerator(meshPatchSize, meshSubdivsShift); //DEBUG some walls foreach (var cell in map.AllCells()) { if (HexXY.Dist(new HexXY(map.size / 2, map.size / 2), cell.Coords) < map.size / 3) { cell.Cell.state = MapCell.State.Excavated; } } //------------- int s = (map.size - 1) / meshPatchSize + 1; patchesX0 = -2 * s; patchesY0 = -1; patchesW = 3 * s; patchesH = 2 * s + 1; mapPatchObjs = new Transform[patchesH * patchesW]; if (patchesRoot != null) { DestroyImmediate(patchesRoot.gameObject); } patchesRoot = new GameObject(mapPatchesName).transform; patchesRoot.localScale = new Vector3(mapScale, mapScale, mapScale); patchesRoot.parent = this.transform; for (int i = 0; i < patchesH; i++) { for (int j = 0; j < patchesW; j++) { Transform patch = CreatePatch(patchesY0 + i, patchesX0 + j); if (patch != null) { patch.SetParent(patchesRoot, false); Vector2 pos = ((patchesX0 + j) * HexMeshGenerator.rhex + (patchesY0 + i) * HexMeshGenerator.rhey) * meshPatchSize; patch.localPosition = new Vector3(pos.x, 0, pos.y); mapPatchObjs[i * patchesW + j] = patch; } } } //DEBUG patches bounds checking... //List<Vector2Int> patchIdxs = new List<Vector2Int>(); //int minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue; //foreach(var cell in map.AllCells()) //{ // HexMeshGenerator.ListPatchIndicesForCell(meshPatchSize, cell.Coords, patchIdxs); // foreach(Vector2Int idx in patchIdxs) // { // minX = Mathf.Min(idx.x, minX); // maxX = Mathf.Max(idx.x, maxX); // minY = Mathf.Min(idx.y, minY); // maxY = Mathf.Max(idx.y, maxY); // } // //Debug.Log("Coords: " + cell.Coords.ToString()); // //Debug.Log(string.Join(" ", patchIdxs.Select(x => x.ToString()).ToArray())); //} //Debug.Log(map.size); //Debug.Log(string.Format("{0},{1},{2},{3}", minX, minY, maxX, maxY)); //if (minX < patchesX0 || maxX >= patchesX0 + patchesW || minY < patchesY0 || maxY >= patchesY0 + patchesH) // Debug.Log(string.Format("Wrong! {0} {1} {2} {3}", patchesX0, patchesY0, patchesX0 + patchesW - 1, patchesY0 + patchesH - 1)); }
void GenetareGrid(WorldBlock wb, float hexInset, List <Vector3> vertices, List <Vector3> normals, List <int> triangles) { float gridYPos = 0.02f; GameObject obj = new GameObject("Grid"); obj.transform.SetParent(transform, false); var meshRenderer = obj.AddComponent <MeshRenderer>(); var meshFilter = obj.AddComponent <MeshFilter>(); var mesh = new Mesh(); meshRenderer.sharedMaterial = (Material)Resources.Load("Terrain/Grid"); for (int x = 0; x < WorldBlock.sz; x++) { for (int y = 0; y < WorldBlock.sz; y++) { var currType = wb.GetCellType(new HexXY(x, y)); for (int n = 0; n < 3; n++) { HexXY curr = new HexXY(x, y); HexXY ng = curr + HexXY.neighbours[n]; HexXY ngr = curr + HexXY.neighbours[(n + 1) % 6]; if (currType == TerrainCellType.Empty && (!WorldBlock.IsInRange(ng) || wb.GetCellType(ng) == TerrainCellType.Empty)) { continue; } Vector2 currCoord = curr.ToPlaneCoordinates(); Vector2 ngCoord = ng.ToPlaneCoordinates(); //Line between two Vector2 vert = cellVertices[n] * hexInset + currCoord; vertices.Add(new Vector3(vert.x, gridYPos, vert.y)); vert = cellVertices[(n + 1) % 6] * hexInset + currCoord; vertices.Add(new Vector3(vert.x, gridYPos, vert.y)); vert = cellVertices[(n + 3) % 6] * hexInset + ngCoord; vertices.Add(new Vector3(vert.x, gridYPos, vert.y)); vert = cellVertices[(n + 4) % 6] * hexInset + ngCoord; vertices.Add(new Vector3(vert.x, gridYPos, vert.y)); triangles.Add(vertices.Count - 4 + 1); triangles.Add(vertices.Count - 4); triangles.Add(vertices.Count - 4 + 2); triangles.Add(vertices.Count - 4 + 2); triangles.Add(vertices.Count - 4); triangles.Add(vertices.Count - 4 + 3); //Triangle between three if (currType == TerrainCellType.Empty && (!WorldBlock.IsInRange(ngr) || wb.GetCellType(ngr) == TerrainCellType.Empty) && (!WorldBlock.IsInRange(ng) || wb.GetCellType(ng) == TerrainCellType.Empty)) { continue; } Vector2 ngrCoord = ngr.ToPlaneCoordinates(); vert = cellVertices[(n + 1) % 6] * hexInset + currCoord; vertices.Add(new Vector3(vert.x, gridYPos, vert.y)); vert = cellVertices[(n + 3) % 6] * hexInset + ngCoord; vertices.Add(new Vector3(vert.x, gridYPos, vert.y)); vert = cellVertices[(n + 5) % 6] * hexInset + ngrCoord; vertices.Add(new Vector3(vert.x, gridYPos, vert.y)); triangles.Add(vertices.Count - 3); triangles.Add(vertices.Count - 3 + 1); triangles.Add(vertices.Count - 3 + 2); } } } for (int i = 0; i < vertices.Count; i++) { normals.Add(new Vector3(0, 1, 0)); } mesh.vertices = vertices.ToArray(); mesh.normals = normals.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateBounds(); meshFilter.mesh = mesh; }
void GenerateNewWalls(WorldBlock wb) { wallsParent = new GameObject("Walls"); wallsParent.SetActive(enableWalls); wallsParent.transform.SetParent(transform, false); wallsParent.transform.localPosition = Vector3.zero; bool[] emptyNs = new bool[6]; for (int x = 0; x < WorldBlock.sz; x++) { for (int y = 0; y < WorldBlock.sz; y++) { HexXY p = new HexXY(x, y); Vector2 coords = p.ToPlaneCoordinates(); if (Level.S.GetCellType(p + wb.Offset) == TerrainCellType.Empty) { continue; } for (int n = 0; n < 6; n++) { emptyNs[n] = Level.S.GetCellType(p + wb.Offset + HexXY.neighbours[n]) == TerrainCellType.Empty; } if (emptyNs.Count(e => e) == 1) { int wallDir = -1; for (int n = 0; n < 6; n++) { if (emptyNs[n]) { wallDir = n; } } var wall = Instantiate(Resources.Load <GameObject>("Prefabs/OutCorner")); wall.transform.SetParent(wallsParent.transform, false); wall.transform.localPosition = new Vector3(coords.x, 0, coords.y); wall.transform.localRotation = Quaternion.Euler(0, -180 + wallDir * 60, 0); wall.SetActive(true); } else if (emptyNs.Count(e => e) == 2) { int wallDir = -1; for (int n = 0; n < 6; n++) { if (emptyNs[n] && emptyNs[(n + 1) % 6]) { wallDir = n; } } if (wallDir == -1) { continue; } var wall = Instantiate(Resources.Load <GameObject>("Prefabs/NewWall")); wall.transform.SetParent(wallsParent.transform, false); wall.transform.localPosition = new Vector3(coords.x, 0, coords.y); wall.transform.localRotation = Quaternion.Euler(0, -60 + wallDir * 60, 0); wall.SetActive(true); } else if (emptyNs.Count(e => e) == 3) { int wallDir = -1; for (int n = 0; n < 6; n++) { if (emptyNs[(n + 5) % 6] && emptyNs[n % 6] && emptyNs[(n + 1) % 6]) { wallDir = n; } } if (wallDir == -1) { continue; } var wall = Instantiate(Resources.Load <GameObject>("Prefabs/InCorner")); wall.transform.SetParent(wallsParent.transform, false); wall.transform.localPosition = new Vector3(coords.x, 0, coords.y); wall.transform.localRotation = Quaternion.Euler(0, wallDir * 60, 0); wall.SetActive(true); } } } }
public AddEntity(Entity ent, HexXY p) { this.ent = ent; this.p = p; }
public MoveEntity(Entity ent, HexXY from, HexXY to) { this.ent = ent; this.from = from; this.to = to; }
public ChangeTriggerZone(HexXY p, uint zone) { this.p = p; this.zone = zone; }
void PaintOneCell(HexXY p, TerrainCellType type, HashSet <WorldBlock> changedBlocks, TerrainCellType oldCellType, bool writeChanges) { var changedWb = Level.S.SetCellType(p, type); if (Level.S.GetCellType(p) != type) { return; //Set failure } if (writeChanges) { changes.Push(new CellChange() { oldCellType = oldCellType, newCellType = type, p = p }); } //Changed blocks if (!changedBlocks.Contains(changedWb)) { changedBlocks.Add(changedWb); } HexXY locp = Level.GetLocalCoords(p); //Other blocks possible walls fix if (locp.x == 0) { var nbl = Level.S.GetBlock(changedWb.position - new HexXY(1, 0)); if (nbl != null && !changedBlocks.Contains(nbl)) { changedBlocks.Add(nbl); } } if (locp.y == 0) { var nbl = Level.S.GetBlock(changedWb.position - new HexXY(0, 1)); if (nbl != null && !changedBlocks.Contains(nbl)) { changedBlocks.Add(nbl); } } if (locp.x == WorldBlock.sz - 1) { var nbl = Level.S.GetBlock(changedWb.position + new HexXY(1, 0)); if (nbl != null && !changedBlocks.Contains(nbl)) { changedBlocks.Add(nbl); } } if (locp.y == WorldBlock.sz - 1) { var nbl = Level.S.GetBlock(changedWb.position + new HexXY(0, 1)); if (nbl != null && !changedBlocks.Contains(nbl)) { changedBlocks.Add(nbl); } } }
void PerformInterfaceTeleport(Interfacing.EntityHandle objHandle, HexXY to) { GameObject obj = entities[objHandle]; obj.GetComponent <EntityGraphics>().Teleport(to); }
void Update() { if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) { return; } HexXY p = getMouseOverTile(); //Info canvas.transform.Find("StatsPanel").Find("CursorCoordsText").GetComponent <Text>().text = p.x + " " + p.y + "\n" + Level.S.GetPFBlockedMap(p).ToString(); //Drawing terrain if (Input.GetMouseButton(0) && brushCellType.HasValue) { if (p != brushOldMousePos) { if (currentTerrainPaint == null) { currentTerrainPaint = new Undos.TerrainPaint(); } currentTerrainPaint.Paint(p, brushSize, shouldPaintOnEmpty, brushCellType.Value); } brushOldMousePos = p; } else { brushOldMousePos = null; if (currentTerrainPaint != null) { var changedBlocks = Level.S.RecalcPassabilityOnEdges(); foreach (var wb in changedBlocks) { TerrainController.S.RecreateHexTerrain(wb); } undos.Push(currentTerrainPaint); redos.Clear(); currentTerrainPaint = null; } } //Putting runes PutRunes(p); //Putting entities PutEntities(p); //Changing passability if (isInPassabilityMode && Input.GetMouseButtonDown(0)) { ChangeStaticPassability(p); } if (isInTriggerSetMode && Input.GetMouseButtonDown(0)) { ChangeTriggerZone(p); } //Undo and redo if (Input.GetKeyDown(KeyCode.BackQuote) && undos.Count > 0) { var op = undos.Pop(); op.Undo(); redos.Push(op); } else if (Input.GetKeyDown(KeyCode.Alpha1) && redos.Count > 0) { var op = redos.Pop(); op.Redo(); undos.Push(op); } }
void PutEntities(HexXY p) { if (!isInEntitySetMode) { return; } //Creating/selecting/rotating if (Input.GetMouseButtonDown(0)) { if (Input.GetKey(KeyCode.LeftControl)) { var ent = Level.S.GetEntities(p).FirstOrDefault(); if (ent != null && ent is IRotatable && !(ent is Rune)) { var op = new Undos.RotateEntity(ent, p); op.Rotate(); undos.Push(op); redos.Clear(); } } else { var existingEntity = Level.S.GetEntities(p).FirstOrDefault(); if (existingEntity != null) { Selection.activeGameObject = entities[existingEntity.graphicsHandle]; draggedEntity = existingEntity; draggedEntityOrigPos = draggedEntityCurrPos = existingEntity.pos; } else //TODO: make possible stacking entities with Alt or something? { var prefab = Selection.activeGameObject; if (prefab == null || PrefabUtility.GetPrefabType(prefab) == PrefabType.None) { return; } var gr = prefab.GetComponent <EntityGraphics>(); if (gr == null) { return; } var ent = gr.CreateEntity(); var op = new Undos.AddEntity(ent, p); op.Add(); undos.Push(op); redos.Clear(); } } } //Deleting if (Input.GetKeyDown(KeyCode.Space)) { var ent = Level.S.GetEntities(p).FirstOrDefault(); if (ent != null) { var op = new Undos.RemoveEntity(ent, p); op.Remove(); undos.Push(op); redos.Clear(); } } //Dragging if (draggedEntity != null) { if (Input.GetMouseButton(0)) { //Drag if (p != draggedEntityCurrPos) { var op = new Undos.MoveEntity(draggedEntity, draggedEntityCurrPos, p); op.Move(); draggedEntityCurrPos = p; } } else { //Release if (draggedEntityCurrPos != draggedEntityOrigPos) { var op = new Undos.MoveEntity(draggedEntity, draggedEntityOrigPos, draggedEntityCurrPos); op.Move(); redos.Clear(); undos.Push(op); } draggedEntity = null; } } }
public RotateEntity(Entity ent, HexXY p) { this.ent = ent; this.p = p; }
public void DrawWithBrush() { if (currentMousePos == null) { return; } HexXY coords = currentMousePos.Value.Coords; HashSet <Vector2Int> patchesChanged = new HashSet <Vector2Int>(); List <Vector2Int> patchIndicesForCell = new List <Vector2Int>(); for (int i = -brushSize + 1; i < brushSize; i++) { for (int j = -brushSize + 1; j < brushSize; j++) { if (HexXY.Dist(new HexXY(i, j), new HexXY(0, 0)) > brushSize - 1) { continue; } HexXY c = coords + new HexXY(i, j); MapCell cell = map.GetCell(c); if (cell == map.externalCell) { continue; } bool changed = false; switch (brushMode) { case BrushMode.Excavate: if (cell.state != MapCell.State.Excavated) { changed = true; cell.state = MapCell.State.Excavated; } break; case BrushMode.Fill: if (cell.state != MapCell.State.Full) { changed = true; cell.state = MapCell.State.Full; } break; } if (changed) { HexMeshGenerator.ListPatchIndicesForCell(meshPatchSize, c, patchIndicesForCell); foreach (Vector2Int pi in patchIndicesForCell) { patchesChanged.Add(pi); } } } } if (patchesChanged.Count > 0) { //Fix outermost wall issues (when using ListPatchIndicesForCell it may happen that we don't redraw outermost walls) //We do this by adding cells just outside the brush if anything is changed for (int i = -brushSize; i < brushSize + 1; i++) { for (int j = -brushSize; j < brushSize + 1; j++) { if (HexXY.Dist(new HexXY(i, j), new HexXY(0, 0)) == brushSize) { HexXY c = coords + new HexXY(i, j); HexMeshGenerator.ListPatchIndicesForCell(meshPatchSize, c, patchIndicesForCell); foreach (Vector2Int pi in patchIndicesForCell) { patchesChanged.Add(pi); } } } } } foreach (Vector2Int c in patchesChanged) { RecreateMapPatchMesh(c); } if (patchesChanged.Count > 0) { EditorUtility.SetDirty(map); } EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); }
public virtual void Attack(HexXY p) { }
public ChangePassability(HexXY p) { this.p = p; }
public void Teleport(HexXY to) { Vector2 planeCoord = to.ToPlaneCoordinates(); transform.position = new Vector3(planeCoord.x, 0, planeCoord.y); }
public override void Spawn(HexXY p) { base.Spawn(p); Level.S.SetPFBlockedMap(p, WorldBlock.PFBlockType.DynamicBlocked); }
public RemoveEntity(Entity ent, HexXY p) { this.ent = ent; this.p = p; }
void EditorStart() { sourceSpellPos = ((Statue)entity).sourceSpellPos; }
public void GenerateMultiple(WorldBlock wb, float hexInset, float terrainTexScale) { int cellvcnt = cellVertices.Length; int celltcnt = cellTriangles.Length; var vertices = new List <Vector3>(); var normals = new List <Vector3>(); var uvs = new List <Vector2>(); var triangles = new List <int>(); for (int k = 1; k < WorldBlock.terrainTypesCount; k++) { if (wb.cellTypeCounts[k] == 0) { continue; } string terrainTypeName = ((TerrainCellType)k).ToString(); GameObject obj = new GameObject(terrainTypeName); obj.transform.SetParent(transform, false); var meshRenderer = obj.AddComponent <MeshRenderer>(); var meshFilter = obj.AddComponent <MeshFilter>(); var mesh = new Mesh(); meshRenderer.sharedMaterial = (Material)Resources.Load("Terrain/" + terrainTypeName); for (int x = 0; x < WorldBlock.sz; x++) { for (int y = 0; y < WorldBlock.sz; y++) { int ctype = (int)wb.GetCellType(new HexXY(x, y)); if (ctype != k) { continue; } //Main tile HexXY curr = new HexXY(x, y); Vector2 coord = curr.ToPlaneCoordinates(); for (int v = 0; v < cellvcnt; v++) { float vx = cellVertices[v].x + coord.x; float vy = cellVertices[v].y + coord.y; vertices.Add(new Vector3(vx, 0, vy)); uvs.Add(new Vector2(vx * terrainTexScale, vy * terrainTexScale)); } for (int t = 0; t < celltcnt; t++) { triangles.Add(vertices.Count - cellvcnt + cellTriangles[t]); } } } for (int i = 0; i < vertices.Count; i++) { normals.Add(new Vector3(0, 1, 0)); } mesh.vertices = vertices.ToArray(); mesh.normals = normals.ToArray(); mesh.uv = uvs.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateBounds(); meshFilter.mesh = mesh; vertices.Clear(); normals.Clear(); uvs.Clear(); triangles.Clear(); } //Grid GenetareNewGrid(wb, hexInset, vertices, normals, triangles); GenerateNewWalls(wb); }
public NearCells(int x1, int y1) { this.Count = 1; this.C1 = new HexXY(x1, y1); this.C2 = this.C3 = new HexXY(); }
public void GenetareNewGrid(WorldBlock wb, float hexInset, List <Vector3> vertices, List <Vector3> normals, List <int> triangles) { var oldGrid = transform.Find("Grid"); if (oldGrid != null) { Destroy(oldGrid.gameObject); } GameObject obj = new GameObject("Grid"); obj.transform.SetParent(transform, false); obj.transform.localPosition = new Vector3(0, 0.005f, 0); var meshRenderer = obj.AddComponent <MeshRenderer>(); var meshFilter = obj.AddComponent <MeshFilter>(); var mesh = new Mesh(); meshRenderer.sharedMaterial = (Material)Resources.Load("Terrain/Grid"); float gridInsetOut = 1f, gridInsetIn = 0.925f; for (int x = 0; x < WorldBlock.sz; x++) { for (int y = 0; y < WorldBlock.sz; y++) { var blType = wb.pfBlockedMap[x, y]; if (blType == WorldBlock.PFBlockType.StaticBlocked || blType == WorldBlock.PFBlockType.EdgeBlocked) { continue; } Vector2 currCoord = new HexXY(x, y).ToPlaneCoordinates(); int vertOffset = vertices.Count; for (int i = 0; i < 6; i++) { Vector2 vertOut = cellVertices[i] * gridInsetOut + currCoord; vertices.Add(new Vector3(vertOut.x, 0, vertOut.y)); Vector2 vertIn = cellVertices[i] * gridInsetIn + currCoord; vertices.Add(new Vector3(vertIn.x, 0, vertIn.y)); } for (int i = 0; i < 10; i += 2) { triangles.Add(vertOffset + i); triangles.Add(vertOffset + i + 2); triangles.Add(vertOffset + i + 1); triangles.Add(vertOffset + i + 1); triangles.Add(vertOffset + i + 2); triangles.Add(vertOffset + i + 3); } triangles.Add(vertOffset); triangles.Add(vertOffset + 11); triangles.Add(vertOffset + 10); triangles.Add(vertOffset); triangles.Add(vertOffset + 1); triangles.Add(vertOffset + 11); } } for (int i = 0; i < vertices.Count; i++) { normals.Add(new Vector3(0, 1, 0)); } mesh.vertices = vertices.ToArray(); mesh.normals = normals.ToArray(); mesh.triangles = triangles.ToArray(); mesh.RecalculateBounds(); meshFilter.mesh = mesh; }
public override void Move(HexXY pos, float timeToGetThere) { //Debug.Log(entityHandle + " move " + args.pos); MovePrecise(pos.ToPlaneCoordinates(), timeToGetThere); }
static void PerformInterfaceSpawn(Interfacing.EntityHandle objHandle, HexXY pos, uint dir) { GameObject obj = entities[objHandle]; obj.GetComponent <EntityGraphics>().Spawn(pos, dir); }
public virtual void Move(HexXY pos, float timeToGetThere) { }
static void PerformInterfaceStop(Interfacing.EntityHandle objHandle, HexXY pos) { GameObject obj = entities[objHandle]; obj.GetComponent <EntityGraphics>().Stop(pos); }
public MapCellAndCoords(MapCell cell, HexXY coords) { this.Cell = cell; this.Coords = coords; }