예제 #1
0
        public override void Generate()
        {
            int poolCount = GameObjectPool.Count();

            if (poolCount > 0)
            {
                Random      random = new Random();
                Level.Level level  = Procreate.MainWindow.ControlPoint.Level;

                // set each level element to a chance-weighted game object from the pool
                foreach (List <Level.LevelElement> row in level.Elements)
                {
                    for (int i = 0; i < row.Count(); ++i)
                    {
                        Level.LevelElement element = row[i];

                        bool elementSet = false;

                        // select a game object from the pool randomally, and use it if its chance is high enough
                        // e.g. if a random value between 0-100 equals 60, the first game object with a chance of 60 or anything lesser (i.e. 60% of the possible values) will be used
                        // whereas any game object with a low chance, e.g. 1 is unlikely to be used because the random value will rarely equal 1 or less (only 1% of the time)
                        while (!elementSet)
                        {
                            int index       = random.Next(poolCount);
                            int randomValue = random.Next(1, 100);

                            GameObjectChancePair candidate = GameObjectPool[index];

                            // make sure the game object's chance is valid (between 0-100)
                            if (candidate.Chance >= 0 && candidate.Chance <= 100)
                            {
                                if (randomValue <= candidate.Chance)
                                {
                                    row[i]     = candidate.GameObject;
                                    elementSet = true;
                                }
                            }
                            else
                            {
                                // TODO: error for candidate game object's chance not being valid
                            }
                        }
                    }
                }
            }
            else
            {
                // TODO: error message for no items in the pool
            }
        }
예제 #2
0
        public override void Generate()
        {
            // do all the pre-algorithms
            foreach (Algorithm algorithm in PreAlgorithms)
            {
                algorithm.Generate();
            }

            Level.Level           level = Procreate.MainWindow.ControlPoint.Level;
            Generation.GameObject wall  = new Generation.GameObject(WallDummy, Level.Level.WallImagePath, 50);
            Generation.GameObject floor = new Generation.GameObject(FloorDummy, Level.Level.FloorImagePath, 50);

            // iterate
            for (int iteration = 0; iteration < IterationCount; ++iteration)
            {
                for (int i = 0; i < level.Elements.Count(); ++i)
                {
                    for (int j = 0; j < level.Elements[i].Count(); ++j)
                    {
                        // set the element to a wall if most of its neighbours are walls
                        // otherwise it's in a grass area, so make it grass
                        int wallTally     = 0;
                        int requiredWalls = 5;

                        for (int y = -1; y <= 1; ++y)
                        {
                            for (int x = -1; x <= 1; ++x)
                            {
                                int rowIndex = i + y;
                                int colIndex = j + x;

                                // require less neighbour walls if this element is a corner or border
                                if (rowIndex < 0 || rowIndex >= level.Elements.Count() ||
                                    colIndex < 0 || colIndex >= level.Elements[i].Count())
                                {
                                    --requiredWalls;
                                }
                                else
                                {
                                    // the neighbour exists - so use it
                                    Level.LevelElement element = level.Elements[rowIndex][colIndex];

                                    if (element.Name == WallDummy)
                                    {
                                        wallTally++;
                                    }
                                }
                            }
                        }

                        if (wallTally >= requiredWalls)
                        {
                            level.Elements[i][j] = wall;
                        }
                        else
                        {
                            level.Elements[i][j] = floor;
                        }
                    }
                }
            }
        }