我想要做的就是爲我的遊戲我開發我有一個使用電報結構消息系統如下:是否可以使用類型變量來轉換對象的變量?
public struct Telegram
{
private int sender;
private int receiver;
public int Receiver
{
get
{ return receiver; }
}
//Message of an enumerated messageToSend in Messages
private Message messageToSend;
public Message MessageToSend
{
get
{ return messageToSend; }
}
//for delayed messages
private double dispatchTime;
public double DispatchTime
{
get
{ return dispatchTime; }
set
{ dispatchTime = value; }
}
//for any additional info
private object extraInfo;
public object ExtraInfo
{
get
{ return extraInfo; }
}
public Telegram(double time, int otherSender, int otherReceiver,
Message otherMessage, object info = null)
{
dispatchTime = time;
sender = otherSender;
receiver = otherReceiver;
messageToSend = otherMessage;
extraInfo = info;
}
}
我希望能夠做的是,因爲額外信息是基於消息類型傳遞的,並且需要是對象,爲了方便不必使用各種額外的信息類型對一堆函數進行編碼,我希望獲取其裝入的對象的類型函數通過額外的信息變量。
我知道我可以用.getType()
做到這一點,並將其存儲在Type
變量中。
這裏有棘手的部分,我不知道我能做些什麼。我想要做的是使用該變量在收到電報的東西根據發送的消息類型處理它時轉換對象。這可能嗎?
不能使用通用的電報類,因爲它導致事情打破,當我只是試圖轉換我的消息代碼。這裏是其餘的相關代碼:
/*Telegrams are stored in a priority queue. Therefore the < and ==
operators are overloaded so the PQ can sort the telegrams
by time priority. Times must be smaller than
SmallestDelay before two Telegrams are considered unique.*/
public const double SmallestDelay = 0.25;
public static bool operator ==(Telegram t1, Telegram t2)
{
return (Math.Abs(t1.dispatchTime - t2.dispatchTime) < SmallestDelay) &&
(t1.sender == t2.sender) &&
(t1.receiver == t2.receiver) &&
(t1.messageToSend == t2.messageToSend);
}
public static bool operator !=(Telegram t1, Telegram t2)
{
return (Math.Abs(t1.dispatchTime - t2.dispatchTime) > SmallestDelay) &&
(t1.sender != t2.sender) &&
(t1.receiver != t2.receiver) &&
(t1.messageToSend != t2.messageToSend);
}
public static bool operator <(Telegram t1, Telegram t2)
{
if (t1 == t2)
return false;
else
return (t1.dispatchTime < t2.dispatchTime);
}
public static bool operator >(Telegram t1, Telegram t2)
{
if (t1 == t2)
return false;
else
return (t1.dispatchTime > t2.dispatchTime);
}
sealed class MessageDispatcher
{
public const double sendMessageImmediately = 0.0;
public const int noAdditionalInfo = 0;
public const int senderIdIrrelevant = -1;
//a set is used as the container for the delayed messages
//because of the benefit of automatic sorting and avoidance
//of duplicates. Messages are sorted by their dispatch time.
private static SortedSet<Telegram> priorityQueue = new SortedSet<Telegram>();
/// <summary>
/// this method is utilized by DispatchMessage or DispatchDelayedMessages.
/// This method calls the messageToSend handling member function of the receiving
/// entity, receiver, with the newly created telegram
/// </summary>
/// <param name="receiver"></param>
/// <param name="messageToSend"></param>
private static void Discharge(ref BaseEntityInfo receiver, ref Telegram message)
{
if (!receiver.HandleMessage(ref message))
{
//telegram could not be handled
}
}
private MessageDispatcher() { }
public static readonly MessageDispatcher instance = new MessageDispatcher();
/// <summary>
/// given a messageToSend, a receiver, a sender and any time delay, this function
/// routes the messageToSend to the correct entity (if no delay) or stores it
/// in the messageToSend queue to be dispatched at the correct time. Entities referenced
/// by iD.
/// </summary>
/// <param name="delay"></param>
/// <param name="sender"></param>
/// <param name="otherReceiver"></param>
/// <param name="messageToSend"></param>
/// <param name="additionalInfo"></param>
public static void DispatchMessage(double delay, int sender,
int otherReceiver, Message message,
object additionalInfo = null)
{
//get the reciever
BaseEntityInfo receiver = EntityMgr.entityManager.GetEntityFromID(otherReceiver);
//make sure the Receiver is valid
if (receiver == null)
return;
//create the telegram
Telegram telegram = new Telegram(0, sender, otherReceiver, message, additionalInfo);
//if there is no delay, route telegram immediately
if (delay <= 0.0)
//send the telegram to the recipient
Discharge(ref receiver, ref telegram);
//else calculate the time when the telegram should be dispatched
else
{
double CurrentTime = Clock.Current();
telegram.DispatchTime = CurrentTime + delay;
//and put it in the queue
priorityQueue.Add(telegram);
}
}
/// <summary>
/// This function dispatches any telegrams with a timestamp that has
/// expired. Any dispatched telegrams are removed from the queue as it
/// sends out any delayed messages. This method is called each time through
/// the main game loop.
/// </summary>
public static void DispatchDelayedMessages()
{
double CurrentTime = Clock.Current();
//now peek at the queue to see if any telegrams need dispatching.
//remove all telegrams from the front of the queue that have gone
//past their sell by date
while (!(priorityQueue.Count == 0) &&
(priorityQueue.ElementAt(0).DispatchTime < CurrentTime) &&
(priorityQueue.ElementAt(0).DispatchTime > 0))
{
//read the telegram from the front of the queue
Telegram telegram = priorityQueue.ElementAt(0);
//find the recipient
BaseEntityInfo receiver = EntityMgr.entityManager.GetEntityFromID(telegram.Receiver);
//send the telegram to the recipient
Discharge(ref receiver, ref telegram);
//remove it from the queue
priorityQueue.Remove(priorityQueue.ElementAt(0));
}
}
}
你能解釋一下爲什麼你認爲'struct'在這裏是個好主意嗎? – spender 2015-02-08 18:11:52
[「類和結構之間進行選擇」](https://msdn.microsoft.com/en-us/library/ms229017%28v=vs.110%29.aspx)表示如下:避免定義一個結構,除非該類型具有以下所有特徵:(a)它邏輯上表示一個單一的值,類似於原始類型(int,double等)。 (b)它具有小於16字節的實例大小。 (c)它是不可變的。 (d)不必頻繁地裝箱。 *根據我的估算,你在四項計數中都失敗了。* – spender 2015-02-08 18:15:11
因爲我不需要電報作爲課程。 – 2015-02-08 18:16:00