Example #1
0
        private TextureHolder CoastWithLandToTheSouthWest(LocationType[] neighbors, TextureHolder defaultValue)
        {
            if (neighbors[Directions.NeighborWest] == LocationType.Water)
            {
                return(defaultValue);
            }
            if (neighbors[Directions.NeighborEast] != LocationType.Water)
            {
                return(defaultValue);
            }
            if (neighbors[Directions.NeighborNorth] != LocationType.Water)
            {
                return(defaultValue);
            }
            if (neighbors[Directions.NeighborSouth] == LocationType.Water)
            {
                return(defaultValue);
            }
            if (neighbors[Directions.NeighborThis] != LocationType.Water)
            {
                return(defaultValue);
            }

            return(water.CoastWithLandToTheSouthWest);
        }
        public void UseProperTextureForIsland()
        {
            var ground = new TextureHolder();
            var window = new Window(new WaterTextures(), ground, null);

            var island = new IslandEntity { Corners = new[] { new GeoPoint { X = 1, Y = 1 } } };
            window.AddIsland(island);

            var view = window.GetWindow(1, 1, 1, 1);
            view.First().Texture.Should().Be(ground);
        }
 public WaterTextures(Texture2D terrain)
 {
     var spriteSize = new Point(32, 32);
     CoastWithLandToTheNorth = new TextureHolder(terrain, new Rectangle(new Point(10 * 32, 0 * 32), spriteSize));
     CoastWithLandToTheSouth = new TextureHolder(terrain, new Rectangle(new Point(4 * 32, 0 * 32), spriteSize));
     CoastWithLandToTheWest = new TextureHolder(terrain, new Rectangle(new Point(6 * 32, 0 * 32), spriteSize));
     CoastWithLandToTheEast = new TextureHolder(terrain, new Rectangle(new Point(18 * 32, 0 * 32), spriteSize));
     CoastWithLandToTheNorthEast = new TextureHolder(terrain, new Rectangle(new Point(8 * 32, 0 * 32), spriteSize));
     CoastWithLandToTheNorthWest = new TextureHolder(terrain, new Rectangle(new Point(0 * 32, 1 * 32), spriteSize));
     CoastWithLandToTheSouthEast = new TextureHolder(terrain, new Rectangle(new Point(14 * 32, 0 * 32), spriteSize));
     CoastWithLandToTheSouthWest = new TextureHolder(terrain, new Rectangle(new Point(6 * 32, 1 * 32), spriteSize));
     Sea = new TextureHolder(terrain, new Rectangle(new Point(2 * 32, 0 * 32), spriteSize));
 }
        public void CityTakesPrecedenceOnTerrain()
        {
            var ground = new TextureHolder();
            var city = new TextureHolder();
            var window = new Window(null, ground, city);

            var island = new IslandEntity { Corners = new[] { new GeoPoint { X = 1, Y = 1 } } };
            window.AddIsland(island);
            window.AddCity(new CityEntity(1, 1));

            var view = window.GetWindow(1, 1, 1, 1).ToArray();
            view.First().Texture.Should().Be(city);
        }
Example #5
0
        public Window(WaterTextures water, TextureHolder city, ITileStrategy fallbackStrategy, params ITileStrategy[] strategies)
        {
            this.water = water;
            this.city  = city;

            functionStrategies.Add(CoastWithLandToTheWest);
            functionStrategies.Add(CoastWithLandToTheEast);
            functionStrategies.Add(CoastWithLandToTheNorthEast);
            functionStrategies.Add(CoastWithLandToTheNorthWest);
            functionStrategies.Add(CoastWithLandToTheSouthEast);
            functionStrategies.Add(CoastWithLandToTheSouthWest);

            this.strategies       = strategies;
            this.fallbackStrategy = fallbackStrategy;
        }
        public void UseProperTextureForIsland()
        {
            var ground = new TextureHolder();
            var window = new Window(new WaterTextures(), ground, null, new GroundStrategy(ground));

            var island = new IslandEntity {
                Corners = new[] { new GeoPoint {
                                      X = 1, Y = 1
                                  } }
            };

            window.AddIsland(island);

            var view = window.GetWindow(1, 1, 1, 1);

            view.First().Texture.Should().Be(ground);
        }
Example #7
0
        public Window(WaterTextures water, TextureHolder ground, TextureHolder city)
        {
            this.water = water;
            this.ground = ground;
            this.city = city;

            strategies.Add(StrategyForCity);
            strategies.Add(StrategyForGround);
            strategies.Add(CoastWithLandToTheNorth);
            strategies.Add(CoastWithLandToTheSouth);
            strategies.Add(CoastWithLandToTheWest);
            strategies.Add(CoastWithLandToTheEast);
            strategies.Add(CoastWithLandToTheNorthEast);
            strategies.Add(CoastWithLandToTheNorthWest);
            strategies.Add(CoastWithLandToTheSouthEast);
            strategies.Add(CoastWithLandToTheSouthWest);
            strategies.Add(NullStrategy);
        }
        public void CityTakesPrecedenceOnTerrain()
        {
            var ground = new TextureHolder();
            var city   = new TextureHolder();
            var window = new Window(null, city, null, new GroundStrategy(ground), new CityStrategy(city));

            var island = new IslandEntity {
                Corners = new[] { new GeoPoint {
                                      X = 1, Y = 1
                                  } }
            };

            window.AddIsland(island);
            window.AddCity(new CityEntity(1, 1));

            var view = window.GetWindow(1, 1, 1, 1).ToArray();

            view.First().Texture.Should().Be(city);
        }
        public void LandUnitTakesPrecedenceOnTerrain()
        {
            var ground   = new TextureHolder();
            var city     = new TextureHolder();
            var landUnit = new TextureHolder();
            var window   = new Window(null, city, new DefaultStrategy(null), new LandUnitStrategy(landUnit));

            var island = new IslandEntity {
                Corners = new[] { new GeoPoint {
                                      X = 1, Y = 1
                                  } }
            };

            window.AddCity(new CityEntity(1, 1));
            window.Include(new LandUnitEntity(1, 1));

            var view = window.GetWindow(1, 1, 1, 1).ToArray();

            view.First().Texture.Should().Be(landUnit);
        }
Example #10
0
        /// <summary>
        /// Generates list of textures for given square, when left bottom corner is provided.
        /// </summary>
        /// <param name="lbx"></param>
        /// <param name="lby"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns>Sequence contains textures, starting from left bottom corner and returning rows first.</returns>
        public IEnumerable <PointContext> GetWindow(int lbx, int lby, int dx, int dy)
        {
            for (int y = 0; y < dy; y++)
            {
                for (int x = 0; x < dx; x++)
                {
                    var geox = lbx + x;
                    var geoy = lby + y;

                    var area         = new LocationType[9];
                    var centerOfArea = new GeoPoint(geox, geoy);

                    for (int i = 0; i < 9; i++)
                    {
                        area[i] = LocationType.Water;
                    }

                    area[Directions.NeighborTopLeft]   = points.ContainsKey(centerOfArea.TopLeft()) ? points[centerOfArea.TopLeft()] : LocationType.Water;
                    area[Directions.NeighborNorth]     = points.ContainsKey(centerOfArea.Top()) ? points[centerOfArea.Top()] : LocationType.Water;
                    area[Directions.NeighborTopRight]  = points.ContainsKey(centerOfArea.TopRight()) ? points[centerOfArea.TopRight()] : LocationType.Water;
                    area[Directions.NeighborWest]      = points.ContainsKey(centerOfArea.Left()) ? points[centerOfArea.Left()] : LocationType.Water;
                    area[Directions.NeighborThis]      = points.ContainsKey(centerOfArea) ? points[centerOfArea] : LocationType.Water;
                    area[Directions.NeighborEast]      = points.ContainsKey(centerOfArea.Right()) ? points[centerOfArea.Right()] : LocationType.Water;
                    area[Directions.NeighborDownLeft]  = points.ContainsKey(centerOfArea.DownLeft()) ? points[centerOfArea.DownLeft()] : LocationType.Water;
                    area[Directions.NeighborSouth]     = points.ContainsKey(centerOfArea.Down()) ? points[centerOfArea.Down()] : LocationType.Water;
                    area[Directions.NeighborDownRight] = points.ContainsKey(centerOfArea.DownRight()) ? points[centerOfArea.DownRight()] : LocationType.Water;

                    var centerTexture = new TextureHolder();
                    var handled       = false;
                    foreach (var strategy in functionStrategies)
                    {
                        var texture = strategy(area, centerTexture);
                        if (texture == centerTexture)
                        {
                            continue;
                        }

                        centerTexture = texture;
                        handled       = true;
                        break;
                    }

                    if (!handled)
                    {
                        foreach (var strategy in strategies)
                        {
                            if (!strategy.CanExecute(area))
                            {
                                continue;
                            }

                            centerTexture = strategy.Execute(area);
                            handled       = true;
                            break;
                        }
                    }

                    if (!handled)
                    {
                        centerTexture = fallbackStrategy.Execute(area);
                    }

                    yield return(new PointContext(centerOfArea, centerTexture));
                }
            }
        }
 public static void Draw(this SpriteBatch spriteBatch, Vector2 position, TextureHolder texture)
 {
     spriteBatch.Draw(texture.Texture2D, position, texture.Source, Color.White);
 }
