Advertisement
mainland_china

Enemy AI

Dec 17th, 2023 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Game Maker 2.29 KB | Source Code | 0 0
  1. obj_setup_pathway - place in room
  2.  
  3. ----------------
  4. //adjust if needed
  5. grid = mp_grid_create(0, 0, room_width/32, room_height/32, 32, 32);
  6.  
  7. mp_grid_add_instances(grid, obj_wall, 0);
  8.  
  9.  
  10.  
  11. obj_enemy
  12.  
  13. ---------------
  14. ~create
  15. s_emit = audio_emitter_create();
  16. max_audio = 200;
  17. drop_audio = 100;
  18.  
  19. audio_falloff_set_model(audio_falloff_linear_distance);
  20. audio_emitter_position(s_emit, obj_enemy.x, obj_enemy.y, 0);
  21. audio_emitter_falloff(s_emit, drop_audio, max_audio, 1);
  22.  
  23. state = 0;
  24.  
  25. seen = false;
  26.  
  27. fade_alpha = 0;
  28. path = path_add();
  29. seen = false;
  30. target_x = obj_player.x;
  31. target_y = obj_player.y;
  32.  
  33. xspd = 0;
  34. yspd = 0;
  35.  
  36. move_spd = 0.8;
  37.  
  38. alarm[0] = 1;
  39.  
  40. hit_play = true;
  41.  
  42. ----------------
  43. ~step
  44.  
  45. if (state == 0){
  46.     audio_play_sound_on(s_emit, snd_footstep, true, 1);
  47.    
  48.     state = 1;
  49. }
  50. audio_emitter_position(s_emit, x, y, 0);
  51.  
  52. var _pd = direction - 90;
  53. var _ad = angle_difference(image_angle, _pd);
  54. image_angle -= min(abs(_ad), 10) * sign(_ad);
  55. image_alpha = fade_alpha;
  56. if (collision_line(x, y, obj_player.x, obj_player.y, obj_wall, 1, 0)) {
  57.   fade_alpha -= 0.12;
  58.   if (fade_alpha <= 0) {
  59.       seen = false;
  60.       visible = false;
  61.       fade_alpha = 0;
  62.   }
  63. } else {
  64.   if (fade_alpha < 1) {
  65.       fade_alpha += 0.12;
  66.   }
  67.   visible = true;
  68.   seen = true;
  69. }
  70.  
  71.  
  72.  
  73. ---------------
  74. ~alarm 0
  75. path_delete(path);
  76. path = path_add();
  77.  
  78. target_x = obj_player.x;
  79. target_y = obj_player.y;
  80.  
  81. //chase state if seen or within the radius
  82. if(collision_circle(x, y, 64, obj_player, false, false)) && seen == true{
  83.     if (hit_play){
  84.         audio_sound_pitch(snd_approach, 1.1);
  85.         audio_play_sound(snd_approach, 1.3, false);
  86.         hit_play = false;
  87.     }
  88.     move_spd = 1.53
  89.    
  90.     mp_grid_path(obj_setup_pathway.grid, path, x, y, target_x, target_y, 1);
  91.     path_start(path, move_spd, path_action_stop, true);
  92.     audio_emitter_gain(s_emit, 100);
  93.     audio_emitter_pitch(s_emit, 1.45);
  94. }
  95.  
  96. //patrol mode
  97. else{
  98.     move_spd = 1.2;
  99.     mp_grid_path(obj_setup_pathway.grid, path, x, y, target_x, target_y, 1);
  100.     audio_emitter_pitch(s_emit, 1);
  101.     audio_emitter_gain(s_emit, 0.3);
  102.     path_start(path, move_spd, path_action_stop, true);
  103.     seen = false;
  104.     hit_play = true;
  105. }
  106.  
  107.  
  108. alarm_set(0, 10);
  109. --------------
  110. ~destroy
  111. audio_emitter_free(s_emit);
  112.  
  113. audio_stop_sound(snd_footstep);
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement