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

游戏

开发平台:

Visual C++

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "rayspr.h"
  4. #include "rayfile.h"
  5. #include "sprfunc.h"
  6. #include "sfvars.h"
  7. #include "defobj.h"
  8. #include "playint.h"
  9. #include "keyinfo.h"
  10. #include "sprtypes.h"
  11. #include "voxinter.h"
  12. #include "message.h"
  13. #include "classes.h"
  14. #include "quit.h"
  15. #include "inventor.h"
  16. #include "tags.h"
  17. #include "shot.h"
  18. #define GUN_HEIGHT cur_obj->type->eye_height-20
  19. #define MAX_TIME_TO_GUN 5
  20. #define MAX_FALL_PER_UPDATE 8
  21. #define NO_INPUT 0
  22. short default_input[MAX_TRACKED_INPUTS]={0,0,0,0,0,0,0};
  23. PSHORT cur_input_buffer=default_input;
  24. typedef struct PLAYER_INFO * pplayer_info;
  25. typedef struct PLAYER_INFO {
  26.    PSHORT input_buffer;
  27.    SHORT tracked_inputs[MAX_TRACKED_INPUTS];
  28.    BOOL fly_mode;
  29.    MYFIXED base_fixed_z, cur_fixed_z;
  30.    angle_type z_move_angle;
  31.    SHORT dis_to_fall;
  32.    LONG time_to_gun;
  33.    angle_type vert_angle;
  34.    pobject_node inventory;
  35. } player_info;
  36. void Setup_Player_Inputs(pplayer_info cur_extra);
  37. void Player_Update(pobject cur_obj, long update_num);
  38. void Player_Update_Z(pobject cur_obj, psector new_sec);
  39. void Player_Load(pobject cur_obj, long offset);
  40. void Z_Correct_Move(pobject cur_obj);
  41. void Reset_Player_Z(pplayer_info cur_extra);
  42. pobject Scan_Inventory(pobject_node inventory, ULONG search_class);
  43. ULONG Player_Message(pobject send_obj, pobject receive_obj, ULONG message, pdata extra_data);
  44. void Init_Player_Obj(func_index index)
  45. {
  46. update_funcs[index]=Player_Update;
  47. update_z_funcs[index]=Player_Update_Z;
  48. load_extra_funcs[index]=Player_Load;
  49. message_funcs[index]=Player_Message;
  50. }
  51. void Player_Load(pobject cur_obj, long offset)
  52. {
  53.    pplayer_info new_info;
  54.    new_info=(pplayer_info)NewPtr(sizeof(player_info));
  55.    for (short cur_input=0; cur_input < MAX_TRACKED_INPUTS; cur_input++) {
  56.       new_info->tracked_inputs[cur_input]=0;
  57.    }
  58.    new_info->fly_mode=FALSE;
  59.    new_info->base_fixed_z=cur_obj->z << SHIFT;
  60.    new_info->cur_fixed_z=new_info->base_fixed_z;
  61.    new_info->z_move_angle=ANGLE_0;
  62.    new_info->dis_to_fall=0;
  63.    new_info->input_buffer=cur_input_buffer;
  64.    new_info->vert_angle=ANGLE_0;
  65.    new_info->inventory=NULL;
  66.    cur_obj->extra_data=new_info;
  67.  
  68. }
  69. void Player_Update_Z(pobject cur_obj, psector new_sec)
  70. {
  71.    pplayer_info cur_extra=(pplayer_info)cur_obj->extra_data;
  72.    short height_dif;
  73.    if (!(new_sec->flags & VOXEL_SECTOR)) {
  74.       height_dif=cur_obj->cur_sec->floor_height-new_sec->floor_height;
  75.    } else {
  76.       height_dif=cur_obj->cur_sec->floor_height-
  77.         Get_Voxel_Alt((PUCHAR)new_sec->extra_data, cur_obj->x, cur_obj->y);
  78.    }
  79.    // if in fly mode, changing sectors has no effect on overall z, so we must alter
  80.    // z for difference in floor height
  81.    if (cur_extra->fly_mode) {
  82.       cur_obj->z+=height_dif;
  83.       cur_extra->base_fixed_z=cur_obj->z<<SHIFT;
  84.       cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  85.    } else {
  86.       if (height_dif>0) {
  87.          cur_obj->z+=height_dif;
  88.          cur_extra->base_fixed_z+=(height_dif<<SHIFT);
  89.          cur_extra->cur_fixed_z+=(height_dif<<SHIFT);
  90.          cur_extra->dis_to_fall+=height_dif;
  91.       } else {
  92.          if (cur_extra->dis_to_fall>0) {
  93.            cur_obj->z-=cur_extra->dis_to_fall;
  94.            cur_extra->base_fixed_z-=(cur_extra->dis_to_fall << SHIFT);
  95.            cur_extra->cur_fixed_z-=(cur_extra->dis_to_fall << SHIFT);
  96.            cur_extra->dis_to_fall=0;
  97.          }
  98.       }
  99.    } /* endif */
  100. }
  101. void Player_Update(pobject cur_obj, long update_num) {
  102.      MYFIXED dx, dy;
  103.      vector2 delta_vec;
  104.      SHORT amount_up, amount_down, amount_left, amount_right, amount_look_up, 
  105.         amount_look_down, raw_input, gun_shot;
  106.      pplayer_info cur_extra=(pplayer_info)cur_obj->extra_data;
  107.      Setup_Player_Inputs(cur_extra);     
  108.      amount_up=cur_extra->tracked_inputs[INDEX_UP];
  109.      amount_down=cur_extra->tracked_inputs[INDEX_DOWN];
  110.      amount_left=cur_extra->tracked_inputs[INDEX_LEFT];
  111.      amount_right=cur_extra->tracked_inputs[INDEX_RIGHT];
  112.      amount_look_up=cur_extra->tracked_inputs[LOOK_UP];
  113.      amount_look_down=cur_extra->tracked_inputs[LOOK_DOWN];
  114.      gun_shot=cur_extra->tracked_inputs[INDEX_GUN];
  115.      raw_input=cur_extra->tracked_inputs[RAW_INPUT];
  116.      cur_extra->time_to_gun--;
  117.      // do gravity for sector change
  118.      if (cur_extra->dis_to_fall>0) {
  119.         if (cur_extra->dis_to_fall>MAX_FALL_PER_UPDATE) {
  120.            cur_extra->dis_to_fall-=MAX_FALL_PER_UPDATE;
  121.            cur_obj->z-=MAX_FALL_PER_UPDATE;
  122.            cur_extra->base_fixed_z-=(MAX_FALL_PER_UPDATE << SHIFT);
  123.            cur_extra->cur_fixed_z-=(MAX_FALL_PER_UPDATE << SHIFT);
  124.         } else {
  125.            cur_obj->z-=cur_extra->dis_to_fall;
  126.            cur_extra->base_fixed_z-=(cur_extra->dis_to_fall << SHIFT);
  127.            cur_extra->cur_fixed_z-=(cur_extra->dis_to_fall << SHIFT);
  128.            cur_extra->dis_to_fall=0;
  129.         }
  130.      }
  131.         // reset deltas
  132.         dx=dy=0;
  133.         // what is user doing
  134.         if (amount_right)
  135.            {
  136.            // rotate player right
  137.            cur_obj->angle=Get_Angle_Difference(cur_obj->angle, amount_right);
  138.            } // end if right
  139.         else
  140.         if (amount_left)
  141.            {
  142.            // rotate player to left
  143.            cur_obj->angle=Get_Angle_Sum(cur_obj->angle, amount_left);
  144.            } // end if left
  145.         if (amount_up)
  146.            {
  147.            // move player along view vector foward
  148.            dx=(rcos_table[cur_obj->angle] * amount_up);
  149.            dy=(rsin_table[cur_obj->angle] * amount_up);
  150.            if (cur_extra->fly_mode) {
  151.               // move player z based on vertical view angle
  152.               cur_extra->base_fixed_z+=(tan_table[cur_extra->vert_angle]*amount_up);
  153.               cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  154.               Z_Correct_Move(cur_obj);
  155.            } else {
  156.               if (cur_obj->cur_sec->flags & VOXEL_SECTOR) {
  157.                  if (!cur_extra->fly_mode) {
  158.                     Reset_Player_Z(cur_extra);
  159.                  }
  160.               } else {
  161.                  // Adjust player z position along a sin wave to simulate walking motion
  162.                  cur_extra->z_move_angle=Get_Angle_Sum(cur_extra->z_move_angle, Z_ANGLE_INC);     
  163.                  cur_extra->cur_fixed_z=(cur_extra->base_fixed_z+(rsin_table[cur_extra->z_move_angle]*Z_MOVE_MAX));
  164.               } /* endif */
  165.            } /* endif */
  166.            } // end if up
  167.         else
  168.         if (amount_down)
  169.            {
  170.            // move player along view vector backward
  171.             dx=(-rcos_table[cur_obj->angle]* amount_down);
  172.             dy=(-rsin_table[cur_obj->angle] * amount_down);
  173.             if (cur_extra->fly_mode) {
  174.                // Move player z base on his vertical view angle
  175.                cur_extra->base_fixed_z-=(tan_table[cur_extra->vert_angle]* amount_down);
  176.                cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  177.                Z_Correct_Move(cur_obj);
  178.             } else {
  179.               if (cur_obj->cur_sec->flags & VOXEL_SECTOR) {
  180.                  if (!cur_extra->fly_mode) {
  181.                     Reset_Player_Z(cur_extra);
  182.                  }
  183.               } else {
  184.                  // Adjust player z position along a sin wave to simulate walking motion
  185.                  cur_extra->z_move_angle=Get_Angle_Difference(cur_extra->z_move_angle, Z_ANGLE_INC);     
  186.                  cur_extra->cur_fixed_z=(cur_extra->base_fixed_z+(rsin_table[cur_extra->z_move_angle]*Z_MOVE_MAX));
  187.               } /* endif */
  188.             } /* endif */
  189.            } // end if down
  190.        else
  191.           {
  192.              if (!cur_extra->fly_mode) {
  193.                 Reset_Player_Z(cur_extra);
  194.              }
  195.           }
  196.         if (gun_shot) {
  197.            if (cur_extra->time_to_gun<=0) {
  198.            Create_Object(cur_obj->x, cur_obj->y, GUN_HEIGHT+cur_obj->z, cur_obj->angle, BULLET_TYPE, cur_obj, cur_obj->team);
  199.            cur_extra->time_to_gun=MAX_TIME_TO_GUN;
  200.            }
  201.         }
  202.         // move player
  203.         delta_vec.x=dx;
  204.         delta_vec.y=dy;
  205.         Move_Object_Vec(cur_obj, &delta_vec);
  206.         // check change in vertical angle
  207.         cur_extra->vert_angle=Get_Angle_Sum(cur_extra->vert_angle, amount_look_up);
  208.         cur_extra->vert_angle=Get_Angle_Difference(cur_extra->vert_angle, amount_look_down);
  209.         switch (raw_input) {
  210.         case U_KEY:
  211.            cur_extra->base_fixed_z+=ONE;
  212.            cur_extra->cur_fixed_z+=ONE;
  213.            Z_Correct_Move(cur_obj);
  214.            break;
  215.         case D_KEY:
  216.            cur_extra->base_fixed_z-=ONE;
  217.            cur_extra->cur_fixed_z-=ONE;
  218.            Z_Correct_Move(cur_obj);
  219.            break;
  220.         case F_KEY:
  221.            cur_extra->fly_mode=(cur_extra->fly_mode == TRUE ? FALSE : TRUE);
  222.            cur_extra->z_move_angle=ANGLE_0;
  223.            cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  224.            break;
  225.         default: 
  226.            break;
  227.         }
  228.         
  229.         // test non-motion keys
  230. if ( (cur_obj->cur_sec->tag == (HOME_SECTOR+cur_obj->team)) && 
  231.       (Scan_Inventory(cur_extra->inventory, FLAG_CLASS)!=NULL) ) {
  232.          Post_Quit(WINNING_GAME);
  233. }
  234. cur_obj->z=cur_extra->cur_fixed_z>>SHIFT;
  235. }
  236. void Reset_Player_Z(pplayer_info cur_extra)
  237. {
  238.    // reset player z if they aren't moving
  239.    if ((cur_extra->z_move_angle % ANGLE_180) >= Z_ANGLE_DELTA) {
  240.       if (cur_extra->z_move_angle % ANGLE_180 < ANGLE_90) {
  241.          cur_extra->z_move_angle=
  242.             Get_Angle_Difference(cur_extra->z_move_angle, Z_ANGLE_INC);
  243.       } else {
  244.          cur_extra->z_move_angle=
  245.             Get_Angle_Sum(cur_extra->z_move_angle, Z_ANGLE_INC);
  246.       } /* endif */
  247.       cur_extra->cur_fixed_z=(cur_extra->base_fixed_z+
  248.          (rsin_table[cur_extra->z_move_angle]*Z_MOVE_MAX));
  249.    } /* endif */
  250. }
  251. void Z_Correct_Move(pobject cur_obj)
  252. {
  253.   pplayer_info cur_extra=(pplayer_info)cur_obj->extra_data;
  254.   if (cur_extra->base_fixed_z < (STEP_LENGTH<<SHIFT) ) {
  255.     cur_extra->base_fixed_z=STEP_LENGTH<<SHIFT;
  256.     cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  257.     }
  258.   long sec_height=cur_obj->cur_sec->ceil_height-cur_obj->cur_sec->floor_height;
  259.   MYFIXED max_height=(sec_height-STEP_LENGTH)<<SHIFT;
  260.   if (cur_extra->base_fixed_z > max_height) {
  261.      cur_extra->base_fixed_z=max_height;
  262.      cur_extra->cur_fixed_z=cur_extra->base_fixed_z;
  263.   }
  264. }
  265. void Setup_Player_Inputs(pplayer_info cur_extra)
  266. {
  267. memcpy(cur_extra->tracked_inputs, cur_extra->input_buffer, MAX_TRACKED_INPUTS * sizeof(short));
  268. }
  269. void Set_Player_Input_Buffer(PSHORT input_buffer)
  270. {
  271.   cur_input_buffer=input_buffer;
  272. }
  273. angle_type Get_Player_Vert_Angle(pobject cur_obj)
  274. {
  275.    return ((pplayer_info)cur_obj->extra_data)->vert_angle;
  276. }
  277. BOOL Is_Flying(pobject test_obj) {
  278.    return ((pplayer_info)test_obj->extra_data)->fly_mode;
  279. }
  280. ULONG Player_Message(pobject send_obj, pobject receive_obj, ULONG message, pdata extra_data) {
  281.    pplayer_info player_data=(pplayer_info)receive_obj->extra_data;
  282.    switch (message) {
  283.    case HIT_OBJ:
  284.    case HIT_BY_OBJ:
  285.          if ((send_obj->type->obj_class==FLAG_CLASS)&&(send_obj->team!=receive_obj->team)) {
  286.             pobject_node new_node=(pobject_node)NewPtr(sizeof(object_node));
  287.             new_node->data=send_obj;
  288.             new_node->back=NULL;
  289.             new_node->front=NULL;
  290.             OL_Push_Node(new_node, player_data->inventory);
  291.             Create_Inventory_Object(send_obj, receive_obj);
  292.             Do_Shot("flag.pcx", "groovy.wav", 20, TRUE);
  293.          }
  294.          break;
  295.    default: 
  296.       break;
  297.    } /* endswitch */
  298.    return Default_Message(send_obj, receive_obj, message, extra_data);
  299. }
  300. pobject Scan_Inventory(pobject_node inventory, ULONG search_class) {
  301.    pobject_node cur_node;
  302.    cur_node=inventory;
  303.    while (!OL_Empty_Node(cur_node)) {
  304.       if ( ((pinventory_data)cur_node->data->extra_data)->old_type->obj_class==search_class) {
  305.          return cur_node->data;
  306.       }
  307.       cur_node=OL_Next_Node(cur_node);
  308.    }
  309.    return NULL;
  310. }