예제 #1
0
        /// <summary>
        /// Processes an area and applies a gradient calculation to each part of the area.
        /// </summary>
        /// <param name="position">The center of the gradient.</param>
        /// <param name="strength">The width of the gradient spread.</param>
        /// <param name="angle">The angle to apply the gradient.</param>
        /// <param name="area">The area to calculate.</param>
        /// <param name="applyAction">The callback called for each part of the area.</param>
        public static void GradientFill(Point cellSize, Point position, int strength, int angle, Rectangle area, ColorGradient gradient, Action <int, int, Color> applyAction)
        {
            double radians = angle * Math.PI / 180; // = Math.Atan2(x1 - x2, y1 - y2);

            Vector2 angleVector = new Vector2((float)(Math.Sin(radians) * strength), (float)(Math.Cos(radians) * strength)) / 2;
            Vector2 location    = new Vector2(position.X, position.Y);

            if (cellSize.X > cellSize.Y)
            {
                angleVector.Y *= cellSize.X / cellSize.Y;
            }

            else if (cellSize.X < cellSize.Y)
            {
                angleVector.X *= cellSize.Y / cellSize.X;
            }

            Vector2 endingPoint   = location + angleVector;
            Vector2 startingPoint = location - angleVector;

            double x1 = (startingPoint.X / (double)area.Width) * 2.0f - 1.0f;
            double y1 = (startingPoint.Y / (double)area.Height) * 2.0f - 1.0f;
            double x2 = (endingPoint.X / (double)area.Width) * 2.0f - 1.0f;
            double y2 = (endingPoint.Y / (double)area.Height) * 2.0f - 1.0f;

            double start = x1 * angleVector.X + y1 * angleVector.Y;
            double end   = x2 * angleVector.X + y2 * angleVector.Y;

            for (int x = area.Left; x < area.Width; x++)
            {
                for (int y = area.Top; y < area.Height; y++)
                {
                    // but we need vectors from (-1, -1) to (1, 1)
                    // instead of pixels from (0, 0) to (width, height)
                    double u = (x / (double)area.Width) * 2.0f - 1.0f;
                    double v = (y / (double)area.Height) * 2.0f - 1.0f;

                    double here = u * angleVector.X + v * angleVector.Y;

                    double lerp = (start - here) / (start - end);

                    //lerp = Math.Abs((lerp - (int)lerp));

                    lerp = MyMathHelper.Clamp((float)lerp, 0f, 1.0f);

                    int counter;
                    for (counter = 0; counter < gradient.Stops.Length && gradient.Stops[counter].Stop < (float)lerp; counter++)
                    {
                        ;
                    }

                    counter--;
                    counter = (int)MyMathHelper.Clamp(counter, 0, gradient.Stops.Length - 2);

                    float newLerp = (gradient.Stops[counter].Stop - (float)lerp) / (gradient.Stops[counter].Stop - gradient.Stops[counter + 1].Stop);

                    applyAction(x, y, ColorHelper.Lerp(gradient.Stops[counter].Color, gradient.Stops[counter + 1].Color, newLerp));
                }
            }
        }
예제 #2
0
        public Color CreateColor(float v)
        {
            var c = Color.Red;

            c = Color.Lerp(c, v > .5f ? Color.Green : Color.Blue, Splosion.Random.NextFloat());
            return(c);
        }
        Color GetColor(int index, float shade)
        {
            int offset = (int)shade;

            index += offset;
            Color c = Machine.Palette.Color[index & 0x3F];

            if ((index & 7) != 7)
            {
                c = Color.Lerp(c, Machine.Palette.Color[(index + 1) & 0x3F], shade - offset);
            }
            return(c);
        }
예제 #4
0
        /// <summary>
        /// Creates a <see cref="ColoredString"/> object from an existing string with the specified foreground gradient, background gradient, and cell effect.
        /// </summary>
        /// <param name="value">The current string.</param>
        /// <param name="startingForeground">The starting foreground color to blend.</param>
        /// <param name="endingForeground">The ending foreground color to blend.</param>
        /// <param name="startingBackground">The starting background color to blend.</param>
        /// <param name="endingBackground">The ending background color to blend.</param>
        /// <returns>A <see cref="ColoredString"/> object instace.</returns>
        public static ColoredString CreateGradient(this string value, Color startingForeground, Color endingForeground, Color startingBackground, Color endingBackground)
        {
            ColoredString newString = new ColoredString(value);

            for (int i = 0; i < value.Length; i++)
            {
                newString[i].Foreground = ColorHelper.Lerp(startingForeground, endingForeground, (float)i / (float)value.Length);
                newString[i].Background = ColorHelper.Lerp(startingBackground, endingBackground, (float)i / (float)value.Length);
            }

            newString.IgnoreMirror = true;

            return(newString);
        }
