2020-01-18 03:41:45 +00:00
|
|
|
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 {
|
|
|
|
GraphicsDeviceManager graphics;
|
|
|
|
RenderTarget2D renderTarget;
|
|
|
|
|
|
|
|
SpriteBatch spriteBatch;
|
|
|
|
SpriteFont font;
|
|
|
|
bool fullScreen = false;
|
2020-01-24 22:49:29 +00:00
|
|
|
bool paused = false;
|
2020-01-18 03:41:45 +00:00
|
|
|
IDisplay display;
|
|
|
|
|
|
|
|
History<Input> input = new History<Input>(2);
|
|
|
|
|
|
|
|
FpsCounter fpsCounter = new FpsCounter();
|
|
|
|
Texture2D grasslandBg1;
|
|
|
|
Texture2D grasslandBg2;
|
|
|
|
|
|
|
|
Player player;
|
|
|
|
World world;
|
|
|
|
Camera camera = new Camera();
|
|
|
|
|
|
|
|
public SneakGame() {
|
|
|
|
graphics = new GraphicsDeviceManager(this);
|
2020-01-25 00:57:07 +00:00
|
|
|
graphics.SynchronizeWithVerticalRetrace = false;
|
|
|
|
IsFixedTimeStep = true;
|
|
|
|
TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 120);
|
2020-01-18 03:41:45 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
renderTarget = new RenderTarget2D(
|
|
|
|
GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
|
|
|
|
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
|
|
|
|
base.Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once per game. Loads all game content.
|
|
|
|
protected override void LoadContent() {
|
|
|
|
spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
|
|
font = Content.Load<SpriteFont>("font");
|
|
|
|
|
|
|
|
player = new Player(Content.Load<Texture2D>("player_1x"));
|
|
|
|
world = new World(Content.Load<Texture2D>("grassland"));
|
|
|
|
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
|
|
|
|
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once per game. Unloads all game content.
|
|
|
|
protected override void UnloadContent() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the game world.
|
|
|
|
protected override void Update(GameTime gameTime) {
|
|
|
|
Debug.Clear();
|
|
|
|
|
|
|
|
input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
|
|
|
|
|
|
|
|
if (input[0].Exit) {
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:49:29 +00:00
|
|
|
if (input[0].Pause && !input[1].Pause) {
|
|
|
|
paused = !paused;
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
if (input[0].FullScreen && !input[1].FullScreen) {
|
|
|
|
fullScreen = !fullScreen;
|
|
|
|
display.SetFullScreen(fullScreen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input[0].Debug && !input[1].Debug) {
|
|
|
|
Debug.Enabled = !Debug.Enabled;
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:49:29 +00:00
|
|
|
if (!paused) {
|
2020-01-24 23:36:55 +00:00
|
|
|
float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
|
2020-01-24 22:49:29 +00:00
|
|
|
List<Rectangle> collisionTargets = world.CollisionTargets();
|
2020-01-24 23:36:55 +00:00
|
|
|
player.Update(modelTime, input, collisionTargets);
|
2020-01-24 22:49:29 +00:00
|
|
|
camera.Update(player.Position);
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
base.Update(gameTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the game should draw itself.
|
|
|
|
protected override void Draw(GameTime gameTime) {
|
|
|
|
// We need to update the FPS counter in Draw() since Update() might get called more
|
|
|
|
// frequently, especially when gameTime.IsRunningSlowly.
|
|
|
|
fpsCounter.Update();
|
2020-01-23 17:19:16 +00:00
|
|
|
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
|
|
|
|
$"{fpsCounter.Fps} FPS";
|
2020-01-24 22:49:29 +00:00
|
|
|
if (paused) {
|
|
|
|
fpsText = fpsText + " (paused)";
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:19:16 +00:00
|
|
|
Debug.SetFpsText(fpsText);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
// Draw scene to RenderTarget.
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Draw player.
|
2020-01-24 23:36:55 +00:00
|
|
|
player.Draw(spriteBatch, camera);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
// Draw foreground tiles.
|
|
|
|
world.Draw(spriteBatch, camera);
|
|
|
|
|
2020-01-23 16:40:32 +00:00
|
|
|
// Draw debug rects & lines.
|
2020-01-18 03:41:45 +00:00
|
|
|
Debug.Draw(spriteBatch, camera);
|
|
|
|
|
|
|
|
// Aaaaand we're done.
|
|
|
|
spriteBatch.End();
|
|
|
|
|
|
|
|
// Draw RenderTarget to screen.
|
|
|
|
GraphicsDevice.SetRenderTarget(null);
|
|
|
|
GraphicsDevice.Clear(Color.Black);
|
|
|
|
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(renderTarget, drawRect, Color.White);
|
|
|
|
|
2020-01-23 21:18:28 +00:00
|
|
|
// Draw debug toasts.
|
|
|
|
Debug.DrawToasts(spriteBatch, font);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
spriteBatch.End();
|
|
|
|
|
|
|
|
base.Draw(gameTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|