A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB

  1. using Microsoft.Xna.Framework;
  2. using System;
  3. // Good background reading, eventually:
  4. // https://gamasutra.com/blogs/ItayKeren/20150511/243083/Scroll_Back_The_Theory_and_Practice_of_Cameras_in_SideScrollers.php
  5. namespace SemiColinGames {
  6. public class Camera {
  7. // Screen size in pixels is 1920x1080 divided by 4.
  8. private Rectangle bbox = new Rectangle(0, 0, 1920 / 4, 1080 / 4);
  9. public int Width { get => bbox.Width; }
  10. public int Height { get => bbox.Height; }
  11. public int Left { get => bbox.Left; }
  12. public int Top { get => bbox.Top; }
  13. public int Bottom { get => bbox.Bottom; }
  14. public Point HalfSize { get => new Point(Width / 2, Height / 2); }
  15. private readonly Random random = new Random();
  16. private float shakeTime = 0.0f;
  17. public Matrix Projection {
  18. get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height + Top, Top, -1, 1);
  19. }
  20. public void Update(float modelTime, Player player, int worldWidth, int worldHeight) {
  21. Vector2 pos = player.Position;
  22. float xDiff = pos.X - bbox.Center.X;
  23. float yDiff = pos.Y - bbox.Center.Y;
  24. if (Math.Abs(xDiff) > 16) {
  25. bbox.Offset((int) (xDiff * 0.1), 0);
  26. }
  27. if (player.StandingOnGround) {
  28. bbox.Offset(0, (int) (yDiff * 0.1));
  29. }
  30. if (yDiff > 16) {
  31. bbox.Offset(0, (int) (yDiff * 0.2));
  32. }
  33. if (bbox.Left < 0) {
  34. bbox.Offset(-bbox.Left, 0);
  35. }
  36. if (bbox.Right > worldWidth) {
  37. bbox.Offset(worldWidth - bbox.Right, 0);
  38. }
  39. if (bbox.Top < 0) {
  40. bbox.Offset(0, -bbox.Top);
  41. }
  42. if (bbox.Bottom > worldHeight) {
  43. bbox.Offset(0, worldHeight - bbox.Bottom);
  44. }
  45. if (shakeTime > 0) {
  46. shakeTime -= modelTime;
  47. int x = random.Next(-4, 5);
  48. int y = random.Next(-4, 5);
  49. bbox.Offset(x, y);
  50. }
  51. Debug.AddToast($"p: {pos.X}, {pos.Y} c: {bbox.Center.X}");
  52. }
  53. public void Shake() {
  54. shakeTime = 0.3f;
  55. }
  56. }
  57. }