sneak/Shared/ShmupWorld.cs
2021-02-08 16:08:01 -05:00

171 lines
5.7 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace SemiColinGames {
public sealed class ShmupWorld : IWorld {
public class ShmupPlayer {
public TextureRef Texture = Textures.Yellow2;
// Center of player sprite.
public Vector2 Position = new Vector2(48, 1080 / 8);
// TODO: use a bounds rect instead of HalfSize.
public Vector2 HalfSize = new Vector2(16, 10);
private float speed = 150f;
private float shotCooldown = 0f;
public void Update(float modelTime, History<Input> input, Rectangle worldBounds,
ProfilingList<Shot> newShots) {
// 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;
if (input[0].Attack && shotCooldown <= 0) {
shotCooldown = 0.2f;
Vector2 shotOffset = new Vector2(12, 2);
Vector2 shotPosition = Vector2.Add(Position, shotOffset);
newShots.Add(new Shot(shotPosition, new Vector2(300, 0)));
}
}
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);
}
}
public class Shot {
static int color = 0;
public TextureRef Texture;
public Vector2 Position;
public Vector2 HalfSize = new Vector2(11, 4);
public Rectangle Bounds;
public Vector2 Velocity;
public Shot(Vector2 position, Vector2 velocity) {
Texture = (color % 5) switch {
0 => Textures.Projectile1,
1 => Textures.Projectile2,
2 => Textures.Projectile3,
3 => Textures.Projectile4,
_ => Textures.Projectile5
};
color++;
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);
}
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);
}
}
public interface IMoveBehavior {
public Vector2 Velocity(float modelTime);
}
public class MoveLeft : IMoveBehavior {
public Vector2 Velocity(float modelTime) {
return new Vector2(-100, 0);
}
}
public class Enemy {
public TextureRef Texture = Textures.Blue1;
// Center of sprite.
public Vector2 Position = new Vector2(1920 / 4 - 48, 1080 / 8);
// TODO: use a bounds rect instead of HalfSize.
public Vector2 HalfSize = new Vector2(16, 10);
public Rectangle Bounds;
private IMoveBehavior moveBehavior = new MoveLeft();
public void Update(float modelTime) {
Vector2 velocity = moveBehavior.Velocity(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);
}
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, null, Color.White, 0f,
spriteCenter, Vector2.One, SpriteEffects.FlipHorizontally, 0f);
}
}
public readonly Rectangle Bounds;
public readonly ShmupPlayer Player;
public readonly ProfilingList<Enemy> Enemies;
public readonly ProfilingList<Shot> Shots;
private ProfilingList<Shot> newShots;
public ShmupWorld() {
Bounds = new Rectangle(0, 0, 1920 / 4, 1080 / 4);
Player = new ShmupPlayer();
Enemies = new ProfilingList<Enemy>(100, "enemies");
Enemies.Add(new Enemy());
Shots = new ProfilingList<Shot>(100, "shots");
newShots = new ProfilingList<Shot>(100, "newShots");
}
~ShmupWorld() {
Dispose();
}
public void Dispose() {
GC.SuppressFinalize(this);
}
public void Update(float modelTime, History<Input> input) {
// Update player, enemies, & shots.
newShots.Clear();
Player.Update(modelTime, input, Bounds, newShots);
foreach (Enemy enemy in Enemies) {
enemy.Update(modelTime);
}
foreach (Shot shot in Shots) {
shot.Update(modelTime);
}
// Add new shots.
Shots.AddRange(newShots);
// Reap off-screen objects.
Rectangle paddedBounds = Bounds;
paddedBounds.Inflate(16, 16);
Shots.RemoveAll(shot => !paddedBounds.Intersects(shot.Bounds));
Enemies.RemoveAll(enemy => !paddedBounds.Intersects(enemy.Bounds));
Debug.AddToast("shots: " + Shots.Count + " enemies: " + Enemies.Count);
}
}
}