MONSTER.CPP
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:6k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "rayspr.h"
  4. #include "sprfunc.h"
  5. #include "sfvars.h"
  6. #include "sprinter.h"
  7. #include "fixed.h"
  8. #include "rayfile.h"
  9. #include "message.h"
  10. #include "classes.h"
  11. #include "defobj.h"
  12. #include "getangle.h"
  13. #include "visible.h"
  14. #include "abs.h"
  15. #define MAX_TIME_TO_LOSE_INTEREST 500
  16. #define GUN_SPEED 50
  17. #define MIN_OBJ_DIS (15*ONE)
  18. #define ANIM_SPEED 31
  19. #define ANIM_SHIFT 4
  20. #define GUN_HEIGHT (cur_object->type->eye_height-10)
  21. typedef struct MONSTER_DATA * pmonster_data;
  22. typedef struct MONSTER_DATA {
  23.    pobject cur_target;
  24.    pvector2 patrol;
  25.    short patrol_count;
  26.    short time_to_lose_interest;
  27.    short time_to_bullet;
  28.    BOOL need_new_point;
  29.    short cur_point;
  30. } monster_data;
  31. void Monster_Create(pobject cur_object, long extra_data_offset);
  32. void Monster_Update(pobject cur_object, long update_num);
  33. ULONG Monster_Message(pobject send_obj, pobject receive_obj, ULONG message, pdata extra_data);
  34. void Init_Monsters(func_index index) {
  35.    update_funcs[index]=Monster_Update;
  36.    load_extra_funcs[index]=Monster_Create;
  37.    message_funcs[index]=Monster_Message;
  38. }
  39. void Monster_Create(pobject cur_object, long extra_data_offset)
  40. {
  41.    pmonster_data new_data;               
  42.    new_data=(pmonster_data)NewPtr(sizeof(monster_data));
  43.  
  44.    if (extra_data_offset==BAD_LOAD_OFFSET) {
  45.       new_data->patrol_count=0;
  46.       new_data->patrol=NULL;
  47.    } else {                
  48.       F_Seek(extra_data_offset);
  49.       F_Get_Short(new_data->patrol_count);
  50.       new_data->patrol=(pvector2)NewPtr(new_data->patrol_count * sizeof(vector2));
  51.       for (short cur_point=0; cur_point<new_data->patrol_count; cur_point++) {
  52.          F_Get_Long(new_data->patrol[cur_point].x);
  53.          F_Get_Long(new_data->patrol[cur_point].y);
  54.       } /* endfor */
  55.    }
  56.    new_data->cur_point=0;
  57.    new_data->need_new_point=TRUE;
  58.    new_data->cur_target=NULL;
  59.    cur_object->extra_data=(pdata)new_data;
  60. }
  61. void Monster_Update(pobject cur_object, long update_num) {
  62.    vector2 move_vec;
  63.    pmonster_data obj_data=(pmonster_data)cur_object->extra_data;
  64.    if (obj_data->cur_target!=NULL) {
  65.       cur_object->angle=Get_Angle_Towards(obj_data->cur_target->x-cur_object->x,
  66.                                                                 obj_data->cur_target->y-cur_object->y);
  67.       if (obj_data->time_to_bullet==0) {
  68.          Create_Object(cur_object->x, cur_object->y, GUN_HEIGHT, 
  69.             cur_object->angle, BULLET_TYPE, cur_object, cur_object->team);
  70.          obj_data->time_to_bullet=GUN_SPEED;
  71.       } else {
  72.          obj_data->time_to_bullet--;
  73.       }                          
  74.       if ( (ABS(obj_data->cur_target->x-cur_object->x) > MIN_OBJ_DIS) ||
  75.            (ABS(obj_data->cur_target->y-cur_object->y) > MIN_OBJ_DIS) ) {
  76.          move_vec.x=cur_object->type->stats.base_speed*rcos_table[cur_object->angle];
  77.          move_vec.y=cur_object->type->stats.base_speed*rsin_table[cur_object->angle];
  78.          Move_Object_Vec(cur_object, &move_vec);
  79.          cur_object->cur_frame=(update_num & ANIM_SPEED)>>ANIM_SHIFT;
  80.       }
  81.       if ((obj_data->time_to_bullet<4) || (obj_data->time_to_bullet>(GUN_SPEED-4)) ) {
  82.          cur_object->cur_frame=4;
  83.       }
  84.       if (--obj_data->time_to_lose_interest==0) {
  85.          Release_Object_Messages(obj_data->cur_target, cur_object);
  86.          obj_data->cur_target=NULL;
  87.       }
  88.    } else {
  89.       if (obj_data->patrol_count!=0) {
  90.                           
  91.       if (obj_data->need_new_point) {
  92.          if ((++obj_data->cur_point)>=obj_data->patrol_count) {
  93.             obj_data->cur_point=0;
  94.          }
  95.                                  
  96.          cur_object->angle=Get_Angle_Towards(obj_data->patrol[obj_data->cur_point].x-
  97.                 cur_object->x, obj_data->patrol[obj_data->cur_point].y-cur_object->y);
  98.          obj_data->need_new_point=FALSE;
  99.          }
  100.       move_vec.x=cur_object->type->stats.base_speed*rsin_table[cur_object->angle];
  101.       move_vec.y=cur_object->type->stats.base_speed*rcos_table[cur_object->angle];
  102.       
  103.       if ((obj_data->patrol[obj_data->cur_point].x-cur_object->x)<=move_vec.x) {
  104.          move_vec.x=obj_data->patrol[obj_data->cur_point].x-cur_object->x;
  105.          move_vec.y=obj_data->patrol[obj_data->cur_point].y-cur_object->y;
  106.          obj_data->need_new_point=TRUE;
  107.       } /* endif */
  108.       if (Move_Object_Vec(cur_object, &move_vec)) {
  109.          obj_data->need_new_point=TRUE;
  110.       }
  111.       cur_object->cur_frame=(update_num & ANIM_SPEED)>>ANIM_SHIFT;
  112.       }
  113.       obj_data->cur_target=Check_For_Visible_Enemies(cur_object);
  114.       if (obj_data->cur_target!=NULL) {
  115.          obj_data->time_to_bullet=GUN_SPEED;
  116.          obj_data->time_to_lose_interest=MAX_TIME_TO_LOSE_INTEREST;
  117.          Request_Object_Messages(obj_data->cur_target, cur_object);
  118.       }
  119.    }
  120. }
  121. ULONG Monster_Message(pobject send_obj, pobject receive_obj, ULONG message, pdata extra_data) {
  122.    pmonster_data obj_data=(pmonster_data)receive_obj->extra_data;
  123.    switch (message) {
  124.       case KILLED_MESSAGE:
  125.          if (send_obj==(obj_data->cur_target))
  126.             obj_data->cur_target=NULL;
  127.          return NORMAL_MESSAGE;
  128.       case HIT_OBJ:
  129.       case HIT_BY_OBJ:
  130.          if ((send_obj->type->obj_class==BULLET_CLASS)&&(send_obj->owner!=NULL)) {
  131.             if (obj_data->cur_target!=NULL)
  132.                Release_Object_Messages(obj_data->cur_target, receive_obj);
  133.             Request_Object_Messages(send_obj->owner, receive_obj);
  134.             obj_data->time_to_bullet=GUN_SPEED;
  135.             obj_data->time_to_lose_interest=MAX_TIME_TO_LOSE_INTEREST;
  136.             obj_data->cur_target=send_obj->owner;
  137.             return NORMAL_MESSAGE;
  138.          }
  139.          break;
  140.       default: 
  141.          return Default_Message(send_obj, receive_obj, message, extra_data);
  142.    } /* end switch */
  143. return Default_Message(send_obj, receive_obj, message, extra_data);  
  144. }