166 lines
5.8 KiB
C#
166 lines
5.8 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Audio;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using System;
|
|
|
|
namespace SemiColinGames {
|
|
public sealed class Scene : IScene {
|
|
const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f;
|
|
|
|
private readonly Color backgroundColor = Color.CornflowerBlue;
|
|
|
|
private readonly GraphicsDevice graphics;
|
|
private readonly Camera camera;
|
|
|
|
private readonly RenderTarget2D sceneTarget;
|
|
private readonly BasicEffect basicEffect;
|
|
private readonly SpriteBatch spriteBatch;
|
|
private readonly SoundEffectInstance music;
|
|
|
|
// 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.
|
|
private int framesToSuppress = 2;
|
|
|
|
public Scene(GraphicsDevice graphics, Camera camera) {
|
|
this.graphics = graphics;
|
|
this.camera = camera;
|
|
|
|
sceneTarget = new RenderTarget2D(
|
|
graphics, camera.Width, camera.Height, false /* mipmap */,
|
|
graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
basicEffect = new BasicEffect(graphics) {
|
|
World = Matrix.CreateTranslation(0, 0, 0),
|
|
View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up),
|
|
VertexColorEnabled = true
|
|
};
|
|
|
|
spriteBatch = new SpriteBatch(graphics);
|
|
|
|
music = SoundEffects.IntroMusic.CreateInstance();
|
|
music.IsLooped = true;
|
|
music.Volume = 0.1f;
|
|
}
|
|
|
|
~Scene() {
|
|
Dispose();
|
|
}
|
|
|
|
public void Dispose() {
|
|
music.Stop();
|
|
music.Dispose();
|
|
sceneTarget.Dispose();
|
|
basicEffect.Dispose();
|
|
spriteBatch.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) {
|
|
World world = (World) iworld;
|
|
graphics.SetRenderTarget(null);
|
|
graphics.Clear(backgroundColor);
|
|
|
|
// Enable the scene after we've gotten enough non-slow frames.
|
|
if (framesToSuppress > 0 && !isRunningSlowly) {
|
|
framesToSuppress--;
|
|
return;
|
|
}
|
|
|
|
// Draw scene to sceneTarget.
|
|
graphics.SetRenderTarget(sceneTarget);
|
|
graphics.Clear(backgroundColor);
|
|
|
|
// Draw parallax backgrounds.
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
|
|
Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
|
|
|
|
float xScale = 1f / 16; // Changes with each layer (the layers further back scroll less).
|
|
float yScale = 1f / 4; // Constant across all layers.
|
|
for (int i = 0; i < Textures.Backgrounds.Length; i++) {
|
|
Texture2D background = Textures.Backgrounds[i].Get;
|
|
float yDiff = (world.Height - camera.Bottom) * yScale;
|
|
float yOffset = background.Height - camera.Height - yDiff;
|
|
Rectangle bgSource = new Rectangle(
|
|
(int) (camera.Left * xScale), (int) yOffset, camera.Width, camera.Height);
|
|
spriteBatch.Draw(background, bgTarget, bgSource, Color.White);
|
|
xScale *= 2;
|
|
}
|
|
spriteBatch.End();
|
|
|
|
// Draw lines of sight.
|
|
basicEffect.Projection = camera.Projection;
|
|
world.LinesOfSight.Draw(graphics, basicEffect);
|
|
|
|
// Set up transformation matrix for drawing world objects.
|
|
Matrix transform = Matrix.CreateTranslation(-camera.Left, -camera.Top, 0);
|
|
spriteBatch.Begin(
|
|
SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
|
|
|
|
// Draw background tiles.
|
|
world.DrawBackground(spriteBatch);
|
|
|
|
// Draw player.
|
|
world.Player.Draw(spriteBatch);
|
|
|
|
// Draw foreground tiles.
|
|
world.DrawForeground(spriteBatch);
|
|
spriteBatch.End();
|
|
|
|
// Draw debug rects & lines on top.
|
|
Debug.Draw(graphics, basicEffect);
|
|
|
|
// Draw in-world UI on top of everything.
|
|
spriteBatch.Begin(
|
|
SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, null);
|
|
for (int i = 0; i < world.Player.MaxHealth; i++) {
|
|
Vector2 pos = new Vector2(16 + 15 * i, 8);
|
|
if (world.Player.Health > i) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
if (paused) {
|
|
string text = Strings.Paused;
|
|
Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize);
|
|
Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White);
|
|
music.Pause();
|
|
} else {
|
|
music.Play();
|
|
}
|
|
spriteBatch.End();
|
|
|
|
// Get ready to draw sceneTarget to screen.
|
|
graphics.SetRenderTarget(null);
|
|
|
|
// 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.
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,
|
|
SamplerState.PointClamp, DepthStencilState.Default,
|
|
RasterizerState.CullNone);
|
|
spriteBatch.Draw(sceneTarget, drawRect, Color.White);
|
|
|
|
// Draw debug toasts.
|
|
Debug.DrawToasts(spriteBatch);
|
|
|
|
spriteBatch.End();
|
|
}
|
|
}
|
|
}
|