Comunidad Game Maker

Ayuda => Preguntas y respuestas => Mensaje iniciado por: Jalealf en Marzo 01, 2021, 02:44:19 PM

Título: Path finder para IA no funciona bien (solucionado)
Publicado por: Jalealf en Marzo 01, 2021, 02:44:19 PM
Buenos días, tardes o noches

estuve preprogramando una IA con estados para simular animales con diferentes estados y que afecten a su comportamiento, cuando este busque comida la idea es que esquive paredes u otras estructuras pero al ejecutarlo este se tranca.

(aviso que lo programado es una mezcla de tutoriales diferentes que vi mas lo que entendí)

en el Obj_Animal:
evento crear:

/// @description
//Estados
enum SubjetStatus{
   Idle,
   Warden,
   Search,
}
myCurrentStatus = SubjetStatus.Idle;

counter = 0;
spd = .5;

my_dir = irandom_range(0,359);
moveX = lengthdir_x(spd, my_dir);
moveY = lengthdir_y(spd, my_dir);

//Stados
food = 100;

//Path
path = path_add()

evento Paso:
/// @description
//Contadores
food -= 0.1;
//Estados
switch(myCurrentStatus){
   case SubjetStatus.Idle:
   #region Idle
   //behavior
   counter += 1;
   // cambio de Comportamiento
   if(counter >= room_speed*3){
      var change = choose(0, 1);
      switch(change){
         case 0: myCurrentStatus = SubjetStatus.Warden;
         case 1: counter = 0;
         break;
      }
      if(food < 50){
         myCurrentStatus = SubjetStatus.Search;
      }
   }
   #endregion
   case SubjetStatus.Warden:
   #region Warden
   //Behavior
   counter += 1;
   x += moveX;
   y += moveY;
   
   //cambio de comportamiento
   if(counter >= room_speed*3){
      var change = choose(0, 1);
      switch(change){
         case 0: myCurrentStatus = SubjetStatus.Idle;
         case 1: my_dir = irandom_range(0,359);
               moveX = lengthdir_x(spd, my_dir);
               moveY = lengthdir_y(spd, my_dir);
               counter = 0;
      }
      
      //Sprite
      sprite_index = Spr_Test;
      image_xscale = sign(moveX);
      if(food < 50){
         myCurrentStatus = SubjetStatus.Search;
      }
   }
   #endregion
   case SubjetStatus.Search:
   #region Seach
   if(food < 50){
   var food_id = instance_nearest(x, y, Obj_food);
   
   positionX = food_id.x;
   positionY = food_id.y;
   
   if(mp_grid_path(global.grid, path, x, y, positionX, positionY, 1)){
      path_start(path, 4, path_action_stop, false);
   }
   }
   #endregion
}
_______________________________________________________________________________________________________________________________________________________________________

Obj_grid:
evento crear:

/// @description
//Crear la Grid
var cell_heigt = 32;
var cell_with = 32;

var colum_cells = room_width div cell_with;
var row_cells = room_height div cell_heigt;

global.grid = mp_grid_create(0, 0, colum_cells, row_cells, cell_with, cell_heigt);

//agregar las paredes
mp_grid_add_instances(global.grid, Obj_wall,false);


Gracias de antemano, cuando este listo probablemente lo publique para que los demás puedan usarlo  :)
Título: Re: Path finder para IA no funciona bien (solucionado)
Publicado por: elviskelley en Marzo 07, 2021, 03:22:06 PM
usa el que te deje adjunto...