encompass-cs-docs/content/concepts/world_builder.md

53 lines
1.5 KiB
Markdown
Raw Normal View History

2019-05-22 20:56:04 +00:00
---
title: "World Builder"
date: 2019-05-22T13:40:25-07:00
weight: 35
---
WorldBuilder is used to construct a World from Engines, Renderers, and an initial state of Entities, Components, and Messages.
2020-07-20 20:57:45 +00:00
The WorldBuilder enforces certain rules about Engine structure. It is forbidden to have messages create cycles between Engines, and no Component may be mutated by more than one Engine without declaring a priority.
2019-05-22 20:56:04 +00:00
2019-12-01 21:43:31 +00:00
The WorldBuilder uses Engines and their Message read/send information to determine a valid ordering of the Engines, which is given to the World.
2019-05-22 20:56:04 +00:00
Here is an example usage with FNA:
2019-05-22 20:56:04 +00:00
2019-12-01 21:43:31 +00:00
```cs
using Encompass;
using Microsoft.Xna.Framework;
2019-05-22 21:46:09 +00:00
2019-12-01 21:43:31 +00:00
public class MyGame : Game
{
2019-05-22 20:56:04 +00:00
private World world;
...
2019-12-01 21:43:31 +00:00
protected override void LoadContent()
{
var worldBuilder = new WorldBuilder();
2019-05-22 20:56:04 +00:00
2019-12-01 21:43:31 +00:00
worldBuilder.AddEngine(new MotionEngine());
2020-07-21 21:48:48 +00:00
worldBuilder.AddOrderedRenderer(new TextureRenderer());
2019-05-22 20:56:04 +00:00
2019-12-01 21:43:31 +00:00
var entity = worldBuilder.CreateEntity();
2019-05-22 20:56:04 +00:00
2020-07-12 19:09:27 +00:00
SetComponent(entity, new PositionComponent(0, 0));
SetComponent(entity, new VelocityComponent(20, 0));
SetComponent(entity, new TextureComponent(
TextureHelper.LoadTexture("Sprites/ball.png");
);
2019-05-22 20:56:04 +00:00
2019-12-01 21:43:31 +00:00
world = worldBuilder.Build();
2019-05-22 20:56:04 +00:00
}
...
}
```
Now our game will initialize with a ball that moves horizontally across the screen!
{{% notice tip %}}
Be extra careful that you remember to actually add Engines to the WorldBuilder when you define them. Otherwise nothing will happen, which can be very embarrassing.
2019-05-22 20:56:04 +00:00
{{% /notice %}}