Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

A possibly useful tip to get component properties on entities. #41

Open
martikaljuve opened this issue Mar 16, 2017 · 1 comment
Open

Comments

@martikaljuve
Copy link
Collaborator

Just thought about this, maybe this is useful for someone. It allows you to use eDoor.door in addition to eDoor.get('door').

require('babel-runtime/core-js');
const Scent = require('scent');

const { Engine, Entity, Component, Symbols } = Scent;

// Add component properties to entity

Entity.componentAdded.notify(function(component) {
	Object.defineProperty(this, component[Symbols.bName], {
		configurable: true,
		get() { return this.get(component[Symbols.bType]); }
	});
});

Entity.componentRemoved.notify(function(componentType) {
	delete this[componentType.typeName];
});

// ----

var engine = new Engine();

var cDoor = new Component('door', 'open');
var cOnFire = new Component('onFire');

engine.registerComponent(cOnFire);

var door = new cDoor([true]);

var eDoor = engine.buildEntity([door]);

console.log('' + eDoor.door); // "Component `door` #2 [open]"
eDoor.remove(cDoor);
console.log('' + eDoor.door); // "undefined"

console.log('' + eDoor.onFire); // "undefined"
eDoor.add('onFire');
console.log('' + eDoor.onFire); // "Component `onFire` #3 []"

RunKit link: https://runkit.com/58ca44e957cfa40015693bdb/58ca44e957cfa40015693bdc/branches/master

@danielkcz
Copy link
Contributor

Rather hacky, but I see the need for that :) I was planning on adding it natively, but I never got to it properly.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants