2016-08-23 49 views
0

我正在爲我的個人使用創建一個框架,它允許我創建環境,實體,物品等...... 現在,我希望能夠檢索位置我剛剛創建並用它做了一些事情。就像所說的地點必須有這種類型的怪物,可能有一個鐵匠幫我打造新武器,而且由於這是一個基於文本的遊戲,我想將一些文本鏈接到這些地點。根據玩家位置添加元素(文本,事件)

我已經得到了在世界級的填充像這樣的列表:

Environment.Environments.Add(new Environment("Dark Forest", "A very dark... very frightening place...", true, true)); 

這裏的大壞代碼:

class Environment 
{  
    public string Name { get; private set; } 
    public string Description { get; private set; } 
    public bool ContainsItems { get; set; } 
    public bool ContainsEntities { get; set; } 
    public static List<Environment> Environments = new List<Environment>(); 

    public Environment(string name, string description, bool containsItems, bool containEntities) 
    { 
     Name = name; 
     Description = description; 
     ContainsItems = containsItems; 
     ContainsEntities = containEntities; 
    } 

    public void SetLocation() 
    { 
     Console.Clear(); 
     Console.ForegroundColor = ConsoleColor.Cyan; 
     Console.WriteLine(); 
     DisplayText("You arrived at {0}", Name); 
     DisplayText(Description); 
     Console.ReadKey(); 

     if (ContainsItems) 
     { 
      Console.ForegroundColor = ConsoleColor.Yellow; 
      DisplayText("If you look carefully, you might find something valuable!"); 
      if(ContainsEntities) 
      { 
       Console.ForegroundColor = ConsoleColor.Red; 
       DisplayText("Proceed with caution, some monsters hides here!"); 
      }    
     } 
     Console.ForegroundColor = ConsoleColor.White;    
    } 
} 

我嘗試不同的東西,如添加被鏈接事件到我的一個命令「看」,但我不確定它是否是我需要的正確工具,我必須說,我從來沒有使用過活動!所以如果你要實現這樣的事情,你會怎麼做呢?!

回答

1

我會創建一個具有位置的網格系統。給一個x/y座標,包含細節集合(Items和Creatures(不要使用「entity」它有它自己的含義))。

public class Environment 
{ 
    public List<Location> Locations {set; get;} 
    public Location CurrentLocation {get; set;} 
    public Environment() 
    { 
     Locations = new List<Locations> {...} //set up Locations in a grid 
     public Location Move(string direction) 
     { 
     //check to see if there is a location in the direction the user wants to move 
     //if so, load the new Location into CurrentLocation. If not, throw an exception 
     } 
    } 
} 

public class Location 
{ 
    public List<Item> Items {set; get;} 
    public List<Creature> Creatures {set; get;} 
    public int X {get; set;} 
    public int Y {get; set;} 
    public void OnLoaded() 
    { 
     //here is where you check if there are items, or creatures, etc, simply by counting the list... 
    } 
} 
+1

正是我在找的東西,非常感謝你! – Maxwell