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.

93 lines
2.9 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace SemiColinGames {
  6. public sealed class ShmupWorld : IWorld {
  7. public class ShmupPlayer {
  8. public TextureRef Texture = Textures.Yellow2;
  9. // Center of player sprite.
  10. public Vector2 Position = new Vector2(48, 1080 / 8);
  11. public Vector2 HalfSize = new Vector2(16, 10);
  12. public float Speed = 150f;
  13. public float ShotCooldown = 0f;
  14. }
  15. public class Shot {
  16. static int color = 0;
  17. public TextureRef Texture;
  18. public Vector2 Position;
  19. public Vector2 HalfSize = new Vector2(11, 4);
  20. public Rectangle Bounds;
  21. public Vector2 Velocity;
  22. public Shot(Vector2 position, Vector2 velocity) {
  23. Texture = (color % 5) switch {
  24. 0 => Textures.Projectile1,
  25. 1 => Textures.Projectile2,
  26. 2 => Textures.Projectile3,
  27. 3 => Textures.Projectile4,
  28. _ => Textures.Projectile5
  29. };
  30. color++;
  31. Position = position;
  32. Velocity = velocity;
  33. Update(0); // set Bounds
  34. }
  35. public void Update(float modelTime) {
  36. Position = Vector2.Add(Position, Vector2.Multiply(Velocity, modelTime));
  37. Bounds = new Rectangle(
  38. (int) (Position.X - HalfSize.X),
  39. (int) (Position.Y - HalfSize.Y),
  40. (int) HalfSize.X * 2,
  41. (int) HalfSize.Y * 2);
  42. }
  43. }
  44. public readonly Rectangle Bounds;
  45. public readonly ShmupPlayer Player;
  46. public readonly ProfilingList<Shot> Shots;
  47. public ShmupWorld() {
  48. Bounds = new Rectangle(0, 0, 1920 / 4, 1080 / 4);
  49. Player = new ShmupPlayer();
  50. Shots = new ProfilingList<Shot>(100, "shots");
  51. }
  52. ~ShmupWorld() {
  53. Dispose();
  54. }
  55. public void Dispose() {
  56. GC.SuppressFinalize(this);
  57. }
  58. public void Update(float modelTime, History<Input> input) {
  59. Vector2 motion = Vector2.Multiply(input[0].Motion, modelTime * Player.Speed);
  60. Player.Position = Vector2.Add(Player.Position, motion);
  61. Player.Position.X = Math.Max(Player.Position.X, Player.HalfSize.X);
  62. Player.Position.X = Math.Min(Player.Position.X, Bounds.Size.X - Player.HalfSize.X);
  63. Player.Position.Y = Math.Max(Player.Position.Y, Player.HalfSize.Y);
  64. Player.Position.Y = Math.Min(Player.Position.Y, Bounds.Size.Y - Player.HalfSize.Y);
  65. Player.ShotCooldown -= modelTime;
  66. foreach (Shot shot in Shots) {
  67. shot.Update(modelTime);
  68. }
  69. // TODO: move this all into Player.Update().
  70. if (input[0].Attack && Player.ShotCooldown <= 0) {
  71. Player.ShotCooldown = 0.2f;
  72. Vector2 shotOffset = new Vector2(12, 2);
  73. Vector2 shotPosition = Vector2.Add(Player.Position, shotOffset);
  74. Shots.Add(new Shot(shotPosition, new Vector2(300, 0)));
  75. }
  76. Shots.RemoveAll(shot => !Bounds.Intersects(shot.Bounds));
  77. Debug.AddToast("shots: " + Shots.Count);
  78. }
  79. }
  80. }