예제 #1
0
    public static float[,] CreateFleeMap(List <Vector2Int> dangerSources)
    {
        float[,] baseMap = Pathfinding.CreateDijkstraMap(dangerSources);
        List <Vector2Int> sources = new List <Vector2Int>();
        List <float>      weights = new List <float>();

        for (int i = 0; i < baseMap.GetLength(0); i++)
        {
            for (int j = 0; j < baseMap.GetLength(1); j++)
            {
                Vector2Int spot = new Vector2Int(i, j);
                if (Map.current.BlocksMovement(spot))
                {
                    continue;
                }
                sources.Add(spot);
                weights.Add(-1.2f * baseMap[i, j]);
            }
        }

        return(Pathfinding.CreateDijkstraMap(Map.current, sources, weights));
    }
예제 #2
0
    public IEnumerator SpawnForFloor(int floor, Map m, int numItems)
    {
        if (floor < 0)
        {
            yield break;
        }

        List <Vector2Int> starts = new List <Vector2Int>();

        starts.AddRange(m.entrances);
        starts.AddRange(m.exits);

        //Create positions map
        float[,] positions = Pathfinding.CreateDijkstraMap(m, starts.ToList());

        yield return(null);

        int[,] tickets = new int[positions.GetLength(0), positions.GetLength(1)];

        int ticketSum = 0;

        for (int i = 0; i < positions.GetLength(0); i++)
        {
            for (int j = 0; j < positions.GetLength(1); j++)
            {
                if (positions[i, j] > 0)
                {
                    tickets[i, j] = Mathf.RoundToInt(Mathf.Log(positions[i, j], 2));
                    ticketSum    += tickets[i, j];
                }
            }
            yield return(null);
        }


        yield return(null);

        for (int i = 0; i < numItems; i++)
        {
            yield return(null);

            Item item = pools[floor].GenerateItem();
            while (item == null)
            {
                yield return(null);

                Debug.LogWarning("Floor failed to spawn item correctly. Retrying...");
                item = pools[floor].GenerateItem();
            }

            Vector2Int pos = new Vector2Int(-1, -1);

            int ticket = UnityEngine.Random.Range(0, ticketSum);
            for (int x = 0; x < tickets.GetLength(0); x++)
            {
                for (int y = 0; y < tickets.GetLength(1); y++)
                {
                    if (ticket < tickets[x, y])
                    {
                        pos = new Vector2Int(x, y);
                        //TODO: Remove an area of tickets
                        ticketSum    -= tickets[x, y];
                        tickets[x, y] = 0;
                        break;
                    }
                    ticket -= tickets[x, y];
                }
                if (pos.x >= 0)
                {
                    break;
                }
            }

            item.transform.parent = m.itemContainer;
            m.GetTile(pos).inventory.Add(item);
        }
    }