using System.Collections; using System.Collections.Generic; using UnityEngine; public class enemy : Role { public string enemyId; [Header("移动速度")] public float moveSpeed; [HideInInspector] public float harmNumber = 0; // 伤害 public float HarmNumber { get => harmNumber; set { harmNumber = value; Debug.Log(enemyId + "总伤害" + harmNumber); } } private float originalMoveSpeed; // 记录初始的移动速度 private float slowDownTime = 0f; // 记录减速开始时间 private bool isSlowed = false; // 标记是否处于减速状态 public override void Start() { base.Start(); originalMoveSpeed = moveSpeed; // 保存初始移动速度 if (camp == Camp.Enemy) { if (AnimationTree != null) { AnimationTree[0].CharacterAnimationFrameInterval = (int)(36 / moveSpeed); } //开始移动 Init(InitEnenyData.instance.GetRandomWaypoints()); } } public void Init(waypoints _waypoints) { base.Navigation.waypoints = _waypoints; //开始移动 Navigation.MoveToNextWaypoint(this.gameObject, moveSpeed); } public override void die() { base.die(); if (Application.isPlaying) { // 将当前敌人对象放入死亡池 if (SpawnPool.intance != null) { SkillBox.instance.AddExperience(6, this.gameObject.transform); } UIContorl.instance.Killnumber += 1; } } private void OnTriggerEnter2D(Collider2D collision) { // 进入范围检测 if (this.camp == Camp.Enemy && collision.tag == "House") { UIContorl.instance.Hp -= 1; Destroy(this.gameObject); } } // 减速并在指定时间后恢复原速度 public void SlowDown(float slowFactor, float duration) { if (!isSlowed) { Debug.Log("减速------------------------"); // 临时设置减速后的速度 moveSpeed *= slowFactor; // 如果有动画控制,更新动画速度 if (AnimationTree != null) { AnimationTree[0].CharacterAnimationFrameInterval = (int)(36 / moveSpeed); } Navigation.ChangeSpeed(slowFactor, duration); // 标记减速状态 isSlowed = true; // 记录减速开始时间 slowDownTime = Time.time; } } void Update() { if (isSlowed) { // 检查减速时间是否到了 if (Time.time - slowDownTime >= 3f) // 3秒后恢复原速度 { // 恢复初始速度 moveSpeed = originalMoveSpeed; // 恢复动画速度 if (AnimationTree != null) { foreach (var anim in AnimationTree) { anim.CharacterAnimationFrameInterval = (int)(36 / moveSpeed); } } // 取消减速状态 isSlowed = false; } } } }