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",
"q": "^1.4.1",
"three": "^0.81.2",
"through2": "^2.0.1",
"tiny-ecs": "^2.0.0"
"through2": "^2.0.1"
},
"repository": "https://github.com/ToywheelDev/game-lib.git",
"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,
up
) {
GameLib.Component.call(
this,
GameLib.D3.API.Mesh
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
@ -136,4 +141,7 @@ GameLib.D3.API.Mesh = function(
up = new GameLib.API.Vector3(0,1,0);
}
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,
currentSpeed,
direction,
mx,
my,
mz,
raycaster,
currentPosition,
futurePosition,
@ -51,6 +48,11 @@ GameLib.D3.API.PathFollowing = function (
rotationVector
) {
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING
);
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
@ -121,21 +123,6 @@ GameLib.D3.API.PathFollowing = function (
}
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)) {
raycaster = new GameLib.D3.API.Raycaster();
}
@ -165,4 +152,13 @@ GameLib.D3.API.PathFollowing = function (
rotationVector = new GameLib.API.Quaternion();
}
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;
if (GameLib.Utils.UndefinedOrNull(entityManager)) {
entityManager = null;
entityManager = new GameLib.API.EntityManager();
}
this.entityManager = entityManager;

View File

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

View File

@ -35,9 +35,6 @@ GameLib.D3.PathFollowing = function RuntimePathFollowing(
apiPathFollowing.currentPathValue,
apiPathFollowing.currentSpeed,
apiPathFollowing.direction,
apiPathFollowing.mx,
apiPathFollowing.my,
apiPathFollowing.mz,
apiPathFollowing.raycaster,
apiPathFollowing.currentPosition,
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.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;
};
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
* @param position GameLib.Vector3

View File

