2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
|
|
|
public class SneakGame : Game {
|
2020-02-03 23:17:28 +00:00
|
|
|
const int TARGET_FPS = 60;
|
|
|
|
const double TARGET_FRAME_TIME = 1.0 / TARGET_FPS;
|
|
|
|
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly GraphicsDeviceManager graphics;
|
2020-02-11 22:06:17 +00:00
|
|
|
RenderTarget2D sceneTarget;
|
|
|
|
RenderTarget2D lightingTarget;
|
|
|
|
|
|
|
|
BasicEffect lightingEffect;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
SpriteBatch spriteBatch;
|
|
|
|
SpriteFont font;
|
|
|
|
bool fullScreen = false;
|
2020-01-24 22:49:29 +00:00
|
|
|
bool paused = false;
|
2020-01-18 03:41:45 +00:00
|
|
|
IDisplay display;
|
|
|
|
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly History<Input> input = new History<Input>(2);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly FpsCounter fpsCounter = new FpsCounter();
|
2020-02-03 23:17:28 +00:00
|
|
|
readonly Timer updateTimer = new Timer(TARGET_FRAME_TIME / 2.0, "UpdateTimer");
|
|
|
|
readonly Timer drawTimer = new Timer(TARGET_FRAME_TIME / 2.0, "DrawTimer");
|
|
|
|
// Draw() needs to be called without IsRunningSlowly this many times before we actually
|
|
|
|
// attempt to draw the scene. This is a workaround for the fact that otherwise the first few
|
|
|
|
// frames can be really slow to draw.
|
|
|
|
int framesToSuppress = 2;
|
2020-01-18 03:41:45 +00:00
|
|
|
Texture2D grasslandBg1;
|
|
|
|
Texture2D grasslandBg2;
|
|
|
|
|
|
|
|
Player player;
|
|
|
|
World world;
|
2020-01-25 02:04:30 +00:00
|
|
|
readonly Camera camera = new Camera();
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
public SneakGame() {
|
2020-01-25 02:09:09 +00:00
|
|
|
graphics = new GraphicsDeviceManager(this) {
|
2020-01-27 03:50:14 +00:00
|
|
|
SynchronizeWithVerticalRetrace = true,
|
|
|
|
GraphicsProfile = GraphicsProfile.HiDef
|
2020-01-25 02:09:09 +00:00
|
|
|
};
|
2020-01-25 00:57:07 +00:00
|
|
|
IsFixedTimeStep = true;
|
2020-02-03 23:17:28 +00:00
|
|
|
TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
|
2020-01-18 03:41:45 +00:00
|
|
|
IsMouseVisible = true;
|
|
|
|
Content.RootDirectory = "Content";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Performs initialization that's needed before starting to run.
|
|
|
|
protected override void Initialize() {
|
|
|
|
display = (IDisplay) Services.GetService(typeof(IDisplay));
|
|
|
|
display.Initialize(Window, graphics);
|
|
|
|
display.SetFullScreen(fullScreen);
|
|
|
|
|
|
|
|
Debug.Initialize(GraphicsDevice);
|
|
|
|
|
2020-02-11 22:06:17 +00:00
|
|
|
sceneTarget = new RenderTarget2D(
|
|
|
|
GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
|
|
|
|
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
lightingTarget = new RenderTarget2D(
|
2020-01-18 03:41:45 +00:00
|
|
|
GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
|
|
|
|
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
|
2020-02-11 22:06:17 +00:00
|
|
|
lightingEffect = new BasicEffect(GraphicsDevice);
|
|
|
|
lightingEffect.World = Matrix.CreateTranslation(0, 0, 0);
|
|
|
|
lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
|
|
|
|
lightingEffect.VertexColorEnabled = true;
|
|
|
|
|
|
|
|
RasterizerState rasterizerState = new RasterizerState() {
|
|
|
|
CullMode = CullMode.None
|
|
|
|
};
|
|
|
|
GraphicsDevice.RasterizerState = rasterizerState;
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
base.Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once per game. Loads all game content.
|
|
|
|
protected override void LoadContent() {
|
|
|
|
spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
|
|
font = Content.Load<SpriteFont>("font");
|
|
|
|
|
2020-01-30 21:42:31 +00:00
|
|
|
player = new Player(Content.Load<Texture2D>("Ninja_Female"));
|
2020-02-11 22:06:17 +00:00
|
|
|
world = new World(Content.Load<Texture2D>("grassland"), Levels.ONE_ONE);
|
2020-01-18 03:41:45 +00:00
|
|
|
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
|
|
|
|
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once per game. Unloads all game content.
|
|
|
|
protected override void UnloadContent() {
|
2020-02-04 14:55:07 +00:00
|
|
|
updateTimer.DumpStats();
|
|
|
|
drawTimer.DumpStats();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the game world.
|
|
|
|
protected override void Update(GameTime gameTime) {
|
2020-02-03 23:17:28 +00:00
|
|
|
updateTimer.Start();
|
2020-01-18 03:41:45 +00:00
|
|
|
input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
|
|
|
|
|
|
|
|
if (input[0].Exit) {
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:49:29 +00:00
|
|
|
if (input[0].Pause && !input[1].Pause) {
|
|
|
|
paused = !paused;
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
if (input[0].FullScreen && !input[1].FullScreen) {
|
|
|
|
fullScreen = !fullScreen;
|
|
|
|
display.SetFullScreen(fullScreen);
|
|
|
|
}
|
|
|
|
|
2020-01-29 21:33:55 +00:00
|
|
|
Debug.Clear(paused);
|
2020-01-18 03:41:45 +00:00
|
|
|
if (input[0].Debug && !input[1].Debug) {
|
|
|
|
Debug.Enabled = !Debug.Enabled;
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:49:29 +00:00
|
|
|
if (!paused) {
|
2020-01-24 23:36:55 +00:00
|
|
|
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
|
2020-01-25 01:42:27 +00:00
|
|
|
Clock.AddModelTime(modelTime);
|
2020-01-29 21:11:28 +00:00
|
|
|
player.Update(modelTime, input, world.CollisionTargets);
|
2020-01-29 23:01:41 +00:00
|
|
|
camera.Update(player.Position, world.Width);
|
2020-01-24 22:49:29 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
base.Update(gameTime);
|
2020-02-03 23:17:28 +00:00
|
|
|
updateTimer.Stop();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the game should draw itself.
|
|
|
|
protected override void Draw(GameTime gameTime) {
|
2020-02-03 23:17:28 +00:00
|
|
|
drawTimer.Start();
|
|
|
|
|
|
|
|
if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
|
|
|
|
framesToSuppress--;
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
// We need to update the FPS counter in Draw() since Update() might get called more
|
|
|
|
// frequently, especially when gameTime.IsRunningSlowly.
|
|
|
|
fpsCounter.Update();
|
2020-01-23 17:19:16 +00:00
|
|
|
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
|
|
|
|
$"{fpsCounter.Fps} FPS";
|
2020-01-24 22:49:29 +00:00
|
|
|
if (paused) {
|
2020-01-25 02:09:09 +00:00
|
|
|
fpsText += " (paused)";
|
2020-01-24 22:49:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 17:19:16 +00:00
|
|
|
Debug.SetFpsText(fpsText);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-02-11 22:06:17 +00:00
|
|
|
// Draw scene to sceneTarget.
|
|
|
|
GraphicsDevice.SetRenderTarget(sceneTarget);
|
2020-01-18 03:41:45 +00:00
|
|
|
GraphicsDevice.Clear(Color.CornflowerBlue);
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
|
|
|
|
|
|
|
|
// Draw background.
|
|
|
|
Rectangle bgSource = new Rectangle(
|
|
|
|
(int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
|
|
|
|
Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
|
|
|
|
spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
|
|
|
|
bgSource = new Rectangle(
|
|
|
|
(int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
|
|
|
|
spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
|
2020-02-04 22:37:42 +00:00
|
|
|
spriteBatch.End();
|
|
|
|
|
|
|
|
// Set up transformation matrix for drawing world objects.
|
|
|
|
Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
|
2020-02-11 22:06:17 +00:00
|
|
|
spriteBatch.Begin(
|
|
|
|
SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
// Draw player.
|
2020-02-04 22:37:42 +00:00
|
|
|
player.Draw(spriteBatch);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
// Draw foreground tiles.
|
2020-02-04 22:37:42 +00:00
|
|
|
world.Draw(spriteBatch);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-01-23 16:40:32 +00:00
|
|
|
// Draw debug rects & lines.
|
2020-02-04 22:37:42 +00:00
|
|
|
Debug.Draw(spriteBatch);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
// Aaaaand we're done.
|
|
|
|
spriteBatch.End();
|
|
|
|
|
2020-02-11 22:06:17 +00:00
|
|
|
// Draw lighting to lightingTarget.
|
|
|
|
GraphicsDevice.SetRenderTarget(lightingTarget);
|
|
|
|
GraphicsDevice.Clear(new Color(0, 0, 0, 0f));
|
|
|
|
lightingEffect.Projection = camera.Projection;
|
|
|
|
DrawFov();
|
|
|
|
|
|
|
|
// Draw sceneTarget to screen.
|
2020-01-18 03:41:45 +00:00
|
|
|
GraphicsDevice.SetRenderTarget(null);
|
2020-02-03 23:17:28 +00:00
|
|
|
GraphicsDevice.Clear(Color.CornflowerBlue);
|
|
|
|
if (framesToSuppress == 0) {
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
|
|
|
|
SamplerState.PointClamp, DepthStencilState.Default,
|
|
|
|
RasterizerState.CullNone);
|
|
|
|
Rectangle drawRect = new Rectangle(
|
|
|
|
0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
|
2020-02-11 22:06:17 +00:00
|
|
|
spriteBatch.Draw(sceneTarget, drawRect, Color.White);
|
|
|
|
spriteBatch.Draw(lightingTarget, drawRect, Color.White);
|
2020-02-03 23:17:28 +00:00
|
|
|
|
|
|
|
// Draw debug toasts.
|
|
|
|
Debug.DrawToasts(spriteBatch, font);
|
|
|
|
|
|
|
|
spriteBatch.End();
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
base.Draw(gameTime);
|
2020-02-03 23:17:28 +00:00
|
|
|
drawTimer.Stop();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-02-11 22:06:17 +00:00
|
|
|
|
|
|
|
private void DrawFov() {
|
|
|
|
// TODO: DrawIndexedPrimitives
|
|
|
|
Color color = Color.FromNonPremultiplied(new Vector4(0, 0, 1, 0.6f));
|
|
|
|
Vector2 eyePos = player.EyePosition;
|
|
|
|
int numConePoints = 60;
|
|
|
|
// TODO: don't new[] every frame.
|
|
|
|
VertexPositionColor[] conePoints = new VertexPositionColor[numConePoints];
|
|
|
|
float visionRange = 150;
|
|
|
|
float visionRangeSq = visionRange * visionRange;
|
|
|
|
float fov = FMath.DegToRad(120);
|
|
|
|
float fovStep = fov / (numConePoints - 1);
|
|
|
|
|
|
|
|
Vector2 ray = new Vector2(visionRange * player.GetFacing, 0);
|
|
|
|
if (player.GetPose == Player.Pose.Stretching) {
|
|
|
|
ray = ray.Rotate(player.GetFacing * FMath.DegToRad(-30));
|
|
|
|
}
|
|
|
|
if (player.GetPose == Player.Pose.Crouching) {
|
|
|
|
ray = ray.Rotate(player.GetFacing * FMath.DegToRad(30));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < conePoints.Length; i++) {
|
|
|
|
float angle = -fov / 2 + fovStep * i;
|
|
|
|
Vector2 rotated = ray.Rotate(angle);
|
|
|
|
Vector2 closestHit = Vector2.Add(eyePos, rotated);
|
|
|
|
float hitTime = 1f;
|
|
|
|
|
|
|
|
Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);
|
|
|
|
for (int j = 0; j < world.CollisionTargets.Length; j++) {
|
|
|
|
AABB box = world.CollisionTargets[j];
|
|
|
|
if (Math.Abs(box.Position.X - player.Position.X) > visionRange + halfTileSize.X) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Vector2 delta = Vector2.Add(halfTileSize, Vector2.Subtract(box.Position, eyePos));
|
|
|
|
if (delta.LengthSquared() > visionRangeSq) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Hit? maybeHit = box.IntersectSegment(eyePos, rotated);
|
|
|
|
if (maybeHit != null) {
|
|
|
|
Hit hit = maybeHit.Value;
|
|
|
|
if (hit.Time < hitTime) {
|
|
|
|
hitTime = hit.Time;
|
|
|
|
closestHit = hit.Position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float tint = 0.6f - hitTime / 2;
|
|
|
|
Color tinted = Color.FromNonPremultiplied(new Vector4(0, 0, 1, tint));
|
|
|
|
conePoints[i] = new VertexPositionColor(new Vector3(closestHit, 0), tinted);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: don't new[] every frame.
|
|
|
|
VertexPositionColor[] vertices = new VertexPositionColor[numConePoints * 3];
|
|
|
|
VertexPositionColor eyeVertex = new VertexPositionColor(new Vector3(eyePos, 0), color);
|
|
|
|
for (int i = 0; i < numConePoints - 1; i++) {
|
|
|
|
vertices[i * 3] = eyeVertex;
|
|
|
|
vertices[i * 3 + 1] = conePoints[i];
|
|
|
|
vertices[i * 3 + 2] = conePoints[i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexBuffer vertexBuffer = new VertexBuffer(
|
|
|
|
GraphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
|
|
|
|
vertexBuffer.SetData<VertexPositionColor>(vertices);
|
|
|
|
|
|
|
|
GraphicsDevice.SetVertexBuffer(vertexBuffer);
|
|
|
|
|
|
|
|
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
|
|
|
|
pass.Apply();
|
|
|
|
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Length / 3);
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|