super-hero/assets/cc-game/scripts/game_play/creep/CreepMove.ts

134 lines
4.1 KiB
TypeScript

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';
const { ccclass, property } = _decorator;
@ccclass('CreepMove')
export class CreepMove extends Component {
moveMode: number = EMOVE_MODE.RANDOM;
nextDestination: Vec3 = new Vec3();
direction: Vec3 = new Vec3();
speed: number = 50;
isContactObstacle = false;
isSensorHero = false;
isContactHero = false;
heroTarget = null;
enemyType: number = ENEMY_TYPE.MELEE;
attackType: number = EATTACK_TYPE.MELEE;
dmg = 5;
moveSpeed = 1;
speedScale = 15;
range = 5;
setMoveData(data, enemyType, attackType) {
this.enemyType = enemyType;
this.attackType = attackType;
this.dmg = data?.Atk || this.dmg;
this.range = data?.AtkRange || this.range;
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.changeMode(EMOVE_MODE.TARGET);
if (this.attackType == EATTACK_TYPE.RANGE)
this.changeMode(EMOVE_MODE.TARGET);
}
else {
this.changeMode(EMOVE_MODE.RANDOM);
}
}
setIsContactHero(isContact) {
// UmLog.log("setIsContactHero => ", isContact);
this.isContactHero = isContact;
if (isContact) {
this.changeMode(EMOVE_MODE.STATIC);
}
else {
if (this.isSensorHero && this.heroTarget) {// && this.attackType == EATTACK_TYPE.MELEE
this.changeMode(EMOVE_MODE.TARGET);
}
else {
this.changeMode(EMOVE_MODE.RANDOM);
}
}
}
makeNewRandomTarget()
{
let x = 100 - UmUtil.getRandomInt(0, 200);
let y = 100 - UmUtil.getRandomInt(0, 200);
// UmLog.log("makeNewRandomTarget => ", x, " | ", y);
this.nextDestination = UmUtil.plusTwoVector3(this.node.position, new Vec3(x, y, 0));
// UmLog.log("distance => ", Vec3.distance(this.node.position, this.nextDestination));
this.direction = UmUtil.subtractTwoVector3(this.nextDestination, this.node.position);
this.direction.normalize();
}
protected update(dt: number): void {
if (!GameGlobalData.Instance.isStatePlay()) return;
if (this.moveMode == EMOVE_MODE.RANDOM)
{
this.node.position = this.getMoveTowardPoint(this.nextDestination, dt);
if ((this.isContactObstacle && !this.isContactHero) || Vec3.distance(this.node.position, this.nextDestination) < 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;
}
changeMode(newMode)
{
UmLog.log("changeMode => ", newMode);
this.moveMode = newMode;
}
getSpeed(dt) {
return 0.1;
return dt * GameDefine.SPEED_TIME_UNIT;
}
}