GitHub
GitHub - xk0fe/k.ECS: s&box ECS library
s&box ECS library. Contribute to xk0fe/k.ECS development by creating an account on GitHub.
var world = World.Default;
var filter = new EntityFilter( world );
filter.With<Position>().With<Input>().Without<Dead>;
foreach(int entity in filter){
var input = world.GetComponent<Input>( entity );
ref var position = ref world.GetComponent<Position>( entity );
if ( input.X != 0 && input.Y != 0) continue;
position.X += input.X;
position.Y += input.Y;
}public class MySwordGameInitializer : InitializerBase
{
protected override void OnAwake()
{
AddFeature( new CombatFeature() );
base.OnAwake(); // must be called after we add all the features
}
}public class CombatFeature : FeatureBase
{
public CombatFeature()
{
Add( new AttackSystem() );
Add( new DefenseSystem() );
}
}public class AttackSystem : SystemBase
{
private EntityFilter _filter = new EntityFilter( World.Default ).With<AttackComponent>();
public override void Update( float deltaTime )
{
foreach ( var entity in _filter )
{
ref var attackComponent = ref entity.Get<AttackComponent>();
// Perform attack logic here
}
}
}// use world world.AddComponent<Input>();
// use extension to add component for entity in Default world entity.AddComponent<Input>();
// use world
world.SetComponent( new Input { Value = InputExample } );// use extension to set component for entity in Default world
entity.SetComponent( new Input { Value = InputExample } );struct for components, it is important to remember how value types differ from reference types. When you retrieve a component from an entity, you're working with a copy - not a reference to the original. This means that modifying the struct directly won't affect the component stored in the ECS unless you explicitly use ref.// ✅ Will change the position ref var position = ref entity.GetComponent<Position>(); position.Value = Vector3.Zero;
// ❌ WONT change the position var position = entity.GetComponent<Position>(); position.Value = Vector3.Zero;
// use world world.RemoveComponent<Input>();
// use extension to remove component for entity in Default world entity.RemoveComponent<Input>();
struct MyComponent class MyComponentProvider : EntityProvider<MyComponent>Filter.With<MyComponent>