Advertisement
Guest User

BlockType and Block

a guest
Dec 26th, 2012
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace Example
  10. {
  11.     enum Face
  12.     {
  13.         Top,
  14.         Bottom,
  15.         //etc...
  16.     }
  17.     class BlockType
  18.     {
  19.         public byte Id;
  20.         public string Name;
  21.         public bool IsSolid; //for collision detection
  22.         public bool IsOpaque; //for mesh generation
  23.         public Color Transparency; //for lighting engine
  24.         public Texture2D Texture;
  25.         public Rectangle TextureRectangle;
  26.  
  27.         public virtual Rectangle GetTextureRect(Face face)
  28.         {
  29.             return TextureRectangle;
  30.         }
  31.  
  32.         public virtual void Tick(Block block, int x, int y)
  33.         {
  34.             //
  35.         }
  36.         public virtual void NeighbourChanged(Block block, int x, int y)
  37.         {
  38.             //not sure, but i think that redstone uses something like this
  39.         }
  40.         //etc
  41.  
  42.         public static List<BlockType> Types; //you may want to encapsulate this
  43.         //but for moddable games, easy access would be cool
  44.  
  45.         public static BlockType Get(string name)
  46.         {
  47.             //you may want to use dictionary for this
  48.             return Types.FirstOrDefault(t => t.Name == name);
  49.         }
  50.  
  51.         public static BlockType Get(byte id)
  52.         {
  53.             return Types[id];
  54.         }
  55.  
  56.         static BlockType()
  57.         {
  58.             //init types here
  59.  
  60.             Types = new List<BlockType>();
  61.             BlockType bt;
  62.             byte index = 0;
  63.  
  64.             bt = new BlockType()
  65.             {
  66.                 Id = index++,
  67.                 Name = "Air"
  68.             };
  69.             Types.Add(bt);
  70.  
  71.             bt = new BlockType()
  72.                 {
  73.                     Id = index++,
  74.                     Name = "Stone",
  75.                     IsOpaque = true,
  76.                     IsSolid =  true
  77.                 };
  78.             Types.Add(bt);
  79.  
  80.             bt = new BlockType()
  81.                 {
  82.                     Id = index++,
  83.                     Name = "Red glass",
  84.                     IsOpaque = false,
  85.                     IsSolid = true,
  86.                     Transparency = new Color(1f, 0, 0)
  87.                 };
  88.         }
  89.     }
  90.  
  91.     struct Block
  92.     {
  93.         public byte TypeId;
  94.         public byte R, G, B, A;
  95.  
  96.         public BlockType Type
  97.         {
  98.             get { return BlockType.Get(TypeId); }
  99.             set { TypeId = value.Id; }
  100.         }
  101.     }
  102.  
  103.  
  104.  
  105.     class Program
  106.     {
  107.         static void Main(string[] args)
  108.         {
  109.             var data = new Block[9];
  110.             data[0].Type = BlockType.Get("Stone");
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement