Skip to content

Getting Started

Thiago edited this page May 13, 2024 · 2 revisions

Creating your first game

For this, we'll be using the Raylib implementations for each engine component

Initializing the engine components

Firstly, we create a new instance of each component and attach it to the engine. Not everything is necessary for the engine to run, the only required components are a Graphics engine and Event bus.

var initParams = new WindowInitParams
{
	Title = "My awesome game",
	Width = 1920,
	Height = 1080,
	TargetFps = 120,
	Fullscreen = false,
	Borderless = true,
	MSAA4X = true,
	PostProcessing = false,
};

var builder = new EngineBuilder();

builder.AddComponent<IGraphicsEngine, RaylibGraphicsEngine> (() => new RaylibGraphicsEngine(initParams), true);
builder.AddComponent<IPhysicsEngine, PhysicsEngine>(true);
builder.AddComponent<IEventBus, EventBus>();
builder.AddComponent<IInputSystem, RaylibInputSystem>();

builder.AddLogger(new ConsoleLogger());

var engine = builder.Build();

Populating the game world.

There are two ways of doing it. Either manually via code, or by loading a file containing the structure for it.

The code way

// A ground for the playet to sit in
var ground = new Entity();
ground.Transform.Position = new Vector3(0, -1, 0);
ground.AddComponent(new BoxRenderer(ground) { Size = new Vector3(30, 2, 30), Color = Color.Green });
ground.AddComponent(new BoxCollider(ground) { Size = new Vector3(30, 2, 30) });
ground.AddComponent(new Rigidbody(ground) { IsKinematic = true });

Engine.CreateEntity(ground); // Static method to instantiate the entity, otherwise it will not do anything.

// The player object
var entity = new CameraController();
entity.Transform.Position = new Vector3(0, 5, 0);
entity.AddComponent(new BoxRenderer(entity) { Color = Color.Red, Offset = new(0, -2, 0)});
entity.AddComponent(new BoxCollider(entity) { Size = new Vector3(1, 1, 1), Offset = new Vector3(0, -2, 0) });
entity.AddComponent(new Rigidbody(entity) { IsKinematic = false });
entity.AddComponent(new Basalt.TestField.Components.PlayerController(entity));

// You may also attach children objects with entity.AddChildren();
Engine.CreateEntity(entity);

The file way

To use this, you must have previously serialized the entities to a file. There is a built in function that does this already, entity.SerializeToJson(). After that, you can convert that json back into an entity by using Entity.DeserializeFromJson(string json).

// Serializing it
var sphere = new Entity();
sphere.Transform.Position = new Vector3(0, 5, 0);
sphere.AddComponent(new SphereRenderer(sphere ) { Size = new Vector3(0.5f), Color = Color.Blue });
string serialized = sphere.SerializeToJson();
File.WriteAllText("sphere.json", json); // Save it to a file however you'd like

// Deserializing it
var json = File.ReadAllText("sphere.json");
Entity deserialized = Entity.DeserializeFromJson(json);

Engine.CreateEntity(deserialized);

Starting the engine

After you've done all the setup for it, you can finally start the engine.

engine.Initialize();

If everything is set up correctly, you should see your game running now.