public AnimatedTile(BlockType block) : base(block) { if (block.Type.HasFlag(TileType.Animated)) Block = block; else throw new InvalidOperationException(block.Name + " is not an AnimatedTile"); }
public BlockMessage(BlockType block, int x, int y, int z) { if (z > 1) //Throw an exception if the Z is out of range throw new System.ArgumentOutOfRangeException("The Z coordinate cannot be more than 1"); this.BlockID = block.ID; this.X = (short)x; this.Y = (short)y; this.Z = (byte)z; this.MessageTime = NetTime.Now; }
/// <summary> /// Places a tile at the specified position, WHILE taking into account it's TileType /// Mainly used for player block placement, if you are looking to place a block through the code /// Either use `Tiles[x,y,z] =` or, set sendMessage to false /// </summary> /// <param name="x">The X position on the grid</param> /// <param name="y">The Y position on the grid</param> /// <param name="layer">The layer, either background or foreground</param> /// <param name="block">The block to place</param> /// <param name="sendMessage">Should the block be sent to the server or not</param> public void PlaceTile(int x, int y, Layer layer, BlockType block, bool sendMessage) { int z = layer == Layer.Foreground ? 1 : 0; if (CanPlaceBlock(x, y, z, block)) { //If the block has changed, and we should send a message, send one if (sendMessage && Tiles[x, y, z].Block.ID != block.ID) { Game.NetManager.Send(new BlockMessage(block, x, y, z)); } //Set the block switch (block.Type) { case TileType.Default: Tiles[x, y, z] = new Tile(block); break; case TileType.Animated: Tiles[x, y, z] = new AnimatedTile(block); break; } } }
/// <summary> /// Determines if a player can place a block at a specified position /// </summary> /// <returns>True if the player can place the block, false otherwise</returns> public bool CanPlaceBlock(int x, int y, int z, BlockType block) { bool Overlaps = false; bool InRange = InBounds(x, y); if (z == Tile.ForegroundIndex && block.ID != BlockType.Empty.ID) { Overlaps = Players.Any(p => (int)(p.SimulationState.Position.X / Tile.Width) == x && (int)(p.SimulationState.Position.Y / Tile.Height) == y); } return (!Overlaps && InRange); }
/// <summary> /// Sets or creates a new block /// </summary> public Tile(BlockType block) { Block = block; }
/// <summary> /// Adds/Creates all of the block type's /// </summary> public static void Init() { Empty = new BlockType("Empty", Layer.All, Rectangle.Empty); Default = new BlockType("Default", Layer.All, new Rectangle(0, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Impassable); Stone = new BlockType("Stone", Layer.All, new Rectangle(Tile.DrawWidth, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Impassable); Dirt = new BlockType("Dirt", Layer.All, new Rectangle(Tile.DrawWidth * 2, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Impassable); Grass = new BlockType("Grass", Layer.All, new Rectangle(Tile.DrawWidth * 3, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Impassable); Wood = new BlockType("Wood", Layer.All, new Rectangle(Tile.DrawWidth * 4, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Impassable); Brick = new BlockType("Brick", Layer.All, new Rectangle(Tile.DrawWidth * 5, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Impassable); Slab = new BlockType("Slab", Layer.All, new Rectangle(Tile.DrawWidth * 6, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Impassable); Glass = new BlockType("Glass", Layer.All, new Rectangle(Tile.DrawWidth * 7, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Impassable); UpArrow = new BlockType("Up Arrow", Layer.Foreground, new Rectangle(Tile.DrawWidth * 8, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Gravity); DownArrow = new BlockType("Down Arrow", Layer.Foreground, new Rectangle(Tile.DrawWidth * 10, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Gravity); LeftArrow = new BlockType("Left Arrow", Layer.Foreground, new Rectangle(Tile.DrawWidth * 9, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Gravity); RightArrow = new BlockType("Right Arrow", Layer.Foreground, new Rectangle(Tile.DrawWidth * 11, 0, Tile.DrawWidth, Tile.DrawHeight), BlockCollision.Gravity); }