예제 #5
0
        public TextSurface GetHeatMapTexture(int width, int height, Tile[,] tiles)
        {
            var surface = new SurfaceEditor(new TextSurface(width, height, Engine.DefaultFont));
            var pixels  = new Color[width * height];

            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    switch (tiles[x, y].HeatType)
                    {
                    case HeatType.Coldest:
                        pixels[x + y * width] = Coldest;
                        break;

                    case HeatType.Colder:
                        pixels[x + y * width] = Colder;
                        break;

                    case HeatType.Cold:
                        pixels[x + y * width] = Cold;
                        break;

                    case HeatType.Warm:
                        pixels[x + y * width] = Warm;
                        break;

                    case HeatType.Warmer:
                        pixels[x + y * width] = Warmer;
                        break;

                    case HeatType.Warmest:
                        pixels[x + y * width] = Warmest;
                        break;
                    }

                    //darken the color if a edge tile
                    if ((int)tiles[x, y].HeightType > 2 && tiles[x, y].Bitmask != 15)
                    {
                        pixels[x + y * width] = ColorHelper.Lerp(pixels[x + y * width], Color.Black, 0.4f);
                    }
                }
            }


            surface.SetPixels(pixels);
            return((TextSurface)surface.TextSurface);
        }
예제 #6
0
        /// <summary>
        /// Luo pystysuuntaisen liukuväritetyn kuvan.
        /// </summary>
        /// <param name="imageWidth">kuvan leveys.</param>
        /// <param name="imageHeight">kuvan korkeus.</param>
        /// <param name="lowerColor">Alareunassa käytettävä väri.</param>
        /// <param name="upperColor">Yläreunassa käytettävä väri.</param>
        /// <returns>Väritetty kuva.</returns>
        public static Image FromGradient(int imageWidth, int imageHeight, Color lowerColor, Color upperColor)
        {
            XnaColor lower = lowerColor.AsXnaColor();
            XnaColor upper = upperColor.AsXnaColor();

            XnaColor[] textureColors = new XnaColor[imageWidth * imageHeight];
            int        i             = 0;

            for (int ver = 0; ver < imageHeight; ver++)
            {
                for (int hor = 0; hor < imageWidth; hor++)
                {
                    textureColors[i++] = XnaColor.Lerp(upper, lower, ((float)ver / (float)imageHeight));
                }
            }

            Texture2D newTexture = new Texture2D(Game.GraphicsDevice, imageWidth, imageHeight, false, SurfaceFormat.Color);

            newTexture.SetData <XnaColor>(textureColors);
            return(new Image(newTexture));
        }
