2019-12-04 18:16:38 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
|
2019-12-10 15:56:51 +00:00
|
|
|
|
using System;
|
2019-12-04 18:16:38 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Jumpy {
|
|
|
|
|
public class JumpyGame : Game {
|
|
|
|
|
GraphicsDeviceManager graphics;
|
2019-12-09 22:21:22 +00:00
|
|
|
|
|
2019-12-10 15:56:51 +00:00
|
|
|
|
// TODO: use a History<RenderTarget2D> but implement functions that let us re-use the
|
|
|
|
|
// RenderTargets instead of re-creating them every frame?
|
2019-12-09 22:21:22 +00:00
|
|
|
|
const int numRenderTargets = 1;
|
|
|
|
|
RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets];
|
|
|
|
|
int renderTargetIdx = 0;
|
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
SpriteBatch spriteBatch;
|
|
|
|
|
SpriteFont font;
|
2019-12-04 20:32:07 +00:00
|
|
|
|
bool fullScreen = false;
|
2019-12-04 22:15:50 +00:00
|
|
|
|
IDisplay display;
|
2019-12-04 18:16:38 +00:00
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
History<KeyboardState> keyboard = new History<KeyboardState>(2);
|
|
|
|
|
History<GamePadState> gamePad = new History<GamePadState>(2);
|
2019-12-10 15:56:51 +00:00
|
|
|
|
|
2019-12-09 02:19:50 +00:00
|
|
|
|
FpsCounter fpsCounter = new FpsCounter();
|
2019-12-10 21:37:49 +00:00
|
|
|
|
Texture2D grasslandBg1;
|
|
|
|
|
Texture2D grasslandBg2;
|
2019-12-11 21:57:30 +00:00
|
|
|
|
|
|
|
|
|
Player player;
|
|
|
|
|
World world;
|
2019-12-17 20:37:32 +00:00
|
|
|
|
Camera camera = new Camera();
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
public JumpyGame() {
|
|
|
|
|
graphics = new GraphicsDeviceManager(this);
|
2019-12-04 20:48:38 +00:00
|
|
|
|
IsMouseVisible = true;
|
2019-12-04 18:16:38 +00:00
|
|
|
|
Content.RootDirectory = "Content";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Performs initialization that's needed before starting to run.
|
|
|
|
|
protected override void Initialize() {
|
2019-12-04 22:15:50 +00:00
|
|
|
|
display = (IDisplay) Services.GetService(typeof(IDisplay));
|
2019-12-04 22:09:02 +00:00
|
|
|
|
display.Initialize(Window, graphics);
|
|
|
|
|
display.SetFullScreen(fullScreen);
|
2019-12-11 22:57:30 +00:00
|
|
|
|
|
|
|
|
|
Debug.Initialize(GraphicsDevice);
|
|
|
|
|
|
2019-12-09 22:21:22 +00:00
|
|
|
|
for (int i = 0; i < renderTargets.Length; i++) {
|
|
|
|
|
renderTargets[i] = new RenderTarget2D(
|
2019-12-17 20:37:32 +00:00
|
|
|
|
GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
|
2019-12-09 22:21:22 +00:00
|
|
|
|
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
|
}
|
2019-12-08 23:51:23 +00:00
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
base.Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called once per game. Loads all game content.
|
|
|
|
|
protected override void LoadContent() {
|
|
|
|
|
spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
|
|
|
font = Content.Load<SpriteFont>("font");
|
2019-12-11 21:57:30 +00:00
|
|
|
|
// TODO: decouple things like Player and World from their textures.
|
2019-12-08 23:51:23 +00:00
|
|
|
|
player = new Player(Content.Load<Texture2D>("player_1x"));
|
2019-12-11 21:57:30 +00:00
|
|
|
|
world = new World(Content.Load<Texture2D>("grassland"));
|
|
|
|
|
// TODO: move backgrounds into World.
|
2019-12-10 21:37:49 +00:00
|
|
|
|
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
|
|
|
|
|
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
|
2019-12-04 18:16:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
2019-12-11 22:57:30 +00:00
|
|
|
|
Debug.Clear();
|
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
gamePad.Add(GamePad.GetState(PlayerIndex.One));
|
|
|
|
|
keyboard.Add(Keyboard.GetState());
|
2019-12-04 18:16:38 +00:00
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
|
2019-12-10 15:56:51 +00:00
|
|
|
|
Exit();
|
2019-12-04 20:32:07 +00:00
|
|
|
|
}
|
2019-12-04 18:16:38 +00:00
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
|
2019-12-20 23:13:56 +00:00
|
|
|
|
keyboard[0].IsKeyDown(Keys.OemPlus) && keyboard[1].IsKeyUp(Keys.OemPlus) ||
|
2019-12-10 16:12:06 +00:00
|
|
|
|
gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
|
2019-12-10 15:56:51 +00:00
|
|
|
|
fullScreen = !fullScreen;
|
|
|
|
|
display.SetFullScreen(fullScreen);
|
2019-12-04 18:16:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-20 23:13:56 +00:00
|
|
|
|
if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) && gamePad[1].IsButtonUp(Buttons.LeftShoulder) ||
|
|
|
|
|
keyboard[0].IsKeyDown(Keys.OemMinus) && keyboard[1].IsKeyUp(Keys.OemMinus)) {
|
2019-12-11 22:57:30 +00:00
|
|
|
|
Debug.Enabled = !Debug.Enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 22:23:30 +00:00
|
|
|
|
List<Rectangle> collisionTargets = world.CollisionTargets();
|
2019-12-20 23:13:56 +00:00
|
|
|
|
player.Update(gameTime, gamePad, keyboard, collisionTargets);
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
2019-12-17 20:37:32 +00:00
|
|
|
|
camera.Update(gameTime, player.Position);
|
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
base.Update(gameTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called when the game should draw itself.
|
|
|
|
|
protected override void Draw(GameTime gameTime) {
|
2019-12-09 02:19:50 +00:00
|
|
|
|
// We need to update the FPS counter in Draw() since Update() might get called more
|
|
|
|
|
// frequently, especially when gameTime.IsRunningSlowly.
|
|
|
|
|
fpsCounter.Update();
|
|
|
|
|
|
2019-12-08 23:51:23 +00:00
|
|
|
|
// Draw scene to RenderTarget.
|
2019-12-09 22:21:22 +00:00
|
|
|
|
RenderTarget2D renderTarget = renderTargets[renderTargetIdx];
|
|
|
|
|
renderTargetIdx = (renderTargetIdx + 1) % renderTargets.Length;
|
2019-12-08 23:51:23 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2019-12-04 18:16:38 +00:00
|
|
|
|
GraphicsDevice.Clear(Color.CornflowerBlue);
|
2019-12-17 20:37:32 +00:00
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null);
|
2019-12-10 21:09:43 +00:00
|
|
|
|
|
2019-12-10 21:37:49 +00:00
|
|
|
|
// Draw background.
|
2019-12-11 21:57:30 +00:00
|
|
|
|
Rectangle bgSource = new Rectangle(
|
2019-12-17 22:53:14 +00:00
|
|
|
|
(int) (camera.Left * 0.25), 0, camera.Width, camera.Height);
|
2019-12-17 20:37:32 +00:00
|
|
|
|
Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height);
|
2019-12-10 21:37:49 +00:00
|
|
|
|
spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
|
2019-12-17 20:37:32 +00:00
|
|
|
|
bgSource = new Rectangle(
|
2019-12-17 22:53:14 +00:00
|
|
|
|
(int) (camera.Left * 0.5), 0, camera.Width, camera.Height);
|
2019-12-10 21:37:49 +00:00
|
|
|
|
spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
|
|
|
|
|
|
2019-12-10 21:09:43 +00:00
|
|
|
|
// Draw player.
|
2019-12-17 20:37:32 +00:00
|
|
|
|
player.Draw(spriteBatch, camera, gameTime);
|
2019-12-10 21:09:43 +00:00
|
|
|
|
|
|
|
|
|
// Draw foreground tiles.
|
2019-12-17 20:37:32 +00:00
|
|
|
|
world.Draw(spriteBatch, camera);
|
2019-12-10 21:09:43 +00:00
|
|
|
|
|
2019-12-11 22:23:30 +00:00
|
|
|
|
// Draw debug rects.
|
2019-12-17 20:37:32 +00:00
|
|
|
|
Debug.Draw(spriteBatch, camera);
|
2019-12-11 22:23:30 +00:00
|
|
|
|
|
2019-12-10 21:09:43 +00:00
|
|
|
|
// Aaaaand we're done.
|
2019-12-04 18:16:38 +00:00
|
|
|
|
spriteBatch.End();
|
|
|
|
|
|
2019-12-08 23:51:23 +00:00
|
|
|
|
// Draw RenderTarget to screen.
|
|
|
|
|
GraphicsDevice.SetRenderTarget(null);
|
|
|
|
|
GraphicsDevice.Clear(Color.Black);
|
2019-12-09 01:33:20 +00:00
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
|
2019-12-08 23:51:23 +00:00
|
|
|
|
SamplerState.PointClamp, DepthStencilState.Default,
|
|
|
|
|
RasterizerState.CullNone);
|
|
|
|
|
Rectangle drawRect = new Rectangle(
|
|
|
|
|
0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
|
|
|
|
|
spriteBatch.Draw(renderTarget, drawRect, Color.White);
|
2019-12-17 02:45:19 +00:00
|
|
|
|
|
|
|
|
|
if (Debug.Enabled) {
|
|
|
|
|
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
|
|
|
|
|
$"{fpsCounter.Fps} FPS";
|
2019-12-17 23:04:06 +00:00
|
|
|
|
spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.Teal);
|
|
|
|
|
Debug.DrawToast(spriteBatch, font);
|
2019-12-17 02:45:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 23:51:23 +00:00
|
|
|
|
spriteBatch.End();
|
2019-12-07 01:00:42 +00:00
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
base.Draw(gameTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|