import * as RE from 'rogue-engine'; import { Object3D, Vector3 } from 'three'; import WorldFPSController from './WorldFPSController.re'; export default class EnemyAI extends RE.Component { @RE.props.object3d() target: Object3D; @RE.props.num() movementSpeed: number = 0.3; @RE.props.num() attackDistance: number = 0.3; targetPosition: Vector3 = new Vector3(); awake() { if(!this.target) { let gameLogic = RE.App.currentScene.getObjectByName("GameLogic") this.target = RE.App.currentScene.getObjectByName("Player") || new Object3D() this.targetPosition = RE.getComponent(WorldFPSController, gameLogic)?.characterOffset.position || this.target.position } else { this.targetPosition = this.target.position } } start() { } update() { const deltaTime = RE.Runtime.deltaTime; this.object3d.lookAt(this.target.position) let movementVector = new Vector3(0, 0, 1) if(this.object3d.position.distanceTo(this.targetPosition) <= this.attackDistance) { movementVector.set(0,0,0) } movementVector .transformDirection(this.object3d.matrix) .multiplyScalar(this.movementSpeed * deltaTime); this.object3d.position.x += movementVector.x; this.object3d.position.z += movementVector.z; } } RE.registerComponent(EnemyAI);