2016-11-28 15:05:02 +01:00
|
|
|
/**
|
2017-01-06 16:53:53 +01:00
|
|
|
* Runtime Entity
|
|
|
|
* @param graphics GameLib.D3.Graphics
|
2017-01-05 19:34:28 +01:00
|
|
|
* @param apiEntity GameLib.D3.API.Entity
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-01-06 16:53:53 +01:00
|
|
|
GameLib.Entity = function (
|
|
|
|
graphics,
|
2016-12-09 20:32:09 +01:00
|
|
|
apiEntity
|
2016-11-28 15:05:02 +01:00
|
|
|
) {
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
2016-12-09 20:32:09 +01:00
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiEntity)) {
|
|
|
|
apiEntity = {};
|
|
|
|
}
|
|
|
|
|
2017-05-11 04:48:02 +02:00
|
|
|
if (apiEntity instanceof GameLib.Entity) {
|
|
|
|
return apiEntity;
|
|
|
|
}
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.API.Entity.call(
|
2016-12-09 20:32:09 +01:00
|
|
|
this,
|
2016-12-15 14:53:39 +01:00
|
|
|
apiEntity.id,
|
|
|
|
apiEntity.name,
|
|
|
|
apiEntity.components
|
2016-12-09 20:32:09 +01:00
|
|
|
);
|
|
|
|
|
2017-01-19 17:50:11 +01:00
|
|
|
this.componentToCreate = 0;
|
|
|
|
|
2017-01-05 19:34:28 +01:00
|
|
|
this.components = this.components.map(
|
2017-01-06 16:53:53 +01:00
|
|
|
|
|
|
|
function (apiComponent) {
|
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.PathFollowing) {
|
|
|
|
return new GameLib.D3.PathFollowing(this.graphics, apiComponent);
|
2017-01-05 19:34:28 +01:00
|
|
|
}
|
2017-01-06 16:53:53 +01:00
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.Renderer) {
|
2017-01-10 17:04:30 +01:00
|
|
|
return new GameLib.D3.Renderer(this.graphics, apiComponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.RenderTarget) {
|
|
|
|
return new GameLib.D3.RenderTarget(this.graphics, apiComponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.Pass) {
|
|
|
|
return new GameLib.D3.Pass(this.graphics, apiComponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.Composer) {
|
|
|
|
return new GameLib.D3.Composer(this.graphics, apiComponent);
|
2017-01-09 15:20:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.Camera) {
|
2017-01-10 17:04:30 +01:00
|
|
|
return new GameLib.D3.Camera(this.graphics, apiComponent);
|
2017-01-06 16:53:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.LookAt) {
|
|
|
|
return new GameLib.D3.LookAt(this.graphics, apiComponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.Follow) {
|
|
|
|
return new GameLib.D3.Follow(this.graphics, apiComponent);
|
|
|
|
}
|
|
|
|
|
2017-02-21 18:55:18 +01:00
|
|
|
if (apiComponent instanceof GameLib.D3.API.Input.Editor) {
|
|
|
|
return new GameLib.D3.Input.Editor(this.graphics, apiComponent);
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
if (apiComponent instanceof GameLib.D3.API.Input.Drive) {
|
|
|
|
return new GameLib.D3.Input.Drive(this.graphics, apiComponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiComponent instanceof GameLib.D3.API.Spline) {
|
|
|
|
return new GameLib.D3.Spline(this.graphics, apiComponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiComponent;
|
|
|
|
|
2017-01-05 19:34:28 +01:00
|
|
|
}.bind(this)
|
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.instance = this.createInstance();
|
2016-12-12 17:24:05 +01:00
|
|
|
};
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.Entity.prototype = Object.create(GameLib.API.Entity.prototype);
|
|
|
|
GameLib.Entity.prototype.constructor = GameLib.Entity;
|
|
|
|
|
2016-12-12 17:24:05 +01:00
|
|
|
/**
|
2016-12-15 14:53:39 +01:00
|
|
|
* Creates an entity instance
|
2016-12-12 17:24:05 +01:00
|
|
|
*/
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.Entity.prototype.createInstance = function() {
|
2016-12-16 16:03:48 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-05-11 13:30:20 +02:00
|
|
|
GameLib.Entity.prototype.addComponent = function(component) {
|
|
|
|
this.components.push(component);
|
|
|
|
component.parentEntity = this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all components of type 'constructor'
|
|
|
|
* @param constructor
|
|
|
|
*/
|
|
|
|
GameLib.Entity.prototype.getComponent = function(constructor) {
|
|
|
|
|
|
|
|
var components = this.components.reduce(
|
|
|
|
function(result, component) {
|
|
|
|
if (component instanceof constructor) {
|
|
|
|
result.push(component);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
return components;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first component or null
|
|
|
|
* @param constructor
|
|
|
|
*/
|
|
|
|
GameLib.Entity.prototype.getFirstComponent = function(constructor) {
|
|
|
|
|
|
|
|
var components = this.getComponent(constructor);
|
2017-01-19 17:50:11 +01:00
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
if (components.length > 0) {
|
|
|
|
return components[0];
|
2017-01-19 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true when this entity has a certain component, false otherwise
|
|
|
|
* @param constructor
|
|
|
|
*/
|
|
|
|
GameLib.Entity.prototype.hasComponent = function(constructor) {
|
|
|
|
|
|
|
|
var has = this.components.reduce(
|
|
|
|
function(result, component) {
|
|
|
|
if (component instanceof constructor) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
return has;
|
2017-01-02 17:05:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param component
|
|
|
|
*/
|
|
|
|
GameLib.Entity.prototype.removeComponent = function(component) {
|
|
|
|
|
|
|
|
component.parentEntity = null;
|
|
|
|
|
|
|
|
var index = this.components.indexOf(component);
|
|
|
|
|
2017-05-11 13:30:20 +02:00
|
|
|
if (index === -1) {
|
2017-01-02 17:05:40 +01:00
|
|
|
console.log('failed to remove component : ', component);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.components.splice(index, 1);
|
|
|
|
return true;
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-12-15 14:53:39 +01:00
|
|
|
* Updates an entity instance
|
2016-12-09 20:32:09 +01:00
|
|
|
*/
|
2016-12-15 14:53:39 +01:00
|
|
|
GameLib.Entity.prototype.updateInstance = function() {
|
|
|
|
this.instance = this.createInstance(true);
|
|
|
|
};
|
2016-12-12 17:24:05 +01:00
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
/**
|
|
|
|
* Converts a GameLib.Entity to GameLib.API.Entity
|
|
|
|
* @returns {GameLib.API.Entity}
|
|
|
|
*/
|
|
|
|
GameLib.Entity.prototype.toApiEntity = function() {
|
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
var apiComponents = this.components.map(
|
|
|
|
function(component) {
|
|
|
|
return component.toApiComponent();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return new GameLib.API.Entity(
|
|
|
|
this.id,
|
|
|
|
this.name,
|
|
|
|
apiComponents
|
|
|
|
);
|
|
|
|
|
2016-12-09 20:32:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2017-01-06 16:53:53 +01:00
|
|
|
* @param graphics GameLib.D3.Graphics
|
2016-12-09 20:32:09 +01:00
|
|
|
* @param objectEntity Object
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-01-06 16:53:53 +01:00
|
|
|
GameLib.Entity.FromObjectEntity = function(graphics, objectEntity) {
|
2016-12-16 16:03:48 +01:00
|
|
|
|
2017-01-05 19:34:28 +01:00
|
|
|
var apiEntity = GameLib.API.Entity.FromObjectEntity(objectEntity);
|
2016-12-15 14:53:39 +01:00
|
|
|
|
2016-12-16 16:03:48 +01:00
|
|
|
return new GameLib.Entity(
|
2017-01-06 16:53:53 +01:00
|
|
|
graphics,
|
2016-12-16 16:03:48 +01:00
|
|
|
apiEntity
|
|
|
|
);
|
2016-11-01 09:43:36 +01:00
|
|
|
};
|