Your First Scene - NocturnalWisp/Tynted-Engine GitHub Wiki
Getting Started
There are two ways to create a scene.
- Create an object of type "Scene." This is not usually used because the other way is much more useful.
- Creating a json file that implements a scene. Often used because you can define entities in a separate file, and quickly as well. So we'll be doing the second way, but you are welcome to do it the first way and just create entities like we've done in the past tutorials.
Creating The File
Make sure you have a resource path that you can access in visual studio or whatever IDE you re using. Create a new file in that location called "scene.json" Without the quotes, and name it whatever you like. You can now open the file in text form. The scene implementation is explained fairly well here.
Another Shell
Just like the previous tutorials, the scene file has a shell you must adhere to. This is the format:
{
//Your version of the scene here.
//The current version can be found at the scene implementation link above.
"version": 0.1,
"name": "sceneName"
"entities": [
]
}
Creating entities
So now that we have the shell, you can see that we have an entities table. This table we can populate with various entities in the scene. For this tutorial, we will just be recreating the entity we created in the game class, but within this scene file. Make sure in the json file properties for visual studio, you set the "Copy to Output Directory" to "Always Copy," or "Copy if Newer."
{
//Your version of the scene here.
//The current version can be found at the scene implementation link above.
"version":0.1,
"name":"sceneName"
"entities":
[
{
"name":"Test1",
"tag":"",
"components":
{
"Transform":[ @pos, 0, @scale ],
"SpriteRenderee":[ @texture ],
"Health":[ 20, 5]
}
}
]
}
Running the Scene
Step 1
Now that we have the scene file, we need to update the game to run that scene. First we will clear out the creation of the entity, because we are doing that in the scene file.
using Tynted;
using Tynted.Components;
using Tynted.SFML.Graphics;
using Box2DNet.Common;
public class YourGameName : Game
{
public YourGameName(GameOptions options) : base(options) { }
public override void Initialize()
{
base.Intitialize();
ECSManager.AddSystem(typeof(MovingObjectSystem));
ECSManager.AddComponent(typeof(Health));
//Make sure you have a texture in your /Res/ folder
var texture = new Texture("Res/img.png");
}
//Print the test health to the console every frame.
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
Health testHealth = (Health)ECSManager.GetEntityComponent("Test1", typeof(Health));
Console.WriteLine(testHealth.health);
}
}
Step 2
Now we need to create the variables that we are using in the scene file such as the "@pos," "@scale," and "@texture."
using Tynted;
using Tynted.Components;
using Tynted.SFML.Graphics;
using Box2DNet.Common;
public class YourGameName : Game
{
public YourGameName(GameOptions options) : base(options) { }
public override void Initialize()
{
base.Intitialize();
ECSManager.AddSystem(typeof(MovingObjectSystem));
ECSManager.AddComponent(typeof(Health));
//Make sure you have a texture in your /Res/ folder
var texture = new Texture("Res/img.png");
var pos = new Vec2(200, 200);
var scale = new Vec2(1, 1);
//Put the path where ever your scene file is.
var scene = new Scene("\Res\scene.json", new Dictionary<string, object>()
{
{"pos", pos},
{"scale", scale},
{"texture", texture}
});
}
//Print the test health to the console every frame.
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
Health testHealth = (Health)ECSManager.GetEntityComponent("Test1", typeof(Health));
Console.WriteLine(testHealth.health);
}
}
Step 3
At last, we can now run the scene using the scene manager.
using Tynted;
using Tynted.Components;
using Tynted.SFML.Graphics;
using Box2DNet.Common;
public class YourGameName : Game
{
public YourGameName(GameOptions options) : base(options) { }
public override void Initialize()
{
base.Initialize();
ECSManager.AddSystem(typeof(MovingObjectSystem));
ECSManager.AddComponent(typeof(Health));
//Make sure you have a texture in your /Res/ folder
var texture = new Texture("Res/img.png");
var pos = new Vec2(200, 200);
var scale = new Vec2(1, 1);
//Put the path where ever your scene file is, and define the variables the scene file can use.
var scene = new Scene("\Res\scene.json", new Dictionary<string, object>()
{
{"pos", pos},
{"scale", scale},
{"texture", texture}
});
//Run the scene here!
SceneManager.LoadScene(scene);
}
//Print the test health to the console every frame.
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
Health testHealth = (Health)ECSManager.GetEntityComponent("Test1", typeof(Health));
Console.WriteLine(testHealth.health);
}
}
Congratulations
You have now created and loaded your first scene! You have completed all the tutorials. You can now go off and create or game, or if you are still struggling, check out the examples.