@ -53,6 +53,11 @@ GameLib.D3.Scene = function Scene(
this.scale
);
this.entityManager = new GameLib.EntityManager(
this,
this.entityManager
);
this.progressCallback = progressCallback;
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);
@ -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(
function(camera) {
@ -162,8 +185,8 @@ GameLib.D3.Scene.prototype.toApiScene = function() {
this.path,
this.name,
apiMeshes,
this.quaternion.toApiQuaternion(),
this.position.toApiVector(),
this.quaternion.toApiQuaternion(),
this.scale.toApiVector(),
this.parentSceneId,
apiLights,

View File

@ -1,13 +1,22 @@
/**
* GameLib.EntityManager
* @param entities GameLib.D3.Entity[]
* @param parentObject
* @param apiEntityManager GameLib.API.EntityManager
* @constructor
*/
GameLib.EntityManager = function(entities) {
GameLib.EntityManager = function(
parentObject,
apiEntityManager
) {
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
this.parentObject = parentObject;
GameLib.API.EntityManager.call(
this,
entities
apiEntityManager.entities
);
this.instance = this.createInstance();
@ -21,25 +30,21 @@ GameLib.EntityManager.prototype.constructor = GameLib.EntityManager;
* @returns {*}
*/
GameLib.EntityManager.prototype.createInstance = function() {
var instance = null;
if (typeof 'require' != 'undefined') {
instance = require('tiny-ecs').EntityManager;
} else {
instance = EntityManager.EntityManager;
}
return instance;
/**
* Fuck the current ECS bullshit on the internet - both tiny-ecs and ecsjs SUCKS ASS
*/
return null;
};
/**
* Creates an GameLib.Entity
* @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);
@ -49,12 +54,10 @@ GameLib.EntityManager.prototype.createEntity = function() {
/**
* Removes an entity
* @param entity GameLib.D3.Entity
* @returns bool true if successful
* @returns boolean true if successful
*/
GameLib.EntityManager.prototype.removeEntity = function(entity) {
entity.instance.remove();
var index = this.entities.indexOf(entity);
if (index == -1) {
@ -63,90 +66,113 @@ GameLib.EntityManager.prototype.removeEntity = function(entity) {
}
this.entities.splice(index, 1);
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
* @param components GameLib.Component[]
*/
GameLib.EntityManager.prototype.queryComponents = function(components) {
return this.instance.queryComponents(components);
GameLib.EntityManager.prototype.query = function(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
* @returns {GameLib.API.Entity}
* @returns {GameLib.API.EntityManager}
*/
GameLib.EntityManager.prototype.toApiEntityManager = function() {
//TODO: refactor / fix
// var apiEntity = new GameLib.API.Entity(
// this.id,
// this.name,
// GameLib.Utils.IdArrayOrEmptyArray(this.components),
// this.position.toApiVector(),
// this.quaternion.toApiQuaternion(),
// this.scale.toApiVector(),
// GameLib.Utils.IdOrNull(this.parentScene),
// GameLib.Utils.IdOrNull(this.mesh)
// );
//
// return apiEntity;
var apiEntities = this.entities.map(
function (entity) {
return entity.toApiEntity();
}
);
var apiEntityManager = new GameLib.API.EntityManager(
apiEntities
);
return apiEntityManager;
};
/**
*
* @param graphics GameLib.D3.Graphics
* @param objectEntity Object
* @param graphics
* @param objectEntities Object
* @constructor
*/
GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntity) {
GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntities) {
//TODO: refactor /fix
// var apiEntity = new GameLib.API.Entity(
// objectEntity.id,
// objectEntity.name,
// objectEntity.components,
// new GameLib.API.Vector3(
// objectEntity.position.x,
// objectEntity.position.y,
// objectEntity.position.z
// ),
// 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
// );
var apiEntities = objectEntities.entities.map(
function (objectEntity) {
objectEntity.components = objectEntity.components.map(
function (component) {
if (component instanceof Object) {
if (component.componentType == GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING) {
return GameLib.D3.PathFollowing.FromObjectComponent(graphics, component);
} else {
console.warn('no component was associated with this object');
throw new Error('no component was associated with this object');
}
} else {
return component;
}
}
);
return new GameLib.API.Entity(
objectEntity.id,
objectEntity.name,
objectEntity.components
)
}
);
var apiEntityManager = new GameLib.API.EntityManager(
apiEntities
);
var entityManager = new GameLib.EntityManager(
this,
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) {
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
// this.components.forEach(
// function(currentValue, index, array) {

View File

@ -34,8 +34,18 @@ GameLib.Entity.prototype.constructor = GameLib.Entity;
* Creates an entity instance
*/
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() {
//TODO: refactor / fix
// var apiEntity = new GameLib.API.Entity(
// this.id,
// this.name,
// GameLib.Utils.IdArrayOrEmptyArray(this.components),
// this.position.toApiVector(),
// this.quaternion.toApiQuaternion(),
// this.scale.toApiVector(),
// GameLib.Utils.IdOrNull(this.parentScene),
// GameLib.Utils.IdOrNull(this.mesh)
// );
//
// return apiEntity;
var apiComponents = this.components.map(
function(component) {
return component.toApiComponent();
}
);
return new GameLib.API.Entity(
this.id,
this.name,
apiComponents
);
};
/**
*
* @param graphics GameLib.D3.Graphics
* @param entityManager GameLib.EntityManager
* @param objectEntity Object
* @constructor
*/
GameLib.Entity.FromObjectEntity = function(graphics, objectEntity) {
GameLib.Entity.FromObjectEntity = function(entityManager, objectEntity) {
//TODO: refactor /fix
// var apiEntity = new GameLib.API.Entity(
// objectEntity.id,
// objectEntity.name,
// objectEntity.components,
// new GameLib.API.Vector3(
// objectEntity.position.x,
// objectEntity.position.y,
// objectEntity.position.z
// ),
// 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
// );
var apiEntity = new GameLib.API.Entity(
objectEntity.id,
objectEntity.name,
objectEntity.components
);
return new GameLib.Entity(
entityManager,
this,
apiEntity
);
};