can save custom components and components based off type

beta.r3js.org
Theunis J. Botha 2016-12-16 16:03:48 +01:00
parent aeee9c4533
commit 15d3c5b446
11 changed files with 337 additions and 160 deletions

View File

@ -16,8 +16,7 @@
"npm": "^4.0.2", "npm": "^4.0.2",
"q": "^1.4.1", "q": "^1.4.1",
"three": "^0.81.2", "three": "^0.81.2",
"through2": "^2.0.1", "through2": "^2.0.1"
"tiny-ecs": "^2.0.0"
}, },
"repository": "https://github.com/ToywheelDev/game-lib.git", "repository": "https://github.com/ToywheelDev/game-lib.git",
"license": "UNLICENSED", "license": "UNLICENSED",

View File

@ -0,0 +1,11 @@
GameLib.Component = function(
componentType
) {
this.componentType = componentType;
};
GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING = 0x1;
GameLib.Component.prototype.toApiComponent = function() {
return this.id;
};

View File

@ -42,6 +42,11 @@ GameLib.D3.API.Mesh = function(
localScale, localScale,
up up
) { ) {
GameLib.Component.call(
this,
GameLib.D3.API.Mesh
);
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId(); id = GameLib.Utils.RandomId();
} }
@ -137,3 +142,6 @@ GameLib.D3.API.Mesh = function(
} }
this.up = up; this.up = up;
}; };
GameLib.D3.API.Mesh.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Mesh.prototype.constructor = GameLib.D3.API.Mesh;

View File

@ -40,9 +40,6 @@ GameLib.D3.API.PathFollowing = function (
currentPathValue, currentPathValue,
currentSpeed, currentSpeed,
direction, direction,
mx,
my,
mz,
raycaster, raycaster,
currentPosition, currentPosition,
futurePosition, futurePosition,
@ -51,6 +48,11 @@ GameLib.D3.API.PathFollowing = function (
rotationVector rotationVector
) { ) {
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING
);
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId(); id = GameLib.Utils.RandomId();
} }
@ -121,21 +123,6 @@ GameLib.D3.API.PathFollowing = function (
} }
this.direction = direction; this.direction = direction;
if (GameLib.Utils.UndefinedOrNull(mx)) {
mx = new GameLib.Utils.MovingAverage(10);
}
this.mx = mx;
if (GameLib.Utils.UndefinedOrNull(my)) {
my = new GameLib.Utils.MovingAverage(10);
}
this.my = my;
if (GameLib.Utils.UndefinedOrNull(mz)) {
mz = new GameLib.Utils.MovingAverage(10);
}
this.mz = mz;
if (GameLib.Utils.UndefinedOrNull(raycaster)) { if (GameLib.Utils.UndefinedOrNull(raycaster)) {
raycaster = new GameLib.D3.API.Raycaster(); raycaster = new GameLib.D3.API.Raycaster();
} }
@ -165,4 +152,13 @@ GameLib.D3.API.PathFollowing = function (
rotationVector = new GameLib.API.Quaternion(); rotationVector = new GameLib.API.Quaternion();
} }
this.rotationVector = rotationVector; this.rotationVector = rotationVector;
this.mx = new GameLib.Utils.MovingAverage(10);
this.my = new GameLib.Utils.MovingAverage(10);
this.mz = new GameLib.Utils.MovingAverage(10);
}; };
GameLib.D3.API.PathFollowing.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.PathFollowing.prototype.constructor = GameLib.D3.API.PathFollowing;

View File

@ -85,7 +85,7 @@ GameLib.D3.API.Scene = function(
this.worlds = worlds; this.worlds = worlds;
if (GameLib.Utils.UndefinedOrNull(entityManager)) { if (GameLib.Utils.UndefinedOrNull(entityManager)) {
entityManager = null; entityManager = new GameLib.API.EntityManager();
} }
this.entityManager = entityManager; this.entityManager = entityManager;

View File

@ -16,7 +16,7 @@ GameLib.D3.API.Spline = function(
this.id = id; this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) { if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'spline-unnamed'; name = 'Spline (unknown)';
} }
this.name = name; this.name = name;

View File

