224 lines
7.5 KiB
C#
224 lines
7.5 KiB
C#
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 {
|
|
const int TARGET_FPS = 60;
|
|
const double TARGET_FRAME_TIME = 1.0 / TARGET_FPS;
|
|
|
|
readonly GraphicsDeviceManager graphics;
|
|
RenderTarget2D sceneTarget;
|
|
RenderTarget2D lightingTarget;
|
|
|
|
BasicEffect lightingEffect;
|
|
|
|
SpriteBatch spriteBatch;
|
|
SpriteFont font;
|
|
bool fullScreen = false;
|
|
bool paused = false;
|
|
IDisplay display;
|
|
|
|
readonly History<Input> input = new History<Input>(2);
|
|
|
|
readonly FpsCounter fpsCounter = new FpsCounter();
|
|
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;
|
|
Texture2D grasslandBg1;
|
|
Texture2D grasslandBg2;
|
|
|
|
Player player;
|
|
World world;
|
|
LinesOfSight linesOfSight;
|
|
Camera camera = new Camera();
|
|
|
|
public SneakGame() {
|
|
graphics = new GraphicsDeviceManager(this) {
|
|
SynchronizeWithVerticalRetrace = true,
|
|
GraphicsProfile = GraphicsProfile.HiDef
|
|
};
|
|
IsFixedTimeStep = true;
|
|
TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
|
|
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);
|
|
|
|
sceneTarget = new RenderTarget2D(
|
|
GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
|
|
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
lightingTarget = new RenderTarget2D(
|
|
GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
|
|
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
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;
|
|
|
|
base.Initialize();
|
|
}
|
|
|
|
// Called once per game. Loads all game content.
|
|
protected override void LoadContent() {
|
|
base.LoadContent();
|
|
spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
font = Content.Load<SpriteFont>("font");
|
|
linesOfSight = new LinesOfSight(GraphicsDevice);
|
|
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
|
|
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
|
|
LoadLevel();
|
|
}
|
|
|
|
private void LoadLevel() {
|
|
camera = new Camera();
|
|
player = new Player(Content.Load<Texture2D>("Ninja_Female"));
|
|
world = new World(Content.Load<Texture2D>("grassland"), Levels.ONE_ONE);
|
|
}
|
|
|
|
// Called once per game. Unloads all game content.
|
|
protected override void UnloadContent() {
|
|
base.UnloadContent();
|
|
updateTimer.DumpStats();
|
|
drawTimer.DumpStats();
|
|
}
|
|
|
|
// Updates the game world.
|
|
protected override void Update(GameTime gameTime) {
|
|
updateTimer.Start();
|
|
input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
|
|
|
|
if (input[0].Exit) {
|
|
Exit();
|
|
}
|
|
|
|
if (input[0].Pause && !input[1].Pause) {
|
|
paused = !paused;
|
|
}
|
|
|
|
if (input[0].FullScreen && !input[1].FullScreen) {
|
|
fullScreen = !fullScreen;
|
|
display.SetFullScreen(fullScreen);
|
|
}
|
|
|
|
if (input[0].Restart && !input[1].Restart) {
|
|
LoadLevel();
|
|
}
|
|
|
|
Debug.Clear(paused);
|
|
if (input[0].Debug && !input[1].Debug) {
|
|
Debug.Enabled = !Debug.Enabled;
|
|
}
|
|
|
|
if (!paused) {
|
|
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
|
|
Clock.AddModelTime(modelTime);
|
|
player.Update(modelTime, input, world.CollisionTargets);
|
|
linesOfSight.Update(player, world.CollisionTargets);
|
|
camera.Update(player.Position, world.Width);
|
|
}
|
|
|
|
base.Update(gameTime);
|
|
updateTimer.Stop();
|
|
}
|
|
|
|
// Called when the game should draw itself.
|
|
protected override void Draw(GameTime gameTime) {
|
|
drawTimer.Start();
|
|
|
|
if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
|
|
framesToSuppress--;
|
|
}
|
|
|
|
// We need to update the FPS counter in Draw() since Update() might get called more
|
|
// frequently, especially when gameTime.IsRunningSlowly.
|
|
fpsCounter.Update();
|
|
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
|
|
$"{fpsCounter.Fps} FPS";
|
|
if (paused) {
|
|
fpsText += " (paused)";
|
|
}
|
|
|
|
Debug.SetFpsText(fpsText);
|
|
|
|
// Draw scene to sceneTarget.
|
|
GraphicsDevice.SetRenderTarget(sceneTarget);
|
|
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);
|
|
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);
|
|
|
|
// Draw foreground tiles.
|
|
world.Draw(spriteBatch);
|
|
|
|
// Aaaaand we're done.
|
|
spriteBatch.End();
|
|
|
|
// Draw lighting to lightingTarget.
|
|
GraphicsDevice.SetRenderTarget(lightingTarget);
|
|
GraphicsDevice.Clear(new Color(0, 0, 0, 0f));
|
|
lightingEffect.Projection = camera.Projection;
|
|
linesOfSight.Draw(player, world.CollisionTargets, GraphicsDevice, lightingEffect);
|
|
|
|
// Draw debug rects & lines on top.
|
|
Debug.Draw(GraphicsDevice, lightingEffect);
|
|
|
|
// Draw sceneTarget to screen.
|
|
GraphicsDevice.SetRenderTarget(null);
|
|
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);
|
|
spriteBatch.Draw(sceneTarget, drawRect, Color.White);
|
|
spriteBatch.Draw(lightingTarget, drawRect, Color.White);
|
|
|
|
// Draw debug toasts.
|
|
Debug.DrawToasts(spriteBatch, font);
|
|
|
|
spriteBatch.End();
|
|
}
|
|
|
|
base.Draw(gameTime);
|
|
drawTimer.Stop();
|
|
}
|
|
}
|
|
}
|