private void AddEvent(MapEvent3D mapEvent) { TacticsTerrainMesh terrain = (TacticsTerrainMesh)target; Map map = terrain.GetComponent <Map>(); GameObjectUtility.SetParentAndAlign(mapEvent.gameObject, map.objectLayer.gameObject); Undo.RegisterCreatedObjectUndo(mapEvent, "Create " + mapEvent.name); mapEvent.SetLocation(new Vector2Int((int)primarySelection.pos.x, (int)primarySelection.pos.z)); Selection.activeObject = mapEvent.gameObject; }
public void Start() { target = initialTarget; if (initialTarget == null) { target = FindObjectOfType <PCEvent>().GetComponent <MapEvent3D>(); } targetDollyPosition = transform.localPosition; CopyTargetPosition(); WarpToTarget(); }
public void WarpToTarget(bool requiresRetarget = false) { if (target == null) { target = initialTarget; } if (requiresRetarget) { targetDollyPosition = transform.localPosition; CopyTargetPosition(); } transform.localPosition = targetDollyPosition; transform.localEulerAngles = targetDollyAngles; cam.transform.localPosition = targetCamPosition; cam.transform.localEulerAngles = targetCamAngles; }
private OrthoDir CalculateDirection() { OrthoDir normalDir = Parent().GetComponent <CharaEvent>().facing; MapCamera cam = Application.isPlaying ? Global.Instance().Maps.Camera : FindObjectOfType <MapCamera>(); if (!cam || !dynamicFacing) { return(normalDir); } Vector3 ourScreen = cam.GetCameraComponent().WorldToScreenPoint(parentEvent.transform.position); Vector3 targetWorld = MapEvent3D.TileToWorldCoords(parentEvent.positionXY + normalDir.XY()); Vector3 targetScreen = cam.GetCameraComponent().WorldToScreenPoint(targetWorld); Vector3 delta = targetScreen - ourScreen; return(OrthoDirExtensions.DirectionOf(new Vector2(delta.x, -delta.y))); }
public void GenerateMesh(MapGenerator lastMap = null) { TacticsTerrainMesh mesh = GetComponent <TacticsTerrainMesh>(); // copy oldbie values if needed if (lastMap != null) { sizeInRoomsMin = lastMap.sizeInRoomsMin; sizeInRoomsMax = lastMap.sizeInRoomsMax; stairLength = lastMap.stairLength; startEventPrefab = lastMap.startEventPrefab; endEventPrefab = lastMap.endEventPrefab; impassEventPrefab = lastMap.impassEventPrefab; defaultImpassTile = lastMap.defaultImpassTile; chestEventPrefab = lastMap.chestEventPrefab; targetPrefab = lastMap.targetPrefab; finalLevel = lastMap.finalLevel; finalLevelPrefab = lastMap.finalLevelPrefab; table = lastMap.table; level = lastMap.level + 1; } if (level == finalLevel) { Map other = Instantiate(finalLevelPrefab); Global.Instance().Maps.activeMap = other; return; } // wipe what's already there mesh.ClearTiles(); foreach (MapEvent toRemove in GetComponent <Map>().GetEvents <MapEvent>()) { if (toRemove.GetComponent <PCEvent>() == null) { if (toRemove.GetComponent <BattleEvent>() && Application.isPlaying) { GetComponent <BattleController>().RemoveUnit(toRemove.GetComponent <BattleEvent>().unit); } GetComponent <Map>().RemoveEvent(toRemove, true); } } // work out some constants if (Flip()) { sizeInRooms = new Vector2Int(sizeInRoomsMin.x, sizeInRoomsMax.y); } else { sizeInRooms = new Vector2Int(sizeInRoomsMax.x, sizeInRoomsMin.y); } int seed = (int)DateTime.Now.Ticks; Random.InitState(seed); Debug.Log("using seed " + seed); rooms = new RoomInfo[sizeInRooms.x, sizeInRooms.y]; sizeInCells = sizeInRooms * 2 - new Vector2Int(1, 1); cells = new CellInfo[sizeInCells.x, sizeInCells.y]; // are we going to add the starting stairwell on the left or right? if (lastMap != null) { if (lastMap.endStairsNW) { entryRoomCoords = new Vector2Int(Random.Range(0, 2), 0); startStairsSW = false; } else { entryRoomCoords = new Vector2Int(0, Random.Range(0, 2)); startStairsSW = true; } exitRoomCoords = new Vector2Int(sizeInRooms.x - 1, sizeInRooms.y - 1); if (Flip()) { exitRoomCoords.x -= Random.Range(0, 2); } else { exitRoomCoords.y -= Random.Range(0, 2); } } else { if (entryRoomCoords == Vector2Int.zero) { startStairsSW = Flip(); } else if (entryRoomCoords.y > 0) { startStairsSW = true; } else { startStairsSW = false; } } if (exitRoomCoords == new Vector2Int(sizeInRooms.x - 1, sizeInRooms.y - 1)) { endStairsNW = Flip(); } else if (exitRoomCoords.x < sizeInRooms.x - 1) { endStairsNW = true; } else { endStairsNW = false; } // set the size and start of each room and hallway cells = new CellInfo[sizeInCells.x, sizeInCells.y]; widths = new int[sizeInCells.x]; heights = new int[sizeInCells.y]; for (int y = 0; y < sizeInCells.y; y += 1) { if (y % 2 == 1) { heights[y] = RandomHallSize(); } else { heights[y] = RandomRoomSize(); } } for (int x = 0; x < sizeInCells.x; x += 1) { if (x % 2 == 1) { widths[x] = RandomHallSize(); } else { widths[x] = RandomRoomSize(); } } for (int y = 0; y < sizeInCells.y; y += 1) { for (int x = 0; x < sizeInCells.x; x += 1) { cells[x, y] = new CellInfo(x, y, widths[x], heights[y]); if (x % 2 == 1 || y % 2 == 1) { if (x % 2 == 1 && y % 2 == 1) { cells[x, y].type = CellInfo.CellType.Pillar; } else { cells[x, y].type = CellInfo.CellType.Hall; } } else { cells[x, y].type = CellInfo.CellType.Room; } if (x == 0) { cells[x, y].startX = startStairsSW ? stairLength : 0; } else { cells[x, y].startX = cells[x - 1, y].startX + widths[x - 1]; } if (y == 0) { cells[x, y].startY = startStairsSW ? 0 : stairLength; } else { cells[x, y].startY = cells[x, y - 1].startY + heights[y - 1]; } } } // set the elevation of each room for (int diag = 0; diag < sizeInRooms.x + sizeInRooms.y; diag += 1) { int x = diag; int y = 0; while (x >= 0) { if (x < sizeInRooms.x && y < sizeInRooms.y) { float z; if (x == 0 && y == 0) { z = stairLength / 2.0f + 0.5f; } else { float oldZ; if (y == 0) { oldZ = rooms[x - 1, y].z; } else if (x == 0) { oldZ = rooms[x, y - 1].z; } else { oldZ = Math.Max(rooms[x, y - 1].z, rooms[x - 1, y].z); } z = RandomNewZ(oldZ); } rooms[x, y] = new RoomInfo(cells[x * 2, y * 2], z); } x -= 1; y += 1; } } // determine the initial passability map cells[0, 0].connected = true; UpdatePassability(); // knock down walls until all rooms are accessible List <RoomInfo> roomsToGo = new List <RoomInfo>(); for (int x = 0; x < sizeInRooms.x; x += 1) { for (int y = 0; y < sizeInRooms.y; y += 1) { roomsToGo.Add(rooms[x, y]); } } while (roomsToGo.Count > 0) { int index = Random.Range(0, roomsToGo.Count); RoomInfo room = roomsToGo[index]; if (room.cell.connected) { roomsToGo.RemoveAt(index); continue; } List <RoomInfo> adjacents = room.cell.AdjacentRooms(this); RandUtils.Shuffle(adjacents); RoomInfo adj = null; foreach (RoomInfo adj2 in adjacents) { if (!adj2.cell.connected) { continue; } CellInfo connector = cells[(room.cell.x + adj2.cell.x) / 2, (room.cell.y + adj2.cell.y) / 2]; if (Mathf.Abs(adj2.z - room.z) / (widths[connector.x] * heights[connector.y]) > 2) { continue; } adj = adj2; break; } if (adj == null) { continue; } roomsToGo.RemoveAt(index); cells[(room.cell.x + adj.cell.x) / 2, (room.cell.y + adj.cell.y) / 2].connected = true; room.cell.connected = adj.cell.connected; if (adj.cell.connected) { UpdatePassability(); } } // convert halls into stairways as needed for (int y = 0; y < sizeInCells.y; y += 1) { for (int x = 0; x < sizeInCells.x; x += 1) { CellInfo cell = cells[x, y]; if (cell.type == CellInfo.CellType.Hall && cell.connected) { List <RoomInfo> rooms = cell.AdjacentRooms(this); float deltaZ = Math.Abs(rooms[0].z - rooms[1].z); if (deltaZ > 1) { if (deltaZ > cell.sizeX / 2 && deltaZ > cell.sizeY / 2) { cell.type = CellInfo.CellType.Switchback; } else { cell.type = CellInfo.CellType.Stairway; } } } } } // set pillar heights to sane values for (int y = 0; y < sizeInCells.y; y += 1) { for (int x = 0; x < sizeInCells.x; x += 1) { CellInfo cell = cells[x, y]; if (cell.type == CellInfo.CellType.Pillar) { CellInfo n = cells[x, y + 1]; CellInfo e = cells[x + 1, y]; CellInfo s = cells[x, y - 1]; CellInfo w = cells[x - 1, y]; if (n.type == CellInfo.CellType.Stairway && e.type == CellInfo.CellType.Stairway) { cell.pillarZ = rooms[(x + 1) / 2, (y + 1) / 2].z; e.stairAnchoredHigh = true; n.stairAnchoredHigh = true; } else if (s.type == CellInfo.CellType.Switchback && s.sizeX > 1) { cell.pillarZ = (s.AdjacentRooms(this)[0].z + s.AdjacentRooms(this)[1].z) / 2.0f; } else if (w.type == CellInfo.CellType.Switchback && s.sizeY > 1) { cell.pillarZ = (w.AdjacentRooms(this)[0].z + w.AdjacentRooms(this)[1].z) / 2.0f; } else { cell.pillarZ = rooms[(x - 1) / 2, (y - 1) / 2].z; } } } } // decorator fringe RoomInfo cornerRoom = rooms[sizeInRooms.x - 1, sizeInRooms.y - 1]; int wallX = cornerRoom.cell.startX + cornerRoom.cell.sizeX + 1; int wallY = cornerRoom.cell.startY + cornerRoom.cell.sizeY + 1; mesh.Resize(new Vector2Int( wallX + (endStairsNW ? 0 : stairLength), wallY + (endStairsNW ? stairLength : 0)), 0.0f); for (int x = startStairsSW ? stairLength : 0; x < wallX; x += 1) { mesh.SetHeight(x, wallY - 1, cornerRoom.z + MaxHeightDelta + 1); mesh.SetTile(x, wallY - 1, defaultImpassTile); } for (int y = startStairsSW ? 0 : stairLength; y < wallY; y += 1) { mesh.SetHeight(wallX - 1, y, cornerRoom.z + MaxHeightDelta + 1); mesh.SetTile(wallX - 1, y, defaultImpassTile); } // render terrain for (int y = 0; y < sizeInCells.y; y += 1) { for (int x = 0; x < sizeInCells.x; x += 1) { CellInfo cell = cells[x, y]; if (cell.type != CellInfo.CellType.Pillar) { cell.RenderTerrain(this, mesh); } } } for (int y = 0; y < sizeInCells.y; y += 1) { for (int x = 0; x < sizeInCells.x; x += 1) { CellInfo cell = cells[x, y]; if (cell.type == CellInfo.CellType.Pillar) { cell.RenderTerrain(this, mesh); } } } // render rooms for (int x = 0; x < sizeInRooms.x; x += 1) { for (int y = 0; y < sizeInRooms.y; y += 1) { rooms[x, y].FillWithVault(mesh); } } // add the starter stairs RoomInfo startRoom = rooms[entryRoomCoords.x, entryRoomCoords.y]; int stairX, stairZ; if (startStairsSW) { stairX = startRoom.cell.startX - 1; stairZ = startRoom.cell.startY + (startRoom.cell.sizeY / 2); } else { stairX = startRoom.cell.startX + (startRoom.cell.sizeX / 2); stairZ = startRoom.cell.startY - 1; } float stairY = startRoom.z - 0.5f; for (int i = 0; i < stairLength; i += 1) { mesh.SetHeight(stairX, stairZ, stairY); MapEvent3D ev; if (i == 0) { ev = Instantiate(startEventPrefab); } else { ev = Instantiate(impassEventPrefab); } GetComponent <Map>().AddEvent(ev); ev.SetLocation(new Vector2Int(stairX, stairZ)); stairY -= 0.5f; stairX += startStairsSW ? -1 : 0; stairZ += startStairsSW ? 0 : -1; } // add the end stairs RoomInfo endRoom = rooms[exitRoomCoords.x, exitRoomCoords.y]; if (endStairsNW) { stairX = endRoom.cell.startX + (endRoom.cell.sizeX / 2); stairZ = endRoom.cell.startY + endRoom.cell.sizeY; } else { stairX = endRoom.cell.startX + endRoom.cell.sizeX; stairZ = endRoom.cell.startY + (endRoom.cell.sizeY / 2); } stairY = endRoom.z + 0.5f; for (int i = 0; i < stairLength; i += 1) { MapEvent3D ev; if (i == 0) { ev = Instantiate(endEventPrefab); } else { ev = Instantiate(impassEventPrefab); } GetComponent <Map>().AddEvent(ev); ev.SetLocation(new Vector2Int(stairX, stairZ)); mesh.SetHeight(stairX, stairZ, stairY); stairY += 0.5f; stairX += endStairsNW ? 0 : 1; stairZ += endStairsNW ? 1 : 0; } // add the zoom target MapEvent3D zoom = Instantiate(targetPrefab); GetComponent <Map>().AddEvent(zoom); zoom.SetLocation(new Vector2Int(mesh.size.x / 2, mesh.size.y / 2)); // generate the encounters List <RoomInfo> validRooms = new List <RoomInfo>(); for (int x = 0; x < sizeInRooms.x; x += 1) { for (int y = 0; y < sizeInRooms.y; y += 1) { if (x != entryRoomCoords.x || y != entryRoomCoords.y) { validRooms.Add(rooms[x, y]); } } } List <Encounter> encounters = table.GenerateEncounters(level); foreach (Encounter encounter in encounters) { RoomInfo room = validRooms[Random.Range(0, validRooms.Count)]; Vector2Int loc = new Vector2Int( Random.Range(room.cell.startX, room.cell.startX + room.cell.sizeX), Random.Range(room.cell.startY, room.cell.startY + room.cell.sizeY)); encounter.PlaceAt(GetComponent <Map>(), loc); } // generate the chests List <Item> items = table.GenerateItems(level); foreach (Item item in items) { ChestEvent chest = Instantiate(chestEventPrefab); GetComponent <Map>().AddEvent(chest.GetComponent <MapEvent>()); Vector2Int loc; do { RoomInfo room = validRooms[Random.Range(0, validRooms.Count)]; loc = new Vector2Int( Random.Range(room.cell.startX + 1, room.cell.startX + room.cell.sizeX - 1), Random.Range(room.cell.startY + 1, room.cell.startY + room.cell.sizeY - 1)); } while (!chest.GetComponent <MapEvent>().CanPassAt(loc)); chest.PopulateAndPlace(item, loc); } mesh.Rebuild(true); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); TacticsTerrainMesh terrain = (TacticsTerrainMesh)target; GUILayout.Space(20.0f); if (GUILayout.Button("Rebuild")) { Rebuild(true); } Vector2Int newSize = EditorGUILayout.Vector2IntField("Size", terrain.size); if (newSize != terrain.size) { terrain.Resize(newSize); Rebuild(true); } GUILayout.Space(20.0f); Vector2Int flooredSelection = new Vector2Int(Mathf.FloorToInt(selectionSize.x), Mathf.FloorToInt(selectionSize.y)); Vector2Int newSelectionSize = EditorGUILayout.Vector2IntField("Brush size", flooredSelection); if (newSelectionSize != flooredSelection) { selectionSize = new Vector2(Mathf.Max(1.0f, newSelectionSize.x), Mathf.Max(1.0f, newSelectionSize.y)); } if (palette == null) { if (terrain.paletteName != null && terrain.paletteName.Length > 0) { string paletteName = "Assets/Tilesets/Palettes/" + terrain.paletteName + ".prefab"; UpdateWithPalette(AssetDatabase.LoadAssetAtPath <GridPalette>(paletteName)); } } GridPalette newPalette = (GridPalette)EditorGUILayout.ObjectField("Tileset", palette, typeof(GridPalette), false); if (newPalette != palette) { UpdateWithPalette(newPalette); } SelectionTool[] ordinals = new SelectionTool[] { SelectionTool.Select, SelectionTool.Paint, SelectionTool.HeightAdjust }; string[] names = new string[] { "Select", "Paint", "HeightAdjust" }; int selectionIndex = GUILayout.SelectionGrid(ArrayUtility.IndexOf(ordinals, tool), names, names.Length); tool = ordinals[selectionIndex]; int controlId = GUIUtility.GetControlID(FocusType.Passive); EventType typeForControl = Event.current.GetTypeForControl(controlId); Vector2 mousePos = Event.current.mousePosition; GUIStyle style = new GUIStyle(); style.padding = new RectOffset(0, 0, 0, 0); if (tileset != null && tool == SelectionTool.Paint) { wraparoundPaintMode = EditorGUILayout.Toggle("Paint all faces", wraparoundPaintMode); Texture2D backer = AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/Resources/Textures/White.png"); for (int y = tileset.size.y - 1; y >= 0; y -= 1) { EditorGUILayout.BeginHorizontal(); for (int x = 0; x < tileset.size.x; x += 1) { Rect selectRect = EditorGUILayout.BeginHorizontal(GUILayout.Width(Map.TileSizePx), GUILayout.Height(Map.TileSizePx)); Tile tile = tileset.GetTile <Tile>(new Vector3Int(x, y, 0)); GUILayout.Box("", style, GUILayout.Width(Map.TileSizePx), GUILayout.Height(Map.TileSizePx)); Rect r = GUILayoutUtility.GetLastRect(); if (r.Contains(Event.current.mousePosition)) { switch (Event.current.type) { case EventType.MouseDown: mode = EditMode.PaletteTileDrag; selectedTileStart = new Vector2(x, y); tileSelectRect = new Rect(x, y, 1, 1); paletteBufferSize = Vector2Int.zero; break; case EventType.MouseDrag: if (mode == EditMode.PaletteTileDrag) { int minX = (int)Mathf.Min(selectedTileStart.x, x); int minY = (int)Mathf.Min(selectedTileStart.y, y); tileSelectRect = new Rect(minX, minY, (int)Mathf.Max(selectedTileStart.x, x) + 1 - minX, (int)Mathf.Max(selectedTileStart.y, y) + 1 - minY); selectionSize = new Vector2(tileSelectRect.width, tileSelectRect.height); } break; case EventType.MouseUp: mode = EditMode.None; break; } } Rect rect = new Rect(tile.sprite.uv[0].x, tile.sprite.uv[3].y, tile.sprite.uv[3].x - tile.sprite.uv[0].x, tile.sprite.uv[0].y - tile.sprite.uv[3].y); GUI.DrawTextureWithTexCoords(r, tile.sprite.texture, rect, true); if (r.Contains(Event.current.mousePosition)) { GUI.DrawTexture(r, backer, ScaleMode.StretchToFill, true, 0.0f, new Color(1, 0, 0, 0.5f), 0.0f, 0.0f); } else if (paletteBufferSize == Vector2Int.zero && tileSelectRect.Contains(new Vector2(x, y))) { GUI.DrawTexture(r, backer, ScaleMode.StretchToFill, true, 0.0f, new Color(1, 1, 1, 0.8f), 0.0f, 0.0f); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndHorizontal(); } } if (tool == SelectionTool.Select) { EditorGUI.BeginDisabledGroup(mode != EditMode.Selected); if (GUILayout.Button("Create MapEvent3D")) { GameObject prefab = AssetDatabase.LoadAssetAtPath <GameObject>(GenericPrefabPath); MapEvent3D mapEvent = Instantiate(prefab).GetComponent <MapEvent3D>(); mapEvent.name = "Event" + Random.Range(1000000, 9999999); AddEvent(mapEvent); } if (GUILayout.Button("Create Tactics Doll")) { GameObject prefab = AssetDatabase.LoadAssetAtPath <GameObject>(TacticsPrefabPath); MapEvent3D mapEvent = ((GameObject)PrefabUtility.InstantiatePrefab(prefab)).GetComponent <MapEvent3D>(); mapEvent.name = "Doll" + Random.Range(1000000, 9999999); AddEvent(mapEvent); } EditorGUI.EndDisabledGroup(); } }