Giter Club home page Giter Club logo

coreecs's Introduction

JuCore ECS

JuCore ECS is a deterministic lightweight ECS framework.

It's heavily inspired in Entitas, but with some differences:

  • It's deterministic (does not rely on hashtables)
  • Does not use code generators (no reflection, less problems)
  • Easier to start using it (more straightforward, see documentation below)
  • No GC collections (when reusing destroyed entities)
  • Similar perfomance (10~15% less, but handles well all requirements for a game)

Any feedback is welcome !

See also

  • JuCore - Core package base (service locator and useful services)
  • JuCore Math - Linear algebra math library, also 2D/3D physics, noise functions and IK

Install (Unity)

Update the dependencies in the /Packages/manifest.json file in your project folder with:

	"com.judelco.core.ecs": "https://github.com/JuDelCo/CoreECS.git#v1.8.0",

Documentation

Note: All components should be structs for best performance and memory efficiency.

Defining components

public struct Speed : IComponent
{
	public float value;

	public Speed(float value) // Optional, syntactic sugar for later
	{
		this.value = value;
	}
}

Create a Context

// IMPORTANT: You need to pass the count of component types you will use to the context
const int COMPONENT_TYPES_COUNT = 9;

var context = new Context(Constants.COMPONENT_TYPES_COUNT);

Creating entities

var entity = context.CreateEntity();

entity.Add(new Speed(2f));

Entity management

entity.Add(new Position(3, 7));
entity.Replace(new Health(10));
entity.Remove<Speed>();

var hasSpeed = entity.Has<Speed>();
var healthData = entity.Get<Health>();

// You can also chain methods!
context.CreateEntity()
	.Add(new Speed(2f))
	.Add(new Position(3, 7))
	.Replace(new Health(10)) // Note: Replace will add the component if doesn't have it yet
	.Remove<Speed>();

Group management

// Returns a group containing always all entities with Position and Speed components.
var group = context.GetGroup(MatcherGenerator.AllOf<Position, Speed>());

var entities = group.GetEntities();

foreach (var e in entities)
{
	// ...
}

Execute systems

// You can also use IInitializeSystem, ICleanupSystem and ITearDownSystem systems
public class MovementSystem : IExecuteSystem
{
	private IGroup group;

	public MovementSystem(IContext context)
	{
		// You can also use a shorthand for AllOf components!
		// This is equivalent to: GetGroup(MatcherGenerator.AllOf<Position, Speed>())
		group = context.GetGroup<Position, Speed>();
	}

	public void Execute()
	{
		foreach (var e in group.GetEntities())
		{
			var position = e.Get<Position>();
			var speed = e.Get<Speed>().value;
			position.x += speed;

			e.Replace(position);
		}
	}
}

Reactive systems

public class RenderPositionSystem : ReactiveSystem
{
	private IContext context;
	private IGroup group;

	public RenderPositionSystem(IContext context) : base(context)
	{
		this.context = context;
	}

	protected override ICollector GetTrigger(IContext context)
	{
		return context.GetGroup<Position, View>().CreateCollector(GroupEvent.Added);
	}

	protected override bool Filter(IEntity entity)
	{
		// Yes, you can use multiple types in Has<T> method !
		return entity.Has<Position, View>();
	}

	protected override void Execute(List<IEntity> entities)
	{
		foreach (var e in entities)
		{
			var position = e.Get<Position>();
			e.Get<View>().gameObject.transform.position = new Vector3(position.x, position.y, position.z);
		}
	}
}

The MIT License (MIT)

Copyright © 2019-2021 Juan Delgado (@JuDelCo)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

coreecs's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

tuita520

coreecs's Issues

Error in IContextExtensions

Just wanted to point out a small omission in the IContextExtensions class, where T4 in GetGroup<> is not being used. I've attached the corrected method.

public static IGroup GetGroup<T1, T2, T3, T4>(this IContext context) where T1 : IComponent where T2 : IComponent where T3 : IComponent where T4 : IComponent
{
	return context.GetGroup(MatcherGenerator.AllOf<T1, T2, T3, T4>());
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.