Exemple #1
0
        /// <summary>
        /// Remove a tile at this position.
        /// This is advised over manually modifiying the tiles array, as this handles multiblocks.
        /// </summary>
        /// <param name="position">Position of tile to remove. If part of a multitile, the entire multitile will be removed.</param>
        /// <returns></returns>
        public bool RemoveTile(Vector3 position)
        {
            Vector3 origin = FindTileOrigin(position);

            int   tileID = tiles[(int)origin.X, (int)origin.Y, (int)origin.Z];
            ITile tile   = Citysim.instance.tileRegistry.GetTile(tileID);
            Point size   = new Point(1, 1);

            if (tile != null)
            {
                size = tile.GetTileSize();
            }

            for (int x = 0; x < size.X; x++)
            {
                for (int y = 0; y < size.Y; y++)
                {
                    Vector3 currentPos = new Vector3(origin.X + x, origin.Y + y, (int)position.Z);
                    tiles[(int)currentPos.X, (int)currentPos.Y, (int)currentPos.Z]       = 0;
                    tileOrigins[(int)currentPos.X, (int)currentPos.Y, (int)currentPos.Z] = null;
                }
            }

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Places a tile.
        /// This is advised over manually modifiying the tiles array, as this handles multiblocks.
        /// </summary>
        /// <param name="tileID">ID of tile to place</param>
        /// <param name="position">Position to place tile. In the case of multiblocks, this is the top-left coordinate.</param>
        /// <returns></returns>
        public bool PlaceTile(int tileID, Vector3 position)
        {
            if (tileID == 0)
            {
                return(RemoveTile(position));
            }

            ITile tile = Citysim.instance.tileRegistry.GetTile(tileID);
            Point size = tile.GetTileSize();

            //Check if the tile will draw out of the world
            if (tileOrigins.GetLength(0) < position.X + size.X ||
                tileOrigins.GetLength(1) < position.Y + size.Y)
            {
                return(false);
            }

            int blockArea = size.X * size.Y;

            if (blockArea > 1)
            {
                // Bottom-right position of multiblock.
                Vector3 position2 = new Vector3(position.X + size.X - 1, position.Y + size.Y - 1, 1);

                // Check Multiblock will fit.
                for (int width = (int)position.X; width <= position2.X; width++)
                {
                    for (int height = (int)position.Y; height <= position2.Y; height++)
                    {
                        if (IsTilePresent(new Vector3(width, height, position.Z)))
                        {
                            return(false); // Cannot fit!
                        }
                    }
                }

                // Place multiblock origin tile references.
                for (int width = (int)position.X; width <= position2.X; width++)
                {
                    for (int height = (int)position.Y; height <= position2.Y; height++)
                    {
                        tiles[width, height, (int)position.Z]       = 0;
                        tileOrigins[width, height, (int)position.Z] = new Vector3(position.X, position.Y, position.Z);
                    }
                }
            }

            // Place origin block.
            tiles[(int)position.X, (int)position.Y, (int)position.Z] = tileID;

            return(true);
        }
Exemple #3
0
        public void Render(SpriteBatch spriteBatch, Citysim game, GameTime gameTime)
        {
            World world = game.city.world;

            int cameraX = (int)game.camera.position.X;
            int cameraY = (int)game.camera.position.Y;

            // Loop through tiles.
            for (int z = 0; z < World.depth; z++)
            {
                for (int y = 0; y < world.height; y++)
                {
                    for (int x = 0; x < world.width; x++)
                    {
                        ITile tile = game.tileRegistry.GetTile(world.tiles[x, y, z]);
                        if (tile == null)
                        {
                            continue; // unknown tile
                        }
                        Rectangle tileRect = new Rectangle(cameraX + x * tileSize, cameraY + y * tileSize, tileSize * tile.GetTileSize().X, tileSize * tile.GetTileSize().Y);

                        spriteBatch.Draw(tile.GetTexture(gameTime), tileRect, Color.White);
                    }
                }
            }

            if (world.InBounds(game.camera.hovering))
            {
                // Draw hitbox
                int hitX = (int)(game.camera.hovering.X * tileSize + game.camera.position.X);
                int hitY = (int)(game.camera.hovering.Y * tileSize + game.camera.position.Y);
                spriteBatch.Draw(hitbox, new Rectangle(hitX, hitY, tileSize, tileSize), Color.White);
            }
        }