/// <summary>
    /// Create hut and/or place in tiled interface
    /// </summary>
    /// <returns></returns>
    public void DebugCreateHut(Transform targetTransform, IRegion region)
    {
        // Note: Ensure below cast is typesafe
        VillageNode village = region as VillageNode;

        var targetPosition      = targetTransform.position;
        var targetRotationEuler = targetTransform.rotation.eulerAngles;
        var targetRotation      = Quaternion.Euler(0, targetRotationEuler.y, 0);

        GameObject dummyObj = new GameObject("dummy object");

        dummyObj.transform.position = targetPosition;
        dummyObj.transform.rotation = targetRotation;
        UtilityFunctions.PutObjectOnGround(dummyObj.transform);
        targetPosition = dummyObj.transform.position;

        Debug.Log($"House construction at {Time.fixedTime}");
        var collidingArea = village.FindAreaIfWithinAny(targetPosition);

        Debug.Log($"Is this part of a residence area? {!(collidingArea is null)}");
        if (collidingArea is null)
        {
            // Hut construction can begin
            village.DebugConstructHut(dummyObj.transform);
            Debug.Log($"Hut constructed at {targetPosition}");
        }
        else
        {
            // Snap to nearest available tile
            village.DebugSnapToAndConstructHut(dummyObj.transform, collidingArea);
            Debug.Log($"Snapped to new position and Hut constructed at {dummyObj.transform.position}");
        }
        Destroy(dummyObj);
    }
    VillageNode InstantiateVillage(Vector3 position)
    {
        GameObject  newVillage  = Instantiate(villagePrefab, position, Quaternion.identity);
        VillageNode villageNode = newVillage.AddComponent <VillageNode>();

        villageNode.SetUp(defaultVillageName);
        return(villageNode);
    }
    private List <VillagerNode> GenerateVillagers(VillageNode villageNode)
    {
        var villagerList = new List <VillagerNode>();

        for (int idx = 0; idx < villageNode.VillageData.HeadCount; idx++)
        {
            villagerList.Add(GenerateVillager(villageNode.CenterPosition, villageNode));
        }
        return(villagerList);
    }
    /// <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);
    }
 // Start is called before the first frame update
 void Start()
 {
     Random.InitState(seed);
     randState   = Random.state;
     _           = StartCoroutine(methodName: nameof(LogicUpdate));
     mainVillage = RandomCreateVillage();
     villagers   = new List <VillagerNode>();
     for (int idx = 0; idx <= initialVillageHeadCount; idx++)
     {
         villagers.Add(RandomCreateVillager(mainVillage.transform.position, spawnRadius));
     }
 }
Example #6
0
    // Helpers
    public void SpawnNewArmy(VillageNode CurrentNode, List <Unit> StartingUnits)
    {
        Army       newArmy      = new Army();
        GameObject repsentation = Instantiate(ArmyPrefab, CurrentNode.transform.position, Quaternion.identity);

        newArmy.Repsentation = repsentation;
        repsentation.GetComponent <ArmyRepresentation>().myArmy = newArmy;
        newArmy.CurrentLocation = CurrentNode.ID;
        newArmy.UnitsInArmy     = StartingUnits;
        newArmy.ID = Game.current.AllArmies.Count;
        Game.current.AllArmies.Add(newArmy);
    }
    /// <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);
    }
    public INode DuplicateNode(bool selfLink = false)
    {
        GameObject  newObject = Instantiate(gameObject);
        VillageNode newNode   = newObject.AddComponent <VillageNode>();

        newNode.SetUp(name);
        foreach (var link in Linked)
        {
            newNode.AddLink(link);
        }
        if (selfLink)
        {
            newNode.AddLink(this);
        }
        return(newNode);
    }
    public VillageNode GenerateVillage(Vector3 centerPosition, float maxRadius, string name = "My Village")
    {
        var oldState = Random.state;

        Random.state = randomState;

        // Obtain Village Parameters (Random Rolls)
        var villageData = GenerateVillageData(villageGeneratorData, maxRadius);

        // Instantiate Village
        VillageNode villageNode = CreateVillageNode(centerPosition, villageData, name);

        // Add Huts to Village (Random Placements)
        var hutsTuple = GenerateHuts(villageNode);

        GenerateVillagers(villageNode);

        Random.state = oldState;
        return(villageNode);
    }
    /// <summary>
    /// Traditional Hut Generation Sequence. Start with creating an Elder Hut in the Middle. Then Generate a number of huts in the sides.
    /// </summary>
    /// <param name="villageNode"></param>
    /// <param name="villageData"></param>
    private Tuple <ElderHutNode, HashSet <HutNode> > GenerateHuts(VillageNode villageNode)
    {
        // Find hut count
        int numberOfHutsToBeBuilt = DecideNumberOfHuts(villageNode.VillageData);

        // Village requires 1 Elder Hut
        var elderHutNode = GenerateElderHut(villageNode);

        numberOfHutsToBeBuilt -= 1;

        // Now Village is filled with rest of huts
        // Generate new huts in a loop. Stop if hut generation failed these many times in a row:
        const int maxTriesInARow = 100;
        int       triesInARow    = 0;

        HashSet <HutNode> hutNodes = new HashSet <HutNode>();

        while (numberOfHutsToBeBuilt > 0)
        {
            bool currentIterationSuccess;

            var newHutNode = TryGenerateHut(villageNode);
            currentIterationSuccess = !(newHutNode is null);

            if (!currentIterationSuccess)
            {
                triesInARow += 1;
                if (triesInARow >= maxTriesInARow)
                {
                    break;
                }
            }
            else
            {
                hutNodes.Add(newHutNode);
                numberOfHutsToBeBuilt -= 1;
                triesInARow            = 0;
            }
        }
        return(new Tuple <ElderHutNode, HashSet <HutNode> >(elderHutNode, hutNodes));
    }
Example #11
0
 public void NodeClicked(VillageNode clickedNode)
 {
     //If path has been found already, clear nodes clicked
     if (numVillagesSelected >= 2 || (numVillagesSelected == 1 && VillagesSelected[0].ID == clickedNode.ID))
     {
         ClearNodesClicked();
     }
     //When node clicked, add it to a list
     VillagesSelected[numVillagesSelected] = clickedNode;
     //clickedNode.IsSelected();
     ++numVillagesSelected;
     //If a node has already been clicked, Calculate the path beteewn the nodes
     if (numVillagesSelected == 2)
     {
         CurrentVertexList = new int[2];
         CurrentVertexList = Djikstra(VillagesSelected[0].ID, VillagesSelected[1].ID);
         //PrintPath(CurrentVertexList);
         HighlightPath(CurrentVertexList);
     }
     //Print the path
 }
 private VillagerNode GenerateVillager(Vector3 spawnPoint, VillageNode villageNode)
 {
     // How to generate villager position? Should we place at center, or place according to hut positions?
     return(null);
 }