sneak/Shared/ShmupWorld.cs

124 lines
4.0 KiB
C#
Raw Normal View History

2020-11-19 19:44:12 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace SemiColinGames {
public sealed class ShmupWorld : IWorld {
2020-11-19 21:36:21 +00:00
public class ShmupPlayer {
public TextureRef Texture = Textures.Yellow2;
2020-11-19 19:44:12 +00:00
// Center of player sprite.
2020-11-19 22:26:50 +00:00
public Vector2 Position = new Vector2(48, 1080 / 8);
2020-11-30 21:23:35 +00:00
// TODO: use a bounds rect instead of HalfSize.
2020-11-19 21:36:21 +00:00
public Vector2 HalfSize = new Vector2(16, 10);
private float speed = 150f;
private float shotCooldown = 0f;
private ProfilingList<Shot> shots = new ProfilingList<Shot>(10, "playerShots");
public ProfilingList<Shot> Update(
float modelTime, History<Input> input, Rectangle worldBounds) {
// Movement update.
Vector2 motion = Vector2.Multiply(input[0].Motion, modelTime * speed);
Position = Vector2.Add(Position, motion);
Position.X = Math.Max(Position.X, HalfSize.X);
Position.X = Math.Min(Position.X, worldBounds.Size.X - HalfSize.X);
Position.Y = Math.Max(Position.Y, HalfSize.Y);
Position.Y = Math.Min(Position.Y, worldBounds.Size.Y - HalfSize.Y);
// Check whether we need to add new shots.
shotCooldown -= modelTime;
shots.Clear();
if (input[0].Attack && shotCooldown <= 0) {
shotCooldown = 0.2f;
Vector2 shotOffset = new Vector2(12, 2);
Vector2 shotPosition = Vector2.Add(Position, shotOffset);
shots.Add(new Shot(shotPosition, new Vector2(300, 0)));
}
return shots;
}
2020-11-30 21:23:35 +00:00
public void Draw(SpriteBatch spriteBatch) {
Texture2D texture = Texture.Get;
Vector2 spriteCenter = new Vector2(texture.Width / 2, texture.Height / 2);
Vector2 drawPos = Vector2.Floor(Vector2.Subtract(Position, spriteCenter));
spriteBatch.Draw(texture, drawPos, Color.White);
}
2020-11-19 22:26:50 +00:00
}
public class Shot {
2020-11-19 23:52:56 +00:00
static int color = 0;
public TextureRef Texture;
2020-11-19 22:26:50 +00:00
public Vector2 Position;
public Vector2 HalfSize = new Vector2(11, 4);
public Rectangle Bounds;
2020-11-19 22:26:50 +00:00
public Vector2 Velocity;
public Shot(Vector2 position, Vector2 velocity) {
2020-11-19 23:52:56 +00:00
Texture = (color % 5) switch {
0 => Textures.Projectile1,
1 => Textures.Projectile2,
2 => Textures.Projectile3,
3 => Textures.Projectile4,
_ => Textures.Projectile5
};
color++;
2020-11-19 22:26:50 +00:00
Position = position;
Velocity = velocity;
Update(0); // set Bounds
}
public void Update(float modelTime) {
Position = Vector2.Add(Position, Vector2.Multiply(Velocity, modelTime));
Bounds = new Rectangle(
(int) (Position.X - HalfSize.X),
(int) (Position.Y - HalfSize.Y),
(int) HalfSize.X * 2,
(int) HalfSize.Y * 2);
2020-11-19 22:26:50 +00:00
}
2020-11-30 21:23:35 +00:00
public void Draw(SpriteBatch spriteBatch) {
Texture2D texture = Texture.Get;
Vector2 center = new Vector2(texture.Width / 2, texture.Height / 2);
spriteBatch.Draw(texture, Vector2.Floor(Vector2.Subtract(Position, center)), Color.White);
}
2020-11-19 19:44:12 +00:00
}
public readonly Rectangle Bounds;
2020-11-19 21:36:21 +00:00
public readonly ShmupPlayer Player;
2020-11-19 22:26:50 +00:00
public readonly ProfilingList<Shot> Shots;
2020-11-19 19:44:12 +00:00
public ShmupWorld() {
Bounds = new Rectangle(0, 0, 1920 / 4, 1080 / 4);
2020-11-19 19:44:12 +00:00
Player = new ShmupPlayer();
2020-11-19 22:26:50 +00:00
Shots = new ProfilingList<Shot>(100, "shots");
2020-11-19 19:44:12 +00:00
}
~ShmupWorld() {
Dispose();
}
public void Dispose() {
GC.SuppressFinalize(this);
}
public void Update(float modelTime, History<Input> input) {
// Update player & shots.
ProfilingList<Shot> newPlayerShots = Player.Update(modelTime, input, Bounds);
2020-11-19 22:26:50 +00:00
foreach (Shot shot in Shots) {
shot.Update(modelTime);
}
// Add new shots.
Shots.AddRange(newPlayerShots);
// Reap off-screen objects.
Rectangle paddedBounds = Bounds;
paddedBounds.Inflate(16, 16);
Shots.RemoveAll(shot => !paddedBounds.Intersects(shot.Bounds));
2020-11-20 00:38:31 +00:00
Debug.AddToast("shots: " + Shots.Count);
2020-11-19 19:44:12 +00:00
}
}
}