【发布时间】:2015-06-11 05:45:25
【问题描述】:
我正在尝试制作一个全局计时器,在特定时间过后需要通知的所有内容。
例如,在游戏中,会有增益和攻击冷却计时器以及物品冷却等等。
单独管理它们很好,但我如何让它们都在同一个计时器上运行?
我尝试使用带有浮点数作为键和委托作为值的 SortedList,以便在时间到时简单地调用,但我似乎无法管理它。尝试使用 Generic 参数的委托,但我无法将其放入排序列表中。
谁能指出我正确的方向?
【问题讨论】:
我正在尝试制作一个全局计时器,在特定时间过后需要通知的所有内容。
例如,在游戏中,会有增益和攻击冷却计时器以及物品冷却等等。
单独管理它们很好,但我如何让它们都在同一个计时器上运行?
我尝试使用带有浮点数作为键和委托作为值的 SortedList,以便在时间到时简单地调用,但我似乎无法管理它。尝试使用 Generic 参数的委托,但我无法将其放入排序列表中。
谁能指出我正确的方向?
【问题讨论】:
我可以指出两个选项:
TimerControlled 的接口(所有名称都可以更改)使用方法TimerTick(whatever arguments you need)(和其他如果需要),它实现了该类的计时器滴答逻辑。在每个使用计时器相关机制的类上实现接口。最后,在您的基础(逻辑)类上,将您的所有 TimerControlled 对象添加到一个数组(TimerControlled)中,这将允许您循环遍历该数组并使用 2 行代码调用这些对象的 TimerTick 方法。界面:
interface TimerControlled
{
void TimerTick();
}
在你的每个类中实现它:
public class YourClass: TimerControlled{
....
public void TimerTick(){
advanceCooldown();
advanceBuffTimers();
}
}
最后将您的课程添加到TimerControlled 列表中:
class YourLogicClass{
List<YourClass> characters= new List<YourClass>();
private timer;
List<TimerControlled> timerControlledObjects = new List<TimerControlled>();
...
public void Initialize(){
... //your code, character creation and such
foreach(YourClass character in characters){ //do the same with all objects that have TimerControlled interface implemented
timerControlledObjects.add(character);
}
timer = new Timer();
timer.Tick += new EventHandler(timerTick)
timer.Start();
}
public void timerTick(Object sender, EventArgs e){
foreach(TimerControlled timerControlledObject in timerControlObjects){
timerControlledObject.TimerTick();
}
}
}
Global.timer,这意味着该计时器仅存在一个实例。然后将事件处理程序附加到每个相关类的计时器以处理计时器滴答声。 代码:
public static class Global{
//I usually create such class for global settings
public static Timer timer= new Timer();
}
class YourLogicClass{
public void Initialize(){
...
Global.timer.Start();
}
}
class YourClass{
public YourClass(){
Global.timer.tick += new EventHandler(timerTick);
}
private void timerTick(Object sender,EventArgs e){
advanceCooldowns();
advanceBuffTimers();
}
}
请记住,我已经在脑海中编写了代码,因此可能存在一些语法错误,但逻辑是正确的。
如果您对答案还有其他问题,请尽管提问。
【讨论】: