import { _decorator, Component, Node } from 'cc'; import { EATTACK_TYPE, EMOVE_MODE, ENEMY_TYPE, GameDefine } from '../../config/GameDefine'; import { Vec3 } from 'cc'; import { UmUtil } from '../../../../cc-common/cc-util/UmUtil'; import { GameGlobalData } from '../../global/GameGlobalData'; import { UmLog } from '../../../../cc-common/cc-util/UmLog'; import { random } from 'cc'; const { ccclass, property } = _decorator; @ccclass('EnemyMove') export class EnemyMove extends Component { moveMode: number = EMOVE_MODE.FREE; nextDestinationMove: Vec3 = new Vec3(); directionMove: Vec3 = new Vec3(); speed: number = 50; isContactObstacle = false; isSensorHero = false; isContactHero = false; heroTarget = null; enemyType: number = ENEMY_TYPE.MELEE; attackType: number = EATTACK_TYPE.MELEE; moveSpeed = 1; speedScale = 20; setMoveData(data, enemyType, attackType) { this.enemyType = enemyType; this.attackType = attackType; this.moveSpeed = data?.MoveSpeed || this.moveSpeed; this.moveSpeed *= this.speedScale; this.speed = this.moveSpeed; } protected start(): void { this.makeNewRandomTarget(); } setIsContactObstacle(isContact) { // UmLog.log("[CreepMove] => setIsContactObstacle ", isContact); this.isContactObstacle = isContact; } setIsSensorHero(isContact, heroTarget = null) { this.isSensorHero = isContact; this.heroTarget = heroTarget; this.speed = this.isSensorHero ? this.moveSpeed * 1.5 : this.moveSpeed; if (isContact && this.heroTarget) { // UmLog.log("setIsSensorHero attackType = ", this.attackType); if (this.attackType == EATTACK_TYPE.MELEE) this.changeMoveMode(EMOVE_MODE.TARGET); if (this.attackType == EATTACK_TYPE.TARGET) this.changeMoveMode(EMOVE_MODE.TARGET); } else { this.changeMoveMode(EMOVE_MODE.FREE); } } setIsContactHero(isContact) { // UmLog.log("setIsContactHero => ", isContact); this.isContactHero = isContact; if (isContact) { this.changeMoveMode(EMOVE_MODE.STATIC); } else { if (this.isSensorHero && this.heroTarget) {// && this.attackType == EATTACK_TYPE.MELEE this.changeMoveMode(EMOVE_MODE.TARGET); } else { this.changeMoveMode(EMOVE_MODE.FREE); } } } makeNewRandomTarget() { let x = this.getRandomMove(); let y = this.getRandomMove(); // UmLog.log("makeNewRandomTarget => ", x, " | ", y); this.nextDestinationMove = UmUtil.plusTwoVector3(this.node.position, new Vec3(x, y, 0)); // UmLog.log("distance => ", Vec3.distance(this.node.position, this.nextDestination)); this.directionMove = UmUtil.subtractTwoVector3(this.nextDestinationMove, this.node.position); this.directionMove.normalize(); } getRandomMove() { var randomRange = 150; return randomRange - UmUtil.getRandomInt(0, randomRange * 2); } protected update(dt: number): void { if (!GameGlobalData.Instance.isStatePlay()) return; this.updateMove(dt); } protected updateMove(dt: number): void { if (this.moveMode == EMOVE_MODE.FREE) { this.node.position = this.getMoveTowardPoint(this.nextDestinationMove, dt); if ((this.isContactObstacle && !this.isContactHero) || Vec3.distance(this.node.position, this.nextDestinationMove) < 1) this.makeNewRandomTarget(); return; } if (this.moveMode == EMOVE_MODE.TARGET && this.heroTarget) { this.node.position = this.getMoveTowardPoint(this.heroTarget.position, dt); return; } } getMoveTowardPoint(target, dt: number) { var result = new Vec3(); Vec3.moveTowards(result, this.node.position, target, this.speed * dt); return result; } changeMoveMode(newMode) { UmLog.log("changeMode => ", newMode); this.moveMode = newMode; } }