Guest User

ember3

a guest
Dec 30th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class cPlayer {
  2.   cameraRadius: number = 10;
  3.   cameraSpeed: number = 0.3;
  4.   movementSpeed: number = 10;
  5.   cameraLength: number = 5;
  6.   cameraAngle: number = 45;
  7.   cameraTilt: number = 40;
  8.  
  9.   private cubeGameObject!: GameObject;
  10.   private mainCamera!: GameObject;
  11.   private playerObject!: GameObject;
  12.   private rigidBody!: RigidBody;
  13.  
  14.   start(): void {
  15.     this.cubeGameObject = new GameObject();
  16.     this.cubeGameObject.name = "cubeObj";
  17.  
  18.     this.mainCamera = root.getObjectWithComponent("Camera");
  19.  
  20.     this.mainCamera.transform.position = new Vec3(10, 10, -10);
  21.     this.mainCamera.transform.lookAt(new Vec3(0, 0, 0));
  22.  
  23.     this.playerObject = root.getObjectWithComponent("RigidBody");
  24.     this.rigidBody = this.playerObject.getComponentByType("RigidBody") as RigidBody;
  25.   }
  26.  
  27.   update(): void {
  28.     const cameraTarget = this.playerObject.transform.position.add(new Vec3(0, 2, 0));
  29.    
  30.     this.mainCamera.transform.position = cameraTarget.add(
  31.       calculateCameraPosition(this.cameraLength, this.cameraAngle, this.cameraTilt)
  32.     );
  33.  
  34.     this.mainCamera.transform.lookAt(cameraTarget);
  35.     let moveDirection = new Vec3(0, 0, 0);
  36.  
  37.     if (Input.Pressed.KEY_SPACE) {
  38.       this.rigidBody.addForce(new Vec3(0, 1000, 0));
  39.     }
  40.  
  41.     if (Input.Key.KEY_W) moveDirection = moveDirection.add(new Vec3(0, 0, -1));
  42.     if (Input.Key.KEY_S) moveDirection = moveDirection.add(new Vec3(0, 0, 1));
  43.     if (Input.Key.KEY_A) moveDirection = moveDirection.add(new Vec3(-1, 0, 0));
  44.     if (Input.Key.KEY_D) moveDirection = moveDirection.add(new Vec3(1, 0, 0));
  45.  
  46.     if (Input.Key.KEY_Q) this.cameraAngle += 1;
  47.     if (Input.Key.KEY_E) this.cameraAngle -= 1;
  48.  
  49.     if (Input.Mouse.Button.RIGHT) {
  50.       if (Input.Mouse.Delta.x) {
  51.         this.cameraAngle -= Input.Mouse.Delta.x / 5.0;
  52.       }
  53.  
  54.       if (Input.Mouse.Delta.y) {
  55.         this.cameraTilt += Input.Mouse.Delta.y / 5.0;
  56.         this.cameraTilt = Utils.clamp(this.cameraTilt, -40, 89);
  57.       }
  58.     }
  59.  
  60.     this.cameraAngle = Utils.wrap(this.cameraAngle, 0, 360);
  61.  
  62.     if (moveDirection.length() > 0) {
  63.       moveDirection = moveDirection.normalize();
  64.       // rotate the direction vector by the camera rotation
  65.       const x = moveDirection.x;
  66.       const z = moveDirection.z;
  67.       const rotY = (360 - this.cameraAngle) * (Math.PI / 180.0);
  68.  
  69.       const xPrime = x * Math.cos(rotY) - z * Math.sin(rotY);
  70.       const zPrime = x * Math.sin(rotY) + z * Math.cos(rotY);
  71.  
  72.       moveDirection = new Vec3(xPrime, 0, zPrime);
  73.       this.rigidBody.setVelocityXZ(moveDirection.mul(this.movementSpeed));
  74.     } else {
  75.       this.rigidBody.setVelocityXZ(new Vec3(0, 0, 0));
  76.     }
  77.   }
  78. }
  79.  
  80. function calculateCameraPosition(cameraLength: number, cameraAngle: number, cameraTilt: number): Vec3 {
  81.   const angleRad = (cameraAngle * Math.PI) / 180;
  82.   const tiltRad = (cameraTilt * Math.PI) / 180;
  83.  
  84.   const x = cameraLength * Math.cos(tiltRad) * Math.sin(angleRad);
  85.   const y = cameraLength * Math.sin(tiltRad);
  86.   const z = cameraLength * Math.cos(tiltRad) * Math.cos(angleRad);
  87.  
  88.   return new Vec3(x, y, z);
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment