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

329 lines
8.9 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;
}*/
2016-11-25 09:57:09 +01:00
// tell path following component to move forward
this.pathFollowingComponent.direction = 1;
2016-11-24 13:07:42 +01:00
2016-11-25 09:57:09 +01:00
// - - -- - - - - -- - - - - - - - - - - - - - -
// D E B U G G I N G
// - - - - - - - - - - - - - - - - - -
2016-11-24 13:07:42 +01:00
if(this.sensorVisualizer) {
2016-11-25 09:57:09 +01:00
for(var i = 0, l = this.sensors.length; i < l; ++i) {
2016-11-24 13:07:42 +01:00
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) {
2016-11-25 09:57:09 +01:00
this.sensorVisualizer.sensors[i].sensorMesh.remove(this.sensorVisualizer.sensors[i].arrow);
2016-11-24 13:07:42 +01:00
}
// create new
2016-11-25 09:57:09 +01:00
this.sensorVisualizer.sensors[i].arrow = new THREE.ArrowHelper(
new THREE.Vector3(
sensor.sensorDirection.x,
sensor.sensorDirection.y,
sensor.sensorDirection.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
)).normalize(),
new THREE.Vector3(
this.parentEntity.position.x,
this.parentEntity.position.y,
this.parentEntity.position.z
).add(new THREE.Vector3(
sensor.sensorPositionOffset.x,
sensor.sensorPositionOffset.y,
sensor.sensorPositionOffset.z
).applyQuaternion(new THREE.Quaternion(
this.parentEntity.quaternion.x,
this.parentEntity.quaternion.y,
this.parentEntity.quaternion.z,
this.parentEntity.quaternion.w
))),
sensor.sensorLength,
sensor.sensorColor
);
this.sensorVisualizer.sensors[i].sensorMesh.add(this.sensorVisualizer.sensors[i].arrow);
2016-11-24 13:07:42 +01:00
}
}
};
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;
2016-11-25 09:57:09 +01:00
var sensorColor = new THREE.Color(0, 1, 0);
2016-11-24 13:07:42 +01:00
// Create sensors
2016-11-25 09:57:09 +01:00
// Front
2016-11-24 13:07:42 +01:00
this.sensors.push
(
{
sensorLength : sensorLength,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
2016-11-25 09:57:09 +01:00
-1,
2016-11-24 13:07:42 +01:00
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
2016-11-25 09:57:09 +01:00
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
2016-11-24 13:07:42 +01:00
0
)
}
);
2016-11-25 09:57:09 +01:00
var sideSensorLengthMultiplier = 0.5;
// Absolute left
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0,
0,
1
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// Tilted left
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-0.5,
0,
0.5
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// left forward
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// Absolute Right
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
0,
0,
-1
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
-boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// right forward
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-1,
0,
0
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
-boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
// Tilted right
this.sensors.push
(
{
sensorLength : sensorLength * sideSensorLengthMultiplier,
sensorColor : sensorColor,
sensorDirection : new THREE.Vector3(
-0.5,
0,
-0.5
).normalize(),
sensorPositionOffset : new THREE.Vector3(
-boundingBox.max.x * this.parentEntity.mesh.scale.x,
boundingBox.max.y * this.parentEntity.mesh.scale.y,
-boundingBox.max.z * this.parentEntity.mesh.scale.z
)
}
);
2016-11-24 13:07:42 +01:00
// debug code
this.sensorVisualizer = {
sensors : []
};
};
//#endif