using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Example
{
enum Face
{
Top,
Bottom,
//etc...
}
class BlockType
{
public byte Id;
public string Name;
public bool IsSolid; //for collision detection
public bool IsOpaque; //for mesh generation
public Color Transparency; //for lighting engine
public Texture2D Texture;
public Rectangle TextureRectangle;
public virtual Rectangle GetTextureRect(Face face)
{
return TextureRectangle;
}
public virtual void Tick(Block block, int x, int y)
{
//
}
public virtual void NeighbourChanged(Block block, int x, int y)
{
//not sure, but i think that redstone uses something like this
}
//etc
public static List<BlockType> Types; //you may want to encapsulate this
//but for moddable games, easy access would be cool
public static BlockType Get(string name)
{
//you may want to use dictionary for this
return Types.FirstOrDefault(t => t.Name == name);
}
public static BlockType Get(byte id)
{
return Types[id];
}
static BlockType()
{
//init types here
Types = new List<BlockType>();
BlockType bt;
byte index = 0;
bt = new BlockType()
{
Id = index++,
Name = "Air"
};
Types.Add(bt);
bt = new BlockType()
{
Id = index++,
Name = "Stone",
IsOpaque = true,
IsSolid = true
};
Types.Add(bt);
bt = new BlockType()
{
Id = index++,
Name = "Red glass",
IsOpaque = false,
IsSolid = true,
Transparency = new Color(1f, 0, 0)
};
}
}
struct Block
{
public byte TypeId;
public byte R, G, B, A;
public BlockType Type
{
get { return BlockType.Get(TypeId); }
set { TypeId = value.Id; }
}
}
class Program
{
static void Main(string[] args)
{
var data = new Block[9];
data[0].Type = BlockType.Get("Stone");
}
}
}