⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 npcrol.java~16~

📁 J2ME游戏引擎,直接在JBUILDER2006下运行,不包含需要的SDK和虚拟机
💻 JAVA~16~
字号:
package GoGoGo;

public class npcROL
{
   //生命
   public  int NPC_hp;
   //移动速度
   public  int NPC_v;
   //环绕速度
   public  int NPC_vX=3;
   public  int NPC_vY=3;
//   boolean  direct=false;//false:正方向 true :反方向
   //AI类型 1:攻击型 2:防守型 3:跟踪性的 4:逃跑型
   public  int NPC_AI;
   //NPC种类
   public  int NPC_Sort;
   //NPC位置 X,Y
   public int  NPC_X;
   public int  NPC_Y ;
   boolean  NPC_ATT;
   boolean  NPC_DEF;
   //方向状态
   public int  Left_run=0;
   public int  right_run=1;
   public int  up_run=2;
   public int  down_run=3;
   public int  Left_stand=4;
   public int  right_stand=5;
   public int  up_stand=6;
   public int  down_stand=7;

   //NPC当前状态
   public int NPCStatic = Left_run;
//
   public npcROL( int hp,int v,int AI,int sort,int x,int y)
    {
     NPC_hp=hp;
       //移动速度
     NPC_v=v;
       //AI类型 0:攻击型 2:防守型 1:跟踪性的 3:逃跑型
     NPC_AI=AI;
       //NPC种类
     NPC_Sort=sort;
     NPC_ATT=false;
     NPC_DEF=false;
     NPCposition( x,y);

    }
    public  void NPCposition(int x, int y)
    {
     NPC_X =x;
     NPC_Y =y;
    }
    //NPC AI 接近
    public  void NPCCome_in(int player_X , int player_Y)
    {
    if( Math.abs(NPC_X -player_X )>10||Math.abs(NPC_Y-player_Y)>10)
    {
      if( NPC_X >player_X )
      {
          NPC_X = NPC_X - NPC_v;
          NPCStatic = Left_run;
      }
      else
          NPC_X= NPC_X+NPC_v;
          NPCStatic = right_run;

      if( NPC_Y>player_Y)
      {
          NPC_Y= NPC_Y- NPC_v;
          NPCStatic = up_run;
      }
      else
          NPC_Y= NPC_Y+NPC_v;
          NPCStatic = down_run;

    }
     NPC_ATT=false;
     NPC_DEF=false;
    }

    //NPCAI 远离
    public  void NPCCome_out(int player_X , int player_Y)
   {
   if( Math.abs(NPC_X -player_X )<10||Math.abs(NPC_Y-player_Y)<10)
   {
       if (NPC_X > player_X)
       {
           NPC_X = NPC_X + NPC_v;
           NPCStatic = Left_run;

       }
       else
           NPC_X = NPC_X - NPC_v;
       NPCStatic = right_run;
       if (NPC_Y > player_Y)
       {
           NPC_Y = NPC_Y + NPC_v;
           NPCStatic = down_run;

       }
       else
           NPC_Y = NPC_Y - NPC_v;
       NPCStatic = up_run;
   }
    NPC_ATT=false;
    NPC_DEF=false;
   }

    //NPCAI 站立
    public  void NPCStand()
  {
      NPC_ATT=false;
      NPC_DEF=false;
  if( Math.abs((int)(System.currentTimeMillis()*1000)%6)==1)
  {
      NPC_X = NPC_X + 1;
      NPC_Y = NPC_Y - 1;
      NPCStatic = Left_stand;
  }
  if (Math.abs( (int) (System.currentTimeMillis() * 1000) % 6) == 0)
  {
      NPC_Y = NPC_Y + 1;
      NPC_X = NPC_X - 1;
      NPCStatic = right_stand;
  }
  if (Math.abs( (int) (System.currentTimeMillis() * 1000) % 6) == 2)
  {
      NPC_Y = NPC_Y - 1;
      NPC_X = NPC_X - 1;
      NPCStatic = up_stand;
  }
  if (Math.abs( (int) (System.currentTimeMillis() * 1000) % 6) == 3)
  {
      NPC_Y = NPC_Y + 1;
      NPC_X = NPC_X + 1;
      NPCStatic = down_stand;
  }

  }
  //NPCAI 攻击
  public  void NPCACC()
   {
    NPC_ATT=true;
    NPC_DEF=false;
   }
    //NPCAI 防御
    public  void NPCDEF()
     {
      NPC_DEF=true;
      NPC_ATT=false;
     }
    //NPCAI 围绕
    public  void NPCCome_aroundi(int player_X , int player_Y, int r)
  {

    if( NPC_X>player_X && NPC_Y<player_Y)
    {
        NPC_X = NPC_X - NPC_vX;
        NPC_Y = NPC_Y - NPC_vY;
        NPCStatic = Left_run;
    }
    if (NPC_X < player_X && NPC_Y < player_Y)
    {
        NPC_X = NPC_X - NPC_vX;
        NPC_Y = NPC_Y + NPC_vY;
        NPCStatic = right_run;
    }
    if (NPC_X < player_X && NPC_Y > player_Y)
    {
        NPC_X = NPC_X + NPC_vX;
        NPC_Y = NPC_Y + NPC_vY;
        NPCStatic = down_run;
    }
    if (NPC_X > player_X && NPC_Y > player_Y)
    {
        NPC_X = NPC_X + NPC_vX;
        NPC_Y = NPC_Y - NPC_vY;
        NPCStatic = up_run;
    }
  }
  public  void Choice_NPCAI(int id ,int player_X,int player_Y)
  {
      switch (id)
      {
          case 0: //物理攻击型NPC
              if (Math.abs(NPC_X - player_X) < 45 ||
                  Math.abs(NPC_Y - player_Y) < 45)
              {
                  NPCCome_in(player_X, player_Y);
              }
              else
              {
                  NPCStand();
              }
              break;
          case 1: //魔法型NPC
              if (Math.abs(NPC_X - player_X) < 40 ||
                  Math.abs(NPC_Y - player_Y) < 40)
              {
                 NPCCome_aroundi(player_X , player_Y,40);

              }
              else if( Math.abs(NPC_X - player_X) < 70 ||
                  Math.abs(NPC_Y - player_Y) < 70)
              {
                   NPCCome_in(player_X, player_Y);
              }
              else
              {
                  NPCStand();
              }
          break;
      }
  }
  //每一方向上帧播放的序号
int Action_run_left = 0;
int Action_run_right = 0;
int Action_run_up = 0;
int Action_run_down = 0;
int Action_stand_left = 0;
int Action_stand_right = 0;
int Action_stand_up = 0;
int Action_stand_down = 0;
  int Station_start[] =
      {0, 0, 0, 0, 5, 5, 7, 7, 0, 0, 0, 0}; //左右上下(跑),左右上下(站立)
  //帧播放结束位置
  int Station_end[] =
      {10, 10, 8, 8, 5, 5, 7, 7, 4, 4, 4, 4}; //左右上下(跑),左右上下(站立)
  //帧间距
  int distance_W[] =
      {32, 32, 64, 64, 32, 32, 64, 64, 75, 75, 75, 75}; //左右上下(跑),左右上下(站立)
  //帧高度
  int distance_H[] =
      {42, 42, 38, 38, 42, 42, 38, 38, 64, 64, 64, 64}; //左右上下(跑),左右上下(站立)

  public int Animation_run(int animation)
     {

//         //动画最大数量


         int Distance = 0;
         switch (animation)
         {
             case 0: //向左跑动
                 Action_run_left++;
                 if (Action_run_left > Station_end[animation])
                 {
                     Action_run_left = Station_start[animation];
                 }
                 Distance = Action_run_left;
                 break;
             case 1: //向右跑动
                 Action_run_right++;
                 if (Action_run_right > Station_end[animation])
                 {
                     Action_run_right = Station_start[animation];
                 }
                 Distance = Action_run_right;
                 break;
             case 2:
                 Action_run_up++;
                 if (Action_run_up > Station_end[animation])
                 {
                     Action_run_up = Station_start[animation];
                 }
                 Distance = Action_run_up;

                 break;
             case 3:
                 Action_run_down++;
                 if (Action_run_down > Station_end[animation])
                 {
                     Action_run_down = Station_start[animation];
                 }
                 Distance = Action_run_down;

                 break;
             case 4:
                 Action_stand_left++;
                 if (Action_stand_left > Station_end[animation])
                 {
                     Action_stand_left = Station_start[animation];
                 }
                 Distance = Action_stand_left;
                 break;
             case 5:
                 Action_stand_right++;
                 if (Action_stand_right > Station_end[animation])
                 {
                     Action_stand_right = Station_start[animation];
                 }
                 Distance = Action_stand_right;

                 break;
             case 6:
                 Action_stand_up++;
                 if (Action_stand_up > Station_end[animation])
                 {
                     Action_stand_up = Station_start[animation];
                 }
                 Distance = Action_stand_up;
                 break;
             case 7:
                 Action_stand_down++;
                 if (Action_stand_down > Station_end[animation])
                 {
                     Action_stand_down = Station_start[animation];
                 }
                 Distance = Action_stand_down;
                 break;
         }
         return Distance * distance_W[animation];
     }
     //原图每帧宽
     public int getwidth(int animation)
     {
         return   distance_W[animation];
     }
     //原图每帧高
     public int getheight(int animation)
     {
         return   distance_H[animation];
     }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -