Ejemplo n.º 1
0
 public Field(Tile[,] initialTiles, int marginFromLeft, int marginFromRight)
 {
     _tiles       = initialTiles;
     leftBorder   = _tiles.GetLowerBound(0) + marginFromLeft;
     rightBorder  = _tiles.GetUpperBound(0) - marginFromRight;
     bottomBorder = _tiles.GetUpperBound(1) - 1;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Pierce a 2D array at odd coordinates if it's not occupied by a room
 /// those coordinates are replaced by -1
 /// </summary>
 /// <param name="map">the map to transform</param>
 /// <returns>the transformed map</returns>
 public static Tile[,] PierceArray(Tile[,] map)
 {
     for (int x = 1; x < map.GetUpperBound(0); x += 2)
     {
         for (int y = 1; y < map.GetUpperBound(1); y += 2)
         {
             map[x, y].Type = Type.Floor;
         }
     }
     return(map);
 }
Ejemplo n.º 3
0
 private static void InitializeMap(Tile[,] tileMap)
 {
     // First, fill everything with grassland
     for (int x = 0; x <= tileMap.GetUpperBound(0); x++)
     {
         for (int y = 0; y <= tileMap.GetUpperBound(1); y++)
         {
             tileMap[x, y] = new Tile(x, y, tileSize);
         }
     }
 }
Ejemplo n.º 4
0
 public Chunk(Vector2 position)
 {
     WorldPos  = position;
     var(x, y) = position;
     ChunkId   = new ChunkIdentifier((int)x, (int)y);
     for (var i = 255; i > Tiles.GetUpperBound(0); i--)
     {
         for (var j = 255; j > Tiles.GetUpperBound(1); j--)
         {
             Tiles[i, j] = null;
         }
     }
 }
Ejemplo n.º 5
0
 private int[] FindTile(Tile t)
 {
     for (int i = 0; i < tiles.GetUpperBound(0); i++)
     {
         for (int j = 0; j < tiles.GetUpperBound(1); j++)
         {
             if (tiles[i, j] == t)
             {
                 return(new int[] { i, j });
             }
         }
     }
     return(null);
 }
Ejemplo n.º 6
0
 public void UpdateAdjacencyFull(Layers layer)
 {
     Tile[,] map = Tiles[(int)layer];
     for (int i = 0; i <= map.GetUpperBound(0); i++)
     {
         for (int j = 0; j <= map.GetUpperBound(1); j++)
         {
             if (map[i, j] != null && map[i, j] is Blob)
             {
                 ((Blob)map[i, j]).UpdateAdjacency(map);
             }
         }
     }
 }
Ejemplo n.º 7
0
        public void SetTileExplored(bool isExplored)
        {
            var matrixWidth  = Tiles.GetUpperBound(0);
            var matrixHeight = Tiles.GetUpperBound(1);

            for (var rowIndex = 0; rowIndex < matrixHeight; rowIndex++)
            {
                for (var columnIndex = 0; columnIndex < matrixWidth; columnIndex++)
                {
                    Tiles[columnIndex, rowIndex].IsExplored = false;
                    Tiles[columnIndex, rowIndex].SetVisible(false);
                }
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Generate a maze using the Recursive Backtracking's Algo
 /// the maze won't overlapse with rooms if they are present
 /// </summary>
 private void GenerateMaze()
 {
     Map = PierceArray(Map);  // we first pierce the array with unvisited cell
     for (int x = 1; x < Map.GetUpperBound(0); x += 2)
     {
         for (int y = 1; y < Map.GetUpperBound(1); y += 2)
         {
             Tile t = Map[x, y];
             if (Tile.Find(t) == t && !t.Room)
             {
                 CarvePassageFrom(t);
             }
         }
     }
 }
Ejemplo n.º 9
0
        public void CheckAllCells()
        {
            //Copy the tiles so we can change the Cell's tiles without screwing up the neighbor detection
            _tiles = Cells.CloneTiles();

            for (int x = 0; x <= _tiles.GetUpperBound(0); x++)
            {
                for (int y = 0; y <= _tiles.GetUpperBound(0); y++)
                {
                    short index         = _tiles[x, y].Index;
                    int   neighborcount = GetNeighborsCount(x, y);

                    //Toggle cell based on Life conditions
                    if (index == ALIVE)
                    {
                        if (neighborcount < 2)
                        {
                            Cells.SetTile(x, y, --index);
                        }
                        else if (neighborcount > 3)
                        {
                            Cells.SetTile(x, y, --index);
                        }
                    }
                    //Check to see if the cell is not alive or dead,
                    //subtract its index if not to create the
                    //death map
                    else
                    {
                        if (index < ALIVE || index > DEAD)
                        {
                            index--;
                            if (index < DEAD)
                            {
                                index = DEAD;
                            }
                            Cells.SetTile(x, y, index);
                        }

                        //Turn it on if it's alive
                        if (neighborcount == 3)
                        {
                            Cells.SetTile(x, y, ALIVE);
                        }
                    }
                }
            }
        }
Ejemplo n.º 10
0
        private static TileJson[,] ConvertToJsonMatrix(Tile[,] source)
        {
            var matrixWidth  = source.GetUpperBound(0) + 1;
            var matrixHeight = source.GetUpperBound(1) + 1;

            var result = new TileJson[matrixWidth, matrixHeight];

            for (var rowIndex = 0; rowIndex < matrixHeight; rowIndex++)
            {
                for (var columnIndex = 0; columnIndex < matrixWidth; columnIndex++)
                {
                    result[columnIndex, rowIndex] = ConvertToTileJson(source[columnIndex, rowIndex], new VectorBase(columnIndex, rowIndex));
                }
            }

            return(result);
        }
Ejemplo n.º 11
0
    /// <summary>
    /// Get all the connectors in the map
    ///
    /// a connector is wall between a room and a wall or another room
    /// </summary>
    /// <param name="map">the map where to retrieve the connector</param>
    /// <returns>a List of tuple, containing coorinates for those connectors</returns>
    private static List <Tile> FindConnectors(Tile[,] map)
    {
        var connectors = new List <Tile>();

        for (int x = 1; x < map.GetUpperBound(0) - 1; x++)
        {
            for (int y = 1; y < map.GetUpperBound(1) - 1; y++)
            {
                if (IsValidConnector(map, x, y))
                {
                    var connector = map[x, y];
                    connectors.Add(connector);
                }
            }
        }
        return(connectors);
    }
Ejemplo n.º 12
0
        void doTick()
        {
            TtValue = 0;
            //TrValue = Math.Round( TValue/(50*50*0.4),5);
            TValue = new List <double>();
            if (FrameBuffer.Instance.reset > 1)
            {
                frameBuffer = new FrameBuffer(0, 0, Console.WindowWidth, Console.WindowHeight - 1);
            }
            Tile[,] grid = Tile.grid;
            foreach (var item in grid)
            {
                if (item != null)
                {
                    item.Update();
                    TValue.Add(item.Value);
                    TtValue += item.Value;
                }
            }
            if (roc)
            {
                Random r = new Random();
                int    x = r.Next(grid.GetUpperBound(0) + 1);
                int    y = r.Next(grid.GetUpperBound(1) + 1);
                Tile.grid[x, y].Value += r.Next(90) / (double)1;
                roc = false;
            }
            if (Console.KeyAvailable)

            {
                // Read one key
                ConsoleKeyInfo cki = Console.ReadKey(true);
                if (cki.Key == ConsoleKey.Spacebar || cki.Key == ConsoleKey.Enter)
                {
                    Random r = new Random();
                    int    x = r.Next(grid.GetUpperBound(0) + 1);
                    int    y = r.Next(grid.GetUpperBound(1) + 1);
                    Tile.grid[x, y].Value += r.Next(90) / (double)10;
                    //TrValue = Math.Round(TValue.Average(), 5);
                }
            }
            TrValue = Math.Round(TValue.Average(), 5);
        }
Ejemplo n.º 13
0
        private static void PutTerrainSources(Tile[,] tileMap, string texture, int lowBound, int upBound, string[] forbiddenTextures)
        {
            int xStart = 0;
            int yStart = 0;

            // Put some random starting points for sources on the map
            for (int i = 0; i < random.Next(lowBound, upBound); i++)
            {
                xStart = random.Next(0, tileMap.GetUpperBound(0));
                yStart = random.Next(0, tileMap.GetUpperBound(1));
                if (tileMap[xStart, yStart].Type == TexturePackerMonoGameDefinitions.texturePackerSpriteAtlas.Grass1 && !forbiddenTextures.Contains(tileMap[xStart, yStart].Type))
                {
                    tileMap[xStart, yStart].IsClear = false;
                    tileMap[xStart, yStart].Type    = texture;
                }
                else
                {
                    i--;
                }
            }
        }
Ejemplo n.º 14
0
        private Adjacencies GetAdjacency(Tile[,] map)
        {
            Adjacencies adj = new Adjacencies();

            adj.N = (GridPos.Y > 0) &&
                    map[GridPos.X, GridPos.Y - 1] is Blob &&
                    ((Blob)map[GridPos.X, GridPos.Y - 1])?.BlobGroup == BlobGroup;

            adj.E = (GridPos.X < map.GetUpperBound(0)) &&
                    map[GridPos.X - 1, GridPos.Y] is Blob &&
                    ((Blob)map[GridPos.X - 1, GridPos.Y])?.BlobGroup == BlobGroup;

            adj.W = (GridPos.X > 0) &&
                    map[GridPos.X + 1, GridPos.Y] is Blob &&
                    ((Blob)map[GridPos.X + 1, GridPos.Y])?.BlobGroup == BlobGroup;

            adj.S = (GridPos.Y < map.GetUpperBound(1)) &&
                    map[GridPos.X, GridPos.Y + 1] is Blob &&
                    ((Blob)map[GridPos.X, GridPos.Y + 1])?.BlobGroup == BlobGroup;
            return(adj);
        }
Ejemplo n.º 15
0
        void GenTerrainChain(int amount, int chainLenght, bool walkableSet, string iconSet, ConsoleColor colorset)
        {
            Random r = new Random();

            int[] genPos;

            for (int i = 0; i < amount; i++)
            {
                genPos = new int[2] {
                    r.Next(1, width), r.Next(1, height)
                };
                for (int j = 0; j < chainLenght; j++)
                {
                    map[GetTile(genPos[0], genPos[1]).pos[0], GetTile(genPos[0], genPos[1]).pos[1]] = new Tile(new int[2] {
                        genPos[0], genPos[1]
                    }, walkableSet, iconSet, colorset);
                    int dim = r.Next(2);
                    genPos[dim] = Q.Clamp(1, map.GetUpperBound(dim) - 1, genPos[dim] + r.Next(-1, 2));
                }
            }
        }
Ejemplo n.º 16
0
        public Stage(Tile[,] tiles, Shadow[,] shadows, bool[,] explored, List <Item> items, List <Actor> actors, VectorBase lastHeroPosition, double abundance)
        {
            Tiles    = tiles;
            Shadows  = shadows;
            Explored = explored;

            var width  = Tiles.GetUpperBound(0) + 1;
            var height = Tiles.GetUpperBound(1) + 1;

            _actorsByTile = new Actor[width, height];

            foreach (var actor in actors)
            {
                AddActor(actor);
            }

            Items = items;

            LastHeroPosition = lastHeroPosition;

            Abundance = abundance;
        }
        public void CheckAllCells()
        {
            //Copy the tiles so we can change the Cell's tiles without screwing up the neighbor detection
            _tiles = Cells.CloneTiles();

            for (int x = 0; x <= _tiles.GetUpperBound(0); x++)
            {
                for (int y = 0; y <= _tiles.GetUpperBound(0); y++)
                {
                    short index = _tiles[x, y].Index;
                    int neighborcount = GetNeighborsCount(x, y);

                    //Toggle cell based on Life conditions
                    if (index == ALIVE)
                    {
                        if (neighborcount < 2) Cells.SetTile(x, y, --index);
                        else if (neighborcount > 3) Cells.SetTile(x, y, --index);
                    }
                    //Check to see if the cell is not alive or dead,
                    //subtract its index if not to create the
                    //death map
                    else
                    {
                        if (index < ALIVE || index > DEAD)
                        {
                            index--;
                            if (index < DEAD) index = DEAD;
                            Cells.SetTile(x, y, index);
                        }

                        //Turn it on if it's alive
                        if (neighborcount == 3)
                            Cells.SetTile(x, y, ALIVE);
                    }
                }
            }
        }
Ejemplo n.º 18
0
    /// <summary>
    /// Return the list of T's neighbors
    /// </summary>
    /// <param name="T"></param>
    /// <returns></returns>
    private static List <Tile> Neighbors(Tile T)
    {
        var res = new List <Tile>();

        if (T.X + 1 <= _map.GetUpperBound(0) && _map[T.X + 1, T.Y].IsFloor())
        {
            res.Add(_map[T.X + 1, T.Y]);
        }
        if (T.X - 1 >= _map.GetLowerBound(0) && _map[T.X - 1, T.Y].IsFloor())
        {
            res.Add(_map[T.X - 1, T.Y]);
        }
        if (T.Y + 1 <= _map.GetUpperBound(1) && _map[T.X, T.Y + 1].IsFloor())
        {
            res.Add(_map[T.X, T.Y + 1]);
        }
        if (T.Y - 1 >= _map.GetLowerBound(1) && _map[T.X, T.Y - 1].IsFloor())
        {
            res.Add(_map[T.X, T.Y - 1]);
        }

        // diagonal
        if (T.X + 1 <= _map.GetUpperBound(0) && T.Y + 1 <= _map.GetUpperBound(1) &&
            res.Contains(_map[T.X + 1, T.Y]) && res.Contains(_map[T.X, T.Y + 1]) && _map[T.X + 1, T.Y + 1].IsFloor())
        {
            res.Add(_map[T.X + 1, T.Y + 1]);
        }
        if (T.X + 1 <= _map.GetUpperBound(0) && T.Y - 1 >= _map.GetLowerBound(1) &&
            res.Contains(_map[T.X + 1, T.Y]) && res.Contains(_map[T.X, T.Y - 1]) && _map[T.X + 1, T.Y - 1].IsFloor())
        {
            res.Add(_map[T.X + 1, T.Y - 1]);
        }
        if (T.X - 1 >= _map.GetLowerBound(0) && T.Y - 1 >= _map.GetLowerBound(1) &&
            res.Contains(_map[T.X - 1, T.Y]) && res.Contains(_map[T.X, T.Y - 1]) && _map[T.X - 1, T.Y - 1].IsFloor())
        {
            res.Add(_map[T.X - 1, T.Y - 1]);
        }
        if (T.X - 1 >= _map.GetLowerBound(0) && T.Y + 1 <= _map.GetUpperBound(1) &&
            res.Contains(_map[T.X - 1, T.Y]) && res.Contains(_map[T.X, T.Y + 1]) && _map[T.X - 1, T.Y + 1].IsFloor())
        {
            res.Add(_map[T.X - 1, T.Y + 1]);
        }
        return(res);
    }
        /// <summary>
        /// Generates the grid.
        /// </summary>
        public void GenerateGrid()
        {
            _tiles = new Tile[Game1.MAXX, Game1.MAXY];
            Vector2 spacing = new Vector2 (Tiles.GRIDWIDTH/ 2f, Tiles.GRIDHEIGHT / 2f);
            spacing.X = (float)Math.Round (spacing.X, 0);
            spacing.Y = (float)Math.Round (spacing.Y, 0);

            //Our point of influence to make the center of the city.
            _influencepoint = new Point (Assets.Random.Next (0, Game1.MAXX),
                                        Assets.Random.Next (0, Game1.MAXY));

            List<Point> corners = new List<Point> () {new Point(0,0), new Point(0, _tiles.GetUpperBound(1)),
                new Point(_tiles.GetUpperBound(0), 0), new Point(_tiles.GetUpperBound(0), _tiles.GetUpperBound(1))};

            int largest = 0;
            foreach (Point c in corners) {
                int distance = GetInfluenceDistance(c);
                if(distance > largest)
                    largest = distance;
            }
            _largestinfluencedistance = largest;

            for (int x = 0; x < Game1.MAXX; x++) {
                for (int y = 0; y < Game1.MAXY; y++) {
                    Tile t = new Tile();
                    t.SetIndex(Tiles.GRASS);
                    t.DrawRect = GetTileDrawRect(x,y);
                    t.ShowInfluence = InfluenceViewIsOn;
                    t.InfluenceColor = new Color ( 1f * GetInfluencePercent(new Point(x,y)),1f - GetInfluencePercent (new Point(x, y)),1f - GetInfluencePercent (new Point(x, y)));
                    _tiles[x,y] = t;
                }
            }
        }
        public void CheckAllCells()
        {
            //Copy the tiles so we can change the Cell's tiles without screwing up the neighbor detection
            _tiles = Cells.CloneTiles();
            List<Color> colorsToMix = new List<Color>();

            for (int x = 0; x <= _tiles.GetUpperBound(0); x++)
            {
                for (int y = 0; y <= _tiles.GetUpperBound(0); y++)
                {
                    colorsToMix.Clear();

                    Tile tile = _tiles[x, y];
                    short index = tile.Index;
                    int neighborcount = GetNeighborsCount(x, y);

                    //Toggle cell based on Life conditions
                    if (index == ALIVE)
                    {
                        if (neighborcount < 2) Cells.SetTile(x, y, --index);
                        else if (neighborcount > 3) Cells.SetTile(x, y, --index);
                    }
                    //Check to see if the cell is not alive or dead,
                    //subtract its index if not to create the
                    //death map
                    else
                    {
                        if (index < ALIVE || index > DEAD)
                        {
                            index--;
                            if (index < DEAD) index = DEAD;
                            Cells.SetTile(x, y, index);
                        }

                        //Turn it on if it's alive
                        if (neighborcount == 3)
                        {
                            Cells.SetTile(x, y, new Tile(ALIVE) { Color = new RGBColor(0, 0, 0, 1) });
                        }
                    }

                    if (Cells.GetTile(x, y).Index == ALIVE)
                    {
                        //Now we get neighbor colors, mix them by average hue, output a new color.
                        colorsToMix = colorsToMix.Concat(GetNeighborColors(x, y)).ToList();

                        float newhue = 0f;

                        //find lowest and highest bands
                        float minhue = float.MaxValue;
                        float maxhue = 0f;
                        foreach (var color in colorsToMix)
                        {
                            float colorhue = color.ToHSVColor().H;
                            minhue = Math.Min(minhue, colorhue);
                            maxhue = Math.Max(maxhue, colorhue);
                        }

                        //Check if the tile was dead, and now is alive
                        if (Cells.GetTile(x, y).Index == ALIVE && _tiles[x, y].Index != ALIVE)
                        {
                            foreach (var color in colorsToMix)
                            {
                                float colorhue = color.ToHSVColor().H;

                                newhue = colorhue - minhue;
                                //now that we have that, we will subtract each color from our min, average it, and get our new color.

                                newhue /= colorsToMix.Count();
                                newhue += minhue;
                            }
                            Tile newtile = new Tile(ALIVE);
                            newtile.Color = ColorMath.HSVtoRGB(new HSVColor(newhue, 1, 1, 1, ColorOutOfBoundsAction.WrapAround));
                            Cells.SetTile(x, y, newtile);
                        }
                        else if (Cells.GetTile(x, y).Index == ALIVE)
                        {
                            //Get our tile's color
                            HSVColor tilecolor = ColorMath.RGBtoHSV(_tiles[x, y].Color);
                            foreach (var color in colorsToMix)
                            {
                                float colorhue = color.ToHSVColor().H;

                                //find out which direction is the closest route to the color.
                                float route = (Math.Abs(colorhue - tilecolor.H) < Math.Abs(1 - colorhue - tilecolor.H))
                                                  ? colorhue - tilecolor.H
                                                  : 1 - colorhue - tilecolor.H;
                                newhue += route;
                            }

                            //TODO: Update color blending with Cie-LCH, for which you need to implment XYZ and Cie-Lab
                            //now that we have that, we will subtract each color from our min, average it, and get our new color.

                            newhue /= colorsToMix.Count();
                            newhue += tilecolor.H;

                            Tile newtile = new Tile(ALIVE);
                            newtile.Color = ColorMath.HSVtoRGB(new HSVColor(newhue, 1, 1, 1));
                            newtile.Color.Action = ColorOutOfBoundsAction.WrapAround;
                            Cells.SetTile(x, y, newtile);
                        }
                    }
                }
            }
        }
Ejemplo n.º 21
0
        public MoveInfo TryToMove(Direction direction, Tile[,] level)
        {
            int newX = Position.X;
            int newY = Position.Y;
            int maxX = level.GetUpperBound(0);
            int maxY = level.GetUpperBound(1);

            switch (direction)
            {
            case Direction.North:
                newY = Position.Y - 1;
                break;

            case Direction.South:
                newY = Position.Y + 1;
                break;

            case Direction.West:
                newX = Position.X - 1;
                break;

            case Direction.East:
                newX = Position.X + 1;
                break;

            case Direction.NorthWest:
                newX = Position.X - 1;
                newY = Position.Y - 1;
                break;

            case Direction.NorthEast:
                newX = Position.X + 1;
                newY = Position.Y - 1;
                break;

            case Direction.SouthWest:
                newX = Position.X - 1;
                newY = Position.Y + 1;
                break;

            case Direction.SouthEast:
                newX = Position.X - 1;
                newY = Position.Y + 1;
                break;
            }

            // Check level bounds so we don't try to walk off the level
            if (newX < 0 || newX > maxX ||
                newY < 0 || newY > maxY)
            {
                return(MoveInfo.OutOfBounds);
            }
            else if (level[newX, newY].HasMonster)
            {
                return(MoveInfo.Occupied);
            }
            else if (level[newX, newY] is Wall)
            {
                return(MoveInfo.BlockedByWall);
            }
            else
            {
                Point newPos = new Point(newX, newY);
                Position = newPos;
                return(MoveInfo.Success);
            }
        }
Ejemplo n.º 22
0
 public override void Update(GameTime gt)
 {
     base.Update(gt);
     GetDependency <Body>(DEPENDENCY_BODY).Width  = TileSize.X * (_tiles.GetUpperBound(0) + 1);
     GetDependency <Body>(DEPENDENCY_BODY).Height = TileSize.Y * (_tiles.GetUpperBound(1) + 1);
 }