예제 #7
0
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (otherScreenHasFocus || IsExiting)
            {
                return;
            }
            //xpos++;
            //if (xpos == 320*3) xpos = 0;
            playerShip.Update(gameTime, map);

            waterLevel = 260;
            waterParallax.Position.Y        = waterLevel;
            underwaterBGParallax.Position.Y = waterLevel + 20;

            if (playerShip.Position.X < 0f)
            {
                playerShip.Position.X = (map.TileWidth * map.Width) + playerShip.Speed.X;
                camera.Position.X     = (playerShip.Position.X + playerShip.Speed.X * 20f) - (camera.Target.X - camera.Position.X);
                //particleController.Wrap((map.TileWidth*map.Width));
                //projectileController.Wrap((map.TileWidth * map.Width));
            }
            if (playerShip.Position.X >= (map.TileWidth * map.Width))
            {
                playerShip.Position.X = 0f + playerShip.Speed.X;
                camera.Position.X     = (playerShip.Position.X + playerShip.Speed.X * 20f) - (camera.Target.X - camera.Position.X);
                //particleController.Wrap(-(map.TileWidth * map.Width));
                //projectileController.Wrap(-(map.TileWidth * map.Width));
                //camera.Target.X += playerShip.Speed.X * 20f;
            }

            if (!playerShip.underWater)
            {
                if (playerShip.Position.Y > waterLevel + 10)
                {
                    playerShip.underWater = true;
                    AudioController.PlaySFX("water_enter", 1f, -0.1f, 0.1f);
                    AudioController._songs["overwater-theme"].Volume  = 0f;
                    AudioController._songs["underwater-theme"].Volume = AudioController.MusicVolume;
                }
                waterParallax.HeightScale = MathHelper.Lerp(waterParallax.HeightScale, 0.65f, 0.1f);
            }
            if (playerShip.underWater)
            {
                if (playerShip.Position.Y < waterLevel - 10)
                {
                    AudioController.PlaySFX("water_leave", 0.8f, -0.1f, 0.1f);
                    AudioController._songs["overwater-theme"].Volume  = AudioController.MusicVolume;
                    AudioController._songs["underwater-theme"].Volume = 0f;
                    playerShip.underWater = false;
                    for (int i = 0; i < 30; i++)
                    {
                        Vector2 pos = new Vector2(Helper.RandomFloat(-5f, 5f), 0f);
                        Color   col = Color.Lerp(new Color(0, 81, 147), new Color(211, 234, 254), Helper.RandomFloat(0f, 1f));
                        particleController.Add(playerShip.Position + pos,
                                               (pos * 0.1f) + new Vector2(playerShip.Speed.X, playerShip.Speed.Y * Helper.RandomFloat(0.25f, 2f)),
                                               0, 2000, 500, true, true, new Rectangle(0, 0, 3, 3),
                                               col, particle => { ParticleFunctions.FadeInOut(particle);
                                                                  if (particle.Position.Y > waterLevel)
                                                                  {
                                                                      particle.State = ParticleState.Done;
                                                                  }
                                               }, 1f, 0f, Helper.RandomFloat(-0.1f, 0.1f), 1, ParticleBlend.Alpha);
                    }
                }

                waterParallax.HeightScale = MathHelper.Lerp(waterParallax.HeightScale, 0.1f, 0.05f);
            }

            particleController.Update(gameTime, map);
            if (!_endOfWave)
            {
                enemyController.Update(gameTime, map);
            }
            projectileController.Update(gameTime, map);
            powerupController.Update(gameTime, map);



            camera.Target    = playerShip.Position;
            camera.Target.X += playerShip.Speed.X * 20f;

            //Enemy head = EnemyController.Instance.Enemies.FirstOrDefault(en => en is Boss && ((Boss) en).Head);
            //if (head != null)
            //{
            //    playerShip.Position = head.Position + new Vector2(0, -16);
            //    camera.Target = head.Position;
            //}

            camera.Update(gameTime, playerShip.underWater, waterLevel);

            waterParallax.Update(gameTime, (camera.Target.X - camera.Position.X) * camera.Speed, (int)camera.Position.X);
            underwaterBGParallax.Update(gameTime, ((camera.Target.X - camera.Position.X) * camera.Speed) * 0.5f, (int)camera.Position.X);
            skyBGParallax.Update(gameTime, ((camera.Target.X - camera.Position.X) * camera.Speed) * 0.1f, (int)camera.Position.X);
            rocksParallax.Update(gameTime, (camera.Target.X - camera.Position.X) * camera.Speed, (int)camera.Position.X);
            cloudsParallax.Update(gameTime, (camera.Target.X - camera.Position.X) * camera.Speed, (int)camera.Position.X);

            hud.Update(gameTime, new Viewport(0, 0, ScreenManager.Game.RenderWidth, ScreenManager.Game.RenderHeight));

            if (enemyController.Enemies.Count == 0 && enemyController.NumToSpawn == 0 && !_endOfWave)
            {
                _endOfWave = true;
                TimerController.Instance.Create("", () =>
                {
                    GameController.Wave++;
                    TweenController.Instance.Create("", TweenFuncs.QuadraticEaseIn, tweenin =>
                    {
                        _waveFade = tweenin.Value;
                        if (tweenin.State == TweenState.Finished)
                        {
                            MapGeneration.Generate(map);
                            enemyController.SpawnInitial(GameController.Wave, map);
                            playerShip.Reset();
                            projectileController.Reset();
                            _firstWave = false;

                            TweenController.Instance.Create("", TweenFuncs.Linear, eowtween =>
                            {
                                //playerShip.Life += 0.2f;
                                _eowTimer = eowtween.Value;
                                if (eowtween.State == TweenState.Finished)
                                {
                                    TweenController.Instance.Create("", TweenFuncs.QuadraticEaseIn, tweenout =>
                                    {
                                        _waveFade = 1f - tweenout.Value;
                                        if (tweenout.State == TweenState.Finished)
                                        {
                                            _endOfWave = false;
                                        }
                                    }, 500, false, false);
                                }
                            }, _tradecost == 0?2000:5000, false, false);
                        }
                    }, 500, false, false);
                }, GameController.Wave > 0?2000:0, false);
            }

            if (playerShip.Life <= 0f && !_gameOver)
            {
                _gameOver = true;
                TimerController.Instance.Create("", () =>
                {
                    TweenController.Instance.Create("", TweenFuncs.QuadraticEaseIn, tweenin =>
                    {
                        _goFade = tweenin.Value;
                        if (tweenin.State == TweenState.Finished)
                        {
                            TweenController.Instance.Create("", TweenFuncs.Linear, eowtween =>
                            {
                                _goTimer = eowtween.Value;
                            }, 2000, false, false);
                        }
                    }, 1000, false, false);
                }, 2000, false);
            }

            TweenController.Instance.Update(gameTime);
            TimerController.Instance.Update(gameTime);

            _tradecost = (int)Math.Ceiling((GameController.Wave * 2f) * (0.01f * (100f - playerShip.Life)));
        }
