开发者

Filling a list with classes which text point to a class

How can I reduce this _tile assignment, so I could make a single assignment?

x is a anonymous list.

x => List {String name, int x, int y}

and a List _tiles

_tiles.AddRange(from tile in x
where tile.Type.ToLower() == "start".ToLower()
select new Start(this)
{
Position = new Vector2(tile.X, tile.Y),
Texture = _te开发者_StackOverflowxture2D

});

_tiles.AddRange(from tile in x
where tile.Type.ToLower() == "One".ToLower()
select new OneTouch(this)
{
Position = new Vector2(tile.X, tile.Y),
Texture = _texture2D

});

...

_tiles.AddRange(from tile in x
where tile.Type.ToLower() == "Two".ToLower()
select new ReverseTouch(this)
{
Position = new Vector2(tile.X, tile.Y),
Texture = _texture2D


IEnumerable<Tile> tiles = x.Select(tile =>
{
  Tile currentTile = null;

  switch (tile.Type.ToLower())
  {
    case "start":
      currentTile = new Start(this);
      break;

    case "One":
      currentTile = new OneTouch(this);
      break;

    case "Two":
      currentTile = new Start(this)
      break;

    ...

    default:
      // You know what to do...
   }

   currentTile.Position = new Vector2(tile.X, tile.Y),
   currentTile.Texture = _texture2D

   return currentTile;
});

From there you can build a list using tiles.ToList() or fill an existing one using _tiles.AddRange(tiles);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