55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Godot;
|
||
|
||
namespace BITKit;
|
||
/// <summary>
|
||
/// 摄像头服务,该服务需要加载到Camera3D节点中
|
||
/// </summary>
|
||
public partial class CameraService:Camera3D
|
||
{
|
||
public static CameraService Singleton { get; private set; }
|
||
/// <summary>
|
||
/// 场景中所有的摄像头
|
||
/// </summary>
|
||
private static readonly List<IVirtualCamera> _cameras=new();
|
||
/// <summary>
|
||
/// 当前已激活的摄像头
|
||
/// </summary>
|
||
public static IVirtualCamera ActiveCamera { get; private set; }
|
||
/// <summary>
|
||
/// 注册摄像头
|
||
/// </summary>
|
||
/// <param name="camera">摄像头</param>
|
||
/// <returns></returns>
|
||
public static bool Register(IVirtualCamera camera) => _cameras.TryAdd(camera);
|
||
/// <summary>
|
||
/// 注销摄像头
|
||
/// </summary>
|
||
/// <param name="camera">摄像头</param>
|
||
/// <returns></returns>
|
||
public static bool UnRegister(IVirtualCamera camera) => _cameras.TryRemove(camera);
|
||
|
||
public override void _Ready()
|
||
{
|
||
Singleton = this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 处理摄像头的位置
|
||
/// </summary>
|
||
/// <param name="delta"></param>
|
||
public override void _Process(double delta)
|
||
{
|
||
//获取所有已启用的相机并加载一个虚拟相机的数据
|
||
foreach (var x in _cameras.Where(x => x.IsEnabled))
|
||
{
|
||
//应用相机坐标
|
||
Position = x.Position;
|
||
//应用相机角度
|
||
Rotation = x.Rotation;
|
||
//已加载相机位置,退出循环
|
||
break;
|
||
}
|
||
}
|
||
} |