Update example in README

wip/bindgen
copygirl 2 years ago
parent 127e957c97
commit b6af74511f
  1. 26
      README.md

@ -48,8 +48,16 @@ var position = world
// The "Position" struct is defined at the bottom of this example. // The "Position" struct is defined at the bottom of this example.
.InitComponent<Position>(); .InitComponent<Position>();
world.New("One").Set(new Position( 0, 0)).Build(); // Create an "Entities" parent with two positioned entities inside.
world.New("Two").Set(new Position(10, 20)).Build(); var entities = world.New("Entities").Build();
entities.NewChild("One").Set(new Position(-5, 0)).Build();
entities.NewChild("Two").Set(new Position(10, 20)).Build();
// Changed my mind: Let's multiply each entity's position by 10.
foreach (var child in entities.GetChildren()) {
ref var pos = ref child.GetRefOrThrow<Position>();
pos = new(pos.X * 10, pos.Y * 10);
}
var onUpdate = world.LookupOrThrow("/flecs/pipeline/OnUpdate"); var onUpdate = world.LookupOrThrow("/flecs/pipeline/OnUpdate");
@ -57,21 +65,21 @@ var onUpdate = world.LookupOrThrow("/flecs/pipeline/OnUpdate");
// the "Position" component downwards by 2 every frame. // the "Position" component downwards by 2 every frame.
world.New("FallSystem").Build() world.New("FallSystem").Build()
.InitSystem(onUpdate, new("Position"), iter => { .InitSystem(onUpdate, new("Position"), iter => {
var positionSpan = iter.Field<Position>(1); var posColumn = iter.Field<Position>(1);
for (var i = 0; i < iter.Count; i++) { for (var i = 0; i < iter.Count; i++) {
ref var position = ref positionSpan[i]; ref var pos = ref posColumn[i];
position = new(position.X, position.Y + 2); pos = new(pos.X, pos.Y + 2);
} }
}); });
// Create a system that will print out entities' positions. // Create a system that will print out entities' positions.
world.New("PrintPositionSystem").Build() world.New("PrintPositionSystem").Build()
.InitSystem(onUpdate, new("[in] Position"), iter => { .InitSystem(onUpdate, new("[in] Position"), iter => {
var positionSpan = iter.Field<Position>(1); var posColumn = iter.Field<Position>(1);
for (var i = 0; i < iter.Count; i++) { for (var i = 0; i < iter.Count; i++) {
var entity = iter.Entity(i); var entity = iter.Entity(i);
var position = positionSpan[i]; var pos = posColumn[i];
Console.WriteLine($"{entity.Name} is at {position}"); Console.WriteLine($"{entity.Name} is at {pos}");
} }
}); });

Loading…
Cancel
Save