2020-01-18 03:41:45 +00:00
|
|
|
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 {
|
2020-03-09 16:19:19 +00:00
|
|
|
public class Camera {
|
2020-02-11 22:06:17 +00:00
|
|
|
// Screen size in pixels is 1920x1080 divided by 4.
|
2020-07-15 15:20:29 +00:00
|
|
|
private Rectangle bbox = new Rectangle(0, 0, 1920 / 4, 1080 / 4);
|
2020-01-18 03:41:45 +00:00
|
|
|
public int Width { get => bbox.Width; }
|
|
|
|
public int Height { get => bbox.Height; }
|
|
|
|
public int Left { get => bbox.Left; }
|
2020-02-04 22:37:42 +00:00
|
|
|
public int Top { get => bbox.Top; }
|
2020-03-25 15:04:49 +00:00
|
|
|
public int Bottom { get => bbox.Bottom; }
|
|
|
|
|
2020-02-27 20:46:16 +00:00
|
|
|
public Point HalfSize { get => new Point(Width / 2, Height / 2); }
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-03-10 19:06:25 +00:00
|
|
|
private readonly Random random = new Random();
|
2020-03-10 19:04:41 +00:00
|
|
|
private float shakeTime = 0.0f;
|
2020-03-10 19:06:25 +00:00
|
|
|
|
2020-02-11 22:06:17 +00:00
|
|
|
public Matrix Projection {
|
2020-03-24 18:41:07 +00:00
|
|
|
get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height + Top, Top, -1, 1);
|
2020-02-11 22:06:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 19:05:53 +00:00
|
|
|
public void Update(float modelTime, Player player, int worldWidth, int worldHeight) {
|
2020-03-24 18:41:07 +00:00
|
|
|
Vector2 pos = player.Position;
|
|
|
|
float xDiff = pos.X - bbox.Center.X;
|
|
|
|
float yDiff = pos.Y - bbox.Center.Y;
|
|
|
|
if (Math.Abs(xDiff) > 16) {
|
|
|
|
bbox.Offset((int) (xDiff * 0.1), 0);
|
|
|
|
}
|
2020-03-24 19:05:53 +00:00
|
|
|
if (player.StandingOnGround) {
|
2020-03-24 18:41:07 +00:00
|
|
|
bbox.Offset(0, (int) (yDiff * 0.1));
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-03-24 19:05:53 +00:00
|
|
|
if (yDiff > 16) {
|
|
|
|
bbox.Offset(0, (int) (yDiff * 0.2));
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
if (bbox.Left < 0) {
|
|
|
|
bbox.Offset(-bbox.Left, 0);
|
|
|
|
}
|
2020-01-29 23:01:41 +00:00
|
|
|
if (bbox.Right > worldWidth) {
|
|
|
|
bbox.Offset(worldWidth - bbox.Right, 0);
|
|
|
|
}
|
2020-03-24 19:05:53 +00:00
|
|
|
if (bbox.Top < 0) {
|
|
|
|
bbox.Offset(0, -bbox.Top);
|
|
|
|
}
|
|
|
|
if (bbox.Bottom > worldHeight) {
|
|
|
|
bbox.Offset(0, worldHeight - bbox.Bottom);
|
|
|
|
}
|
2020-03-10 19:04:41 +00:00
|
|
|
if (shakeTime > 0) {
|
|
|
|
shakeTime -= modelTime;
|
|
|
|
int x = random.Next(-4, 5);
|
2020-03-24 18:42:14 +00:00
|
|
|
int y = random.Next(-4, 5);
|
|
|
|
bbox.Offset(x, y);
|
2020-03-10 19:04:41 +00:00
|
|
|
}
|
2020-03-24 18:41:07 +00:00
|
|
|
Debug.AddToast($"p: {pos.X}, {pos.Y} c: {bbox.Center.X}");
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-03-10 19:04:41 +00:00
|
|
|
|
|
|
|
public void Shake() {
|
2020-03-24 18:42:14 +00:00
|
|
|
shakeTime = 0.3f;
|
2020-03-10 19:04:41 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|