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.

92 lines
3.0 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. }
  14. public class Shot {
  15. static int color = 0;
  16. public TextureRef Texture;
  17. public Vector2 Position;
  18. public Vector2 HalfSize = new Vector2(11, 4);
  19. public Rectangle Bounds;
  20. public Vector2 Velocity;
  21. public Shot(Vector2 position, Vector2 velocity) {
  22. Texture = (color % 5) switch {
  23. 0 => Textures.Projectile1,
  24. 1 => Textures.Projectile2,
  25. 2 => Textures.Projectile3,
  26. 3 => Textures.Projectile4,
  27. _ => Textures.Projectile5
  28. };
  29. color++;
  30. Position = position;
  31. Velocity = velocity;
  32. Update(0); // set Bounds
  33. }
  34. public void Update(float modelTime) {
  35. Position = Vector2.Add(Position, Vector2.Multiply(Velocity, modelTime));
  36. Bounds = new Rectangle(
  37. (int) (Position.X - HalfSize.X),
  38. (int) (Position.Y - HalfSize.Y),
  39. (int) HalfSize.X * 2,
  40. (int) HalfSize.Y * 2);
  41. }
  42. }
  43. public readonly Rectangle Bounds;
  44. public readonly ShmupPlayer Player;
  45. public readonly ProfilingList<Shot> Shots;
  46. public ShmupWorld() {
  47. Bounds = new Rectangle(0, 0, 1920 / 4, 1080 / 4);
  48. Player = new ShmupPlayer();
  49. Shots = new ProfilingList<Shot>(100, "shots");
  50. }
  51. ~ShmupWorld() {
  52. Dispose();
  53. }
  54. public void Dispose() {
  55. GC.SuppressFinalize(this);
  56. }
  57. public void Update(float modelTime, History<Input> input) {
  58. Vector2 motion = Vector2.Multiply(input[0].Motion, modelTime * Player.Speed);
  59. Player.Position = Vector2.Add(Player.Position, motion);
  60. Player.Position.X = Math.Max(Player.Position.X, Player.HalfSize.X);
  61. Player.Position.X = Math.Min(Player.Position.X, Bounds.Size.X - Player.HalfSize.X);
  62. Player.Position.Y = Math.Max(Player.Position.Y, Player.HalfSize.Y);
  63. Player.Position.Y = Math.Min(Player.Position.Y, Bounds.Size.Y - Player.HalfSize.Y);
  64. foreach (Shot shot in Shots) {
  65. shot.Update(modelTime);
  66. }
  67. if (input[0].Attack && !input[1].Attack) {
  68. Vector2 shotOffset = new Vector2(12, 2);
  69. Vector2 shotPosition = Vector2.Add(Player.Position, shotOffset);
  70. Shots.Add(new Shot(shotPosition, new Vector2(300, 40)));
  71. Shots.Add(new Shot(shotPosition, new Vector2(300, 20)));
  72. Shots.Add(new Shot(shotPosition, new Vector2(300, 0)));
  73. Shots.Add(new Shot(shotPosition, new Vector2(300, -20)));
  74. Shots.Add(new Shot(shotPosition, new Vector2(300, -40)));
  75. new Shot(shotPosition, Vector2.Zero);
  76. }
  77. Shots.RemoveAll(shot => !Bounds.Intersects(shot.Bounds));
  78. }
  79. }
  80. }