r3-legacy/src/game-lib-component-path-ai.js

158 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-11-24 13:07:42 +01:00
/**
*
* @param id
* @param name
* @param sensorLength
* @constructor
*/
GameLib.D3.ComponentPathAI = function ComponentPathAI(
id,
name,
sensorLength
) {
this.id = id || GameLib.D3.Tools.RandomId();
if (typeof name == 'undefined') {
name = this.constructor.name;
}
this.name = name;
this.parentEntity = null;
this.sensorLength = sensorLength || 5;
GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathAI, GameLib.D3.ComponentInterface);
//#ifdef RUNTIME__
this.sensors = [];
//#endif
};
//#ifdef RUNTIME__
///////////////////////// Methods to override //////////////////////////
GameLib.D3.ComponentPathAI.prototype.onUpdate = function(
deltaTime,
parentEntity
) {
var forward = false;
var backward = false;
var left = false;
var right = false;
/*if (this.keyForwardPressed) { // Forward [i]
this.pathFollowingComponent.direction = 1;
} else if (this.keyBackPressed){
this.pathFollowingComponent.direction = -1;
} else {
this.pathFollowingComponent.direction = 0;
}
// left right
if (this.keyLeftPressed) { // Left [j]
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = 1;
} else if (this.keyRightPressed) { // Right [l]
this.pathFollowingComponent.offset.x = 0;
this.pathFollowingComponent.offset.y = 0;
this.pathFollowingComponent.offset.z = -1;
}*/
if(this.sensorVisualizer) {
for(var i = 0, i = this.sensors.length; i < l; ++i) {
var sensor = this.sensors[i];
if(!this.sensorVisualizer.sensors[i]) {
var emptyGeometry = new THREE.Geometry();
var sensorMesh = new THREE.Mesh(
emptyGeometry,
new THREE.MeshBasicMaterial(
{
color : sensor.sensorColor,
wireframe : true
}
)
);
this.sensorVisualizer.sensors[i] = {
sensorMesh : sensorMesh
};
sys.game.scenes["MainScene"].instance.add(this.sensorVisualizer.sensors[i].sensorMesh);
}
if(this.sensorVisualizer.sensors[i].arrow) {
this.sensorVisualizer.sensors[s].sensorMesh.remove(this.sensorVisualizer.sensors[i].arrow);
}
// create new
}
}
};
GameLib.D3.ComponentPathAI.prototype.onSetParentEntity = function(
parentScene,
parentEntity
) {
this.parentEntity = parentEntity;
this.pathFollowingComponent = parentEntity.getComponent(GameLib.D3.ComponentPathFollowing);
if(!this.pathFollowingComponent) {
console.error("ComponentPathAI. NO PATH FOLLOWING COMPONENT");
}
// Compute bounding box
if(!this.parentEntity.mesh.geometry.boundingBox) {
this.parentEntity.mesh.geometry.computeBoundingBox();
}
var boundingBox = this.parentEntity.mesh.geometry.boundingBox;
var sensorLength = this.sensorLength;
var sensorColor = new THREE.Color(1, 0, 0);
// Create sensors
this.sensors.push
(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.z * this.parentEntity.mesh.scale.z,
0
)
}
);
// debug code
this.sensorVisualizer = {
sensors : []
};
};
//#endif