Example #12
0
        /// <summary>
        /// Generates list of textures for given square, when left bottom corner is provided.
        /// </summary>
        /// <param name="lbx"></param>
        /// <param name="lby"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns>Sequence contains textures, starting from left bottom corner and returning rows first.</returns>
        public IEnumerable<PointContext> GetWindow(int lbx, int lby, int dx, int dy)
        {
            for (int y = 0; y < dy; y++)
                for (int x = 0; x < dx; x++)
                {
                    var geox = lbx + x;
                    var geoy = lby + y;

                    var area = new LocationType[9];
                    var centerOfArea = new GeoPoint(geox, geoy);

                    for (int i = 0; i < 9; i++) area[i] = LocationType.Water;

                    area[NeighborTopLeft] = points.ContainsKey(centerOfArea.TopLeft()) ? points[centerOfArea.TopLeft()] : LocationType.Water;
                    area[NeighborNorth] = points.ContainsKey(centerOfArea.Top()) ? points[centerOfArea.Top()] : LocationType.Water;
                    area[NeighborTopRight] = points.ContainsKey(centerOfArea.TopRight()) ? points[centerOfArea.TopRight()] : LocationType.Water;
                    area[NeighborWest] = points.ContainsKey(centerOfArea.Left()) ? points[centerOfArea.Left()] : LocationType.Water;
                    area[NeighborThis] = points.ContainsKey(centerOfArea) ? points[centerOfArea] : LocationType.Water;
                    area[NeighborEast] = points.ContainsKey(centerOfArea.Right()) ? points[centerOfArea.Right()] : LocationType.Water;
                    area[NeighborDownLeft] = points.ContainsKey(centerOfArea.DownLeft()) ? points[centerOfArea.DownLeft()] : LocationType.Water;
                    area[NeighborSouth] = points.ContainsKey(centerOfArea.Down()) ? points[centerOfArea.Down()] : LocationType.Water;
                    area[NeighborDownRight] = points.ContainsKey(centerOfArea.DownRight()) ? points[centerOfArea.DownRight()] : LocationType.Water;

                    var centerTexture = new TextureHolder();
                    foreach (var strategy in strategies)
                    {
                        var texture = strategy(area, centerTexture);
                        if (texture == centerTexture) continue;

                        centerTexture = texture;
                        break;
                    }

                    yield return new PointContext(centerOfArea, centerTexture);
                }
        }
Example #13
0
 public PointContext(GeoPoint geopoint, TextureHolder texture)
 {
     GeoPoint = geopoint;
     Texture  = texture;
 }
Example #14
0
        private TextureHolder CoastWithLandToTheWest(LocationType[] neighbors, TextureHolder defaultValue)
        {
            if (neighbors[NeighborWest] != LocationType.Water) return defaultValue;
            if (neighbors[NeighborEast] == LocationType.Water) return defaultValue;
            if (neighbors[NeighborNorth] != LocationType.Water) return defaultValue;
            if (neighbors[NeighborSouth] != LocationType.Water) return defaultValue;
            if (neighbors[NeighborThis] != LocationType.Water) return defaultValue;

            return water.CoastWithLandToTheWest;
        }
Example #15
0
 // should be invoked at the end of strategies
 private TextureHolder NullStrategy(LocationType[] neighbors, TextureHolder defaultValue)
 {
     return water.Sea;
 }
Example #16
0
        private TextureHolder StrategyForCity(LocationType[] neighbors, TextureHolder defaultValue)
        {
            if (neighbors[NeighborThis] == LocationType.City) return city;

            return defaultValue;
        }
Example #17
0
 public PointContext(GeoPoint geopoint, TextureHolder texture)
 {
     GeoPoint = geopoint;
     Texture = texture;
 }
Example #18
0
        private TextureHolder StrategyForGround(LocationType[] neighbors, TextureHolder defaultValue)
        {
            if (neighbors[NeighborThis] == LocationType.Ground) return ground;

            return defaultValue;
        }