@ -35,9 +35,6 @@ GameLib.D3.PathFollowing = function RuntimePathFollowing(
apiPathFollowing.currentPathValue, apiPathFollowing.currentPathValue,
apiPathFollowing.currentSpeed, apiPathFollowing.currentSpeed,
apiPathFollowing.direction, apiPathFollowing.direction,
apiPathFollowing.mx,
apiPathFollowing.my,
apiPathFollowing.mz,
apiPathFollowing.raycaster, apiPathFollowing.raycaster,
apiPathFollowing.currentPosition, apiPathFollowing.currentPosition,
apiPathFollowing.futurePosition, apiPathFollowing.futurePosition,
@ -110,3 +107,113 @@ GameLib.D3.PathFollowing = function RuntimePathFollowing(
GameLib.D3.PathFollowing.prototype = Object.create(GameLib.D3.API.PathFollowing.prototype); GameLib.D3.PathFollowing.prototype = Object.create(GameLib.D3.API.PathFollowing.prototype);
GameLib.D3.PathFollowing.prototype.constructor = GameLib.D3.PathFollowing; GameLib.D3.PathFollowing.prototype.constructor = GameLib.D3.PathFollowing;
GameLib.D3.PathFollowing.prototype.toApiComponent = function() {
var apiPathFollowing = new GameLib.D3.API.PathFollowing(
this.id,
this.name,
this.spline,
this.raytraceMesh,
this.accelleration,
this.maxSpeed,
this.baseOffset.toApiVector(),
this.maxOffset.toApiVector(),
this.steeringSpeed,
this.targetOffset.toApiVector(),
this.currentOffset.toApiVector(),
this.currentPathValue,
this.currentSpeed,
this.direction,
this.raycaster.toApiRaycaster(),
this.currentPosition.toApiVector(),
this.futurePosition.toApiVector(),
this.up.toApiVector(),
this.rotationMatrix.toApiMatrix(),
this.rotationVector.toApiQuaternion()
);
return apiPathFollowing;
};
GameLib.D3.PathFollowing.FromObjectComponent = function(graphics, objectComponent) {
var apiPathFollowing = new GameLib.D3.API.PathFollowing(
objectComponent.id,
objectComponent.name,
objectComponent.spline,
objectComponent.raytraceMesh,
objectComponent.accelleration,
objectComponent.maxSpeed,
new GameLib.API.Vector3(
objectComponent.baseOffset.x,
objectComponent.baseOffset.y,
objectComponent.baseOffset.z
),
new GameLib.API.Vector3(
objectComponent.maxOffset.x,
objectComponent.maxOffset.y,
objectComponent.maxOffset.z
),
objectComponent.steeringSpeed,
new GameLib.API.Vector3(
objectComponent.targetOffset.x,
objectComponent.targetOffset.y,
objectComponent.targetOffset.z
),
new GameLib.API.Vector3(
objectComponent.currentOffset.x,
objectComponent.currentOffset.y,
objectComponent.currentOffset.z
),
this.currentPathValue,
this.currentSpeed,
this.direction,
new GameLib.D3.API.Raycaster(
new GameLib.API.Vector3(
objectComponent.raycaster.position.x,
objectComponent.raycaster.position.y,
objectComponent.raycaster.position.z
),
new GameLib.API.Vector3(
objectComponent.raycaster.direction.x,
objectComponent.raycaster.direction.y,
objectComponent.raycaster.direction.z
)
),
new GameLib.API.Vector3(
objectComponent.currentPosition.x,
objectComponent.currentPosition.y,
objectComponent.currentPosition.z
),
new GameLib.API.Vector3(
objectComponent.futurePosition.x,
objectComponent.futurePosition.y,
objectComponent.futurePosition.z
),
new GameLib.API.Vector3(
objectComponent.up.x,
objectComponent.up.y,
objectComponent.up.z
),
//TODO : objectComponent rotationVector matrix4
new GameLib.API.Matrix4(
new GameLib.API.Quaternion(),
new GameLib.API.Quaternion(),
new GameLib.API.Quaternion(),
new GameLib.API.Quaternion()
),
new GameLib.API.Quaternion(
objectComponent.rotationVector.x,
objectComponent.rotationVector.y,
objectComponent.rotationVector.z,
objectComponent.rotationVector.w
)
);
return new GameLib.D3.PathFollowing(
graphics,
this,
apiPathFollowing
);
};

View File

@ -65,6 +65,13 @@ GameLib.D3.Raycaster.prototype.createInstance = function(update) {
return instance; return instance;
}; };
GameLib.D3.Raycaster.prototype.toApiRaycaster = function() {
return new GameLib.D3.API.Raycaster(
this.position.toApiVector(),
this.direction.toApiVector()
)
};
/** /**
* Sets the direction and position of this raycaster * Sets the direction and position of this raycaster
* @param position GameLib.Vector3 * @param position GameLib.Vector3

View File

@ -53,6 +53,11 @@ GameLib.D3.Scene = function Scene(
this.scale this.scale
); );
this.entityManager = new GameLib.EntityManager(
this,
this.entityManager
);
this.progressCallback = progressCallback; this.progressCallback = progressCallback;
this.instance = this.createInstance(); this.instance = this.createInstance();
@ -79,6 +84,21 @@ GameLib.D3.Scene = function Scene(
} }
} }
} }
this.entityManager.entities.map(
function (entity) {
this.idToObject[entity.id] = entity;
entity.components.map(
function(component) {
if (component instanceof GameLib.Component) {
this.idToObject[component.id] = component;
}
}.bind(this)
)
}.bind(this)
);
this.entityManager.linkObjects(this.idToObject);
}; };
GameLib.D3.Scene.prototype = Object.create(GameLib.D3.API.Scene.prototype); GameLib.D3.Scene.prototype = Object.create(GameLib.D3.API.Scene.prototype);
@ -131,7 +151,10 @@ GameLib.D3.Scene.prototype.toApiScene = function() {
} }
); );
var apiEntityManager = this.entityManager.toApiEntityManager(); var apiEntityManager = null;
if (this.entityManager) {
apiEntityManager = this.entityManager.toApiEntityManager();
}
var apiCameras = this.cameras.map( var apiCameras = this.cameras.map(
function(camera) { function(camera) {
@ -162,8 +185,8 @@ GameLib.D3.Scene.prototype.toApiScene = function() {
this.path, this.path,
this.name, this.name,
apiMeshes, apiMeshes,
this.quaternion.toApiQuaternion(),
this.position.toApiVector(), this.position.toApiVector(),
this.quaternion.toApiQuaternion(),
this.scale.toApiVector(), this.scale.toApiVector(),
this.parentSceneId, this.parentSceneId,
apiLights, apiLights,

View File

@ -1,13 +1,22 @@
/** /**
* GameLib.EntityManager * GameLib.EntityManager
* @param entities GameLib.D3.Entity[] * @param parentObject
* @param apiEntityManager GameLib.API.EntityManager
* @constructor * @constructor
*/ */
GameLib.EntityManager = function(entities) { GameLib.EntityManager = function(
parentObject,
apiEntityManager
) {
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
GameLib.API.EntityManager.call( GameLib.API.EntityManager.call(
this, this,
entities apiEntityManager.entities
); );
this.instance = this.createInstance(); this.instance = this.createInstance();
@ -21,25 +30,21 @@ GameLib.EntityManager.prototype.constructor = GameLib.EntityManager;
* @returns {*} * @returns {*}
*/ */
GameLib.EntityManager.prototype.createInstance = function() { GameLib.EntityManager.prototype.createInstance = function() {
/**
var instance = null; * Fuck the current ECS bullshit on the internet - both tiny-ecs and ecsjs SUCKS ASS
*/
if (typeof 'require' != 'undefined') { return null;
instance = require('tiny-ecs').EntityManager;
} else {
instance = EntityManager.EntityManager;
}
return instance;
}; };
/** /**
* Creates an GameLib.Entity * Creates an GameLib.Entity
* @returns {*} * @returns {*}
*/ */
GameLib.EntityManager.prototype.createEntity = function() { GameLib.EntityManager.prototype.createEntity = function(name) {
var entity = new GameLib.Entity(this); var apiEntity = new GameLib.API.Entity(null, name);
var entity = new GameLib.Entity(this, this, apiEntity);
this.entities.push(entity); this.entities.push(entity);
@ -49,12 +54,10 @@ GameLib.EntityManager.prototype.createEntity = function() {
/** /**
* Removes an entity * Removes an entity
* @param entity GameLib.D3.Entity * @param entity GameLib.D3.Entity
* @returns bool true if successful * @returns boolean true if successful
*/ */
GameLib.EntityManager.prototype.removeEntity = function(entity) { GameLib.EntityManager.prototype.removeEntity = function(entity) {
entity.instance.remove();
var index = this.entities.indexOf(entity); var index = this.entities.indexOf(entity);
if (index == -1) { if (index == -1) {
@ -63,90 +66,113 @@ GameLib.EntityManager.prototype.removeEntity = function(entity) {
} }
this.entities.splice(index, 1); this.entities.splice(index, 1);
return true; return true;
}; };
/**
* Adds a component to an entity
* @param entity GameLib.D3.Entity
* @param component Object.constructor
*/
GameLib.EntityManager.prototype.entityAddComponent = function(entity, component) {
this.instance.entityAddComponent(entity.instance, component);
};
/** /**
* Returns all the objects with the following components * Returns all the objects with the following components
* @param components GameLib.Component[] * @param components GameLib.Component[]
*/ */
GameLib.EntityManager.prototype.queryComponents = function(components) { GameLib.EntityManager.prototype.query = function(components) {
return this.instance.queryComponents(components);
return this.entities.map(
function(__queryComponents) {
return function(entity) {
__queryComponents.map(
function(__entity) {
return function(queryComponent) {
__entity.components.map(
function(__queryComponent) {
return function(entityComponent) {
if (__queryComponent == entityComponent.constructor.name) {
// arrow should point towards a window or sergej --->
return entityComponent;
}
}
}(queryComponent)
);
}
}(entity)
)
}
}(components)
);
}; };
/** /**
* Converts a GameLib.Entity to GameLib.API.Entity * Converts a GameLib.Entity to GameLib.API.Entity
* @returns {GameLib.API.Entity} * @returns {GameLib.API.EntityManager}
*/ */
GameLib.EntityManager.prototype.toApiEntityManager = function() { GameLib.EntityManager.prototype.toApiEntityManager = function() {
//TODO: refactor / fix var apiEntities = this.entities.map(
// var apiEntity = new GameLib.API.Entity( function (entity) {
// this.id, return entity.toApiEntity();
// this.name, }
// GameLib.Utils.IdArrayOrEmptyArray(this.components), );
// this.position.toApiVector(),
// this.quaternion.toApiQuaternion(), var apiEntityManager = new GameLib.API.EntityManager(
// this.scale.toApiVector(), apiEntities
// GameLib.Utils.IdOrNull(this.parentScene), );
// GameLib.Utils.IdOrNull(this.mesh)
// ); return apiEntityManager;
//
// return apiEntity;
}; };
/** /**
* *
* @param graphics GameLib.D3.Graphics * @param graphics
* @param objectEntity Object * @param objectEntities Object
* @constructor * @constructor
*/ */
GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntity) { GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntities) {
//TODO: refactor /fix var apiEntities = objectEntities.entities.map(
// var apiEntity = new GameLib.API.Entity( function (objectEntity) {
// objectEntity.id,
// objectEntity.name, objectEntity.components = objectEntity.components.map(
// objectEntity.components, function (component) {
// new GameLib.API.Vector3( if (component instanceof Object) {
// objectEntity.position.x, if (component.componentType == GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) {
// objectEntity.position.y, return GameLib.D3.PathFollowing.FromObjectComponent(graphics, component);
// objectEntity.position.z } else {
// ), console.warn('no component was associated with this object');
// new GameLib.API.Quaternion( throw new Error('no component was associated with this object');
// objectEntity.quaternion.x, }
// objectEntity.quaternion.y, } else {
// objectEntity.quaternion.z, return component;
// objectEntity.quaternion.w, }
// new GameLib.API.Vector3( }
// objectEntity.quaternion.axis.x, );
// objectEntity.quaternion.axis.y,
// objectEntity.quaternion.axis.z return new GameLib.API.Entity(
// ) objectEntity.id,
// ), objectEntity.name,
// new GameLib.API.Vector3( objectEntity.components
// objectEntity.scale.x, )
// objectEntity.scale.y, }
// objectEntity.scale.z );
// ),
// objectEntity.parentScene, var apiEntityManager = new GameLib.API.EntityManager(
// objectEntity.mesh apiEntities
// ); );
//
// return new GameLib.Entity( var entityManager = new GameLib.EntityManager(
// graphics, this,
// apiEntity apiEntityManager
// ); );
entityManager.entities = entityManager.entities.map(
function(apiEntity) {
return new GameLib.Entity(
entityManager,
entityManager,
apiEntity
)
}
);
return entityManager;
}; };
/** /**
@ -155,6 +181,20 @@ GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntity)
*/ */
GameLib.EntityManager.prototype.linkObjects = function(idToObject) { GameLib.EntityManager.prototype.linkObjects = function(idToObject) {
this.entities.map(
function(entity) {
entity.components.map(
function (componentId, index, array) {
if (componentId instanceof GameLib.Component) {
array[index] = componentId;
} else {
array[index] = idToObject[componentId];
}
}
)
}
);
// TODO: fix // TODO: fix
// this.components.forEach( // this.components.forEach(
// function(currentValue, index, array) { // function(currentValue, index, array) {

View File

@ -34,8 +34,18 @@ GameLib.Entity.prototype.constructor = GameLib.Entity;
* Creates an entity instance * Creates an entity instance
*/ */
GameLib.Entity.prototype.createInstance = function() { GameLib.Entity.prototype.createInstance = function() {
var instance = this.entityManager.createEntity(); /**
return instance; * FUCK ecsjs and tiny-ecs - no client-side support and shitty code (only takes constructors as args)
*/
return null;
};
/**
* Adds a component to this entity through the instance (should notify the entity manager instance)
* @param component
*/
GameLib.Entity.prototype.addComponent = function(component) {
this.components.push(component);
}; };
/** /**
@ -51,61 +61,37 @@ GameLib.Entity.prototype.updateInstance = function() {
*/ */
GameLib.Entity.prototype.toApiEntity = function() { GameLib.Entity.prototype.toApiEntity = function() {
//TODO: refactor / fix var apiComponents = this.components.map(
// var apiEntity = new GameLib.API.Entity( function(component) {
// this.id, return component.toApiComponent();
// this.name, }
// GameLib.Utils.IdArrayOrEmptyArray(this.components), );
// this.position.toApiVector(),
// this.quaternion.toApiQuaternion(), return new GameLib.API.Entity(
// this.scale.toApiVector(), this.id,
// GameLib.Utils.IdOrNull(this.parentScene), this.name,
// GameLib.Utils.IdOrNull(this.mesh) apiComponents
// ); );
//
// return apiEntity;
}; };
/** /**
* *
* @param graphics GameLib.D3.Graphics * @param entityManager GameLib.EntityManager
* @param objectEntity Object * @param objectEntity Object
* @constructor * @constructor
*/ */
GameLib.Entity.FromObjectEntity = function(graphics, objectEntity) { GameLib.Entity.FromObjectEntity = function(entityManager, objectEntity) {
//TODO: refactor /fix var apiEntity = new GameLib.API.Entity(
// var apiEntity = new GameLib.API.Entity( objectEntity.id,
// objectEntity.id, objectEntity.name,
// objectEntity.name, objectEntity.components
// objectEntity.components, );
// new GameLib.API.Vector3(
// objectEntity.position.x, return new GameLib.Entity(
// objectEntity.position.y, entityManager,
// objectEntity.position.z this,
// ), apiEntity
// new GameLib.API.Quaternion( );
// objectEntity.quaternion.x,
// objectEntity.quaternion.y,
// objectEntity.quaternion.z,
// objectEntity.quaternion.w,
// new GameLib.API.Vector3(
// objectEntity.quaternion.axis.x,
// objectEntity.quaternion.axis.y,
// objectEntity.quaternion.axis.z
// )
// ),
// new GameLib.API.Vector3(
// objectEntity.scale.x,
// objectEntity.scale.y,
// objectEntity.scale.z
// ),
// objectEntity.parentScene,
// objectEntity.mesh
// );
//
// return new GameLib.Entity(
// graphics,
// apiEntity
// );
}; };