/// <summary> /// Finds a random location and tries to create a hut at that location. Returns null on failure /// </summary> /// <param name="villageNode"></param> /// <returns></returns> private HutNode TryGenerateHut(VillageNode villageNode) { var villageData = villageNode.VillageData; // Strategy: A hut is spawned in a circular radius around the center of the village. Vector3 hutPosition = UtilityFunctions.GetRandomVector3(villageNode.transform.position, villageData.hutSpawnRange); // Ensure no blockage by existing huts var dummyObj = new GameObject("Dummy Object"); dummyObj.transform.position = hutPosition; var dummyArea = new TempArea(dummyObj.transform, hutDimensions); var collidingArea = villageNode.FindAreaIfColliding(dummyArea); HutNode newHutNode; // If no colliding area, then can safely add a new hut if (collidingArea is null) { // Add new hut routine // Location already determined. Set rotation towards center of village Vector3 hutForward = villageNode.transform.position - dummyArea.Center; dummyArea.ObjectTransform.forward = hutForward; string hutName = $"{villageNode.Name}.Hut"; // Instantiation var hutNode = villageNode.CreateResidence <HutNode>(villageData.hutPrefab, dummyArea.Center, dummyArea.ObjectTransform.rotation, residenceName: hutName); newHutNode = hutNode; } else { // If snapping process is success, add Hut to this TiledArea var colldingTiledArea = collidingArea.TiledArea; // Snap to nearest empty space from colliding hut var newPosition = colldingTiledArea.SnapToClosestOpenSpace(dummyArea); dummyArea.ObjectTransform.position = newPosition; // Check if resultant open space is colliding with anything else var newCollidingArea = villageNode.FindAreaIfColliding(dummyArea); if (newCollidingArea is null) { // Add new hut routine string hutName = $"{villageNode.Name}.Hut"; // Instantiation var hutNode = villageNode.CreateResidence <HutNode>(villageData.hutPrefab, dummyArea.Center, dummyArea.ObjectTransform.rotation, parentTiledArea: colldingTiledArea, residenceName: hutName); newHutNode = hutNode; } else { newHutNode = null; } } Destroy(dummyObj); return(newHutNode); }
/// <summary> /// Finds a suitable location near center of village, creates elder hut along with TiledArea, and returns the ElderHutNode /// </summary> /// <param name="villageNode"></param> /// <param name="villageData"></param> /// <returns></returns> private ElderHutNode GenerateElderHut(VillageNode villageNode) { var villageData = villageNode.VillageData; // Strategy: In the center, the elder hut is first placed Vector3 elderHutPosition = UtilityFunctions.GetRandomVector3(villageNode.transform.position, villageData.elderHutSpawnRange); // Orientation of the elder hut will always be towards the center of the village Vector3 elderHutForward = villageNode.transform.position - elderHutPosition; Quaternion elderHutRotation = Quaternion.FromToRotation(Vector3.forward, elderHutForward); string elderHutName = $"{villageNode.Name}.Elder Hut"; // Instantiation var elderHutNode = villageNode.CreateResidence <ElderHutNode>(villageData.elderHutPrefab, elderHutPosition, elderHutRotation, residenceName: elderHutName); return(elderHutNode); }