我想要做的就是为我的游戏我开发我有一个使用电报结构消息系统如下:是否可以使用类型变量来转换对象的变量?
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