Skip to content

Commit

Permalink
Added zoom functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
caden-maxwell committed Apr 1, 2024
1 parent d0d7f41 commit 2509570
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public bool initialize(ContentManager contentManager, Controls controls, Graphic
m_contentManager = contentManager;
m_entities = new Dictionary<uint, Entity>();
m_systemInterpolation = new Systems.Interpolation();
m_systemCamera = new Systems.Camera();
m_systemCamera = new Systems.Camera(new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));
m_systemRenderer = new Systems.Renderer(m_systemCamera, graphics);
m_systemNetwork = new Systems.Network();

Expand Down
16 changes: 11 additions & 5 deletions src/Client/Systems/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ public class Camera : Shared.Systems.System
{
private Rectangle m_viewport = new();
public Rectangle Viewport { get { return m_viewport; } }
private float m_zoom = 1.0f;
public float Zoom { get { return m_zoom; } }

public Camera() :
public Camera(Vector2 viewportSize) :
base(
typeof(Shared.Components.Position),
typeof(Shared.Components.Movement),
typeof(Shared.Components.Input)
typeof(Shared.Components.Input),
typeof(Shared.Components.Size)
)
{ }
{ m_viewport.Size = viewportSize.ToPoint(); }

public override void update(TimeSpan elapsedTime)
{
Expand All @@ -31,8 +34,11 @@ public override void update(TimeSpan elapsedTime)
}

Entity player = m_entities.Values.ToArray()[0];
Point pos = player.get<Shared.Components.Position>().position.ToPoint();
Vector2 pos = player.get<Shared.Components.Position>().position;
Vector2 size = player.get<Shared.Components.Size>().size;

m_viewport.Location = pos;
m_viewport.Location = pos.ToPoint() - (size / 2).ToPoint();

// TODO: Change zoom depending on factors (player size, player death, etc.)
}
}
10 changes: 7 additions & 3 deletions src/Client/Systems/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Shared.Components.Appearance;

using System;
using System.Reflection.Metadata.Ecma335;

namespace Client.Systems;

Expand All @@ -30,12 +31,15 @@ public override void update(TimeSpan elapsedTime) { }

public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch)
{
float scale = m_camera.Zoom;
Matrix matrix = Matrix.Identity;
Vector2 offset = -m_camera.Viewport.Location.ToVector2()
+ new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2);
matrix.Translation = new Vector3(offset, 0);
+ new Vector2(m_camera.Viewport.Width,m_camera.Viewport.Height) / scale / 2;
float scaleX = m_graphics.PreferredBackBufferWidth / (float)m_camera.Viewport.Width * scale;
float scaleY = m_graphics.PreferredBackBufferHeight / (float)m_camera.Viewport.Height * scale;

// TODO: Account for viewport width and height
matrix *= Matrix.CreateTranslation(new Vector3(offset, 0));
matrix *= Matrix.CreateScale(scaleX, scaleY, 1);

spriteBatch.Begin(transformMatrix: matrix);
foreach (Entity entity in m_entities.Values)
Expand Down

0 comments on commit 2509570

Please sign in to comment.