/
EugenyCh7
/
Puppet2D.Net
Обзор
Документация
Войти
/
EugenyCh7
/
Puppet2D.Net
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Puppet2D.Engine/Common/Camera.cs
134 строки
4 KB
EugenyCh7
- Puppet2D.Common.csproj
24 авг 2025, 13:32
24 авг 2025, 13:32
bcb1204
Код
Авторство
О чём код?
using Puppet2D.Engine.Components; using Puppet2D.Engine.GameWindows; using Puppet2D.Graphics; using Puppet2D.System; using System; namespace Puppet2D.Engine.Common { public class Camera : IDrawable { private Vector2 previousPosition; private readonly RectangleShape back = new RectangleShape(new Vector2(1.0f, 1.0f)); private readonly View backView = new View(new Vector2(0.5f, 0.5f), new Vector2(1.0f, 1.0f)); internal readonly View view = new View(); public Camera() { Background = Color.Transparent; Viewport = new FloatRect(0, 0, 1, 1); } /// <summary> /// Is the camera active? /// </summary> public bool IsActive { get; set; } = true; /// <summary> /// The background of the camera. /// </summary> public Color Background { get => back.FillColor; set => back.FillColor = value; } /// <summary> /// The degree of response of the camera to the movement of the target. /// </summary> public float Response { get; set { if (value < 0.0f || value > 1.0f) throw new ArgumentOutOfRangeException(); field = value; } } = 1.0f; /// <summary> /// If the target is not specified, it sets the center point of the camera. /// If the target is specified, it sets the center point of the camera relative to the target. /// </summary> public Vector2 Center { get; set; } /// <summary> /// The size of the camera area in meters. /// </summary> public Vector2 Size { get; set; } /// <summary> /// Screen zoom level (1.0 by default). /// </summary> public float Zoom { get; set { if (value <= 0.0f) throw new ArgumentOutOfRangeException(); field = value; } } = 1.0f; /// <summary> /// The camera's target. /// </summary> public Transformation Target { get; set; } /// <summary> /// Target viewport of the camera, defined as a factor of the size of the target to which the view is applied. /// </summary> public FloatRect Viewport { get => view.Viewport; set { view.Viewport = value; backView.Viewport = value; } } /// <summary> /// The current coordinates where the camera is looking at. /// </summary> public Vector2 ViewCenter => view.Center; /// <summary> /// Set the position instantly. /// </summary> public void MoveTo(Vector2 worldPoint) { previousPosition = worldPoint; } internal void UpdateSize(GameWindow window) { Vector2 newViewSize = new Vector2( Size.X * window.WindowSize.X / (window.InitialWindowSize.X * Zoom), Size.Y * window.WindowSize.Y / (window.InitialWindowSize.Y * Zoom)); view.Size = newViewSize; } internal void UpdateCenter() { if (Target == null) { previousPosition = Center; view.Center = previousPosition; } else { previousPosition = Target.Position * Response + (1.0f - Response) * previousPosition; view.Center = previousPosition + Center; } } public void Draw(IRenderTarget target, RenderStates states) { target.SetView(backView); target.Draw(back, states); } } }