我想我自己的List
其在foreach loop
工作的能力。除了其他重要命令之外,List
也有類似IndexOf(這是我不喜歡List的唯一東西,因爲它是動態更改的)。我列表中的一個必須跟蹤所有的索引。以及Add
,Contains
,Count
,Remove
,以及[]
訪問器(不知道如何做到這一點),所以現在Get
應該做的伎倆。C#試圖讓我自己列出具有靜態指標計數器
名單應鑄造到名爲實體的基類,這是因爲它是相似的其他兩個類NPC /播放機之間共享英寸
反正我有超過我編碼此服務器也沒有客戶端的控制,但該協議specifition要求所有球員以跟蹤指數沒有做任何劇烈的動態變化規律名單做索引。
我一直關注的教程如何使自己的Collections
我下來,我解決不了3個錯誤。
Argument 3: cannot convert from 'EntityList<T>' to 'EntityList<Entity>'
Cannot apply indexing with [] to an expression of type 'EntityList<Entity>'
The best overloaded method match for 'EntityListEnumerator<T>.EntityListEnumerator(object[], System.Collections.Generic.HashSet<int>, EntityList<Entity>)' has some invalid arguments
此外,我想請問我這樣做對嗎?或者看起來有些狡猾。
感謝您的幫助,我很感激..的C#這部分似乎極爲先進和難以把握的概念。
源到EntityList
public class EntityList<T> : ICollection<T> where T : Entity
{
private const int DEFAULT_CAPACITY = 1600, MIN_VALUE = 1;
public object[] entities;
public HashSet<int> indicies = new HashSet<int>();
public int curIndex = MIN_VALUE;
public int capacity;
public EntityList(int capacity) {
entities = new object[capacity];
this.capacity = capacity;
}
public EntityList()
: this(DEFAULT_CAPACITY) {}
public bool Add(T entity)
{
Add(entity, curIndex);
return true;
}
public void Add(T entity, int index) {
if (entities[curIndex] != null) {
increaseIndex();
Add(entity, curIndex);
} else {
entities[curIndex] = entity;
entity.setIndex(index);
indicies.Add(curIndex);
increaseIndex();
}
}
public T Get(int index)
{
return (T)entities[index];
}
public void Remove(T entity)
{
entities[entity.getIndex()] = null;
indicies.Remove(entity.getIndex());
decreaseIndex();
}
public T Remove(int index)
{
Object temp = entities[index];
entities[index] = null;
indicies.Remove(index);
decreaseIndex();
return (T)temp;
}
IEnumerator IEnumerable.GetEnumerator()
{
return new EntityListEnumerator<T>(entities, indicies, this);
}
private void increaseIndex() {
curIndex++;
if (curIndex >= capacity) {
curIndex = MIN_VALUE;
}
}
private void decreaseIndex()
{
curIndex--;
if (curIndex <= capacity)
curIndex = MIN_VALUE;
}
public int IndexOf(T entity) {
foreach(int index in indicies) {
if (entities[index].Equals(entity)) {
return index;
}
}
return -1;
}
public bool Contains(T entity)
{
return IndexOf(entity) > -1;
}
public int Count {
get
{
return indicies.Count();
}
}
}
源到EntityListEnumerator
class EntityListEnumerator<E> : IEnumerator<E> where E : Entity
{
private int[] indicies;
private object[] entities;
private EntityList<Entity> entityList;
protected int curIndex; //current index
protected E _current; //current enumerated object in the collection
public EntityListEnumerator(object[] entities, HashSet<int> indicies, EntityList<Entity> entityList)
{
this.entities = entities;
this.indicies = indicies.ToArray();
this.entityList = entityList;
curIndex = -1;
}
public virtual E Current
{
get
{
return _current;
}
}
public virtual bool MoveNext()
{
//make sure we are within the bounds of the collection
if (++curIndex >= entityList.Count)
{
//if not return false
return false;
}
else
{
//if we are, then set the current element
//to the next object in the collection
_current = entityList[indicies[curIndex]];
}
//return true
return true;
}
public void Remove() {
if (curIndex >= 1)
{
entityList.Remove(indicies[curIndex - 1]);
}
}
// Reset the enumerator
public virtual void Reset()
{
_current = default(E); //reset current object
curIndex = -1;
}
// Dispose method
public virtual void Dispose()
{
entityList = null;
_current = default(E);
curIndex = -1;
}
}
對不起問一下,爲什麼呢? –
爲什麼?爲什麼我試圖做到這一點?好吧,從長遠來看它會更加靈活,我認爲它就像隱藏在引擎蓋下的引擎,因爲你知道它會正常工作,你不必擔心它太多。那麼再加上它有點重用代碼,因爲如果沒有這個類,我會爲Npc/Player兩套索引保持器。呃,我不知道爲什麼,但我想解決這個問題,請告訴我,如果你知道最新錯誤我是一個學習如何在C#中做事的新手。 – SSpoke
如果你的意思是爲什麼我不使用List?以及列表的索引是動態的..我希望他們是靜態的..那總結起來。在第500個索引中說一個玩家註銷..500以後的所有球員都將被移位,並搞砸索引的順序。地獄,如果第一個球員註銷..每個人都擰了..與npcs相同。 – SSpoke