Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smooth interpolation in viewport position #112

Merged
merged 7 commits into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/Client/Systems/Camera.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
using Microsoft.Xna.Framework;
using Shared.Entities;
using System;
using System.Diagnostics;
using Shared.Components;
using System.Diagnostics;

namespace Client.Systems;

public class Camera : Shared.Systems.System
{
private Rectangle m_viewport = new();
public Rectangle Viewport { get { return m_viewport; } }
private const float m_maxScale = 0.6f;
private const float m_maxScale = 0.7f;
private const float m_minScale = 0.3f;
private const int m_zoomInTime = 1000;
private const int m_zoomOutTime = 15000;
private float m_currentZoom = m_minScale;
public float Zoom { get { return m_currentZoom; } }
private PlayerData m_playerData;
private readonly PlayerData m_playerData;
private bool m_playing = false;
private bool m_newGame = true;
private float m_bezierCurveMS = 0;
private float m_interpTime;
private Vector2 m_prevDirection;
private float m_timeSincePrevDirection = 0;

public Camera(Vector2 viewportSize, PlayerData playerData) : base(typeof(Input))
{
Expand All @@ -46,6 +49,7 @@ public override void update(TimeSpan elapsedTime)

if (!m_playing)
{
m_newGame = true;
m_playing = true;
m_interpTime = m_zoomInTime;
m_bezierCurveMS = 0;
Expand All @@ -57,16 +61,30 @@ public override void update(TimeSpan elapsedTime)
if (e.contains<Name>() && e.get<Name>().name == m_playerData.playerName)
{
Entity player = e;
Vector2 pos = player.get<Position>().position;
Vector2 targetPos = player.get<Position>().position;

if (m_newGame)
{
m_newGame = false;
m_viewport.Location = targetPos.ToPoint();
}

// TODO: Change zoom depending on player size
Vector2 cameraOffset = targetPos - m_viewport.Location.ToVector2();
float interpolationSpeed = 0.05f;
Vector2 interpolatedPos = m_viewport.Location.ToVector2() + cameraOffset * interpolationSpeed;
m_viewport.Location = interpolatedPos.ToPoint();

m_viewport.Location = pos.ToPoint();
return;
break;
}
}
}

private static Vector2 RadiansToVector(float radians)
{
Vector2 directionVec = new((float)Math.Cos(radians), (float)Math.Sin(radians));
return Vector2.Normalize(directionVec);
}

private void BezierZoom(TimeSpan elapsedTime, bool zoomIn = true)
{
int direction = zoomIn ? 1 : -1;
Expand Down Expand Up @@ -119,7 +137,7 @@ public static Vector2 GetPoint(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, f
}

// Linear interpolation between two vectors
private static Vector2 Lerp(Vector2 a, Vector2 b, float t)
public static Vector2 Lerp(Vector2 a, Vector2 b, float t)
{
return a + (b - a) * t;
}
Expand Down
Loading