2020-02-18 02:08:01 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
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-02-19 21:30:34 +00:00
|
|
|
|
class Scene : IDisposable {
|
2020-02-19 16:19:23 +00:00
|
|
|
|
public bool Enabled = false;
|
|
|
|
|
|
2020-02-18 02:08:01 +00:00
|
|
|
|
Color backgroundColor = Color.CornflowerBlue;
|
|
|
|
|
|
|
|
|
|
readonly GraphicsDevice graphics;
|
|
|
|
|
readonly Camera camera;
|
|
|
|
|
|
|
|
|
|
readonly RenderTarget2D sceneTarget;
|
2020-02-19 22:33:09 +00:00
|
|
|
|
readonly BasicEffect basicEffect;
|
2020-02-18 02:08:01 +00:00
|
|
|
|
readonly SpriteBatch spriteBatch;
|
|
|
|
|
|
2020-02-19 16:19:23 +00:00
|
|
|
|
public Scene(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-02-19 21:30:34 +00:00
|
|
|
|
~Scene() {
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
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-02-18 02:08:01 +00:00
|
|
|
|
public void Draw(World world, Player player, LinesOfSight linesOfSight) {
|
|
|
|
|
graphics.SetRenderTarget(null);
|
|
|
|
|
graphics.Clear(backgroundColor);
|
|
|
|
|
if (!Enabled) {
|
|
|
|
|
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-02-19 20:00:29 +00:00
|
|
|
|
float xScale = 1f / 16;
|
|
|
|
|
for (int i = 0; i < Textures.Backgrounds.Length; i++) {
|
|
|
|
|
int yOffset = Textures.Backgrounds[i].Height - camera.Height - 24;
|
|
|
|
|
Rectangle bgSource = new Rectangle(
|
|
|
|
|
(int) (camera.Left * xScale), yOffset, camera.Width, camera.Height);
|
|
|
|
|
spriteBatch.Draw(Textures.Backgrounds[i], bgTarget, bgSource, Color.White);
|
|
|
|
|
xScale *= 2;
|
|
|
|
|
}
|
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;
|
|
|
|
|
if (Debug.Enabled) {
|
|
|
|
|
linesOfSight.Draw(graphics, basicEffect);
|
|
|
|
|
}
|
|
|
|
|
|
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-02-18 02:08:01 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
// Draw sceneTarget to screen.
|
|
|
|
|
graphics.SetRenderTarget(null);
|
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
|
|
|
|
|
SamplerState.PointClamp, DepthStencilState.Default,
|
|
|
|
|
RasterizerState.CullNone);
|
|
|
|
|
Rectangle drawRect = new Rectangle(
|
|
|
|
|
0, 0, graphics.Viewport.Width, graphics.Viewport.Height);
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|