2020-02-18 02:08:01 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
2020-03-02 21:46:13 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Audio;
|
2020-02-18 02:08:01 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-02-19 21:30:34 +00:00
|
|
|
|
using System;
|
2020-02-18 02:08:01 +00:00
|
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-07-15 19:17:19 +00:00
|
|
|
|
public sealed class SneakScene : IScene {
|
2020-02-26 01:23:42 +00:00
|
|
|
|
const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
|
|
|
|
|
|
2020-07-15 18:53:03 +00:00
|
|
|
|
private readonly Color backgroundColor = Color.CornflowerBlue;
|
2020-02-18 02:08:01 +00:00
|
|
|
|
|
2020-07-15 18:53:03 +00:00
|
|
|
|
private readonly GraphicsDevice graphics;
|
|
|
|
|
private readonly Camera camera;
|
2020-02-18 02:08:01 +00:00
|
|
|
|
|
2020-07-15 18:53:03 +00:00
|
|
|
|
private readonly RenderTarget2D sceneTarget;
|
|
|
|
|
private readonly BasicEffect basicEffect;
|
|
|
|
|
private readonly SpriteBatch spriteBatch;
|
2020-11-18 16:26:21 +00:00
|
|
|
|
private readonly MusicPlayer musicPlayer;
|
2020-03-02 21:46:13 +00:00
|
|
|
|
|
|
|
|
|
// 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.
|
2020-07-15 18:53:03 +00:00
|
|
|
|
private int framesToSuppress = 2;
|
2020-02-18 02:08:01 +00:00
|
|
|
|
|
2020-07-15 19:17:19 +00:00
|
|
|
|
public SneakScene(GraphicsDevice graphics, Camera camera) {
|
2020-02-18 02:08:01 +00:00
|
|
|
|
this.graphics = graphics;
|
|
|
|
|
this.camera = camera;
|
|
|
|
|
|
|
|
|
|
sceneTarget = new RenderTarget2D(
|
|
|
|
|
graphics, camera.Width, camera.Height, false /* mipmap */,
|
|
|
|
|
graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
|
|
2020-02-19 22:33:09 +00:00
|
|
|
|
basicEffect = new BasicEffect(graphics) {
|
2020-02-19 16:24:44 +00:00
|
|
|
|
World = Matrix.CreateTranslation(0, 0, 0),
|
|
|
|
|
View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
|
|
|
|
|
VertexColorEnabled = true
|
|
|
|
|
};
|
2020-02-18 02:08:01 +00:00
|
|
|
|
|
|
|
|
|
spriteBatch = new SpriteBatch(graphics);
|
2020-11-18 16:26:21 +00:00
|
|
|
|
musicPlayer = new MusicPlayer();
|
|
|
|
|
musicPlayer.Load(SoundEffects.IntroMusic);
|
2020-02-18 02:08:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 19:17:19 +00:00
|
|
|
|
~SneakScene() {
|
2020-02-19 21:30:34 +00:00
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
2020-11-18 16:26:21 +00:00
|
|
|
|
musicPlayer.Dispose();
|
2020-02-19 21:30:34 +00:00
|
|
|
|
sceneTarget.Dispose();
|
2020-02-19 22:33:09 +00:00
|
|
|
|
basicEffect.Dispose();
|
2020-02-19 21:30:34 +00:00
|
|
|
|
spriteBatch.Dispose();
|
2020-02-19 22:18:00 +00:00
|
|
|
|
GC.SuppressFinalize(this);
|
2020-02-19 21:30:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 15:33:59 +00:00
|
|
|
|
public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
|
2020-07-15 19:17:19 +00:00
|
|
|
|
SneakWorld world = (SneakWorld) iworld;
|
2020-02-18 02:08:01 +00:00
|
|
|
|
graphics.SetRenderTarget(null);
|
|
|
|
|
graphics.Clear(backgroundColor);
|
2020-03-02 21:46:13 +00:00
|
|
|
|
|
|
|
|
|
// Enable the scene after we've gotten enough non-slow frames.
|
|
|
|
|
if (framesToSuppress > 0 && !isRunningSlowly) {
|
|
|
|
|
framesToSuppress--;
|
2020-02-18 02:08:01 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 20:00:29 +00:00
|
|
|
|
// Draw scene to sceneTarget.
|
2020-02-18 02:08:01 +00:00
|
|
|
|
graphics.SetRenderTarget(sceneTarget);
|
|
|
|
|
graphics.Clear(backgroundColor);
|
|
|
|
|
|
2020-02-19 22:33:09 +00:00
|
|
|
|
// Draw parallax backgrounds.
|
2020-02-19 20:00:29 +00:00
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
|
2020-02-18 02:08:01 +00:00
|
|
|
|
Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
|
2020-02-19 18:07:11 +00:00
|
|
|
|
|
2020-03-25 16:38:56 +00:00
|
|
|
|
float xScale = 1f / 16; // Changes with each layer (the layers further back scroll less).
|
|
|
|
|
float yScale = 1f / 4; // Constant across all layers.
|
2020-02-19 20:00:29 +00:00
|
|
|
|
for (int i = 0; i < Textures.Backgrounds.Length; i++) {
|
2020-03-25 16:38:56 +00:00
|
|
|
|
Texture2D background = Textures.Backgrounds[i].Get;
|
|
|
|
|
float yDiff = (world.Height - camera.Bottom) * yScale;
|
|
|
|
|
float yOffset = background.Height - camera.Height - yDiff;
|
2020-02-19 20:00:29 +00:00
|
|
|
|
Rectangle bgSource = new Rectangle(
|
2020-03-25 16:38:56 +00:00
|
|
|
|
(int) (camera.Left * xScale), (int) yOffset, camera.Width, camera.Height);
|
|
|
|
|
spriteBatch.Draw(background, bgTarget, bgSource, Color.White);
|
|
|
|
|
xScale *= 2;
|
2020-02-19 20:00:29 +00:00
|
|
|
|
}
|
2020-02-18 02:08:01 +00:00
|
|
|
|
spriteBatch.End();
|
|
|
|
|
|
2020-02-19 22:33:09 +00:00
|
|
|
|
// Draw lines of sight.
|
|
|
|
|
basicEffect.Projection = camera.Projection;
|
2020-03-09 18:57:39 +00:00
|
|
|
|
world.LinesOfSight.Draw(graphics, basicEffect);
|
2020-02-19 22:33:09 +00:00
|
|
|
|
|
2020-02-18 02:08:01 +00:00
|
|
|
|
// Set up transformation matrix for drawing world objects.
|
|
|
|
|
Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
|
2020-02-19 22:33:09 +00:00
|
|
|
|
spriteBatch.Begin(
|
|
|
|
|
SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
|
2020-02-20 17:25:46 +00:00
|
|
|
|
|
|
|
|
|
// Draw background tiles.
|
|
|
|
|
world.DrawBackground(spriteBatch);
|
|
|
|
|
|
|
|
|
|
// Draw player.
|
2020-03-03 18:04:29 +00:00
|
|
|
|
world.Player.Draw(spriteBatch);
|
2020-02-18 02:20:52 +00:00
|
|
|
|
|
|
|
|
|
// Draw foreground tiles.
|
2020-02-20 17:25:46 +00:00
|
|
|
|
world.DrawForeground(spriteBatch);
|
2020-02-18 02:08:01 +00:00
|
|
|
|
spriteBatch.End();
|
|
|
|
|
|
|
|
|
|
// Draw debug rects & lines on top.
|
2020-02-19 22:33:09 +00:00
|
|
|
|
Debug.Draw(graphics, basicEffect);
|
2020-02-18 02:08:01 +00:00
|
|
|
|
|
2020-02-26 22:43:31 +00:00
|
|
|
|
// Draw in-world UI on top of everything.
|
2020-02-27 21:26:22 +00:00
|
|
|
|
spriteBatch.Begin(
|
|
|
|
|
SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null);
|
2020-03-03 18:04:29 +00:00
|
|
|
|
for (int i = 0; i < world.Player.MaxHealth; i++) {
|
2020-02-27 21:50:01 +00:00
|
|
|
|
Vector2 pos = new Vector2(16 + 15 * i, 8);
|
2020-03-03 18:04:29 +00:00
|
|
|
|
if (world.Player.Health > i) {
|
2020-02-27 21:26:22 +00:00
|
|
|
|
spriteBatch.Draw(Textures.Heart.Get, pos, new Rectangle(0, 0, 16, 16), Color.White);
|
|
|
|
|
} else {
|
|
|
|
|
spriteBatch.Draw(Textures.Heart.Get, pos, new Rectangle(16, 0, 16, 16), Color.White);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-27 16:54:21 +00:00
|
|
|
|
|
2020-02-27 21:26:22 +00:00
|
|
|
|
if (paused) {
|
2020-03-09 16:46:19 +00:00
|
|
|
|
string text = Strings.Paused;
|
2020-02-27 20:46:16 +00:00
|
|
|
|
Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize);
|
|
|
|
|
Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White);
|
2020-11-18 16:26:21 +00:00
|
|
|
|
musicPlayer.Pause();
|
2020-03-02 21:46:13 +00:00
|
|
|
|
} else {
|
2020-11-18 16:26:21 +00:00
|
|
|
|
musicPlayer.Play();
|
2020-02-26 22:43:31 +00:00
|
|
|
|
}
|
2020-02-27 21:26:22 +00:00
|
|
|
|
spriteBatch.End();
|
2020-02-26 22:43:31 +00:00
|
|
|
|
|
2020-02-26 01:23:42 +00:00
|
|
|
|
// Get ready to draw sceneTarget to screen.
|
2020-02-18 02:08:01 +00:00
|
|
|
|
graphics.SetRenderTarget(null);
|
2020-02-26 01:23:42 +00:00
|
|
|
|
|
|
|
|
|
// Letterbox the scene if needed.
|
|
|
|
|
float aspectRatio = 1.0f * graphics.Viewport.Width / graphics.Viewport.Height;
|
|
|
|
|
Rectangle drawRect;
|
|
|
|
|
if (aspectRatio > DESIRED_ASPECT_RATIO) {
|
|
|
|
|
// Need to letterbox the sides.
|
|
|
|
|
int desiredWidth = (int) (graphics.Viewport.Height * DESIRED_ASPECT_RATIO);
|
|
|
|
|
int padding = (graphics.Viewport.Width - desiredWidth) / 2;
|
|
|
|
|
drawRect = new Rectangle(padding, 0, desiredWidth, graphics.Viewport.Height);
|
|
|
|
|
} else {
|
|
|
|
|
// Need to letterbox the top / bottom.
|
|
|
|
|
int desiredHeight = (int) (graphics.Viewport.Width / DESIRED_ASPECT_RATIO);
|
|
|
|
|
int padding = (graphics.Viewport.Height - desiredHeight) / 2;
|
|
|
|
|
drawRect = new Rectangle(0, padding, graphics.Viewport.Width, desiredHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Actually draw to screen.
|
2020-02-27 21:26:22 +00:00
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
|
2020-02-18 02:08:01 +00:00
|
|
|
|
SamplerState.PointClamp, DepthStencilState.Default,
|
|
|
|
|
RasterizerState.CullNone);
|
|
|
|
|
spriteBatch.Draw(sceneTarget, drawRect, Color.White);
|
|
|
|
|
|
|
|
|
|
// Draw debug toasts.
|
2020-02-19 16:19:23 +00:00
|
|
|
|
Debug.DrawToasts(spriteBatch);
|
2020-02-18 02:08:01 +00:00
|
|
|
|
|
|
|
|
|
spriteBatch.End();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|