FlyingAI.gml 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function enemy_flying_ai()
  2. {
  3. // 索敌
  4. var _target = noone;
  5. if instance_exists(oPlayer)
  6. if distance_to_object(oPlayer) < chase_range
  7. _target = oPlayer;
  8. // 行为逻辑
  9. if _target != noone
  10. {
  11. state = "CHASE";
  12. var _dir = point_direction(x, y, _target.x, _target.y);
  13. // 施加加速度
  14. x_spd += lengthdir_x(accel, _dir) * global.time_scale;
  15. y_spd += lengthdir_y(accel, _dir) * global.time_scale;
  16. // 面向处理
  17. facing = (x < _target.x) ? 1 : -1;
  18. }
  19. else
  20. {
  21. state = "IDLE";
  22. var _dist_to_spawn = point_distance(x, y, spawn_x, spawn_y);
  23. if (_dist_to_spawn > 5) {
  24. var _dir_home = point_direction(x, y, spawn_x, spawn_y);
  25. x_spd += lengthdir_x(accel * 0.5, _dir_home) * global.time_scale;
  26. y_spd += lengthdir_y(accel * 0.5, _dir_home) * global.time_scale;
  27. } else {
  28. // 接近出生点时,应用空气阻力让它停下
  29. x_spd = lerp(x_spd, 0, air_friction);
  30. y_spd = lerp(y_spd, 0, air_friction);
  31. }
  32. }
  33. // 3. 速度限制 (Clamp)
  34. var _current_spd = point_distance(0, 0, x_spd, y_spd);
  35. if (_current_spd > max_spd) {
  36. var _scale = max_spd / _current_spd;
  37. x_spd *= _scale;
  38. y_spd *= _scale;
  39. }
  40. // 4. 动画更新 (之前讨论的手动控制)
  41. set_sprite(sEnemyFlying_Fly);
  42. }