79 lines
3.1 KiB
Markdown
79 lines
3.1 KiB
Markdown
---
|
|
title: "World"
|
|
date: 2019-05-22T12:51:08-07:00
|
|
weight: 100
|
|
---
|
|
|
|
World is the pie crust that contains all the delicious Encompass ingredients together.
|
|
|
|
The World's *Update* function drives the simulation and should be controlled from your engine's update loop.
|
|
|
|
The World's *Draw* function tells the Renderers to draw the scene.
|
|
|
|
In FNA, the game loop looks something like this:
|
|
|
|
```cs
|
|
using Encompass;
|
|
using Microsoft.Xna.Framework;
|
|
|
|
public class MyGame : Game
|
|
{
|
|
private World world;
|
|
SpriteBatch spriteBatch;
|
|
|
|
RenderTarget2D gameRenderTarget;
|
|
RenderTarget2D levelBrowserRenderTarget;
|
|
RenderTarget2D uiRenderTarget;
|
|
|
|
...
|
|
|
|
/// <summary>
|
|
/// Allows the game to run logic such as updating the world,
|
|
/// checking for collisions, gathering input, and playing audio.
|
|
/// </summary>
|
|
/// <param name="gameTime">Provides a snapshot of timing values.</param>
|
|
protected override void Update(GameTime gameTime)
|
|
{
|
|
if (GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
|
Exit();
|
|
|
|
world.Update(gameTime.ElapsedGameTime.TotalSeconds);
|
|
|
|
base.Update(gameTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// This is called when the game should draw itself.
|
|
/// </summary>
|
|
/// <param name="gameTime">Provides a snapshot of timing values.</param>
|
|
protected override void Draw(GameTime gameTime)
|
|
{
|
|
world.Draw();
|
|
|
|
GraphicsDevice.SetRenderTarget(null);
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp);
|
|
spriteBatch.Draw(gameRenderTarget, windowDimensions, Color.White);
|
|
spriteBatch.Draw(levelBrowserRenderTarget, windowDimensions, Color.White);
|
|
spriteBatch.Draw(uiRenderTarget, windowDimensions, Color.White);
|
|
spriteBatch.End();
|
|
|
|
base.Draw(gameTime);
|
|
}
|
|
}
|
|
```
|
|
|
|
But you can call these methods wherever you see fit.
|
|
|
|
{{% notice tip %}}
|
|
It is valid for your game to have multiple Worlds, but be warned that it is difficult to share information between Worlds by design and this may not be a good choice.
|
|
{{% /notice %}}
|
|
|
|
**What's that whole gameTime business about?**
|
|
|
|
`gameTime` lets us utilize the concept of delta-time. Delta-time means that we take into account the amount of time that has elapsed between frames. Correct usage of delta-time is crucial to make sure that your game does not become *frame-dependent*, which is very bad. We'll talk more about frame-dependence later in the tutorial, but to briefly summarize, if your game is frame-dependent you will run into very frustrating behavior if you ever want your game to run at a different framerate than the one you are developing with.
|
|
|
|
Even if you lock your game to a fixed timestep (which can be a very good idea for achieving precise game physics), writing your game with delta-time in mind can be the difference between changing the timestep being a one-line tweak or a months long hair-pulling nightmare.
|
|
|
|
That's it! Now that we have these high-level concepts down, let's build an actual, for-real game.
|