CameraMoveScript.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. function CameraMoveScript() {
  2. CameraMoveScript.super(this);
  3. this.lastMouseX = NaN;
  4. this.lastMouseY = NaN;
  5. this.yawPitchRoll = new Laya.Vector3();
  6. this.tempRotationZ = new Laya.Quaternion();
  7. this.isMouseDown = false;
  8. this.rotaionSpeed = 0.00006;
  9. this.mainCameraAnimation = null;
  10. this.scene = null;
  11. }
  12. Laya.class(CameraMoveScript, "CameraMoveScript", Laya.Script);
  13. CameraMoveScript.prototype._initialize = function (owner) {
  14. var _this = this;
  15. CameraMoveScript.__super.prototype._initialize.call(this,owner);
  16. Laya.stage.on("mousedown", this, this.mouseDown);
  17. Laya.stage.on("mouseup", this, this.mouseUp);
  18. Laya.stage.on("mouseout", this, this.mouseOut);
  19. _this.camera = owner;
  20. }
  21. CameraMoveScript.prototype._update = function (state) {
  22. CameraMoveScript.__super.prototype._update.call(this,state);
  23. this.updateCamera(state.elapsedTime);
  24. }
  25. CameraMoveScript.prototype.updateCamera = function (elapsedTime) {
  26. if (!isNaN(this.lastMouseX) && !isNaN(this.lastMouseY)) {
  27. var scene = this.owner.scene;
  28. Laya.KeyBoardManager.hasKeyDown(87) && this.camera.moveForward(-0.002 * elapsedTime);
  29. Laya.KeyBoardManager.hasKeyDown(83) && this.camera.moveForward(0.002 * elapsedTime);
  30. Laya.KeyBoardManager.hasKeyDown(65) && this.camera.moveRight(-0.002 * elapsedTime);
  31. Laya.KeyBoardManager.hasKeyDown(68) && this.camera.moveRight(0.002 * elapsedTime);
  32. Laya.KeyBoardManager.hasKeyDown(81) && this.camera.moveVertical(0.002 * elapsedTime);
  33. Laya.KeyBoardManager.hasKeyDown(69) && this.camera.moveVertical(-0.002 * elapsedTime);
  34. if (this.isMouseDown) {
  35. var offsetX = Laya.stage.mouseX - this.lastMouseX;
  36. var offsetY = Laya.stage.mouseY - this.lastMouseY;
  37. var yprElem = this.yawPitchRoll.elements;
  38. yprElem[0] -= offsetX * this.rotaionSpeed * elapsedTime;
  39. yprElem[1] -= offsetY * this.rotaionSpeed * elapsedTime;
  40. this.updateRotation();
  41. }
  42. }
  43. this.lastMouseX = Laya.stage.mouseX;
  44. this.lastMouseY = Laya.stage.mouseY;
  45. }
  46. CameraMoveScript.prototype.updateRotation = function () {
  47. var yprElem = this.yawPitchRoll.elements;
  48. if (Math.abs(yprElem[1]) < 1.50) {
  49. Laya.Quaternion.createFromYawPitchRoll(yprElem[0], yprElem[1], yprElem[2], this.tempRotationZ);
  50. this.camera.transform.localRotation = this.tempRotationZ;
  51. }
  52. }
  53. CameraMoveScript.prototype.mouseDown = function (e) {
  54. this.camera.transform.localRotation.getYawPitchRoll(this.yawPitchRoll);
  55. this.lastMouseX = Laya.stage.mouseX;
  56. this.lastMouseY = Laya.stage.mouseY;
  57. this.isMouseDown = true;
  58. }
  59. CameraMoveScript.prototype.mouseUp = function (e) {
  60. this.isMouseDown = false;
  61. }
  62. CameraMoveScript.prototype.mouseOut = function (e) {
  63. this.isMouseDown = false;
  64. }