2016-11-23 10:23:24 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* @param name
|
|
|
|
* @constructor
|
|
|
|
*/
|
2016-11-23 13:02:23 +01:00
|
|
|
GameLib.D3.ComponentPathControls = function ComponentPathControls(
|
2016-11-23 10:23:24 +01:00
|
|
|
id,
|
|
|
|
name
|
|
|
|
) {
|
|
|
|
this.id = id || GameLib.D3.Tools.RandomId();
|
|
|
|
|
|
|
|
if (typeof name == 'undefined') {
|
|
|
|
name = this.constructor.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.name = name;
|
|
|
|
this.parentEntity = null;
|
|
|
|
|
|
|
|
// runtime
|
|
|
|
this.pathFollowingComponent = null;
|
|
|
|
this.keyLeftPressed = false;
|
|
|
|
this.keyRightPressed = false;
|
|
|
|
this.keyForwardPressed = false;
|
|
|
|
this.keyBackPressed = false;
|
|
|
|
this.keyBreakPressed = false;
|
|
|
|
|
|
|
|
GameLib.D3.Utils.Extend(GameLib.D3.ComponentPathControls, GameLib.D3.ComponentInterface);
|
|
|
|
};
|
|
|
|
|
2016-11-23 13:02:23 +01:00
|
|
|
//#ifdef RUNTIME__
|
|
|
|
|
2016-11-23 10:23:24 +01:00
|
|
|
///////////////////////// Methods to override //////////////////////////
|
|
|
|
GameLib.D3.ComponentPathControls.prototype.onUpdate = function(
|
|
|
|
deltaTime,
|
|
|
|
parentEntity
|
|
|
|
) {
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.D3.ComponentPathControls.prototype.onSetParentEntity = function(
|
|
|
|
parentScene,
|
|
|
|
parentEntity
|
|
|
|
) {
|
|
|
|
|
|
|
|
this.pathFollowingComponent = parentEntity.getComponent(GameLib.D3.ComponentPathFollowing);
|
|
|
|
if(!this.pathFollowingComponent) {
|
|
|
|
console.error("ComponentPathControls. NO PATH FOLLOWING COMPONENT");
|
|
|
|
}
|
|
|
|
|
|
|
|
var component = this;
|
|
|
|
|
|
|
|
document.addEventListener('keydown', function(event) {
|
|
|
|
|
|
|
|
if (event.keyCode == 73) { // Forward [i]
|
|
|
|
|
|
|
|
component.keyForwardPressed = true;
|
|
|
|
|
|
|
|
} else if (event.keyCode == 75) { // Back [k]
|
|
|
|
|
|
|
|
component.keyBackPressed = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode == 74) { // Left [j]
|
|
|
|
|
|
|
|
component.keyLeftPressed = true;
|
|
|
|
|
|
|
|
} else if (event.keyCode == 76) { // Right [l]
|
|
|
|
|
|
|
|
component.keyRightPressed = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode == 66) {
|
|
|
|
|
|
|
|
component.keyBreakPressed = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
document.addEventListener('keyup', function(event) {
|
|
|
|
|
|
|
|
if (event.keyCode == 73) { // Forward [i]
|
|
|
|
|
|
|
|
component.keyForwardPressed = false;
|
|
|
|
|
|
|
|
} else if (event.keyCode == 75) { // Back [k]
|
|
|
|
|
|
|
|
component.keyBackPressed = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode == 74) { // Left [j]
|
|
|
|
|
|
|
|
component.keyLeftPressed = false;
|
|
|
|
|
|
|
|
} else if (event.keyCode == 76) { // Right [l]
|
|
|
|
|
|
|
|
component.keyRightPressed = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.keyCode == 66) {
|
|
|
|
|
|
|
|
component.keyBreakPressed = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, false);
|
2016-11-23 13:02:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//#endif
|