Player.re.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { Vec3 } from 'cannon-es'
  2. import * as RE from 'rogue-engine'
  3. import CannonBody from '../../rogue_packages/rogue-cannon/Components/CannonBody.re'
  4. export default class Player extends RE.Component {
  5. @RE.Prop("Number") speed = 6
  6. @RE.Prop("Number") angle = 0
  7. @RE.Prop("Number") rotationSpeed = Math.PI / 32
  8. @RE.Prop("Boolean") isThrusting = false
  9. @RE.Prop("Object3D") thrustFlames
  10. @RE.PropList("Object3D") shipTypes = []
  11. @RE.Prop("Object3D") activeShipType
  12. @RE.Prop("Color") shipColor
  13. @RE.Prop("Material") shipColorMaterial
  14. @RE.Prop("Audio") thrustSound
  15. awake() {
  16. // RE.Input.mouse.lock();
  17. }
  18. start() {
  19. this.bodyComponent = RE.getComponent(CannonBody, this.object3d)
  20. this.thrustFlames = this.object3d.getObjectByName("ThrustFlames")
  21. this.thrustFlames.visible = false
  22. this.targetAngle = this.angle
  23. this.refreshShipDisplay()
  24. window.addEventListener('keydown', event => { if(["Tab"].includes(event.code)) event.preventDefault() })
  25. // THREE.AudioContext.getContext().resume()
  26. this.thrustSound.setVolume(0)
  27. this.thrustSound.setLoop(true)
  28. this.thrustSound.play()
  29. }
  30. refreshShipDisplay() {
  31. this.shipTypes.forEach(obj3d => {
  32. obj3d.visible = false
  33. })
  34. this.activeShipType.visible = true
  35. this.shipColorMaterial.color = this.shipColor
  36. }
  37. worldInputControl() {
  38. let keysDown = false
  39. if(RE.Input.keyboard.getKeyPressed("KeyW")) {
  40. keysDown = true
  41. this.targetAngle = Math.PI / 2
  42. if(RE.Input.keyboard.getKeyPressed("KeyA")) {
  43. this.targetAngle = 3 * Math.PI / 4
  44. } else if (RE.Input.keyboard.getKeyPressed("KeyD")) {
  45. this.targetAngle = Math.PI / 4
  46. }
  47. } else if(RE.Input.keyboard.getKeyPressed("KeyS")) {
  48. keysDown = true
  49. this.targetAngle = -Math.PI / 2
  50. if(RE.Input.keyboard.getKeyPressed("KeyA")) {
  51. this.targetAngle = -3 * Math.PI / 4
  52. } else if (RE.Input.keyboard.getKeyPressed("KeyD")) {
  53. this.targetAngle = -Math.PI / 4
  54. }
  55. } else if(RE.Input.keyboard.getKeyPressed("KeyA")) {
  56. keysDown = true
  57. this.targetAngle = Math.PI
  58. } else if(RE.Input.keyboard.getKeyPressed("KeyD")) {
  59. keysDown = true
  60. this.targetAngle = 0
  61. }
  62. this.bodyComponent.body.quaternion.setFromAxisAngle(new Vec3(0,1,0), this.angle)
  63. if(keysDown) {
  64. if(this.targetAngle != this.angle) {
  65. this.rotateTowardsTarget()
  66. }
  67. this.thrust()
  68. }
  69. }
  70. thrust() {
  71. this.isThrusting = true
  72. let modelOffset = Math.PI
  73. this.bodyComponent.body.force.x = this.speed * Math.sin(this.angle + modelOffset)
  74. this.bodyComponent.body.force.z = this.speed * Math.cos(this.angle + modelOffset)
  75. }
  76. shipInputControl() {
  77. let keyControlled = false
  78. if(RE.Input.keyboard.getKeyPressed("KeyA") || RE.Input.keyboard.getKeyPressed("ArrowLeft")) {
  79. this.angle += this.rotationSpeed
  80. this.targetAngle = this.angle
  81. keyControlled = true
  82. }
  83. if(RE.Input.keyboard.getKeyPressed("KeyD") || RE.Input.keyboard.getKeyPressed("ArrowRight")) {
  84. this.angle -= this.rotationSpeed
  85. this.targetAngle = this.angle
  86. keyControlled = true
  87. }
  88. if(!keyControlled && this.targetAngle != this.angle) {
  89. this.rotateTowardsTarget()
  90. }
  91. if(RE.Input.keyboard.getKeyPressed("KeyW") || RE.Input.keyboard.getKeyPressed("ArrowUp") || RE.Input.mouse.getButtonPressed(0) ) {
  92. this.thrust()
  93. }
  94. if(this.bodyComponent.body) {
  95. this.bodyComponent.body.quaternion.setFromAxisAngle(new Vec3(0,1,0), this.angle)
  96. }
  97. }
  98. rotateTowardsTarget() {
  99. let delta = this.targetAngle - this.angle
  100. if(Math.abs(delta) > Math.PI) {
  101. this.angle += Math.sign(delta) * (2 * Math.PI)
  102. }
  103. if(Math.abs(delta) < this.rotationSpeed ) {
  104. this.angle += delta;
  105. } else {
  106. this.angle += Math.sign(delta) * this.rotationSpeed
  107. }
  108. }
  109. update() {
  110. if(RE.Input.keyboard.getKeyDown("Tab") || RE.Input.keyboard.getKeyDown("Digit1")) {
  111. let index = this.shipTypes.indexOf(this.activeShipType) + 1
  112. this.activeShipType = this.shipTypes[index % this.shipTypes.length]
  113. this.refreshShipDisplay()
  114. }
  115. this.isThrusting = false
  116. // this.worldInputControl()
  117. this.shipInputControl()
  118. this.thrustFlames.visible = this.isThrusting
  119. if (this.isThrusting && this.thrustSound && this.thrustSound.context.state === "running") {
  120. this.thrustSound.setVolume(1)
  121. } else {
  122. this.thrustSound.setVolume(0)
  123. }
  124. // this.bodyComponent.body.quaternion.setFromAxisAngle(new Vec3(0,0,1), 0.5 * Math.sin(0.01 * new Date().getTime()))
  125. // this.bodyComponent.body.position.y = 0.4 * Math.sin(0.001 * new Date().getTime())
  126. }
  127. }
  128. RE.registerComponent(Player)