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.

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