sneak/Shared/ShmupWorld.cs

69 lines
2.4 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-19 21:36:21 +00:00
public Vector2 HalfSize = new Vector2(16, 10);
2020-11-19 22:26:50 +00:00
public float Speed = 150f;
}
public class Shot {
public TextureRef Texture = Textures.Projectile1;
public Vector2 Position;
public Vector2 HalfSize = new Vector2(11, 8);
public Vector2 Velocity;
public Shot(Vector2 position, Vector2 velocity) {
Position = position;
Velocity = velocity;
}
2020-11-19 19:44:12 +00:00
}
2020-11-19 21:36:21 +00:00
public readonly Point Size;
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() {
2020-11-19 21:36:21 +00:00
Size = new Point(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");
Vector2 velocity = new Vector2(100, 0);
Shots.Add(new Shot(new Vector2(96, 1080 / 8), velocity));
Shots.Add(new Shot(new Vector2(96 * 2, 1080 / 8), velocity));
Shots.Add(new Shot(new Vector2(96 * 4, 1080 / 8), velocity));
2020-11-19 19:44:12 +00:00
}
~ShmupWorld() {
Dispose();
}
public void Dispose() {
GC.SuppressFinalize(this);
}
public void Update(float modelTime, History<Input> input) {
2020-11-19 22:26:50 +00:00
Vector2 motion = Vector2.Multiply(input[0].Motion, modelTime * Player.Speed);
2020-11-19 19:44:12 +00:00
Player.Position = Vector2.Add(Player.Position, motion);
2020-11-19 21:36:21 +00:00
Player.Position.X = Math.Max(Player.Position.X, Player.HalfSize.X);
Player.Position.X = Math.Min(Player.Position.X, Size.X - Player.HalfSize.X);
Player.Position.Y = Math.Max(Player.Position.Y, Player.HalfSize.Y);
Player.Position.Y = Math.Min(Player.Position.Y, Size.Y - Player.HalfSize.Y);
2020-11-19 22:26:50 +00:00
foreach (Shot shot in Shots) {
shot.Position = Vector2.Add(shot.Position, Vector2.Multiply(shot.Velocity, modelTime));
shot.Position.X = Math.Max(shot.Position.X, shot.HalfSize.X);
shot.Position.X = Math.Min(shot.Position.X, Size.X - shot.HalfSize.X);
shot.Position.X = Math.Max(shot.Position.X, shot.HalfSize.X);
shot.Position.X = Math.Min(shot.Position.X, Size.X - shot.HalfSize.X);
}
// Shots.RemoveAll(shot => shot.Position.X > Size.X);
2020-11-19 19:44:12 +00:00
}
}
}