예제 #8
0
        public TextSurface GetBiomeMapTexture(int width, int height, Tile[,] tiles, float coldest, float colder, float cold)
        {
            var surface = new SurfaceEditor(new TextSurface(width, height, Engine.DefaultFont));
            var pixels  = new Color[width * height];

            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    BiomeType value = tiles[x, y].BiomeType;

                    switch (value)
                    {
                    case BiomeType.Ice:
                        pixels[x + y * width] = Ice;
                        break;

                    case BiomeType.BorealForest:
                        pixels[x + y * width] = BorealForest;
                        break;

                    case BiomeType.Desert:
                        pixels[x + y * width] = Desert;
                        break;

                    case BiomeType.Grassland:
                        pixels[x + y * width] = Grassland;
                        break;

                    case BiomeType.SeasonalForest:
                        pixels[x + y * width] = SeasonalForest;
                        break;

                    case BiomeType.Tundra:
                        pixels[x + y * width] = Tundra;
                        break;

                    case BiomeType.Savanna:
                        pixels[x + y * width] = Savanna;
                        break;

                    case BiomeType.TemperateRainforest:
                        pixels[x + y * width] = TemperateRainforest;
                        break;

                    case BiomeType.TropicalRainforest:
                        pixels[x + y * width] = TropicalRainforest;
                        break;

                    case BiomeType.Woodland:
                        pixels[x + y * width] = Woodland;
                        break;
                    }

                    // Water tiles
                    if (tiles[x, y].HeightType == HeightType.DeepWater)
                    {
                        pixels[x + y * width] = DeepColor;
                    }
                    else if (tiles[x, y].HeightType == HeightType.ShallowWater)
                    {
                        pixels[x + y * width] = ShallowColor;
                    }

                    // draw rivers
                    if (tiles[x, y].HeightType == HeightType.River)
                    {
                        float heatValue = tiles[x, y].HeatValue;

                        if (tiles[x, y].HeatType == HeatType.Coldest)
                        {
                            pixels[x + y * width] = ColorHelper.Lerp(IceWater, ColdWater, (heatValue) / (coldest));
                        }
                        else if (tiles[x, y].HeatType == HeatType.Colder)
                        {
                            pixels[x + y * width] = ColorHelper.Lerp(ColdWater, RiverWater, (heatValue - coldest) / (colder - coldest));
                        }
                        else if (tiles[x, y].HeatType == HeatType.Cold)
                        {
                            pixels[x + y * width] = ColorHelper.Lerp(RiverWater, ShallowColor, (heatValue - colder) / (cold - colder));
                        }
                        else
                        {
                            pixels[x + y * width] = ShallowColor;
                        }
                    }


                    // add a outline
                    if (tiles[x, y].HeightType >= HeightType.Shore && tiles[x, y].HeightType != HeightType.River)
                    {
                        if (tiles[x, y].BiomeBitmask != 15)
                        {
                            pixels[x + y * width] = ColorHelper.Lerp(pixels[x + y * width], Color.Black, 0.35f);
                        }
                    }
                }
            }

            surface.SetPixels(pixels);
            return((TextSurface)surface.TextSurface);
        }