161 lines
5.5 KiB
C#
161 lines
5.5 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Jumpy {
|
|
public class JumpyGame : Game {
|
|
GraphicsDeviceManager graphics;
|
|
|
|
// TODO: use a History<RenderTarget2D> but implement functions that let us re-use the
|
|
// RenderTargets instead of re-creating them every frame?
|
|
const int numRenderTargets = 1;
|
|
RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets];
|
|
int renderTargetIdx = 0;
|
|
|
|
SpriteBatch spriteBatch;
|
|
SpriteFont font;
|
|
bool fullScreen = false;
|
|
IDisplay display;
|
|
|
|
History<KeyboardState> keyboard = new History<KeyboardState>(2);
|
|
History<GamePadState> gamePad = new History<GamePadState>(2);
|
|
|
|
FpsCounter fpsCounter = new FpsCounter();
|
|
Texture2D grasslandBg1;
|
|
Texture2D grasslandBg2;
|
|
|
|
Player player;
|
|
World world;
|
|
Camera camera = new Camera();
|
|
|
|
public JumpyGame() {
|
|
graphics = new GraphicsDeviceManager(this);
|
|
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);
|
|
|
|
for (int i = 0; i < renderTargets.Length; i++) {
|
|
renderTargets[i] = 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");
|
|
// TODO: decouple things like Player and World from their textures.
|
|
player = new Player(Content.Load<Texture2D>("player_1x"));
|
|
world = new World(Content.Load<Texture2D>("grassland"));
|
|
// TODO: move backgrounds into World.
|
|
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
|
|
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
|
|
}
|
|
|
|
// Called once per game. Unloads all game content.
|
|
protected override void UnloadContent() {
|
|
// TODO: Unload any non ContentManager content here.
|
|
}
|
|
|
|
// Updates the game world.
|
|
protected override void Update(GameTime gameTime) {
|
|
Debug.Clear();
|
|
|
|
gamePad.Add(GamePad.GetState(PlayerIndex.One));
|
|
keyboard.Add(Keyboard.GetState());
|
|
|
|
if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
|
|
Exit();
|
|
}
|
|
|
|
if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
|
|
gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
|
|
fullScreen = !fullScreen;
|
|
display.SetFullScreen(fullScreen);
|
|
}
|
|
|
|
if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) &&
|
|
gamePad[1].IsButtonUp(Buttons.LeftShoulder)) {
|
|
Debug.Enabled = !Debug.Enabled;
|
|
}
|
|
|
|
List<Rectangle> collisionTargets = world.CollisionTargets();
|
|
player.Update(gameTime, gamePad, collisionTargets);
|
|
|
|
camera.Update(gameTime, player.Position);
|
|
|
|
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();
|
|
|
|
// Draw scene to RenderTarget.
|
|
RenderTarget2D renderTarget = renderTargets[renderTargetIdx];
|
|
renderTargetIdx = (renderTargetIdx + 1) % renderTargets.Length;
|
|
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.
|
|
player.Draw(spriteBatch, camera, gameTime);
|
|
|
|
// Draw foreground tiles.
|
|
world.Draw(spriteBatch, camera);
|
|
|
|
// Draw debug rects.
|
|
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);
|
|
|
|
if (Debug.Enabled) {
|
|
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
|
|
$"{fpsCounter.Fps} FPS";
|
|
spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.Teal);
|
|
Debug.DrawToast(spriteBatch, font);
|
|
}
|
|
|
|
spriteBatch.End();
|
|
|
|
base.Draw(gameTime);
|
|
}
|
|
}
|
|
}
|