74 lines
2.0 KiB
C#
74 lines
2.0 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
namespace Cosmobox
|
|
{
|
|
public partial class TickControl : Node
|
|
{
|
|
[Export] public bool pause = false;
|
|
|
|
private List<IThing> things = new List<IThing>();
|
|
private List<IThingPhysics> thingsPhysics = new List<IThingPhysics>();
|
|
private List<IThingUI> thingsUI = new List<IThingUI>();
|
|
public override void _Ready()
|
|
{
|
|
// 从场景树的根节点开始查找实现了 IThing 接口的节点
|
|
FindThingsInScene(GetTree().GetRoot());
|
|
}
|
|
|
|
private void FindThingsInScene(Node node)
|
|
{
|
|
// 检查当前节点是否实现了 IThing 接口
|
|
if (node is IThing thing)
|
|
{
|
|
things.Add(thing);
|
|
}
|
|
|
|
// 检查当前节点是否实现了 IThingPhysics 接口
|
|
if (node is IThingPhysics physicsThing)
|
|
{
|
|
thingsPhysics.Add(physicsThing);
|
|
}
|
|
|
|
// 检查当前节点是否实现了 IThingUI 接口
|
|
if (node is IThingUI uiThing)
|
|
{
|
|
thingsUI.Add(uiThing);
|
|
}
|
|
|
|
// 递归查找子节点
|
|
foreach (Node child in node.GetChildren())
|
|
{
|
|
FindThingsInScene(child);
|
|
}
|
|
}
|
|
public override void _Process(double delta)
|
|
{
|
|
if (!pause)
|
|
{
|
|
foreach (var thing in things)
|
|
{
|
|
thing.Update(delta);
|
|
}
|
|
}
|
|
foreach (var thing in thingsUI)
|
|
{
|
|
thing.UpdateUI();
|
|
}
|
|
}
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
if (!pause)
|
|
{
|
|
foreach (var thing in thingsPhysics)
|
|
{
|
|
thing.PhysicsUpdate(delta);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
} |