2020-02-18 02:08:01 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Content;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
|
|
|
|
class Scene {
|
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;
|
|
|
|
|
readonly RenderTarget2D lightingTarget;
|
|
|
|
|
readonly BasicEffect lightingEffect;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
lightingTarget = new RenderTarget2D(
|
|
|
|
|
graphics, camera.Width, camera.Height, false /* mipmap */,
|
|
|
|
|
graphics.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
|
|
2020-02-19 16:24:44 +00:00
|
|
|
|
lightingEffect = new BasicEffect(graphics) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Draw(World world, Player player, LinesOfSight linesOfSight) {
|
|
|
|
|
graphics.SetRenderTarget(null);
|
|
|
|
|
graphics.Clear(backgroundColor);
|
|
|
|
|
if (!Enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graphics.SetRenderTarget(sceneTarget);
|
|
|
|
|
graphics.Clear(backgroundColor);
|
|
|
|
|
|
|
|
|
|
// Draw scene to sceneTarget.
|
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
|
|
|
|
|
|
|
|
|
|
// Draw background.
|
2020-02-19 18:07:11 +00:00
|
|
|
|
|
|
|
|
|
// We assume that all backgrounds are the same size; if not, the computation of yOffset needs
|
|
|
|
|
// to be computed separately for every background.
|
|
|
|
|
int yOffset = Textures.Background1.Height - camera.Height - 64;
|
|
|
|
|
Color bgBlend = Color.FromNonPremultiplied(new Vector4(1, 1, 1, 1));
|
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
|
|
|
|
|
|
|
|
|
Rectangle bgSource = new Rectangle(
|
|
|
|
|
(int) (camera.Left * 1f / 16), yOffset, camera.Width, camera.Height);
|
|
|
|
|
spriteBatch.Draw(Textures.Background1, bgTarget, bgSource, bgBlend);
|
|
|
|
|
|
|
|
|
|
bgSource = new Rectangle(
|
|
|
|
|
(int) (camera.Left * 1f / 8), yOffset, camera.Width, camera.Height);
|
2020-02-19 16:19:23 +00:00
|
|
|
|
spriteBatch.Draw(Textures.Background2, bgTarget, bgSource, bgBlend);
|
2020-02-19 18:07:11 +00:00
|
|
|
|
|
2020-02-18 02:08:01 +00:00
|
|
|
|
bgSource = new Rectangle(
|
2020-02-19 18:07:11 +00:00
|
|
|
|
(int) (camera.Left * 1f / 4), yOffset, camera.Width, camera.Height);
|
|
|
|
|
spriteBatch.Draw(Textures.Background3, bgTarget, bgSource, bgBlend);
|
|
|
|
|
|
|
|
|
|
bgSource = new Rectangle(
|
|
|
|
|
(int) (camera.Left * 1f / 2), yOffset, camera.Width, camera.Height);
|
|
|
|
|
spriteBatch.Draw(Textures.Background4, bgTarget, bgSource, bgBlend);
|
|
|
|
|
|
2020-02-18 02:08:01 +00:00
|
|
|
|
spriteBatch.End();
|
|
|
|
|
|
|
|
|
|
// 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 player.
|
|
|
|
|
player.Draw(spriteBatch);
|
2020-02-18 02:20:52 +00:00
|
|
|
|
|
|
|
|
|
// Draw foreground tiles.
|
|
|
|
|
world.Draw(spriteBatch);
|
2020-02-18 02:08:01 +00:00
|
|
|
|
|
|
|
|
|
// Aaaaand we're done.
|
|
|
|
|
spriteBatch.End();
|
|
|
|
|
|
|
|
|
|
// Draw lighting to lightingTarget.
|
|
|
|
|
graphics.SetRenderTarget(lightingTarget);
|
|
|
|
|
graphics.Clear(new Color(0, 0, 0, 0f));
|
|
|
|
|
lightingEffect.Projection = camera.Projection;
|
|
|
|
|
linesOfSight.Draw(player, world.CollisionTargets, graphics, lightingEffect);
|
|
|
|
|
|
|
|
|
|
// Draw debug rects & lines on top.
|
|
|
|
|
Debug.Draw(graphics, lightingEffect);
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
spriteBatch.Draw(lightingTarget, 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|