Giter Club home page Giter Club logo

exp-ecs's Introduction

Macro-powered Entity-Component-System framework

Build Status

This is a Entity-Component-System framework, inspired by the Ash Framework
Thanks to the Haxe macro system we are able to reduce boilerplate code and allow some optimizations.

This is platform-agnostic, you can use it on any targets supported by Haxe (e.g. cpp, js, lua, etc)
This is also game-framework-agnostic, you can use it with any game frameworks such as Kha, OpenFL, Heaps, etc.

Elements of the framework

Engine
The "core" that manages everything

Entity
A container holding various components

Component
Building blocks of an entity, contains attributes/properties but not any logics

System
Logics that operate on Components

Node
A container holding an Entity and the Components of interest. This is mostly for optimization, pre-fetching components from the entity so that we don't need to do so on every iteration.

NodeList
A list of Nodes. The specialized TrackingNodeList will keep track of entities together with their components and add/remove them from the list when appropriate.

Example

import exp.ecs.*;
import exp.ecs.node.*;
import exp.ecs.entity.*;
import exp.ecs.system.*;
import component.*;
import haxe.Timer;

class Playground {
	static function main() {
		// create an engine
		var engine = new Engine();
		
		// create an entity and adds 2 components to it
		var entity = new Entity();
		entity.add(new Velocity(1, 0));
		entity.add(new Position(0, 0));
		
		// add entity to engine
		engine.entities.add(entity);
		
		// create and add 2 systems to engine
		engine.systems.add(new MovementSystem());
		engine.systems.add(new RenderSystem());
		engine.systems.add(new CustomSystem());
		
		// run the engine
		new Timer(16).run = function() engine.update(16 / 1000);
	}
}

// Use metadata `@:nodes` to create a TrackingNodeList that contains nodes of entities that contains the specified components
// NodeList created this way will be cached in the engine, i.e. multiple systems will share the same NodeList instance if their Node type is the same
class MovementSystem extends System {
	// prepares a NodeList that contains entities having both the Position and Velocity components
	@:nodes var nodes:Node<Position, Velocity>;
	
	override function update(dt:Float) {
		// on each update we iterate all the nodes and update their Position components
		for(node in nodes) {
			node.position.x += node.velocity.x * dt;
			node.position.y += node.velocity.y * dt;
		}
	}
}

class RenderSystem extends System {
	// prepares a NodeList that contains entities having the Position component
	@:nodes var nodes:Node<Position>;
	
	override function update(dt:Float) {
		// on each update we iterate all the nodes and print out their positions on screen
		for(node in nodes) {
			trace('${node.entity} @ ${node.position.x}, ${node.position.y}');
		}
	}
}

// Besides using `@:nodes`, you can also create a NodeList manually
class CustomSystem extends System {
	var nodes:NodeList<CustomNode>;
	
	override function update(dt:Float) {
		for(node in nodes) {
			$type(node); // CustomNode
		}
	}
	
	override function onAdded(engine) {
		super.onAdded(engine);
		nodes = new TrackingNodeList(engine, CustomNode.new, entity -> entity.has(Position));
	}
	
	override function onRemoved(engine) {
		super.onRemoved(engine);
		nodes = null;
	}
}

// Manually declare a Node type
class CustomNode implements NodeBase {
	public var entity(default, null):Entity;
	public function new(entity) this.entity = entity;
}

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.