Пример #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                Generate();
            }

            int   mouseX     = Mouse.GetState().X / SCALE;
            int   mouseY     = Mouse.GetState().Y / SCALE + START_Y;
            Biome hoverBiome = world.GetBiomeAt(mouseX, mouseY);

            hoverBiomeName = mouseX + ", " + mouseY + ": " + hoverBiome.name;

            base.Update(gameTime);
        }
Пример #2
0
        private string GetOreAt(int worldX, int worldY, Biome biome, string baseBlock)
        {
            Block block = world.resources.GetBlock(baseBlock);

            if (!block.solid)
            {
                return(baseBlock);                          //Only allow ore on solid terrain blocks
            }
            foreach (string oreName in biome.ores)
            {
                Ore   ore      = world.resources.GetOre(oreName);
                float oreNoise = world.generate.Ore(worldX, worldY, oreName, ore.scale);
                if (oreNoise > ore.cutoff)
                {
                    return(oreName);
                }
            }

            return(baseBlock);
        }
Пример #3
0
        private void Generate()
        {
            colors = new Color[WIDTH][];

            int seed = random.Next();

            world = new World("BiomeTest", seed, resources);

            for (int x = 0; x < WIDTH; x++)
            {
                colors[x] = new Color[HEIGHT];
                for (int y = START_Y; y < END_Y; y++)
                {
                    Biome biome = world.GetBiomeAt(x, y);
                    Tuple <float, float> values = world.generate.Terrain(x, y, biome.minHeight, biome.maxHeight);
                    float value = values.Item2;
                    value = value < -0.5f ? (values.Item1 < -0.4f ? 0.1f : 0.25f) : value < -0.4f ? 0.8f : 1;
                    if (y < World.SEA_LEVEL)
                    {
                        value /= 2;
                    }

                    int[] c     = biome.color;
                    Color color = new Color((int)(c[0] * value), (int)(c[1] * value), (int)(c[2] * value));

                    if (values.Item2 > -0.5)
                    {
                        foreach (string oreName in biome.ores)
                        {
                            Ore ore = resources.GetOre(oreName);
                            if (world.generate.Ore(x, y, oreName, ore.scale) > ore.cutoff)
                            {
                                color = new Color((uint)oreName.GetHashCode());
                            }
                        }
                    }

                    colors[x][y - START_Y] = color;
                }
            }
        }
Пример #4
0
        public static Biome Lerp(Biome a, Biome b, float t)
        {
            Biome dominantBiome = t <= 0.5f ? a : b;
            Biome biome         = new Biome
            {
                name            = "Mix (" + a.name + ", " + b.name + ")",
                temperature     = MathUtils.Lerp(a.temperature, b.temperature, t),
                liveliness      = MathUtils.Lerp(a.liveliness, b.liveliness, t),
                depth           = MathUtils.Lerp(a.depth, b.depth, t),
                baseBlock       = dominantBiome.baseBlock,
                surfaceBlock    = dominantBiome.surfaceBlock,
                minHeight       = MathUtils.Lerp(a.minHeight, b.minHeight, t),
                maxHeight       = MathUtils.Lerp(a.maxHeight, b.maxHeight, t),
                ores            = dominantBiome.ores,
                structures      = dominantBiome.structures,
                color           = dominantBiome.color,
                backgroundColor = Color.Lerp(a.backgroundColor, b.backgroundColor, t)
            };

            return(biome);
        }
Пример #5
0
        private void Generate()
        {
            colors = new Color[maxDepth - minDepth][][];
            for (int d = 0; d < maxDepth - minDepth; d++)
            {
                colors[d] = new Color[WIDTH][];

                for (int x = 0; x < WIDTH; x++)
                {
                    colors[d][x] = new Color[HEIGHT];
                    for (int y = 0; y < HEIGHT; y++)
                    {
                        float sX    = (float)((2 * x) - WIDTH) / WIDTH;
                        float sY    = (float)((2 * y) - HEIGHT) / HEIGHT;
                        Biome biome = resources.GetBiome(sX, sY, d + minDepth);
                        int[] c     = biome.color;
                        Color color = new Color(c[0], c[1], c[2]);
                        colors[d][x][y] = color;
                    }
                }
            }
        }
Пример #6
0
        public Biome GetBiome(float temperature, float liveliness, float depth)
        {
            float minDistance     = float.MaxValue;
            float nextMinDistance = float.MaxValue;
            Biome bestBiome       = null;
            Biome nextBestBiome   = null;

            foreach (Biome biome in GetBiomes())
            {
                Vector3 biomePos   = new Vector3(biome.temperature, biome.liveliness, biome.depth / Biome.DEPTH_SCALE);
                Vector3 currentPos = new Vector3(temperature, liveliness, depth / Biome.DEPTH_SCALE);
                float   distance   = Vector3.Distance(biomePos, currentPos);
                if (distance < minDistance)
                {
                    nextMinDistance = minDistance;
                    nextBestBiome   = bestBiome;

                    minDistance = distance;
                    bestBiome   = biome;
                }
                else if (distance < nextMinDistance)
                {
                    nextMinDistance = distance;
                    nextBestBiome   = biome;
                }
            }

            if (nextBestBiome != null && Math.Abs(minDistance - nextMinDistance) < Biome.BLEND_DISTANCE)
            {
                //lerp between biomes if just about halfway between them (so that heights smoothly transition)
                //if minDistance == nextMinDistance: t = 0.5
                //if Math.Abs(minDistance - nextMinDistance) == 0.1f: t = 0
                float t            = MathUtils.Lerp(0.5f, 0, Math.Abs(minDistance - nextMinDistance) / Biome.BLEND_DISTANCE);
                Biome blendedBiome = Biome.Lerp(bestBiome, nextBestBiome, t);
                return(blendedBiome);
            }
            return(bestBiome);
        }
Пример #7
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            float mouseX     = (float)(Mouse.GetState().X / SCALE * 2 - WIDTH) / WIDTH;
            float mouseY     = (float)(Mouse.GetState().Y / SCALE * 2 - HEIGHT) / HEIGHT;
            Biome hoverBiome = resources.GetBiome(mouseX, mouseY, depth);

            hoverBiomeName = mouseX + ", " + mouseY + ": " + hoverBiome.name;

            if (!Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                depth++;
                if (depth >= maxDepth)
                {
                    depth = minDepth;
                }
            }

            base.Update(gameTime);
        }
Пример #8
0
 private static string GetBlockFromNoise(int worldX, int worldY, float terrainNoise, bool background, Biome biome)
 {
     //TODO variable thresholds from biome
     if (terrainNoise > -0.5)
     {
         string block = terrainNoise > -0.4 ? biome.baseBlock : biome.surfaceBlock;
         return(block);
     }
     else if (worldY > World.SEA_LEVEL)
     {
         return("water");
     }
     else
     {
         return("air");
     }
 }
Пример #9
0
        public static string GetTerrainAt(World world, int worldX, int worldY, bool background, Biome biome)
        {
            Tuple <float, float> noise = world.generate.Terrain(worldX, worldY, biome.minHeight, biome.maxHeight);

            return(GetBlockFromNoise(worldX, worldY, background ? noise.Item1 : noise.Item2, background, biome));
        }