Colin McMillen
93a5d477bb
Saved for posterity here, approximately: https://twitter.com/mcmillen/status/1227326054949408768 GitOrigin-RevId: e960dad1d9241c08dbf1292c6856311d4ebd7a85
35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using Microsoft.Xna.Framework;
|
|
using System;
|
|
|
|
// Good background reading, eventually:
|
|
// https://gamasutra.com/blogs/ItayKeren/20150511/243083/Scroll_Back_The_Theory_and_Practice_of_Cameras_in_SideScrollers.php
|
|
namespace SemiColinGames {
|
|
class Camera {
|
|
// Screen size in pixels is 1920x1080 divided by 4.
|
|
private Rectangle bbox = new Rectangle(0, 0, 480, 270);
|
|
|
|
public int Width { get => bbox.Width; }
|
|
public int Height { get => bbox.Height; }
|
|
public int Left { get => bbox.Left; }
|
|
public int Top { get => bbox.Top; }
|
|
|
|
public Matrix Projection {
|
|
get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height, 0, -1, 1);
|
|
}
|
|
|
|
public void Update(Point player, int worldWidth) {
|
|
int diff = player.X - bbox.Center.X;
|
|
if (Math.Abs(diff) > 16) {
|
|
bbox.Offset((int) (diff * 0.1), 0);
|
|
}
|
|
if (bbox.Left < 0) {
|
|
bbox.Offset(-bbox.Left, 0);
|
|
}
|
|
if (bbox.Right > worldWidth) {
|
|
bbox.Offset(worldWidth - bbox.Right, 0);
|
|
}
|
|
Debug.AddToast($"p: {player.X}, {player.Y} c: {bbox.Center.X}");
|
|
}
|
|
}
|
|
}
|