r3-legacy/src/game-lib-component-path-con...

128 lines
3.0 KiB
JavaScript
Raw Normal View History

/**
*
* @param id
* @param name
* @constructor
*/
GameLib.D3.ComponentPathControls = function ComponentPathFollowing(
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);
};
///////////////////////// 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);
};