EnemyAI.re.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import * as RE from 'rogue-engine';
  2. import { Object3D, Vector3 } from 'three';
  3. import WorldFPSController from './WorldFPSController.re';
  4. export default class EnemyAI extends RE.Component {
  5. @RE.props.object3d() target: Object3D;
  6. @RE.props.num() movementSpeed: number = 0.3;
  7. @RE.props.num() attackDistance: number = 0.3;
  8. targetPosition: Vector3 = new Vector3();
  9. awake() {
  10. if(!this.target) {
  11. let gameLogic = RE.App.currentScene.getObjectByName("GameLogic")
  12. this.target = RE.App.currentScene.getObjectByName("Player") || new Object3D()
  13. this.targetPosition = RE.getComponent(WorldFPSController, gameLogic)?.characterOffset.position || this.target.position
  14. } else {
  15. this.targetPosition = this.target.position
  16. }
  17. }
  18. start() {
  19. }
  20. update() {
  21. const deltaTime = RE.Runtime.deltaTime;
  22. this.object3d.lookAt(this.target.position)
  23. let movementVector = new Vector3(0, 0, 1)
  24. if(this.object3d.position.distanceTo(this.targetPosition) <= this.attackDistance) {
  25. movementVector.set(0,0,0)
  26. }
  27. movementVector
  28. .transformDirection(this.object3d.matrix)
  29. .multiplyScalar(this.movementSpeed * deltaTime);
  30. this.object3d.position.x += movementVector.x;
  31. this.object3d.position.z += movementVector.z;
  32. }
  33. }
  34. RE.registerComponent(EnemyAI);