Input.cpp
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:10k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Input.h"
  3. CInput::CInput(HINSTANCE hInstance,HWND hwnd,int nIcon)
  4. {   
  5. NIcon=nIcon;
  6.     mygod=new bool [NIcon];
  7.     motherfuck=new bool[NIcon];
  8. for(int i=0;i<3;i++)
  9. {
  10. mygod[i]=false;
  11. motherfuck[i]=false;
  12. }
  13. control=true;
  14. m_hInst=hInstance;
  15.     m_hWnd=hwnd;
  16. if(DI_OK!=DirectInput8Create(m_hInst,DIRECTINPUT_VERSION,IID_IDirectInput8,(LPVOID*)&m_pInput,NULL))
  17. {
  18. return ;
  19. }
  20. if(DI_OK!=m_pInput->CreateDevice(GUID_SysKeyboard,&m_pInputDevice,NULL))
  21. {
  22.        return ;
  23. }
  24. if(DI_OK!=m_pInputDevice->SetDataFormat(&c_dfDIKeyboard))
  25. {
  26.        return ;
  27. }
  28. m_pInputDevice->SetCooperativeLevel(m_hWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
  29. m_pInputDevice->Acquire();
  30. memset(&m_cKeyboardState,0,sizeof(m_cKeyboardState));
  31. memset(&m_cKeyboardStateOld,0,sizeof(m_cKeyboardStateOld));
  32. if(DI_OK!=m_pInput->CreateDevice(GUID_SysMouse,&m_pMouseInput,NULL))
  33. {
  34.        return ;
  35. }
  36. if(DI_OK!=m_pMouseInput->SetDataFormat(&c_dfDIMouse))
  37. {
  38. return ;
  39. }
  40. m_pMouseInput->SetCooperativeLevel(m_hWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
  41. m_pMouseInput->Acquire();
  42. memset(&m_MouseState,0,sizeof(m_MouseState));
  43. memset(&m_MouseStateOld,0,sizeof(m_MouseStateOld));
  44. m_Up=0;
  45. m_PreUp=0;
  46.    run=true;
  47.  
  48. }
  49. CInput::~CInput()
  50. {   m_pMouseInput->Unacquire();
  51.     m_pMouseInput->Release();
  52. m_pInputDevice->Unacquire();
  53. m_pInputDevice->Release();
  54. m_pInput->Release();
  55. }
  56. LRESULT CInput::GetKeyState(DWORD Keycode)
  57. {
  58.   if(Keycode<MK_MIN)
  59.   {
  60.     return (0x80 & m_cKeyboardState[Keycode])|m_cKeyboardStateOld[Keycode]>>4;
  61.   }
  62. }
  63. LRESULT CInput::Update()
  64. {
  65. memcpy(m_cKeyboardStateOld,m_cKeyboardState,sizeof(m_cKeyboardState));
  66. if(DI_OK!=m_pInputDevice->GetDeviceState(sizeof(m_cKeyboardState),m_cKeyboardState))
  67. {
  68. memset(&m_cKeyboardState,0,sizeof(m_cKeyboardState));
  69. m_pInputDevice->Acquire();
  70. }
  71. memcpy(&m_MouseStateOld,&m_MouseState,sizeof(m_MouseState));
  72. if(DI_OK!=m_pMouseInput->GetDeviceState(sizeof(m_MouseState),&m_MouseState))
  73. {
  74. memset(&m_MouseState,0,sizeof(m_MouseState));
  75. m_pMouseInput->Acquire();
  76. }
  77.     return DI_OK;
  78. }
  79. void CInput::Control(bool * flag,D3DXVECTOR3 * p1,float * up,bool *shoot,
  80.  POSS * NumOfPos,bool * CallPress,bool * ButtonPress,
  81.  bool * Result,POSITION * ViewPos,COLLIPE * col,
  82.  CTerrain * m_pTerrain,NPOSITION * BossPos,bool * nomoney)
  83. {   
  84.     static bool CrossBridge=false;
  85. static DWORD time=timeGetTime()/1000;
  86. DWORD timeNow=timeGetTime()/1000;
  87. //bool LPress;
  88. float jiaoduX=-m_MouseState.lX/5;
  89. float jiaoduY=m_MouseState.lY/5;
  90. float suofang=-m_MouseState.lZ/5;
  91. ViewPos->cc+=suofang;
  92.     if(0x80&m_cKeyboardState[DIK_Q])
  93. {
  94. ViewPos->cc+=5;
  95. }
  96. if(0x80&m_cKeyboardState[DIK_E])
  97. {
  98. ViewPos->cc-=5;
  99. }
  100. if(ViewPos->cc>=300)
  101. ViewPos->cc=300;
  102. if(ViewPos->cc<=50)
  103. ViewPos->cc=50;
  104. if(0x80&m_cKeyboardState[DIK_UP])
  105. {
  106. ViewPos->cc+=5;
  107. }
  108. if(0x80&m_cKeyboardState[DIK_DOWN])
  109. {
  110. ViewPos->cc-=5;
  111. }
  112.    
  113. if((0x80&m_MouseState.rgbButtons[0])|(0x80&m_MouseState.rgbButtons[1]))
  114. {     
  115. if((0x80&m_MouseState.rgbButtons[1]))
  116. {
  117. ViewPos->a+=jiaoduX;
  118. }
  119. ViewPos->ca+=jiaoduX;
  120. if(*flag==false)
  121. {
  122.         
  123. if(ViewPos->cb>90)
  124. ViewPos->cb=90;
  125.     if(ViewPos->cb<-90)
  126. ViewPos->cb=-90;
  127.     ViewPos->cb+=jiaoduY;
  128. m_Up=0;
  129. m_PreUp=0;
  130. }
  131. else
  132. {
  133. m_PreUp=m_Up;
  134.             m_Up+=jiaoduY;
  135. if(m_Up<=0)
  136. {*p1=D3DXVECTOR3(ViewPos->cc*cos(D3DX_PI/180*jiaoduY)*cos(D3DX_PI/180*ViewPos->ca),ViewPos->cc*sin(D3DX_PI/180*jiaoduY),ViewPos->cc*cos(D3DX_PI/180*jiaoduY)*sin(D3DX_PI/180*ViewPos->ca));
  137. *up=m_Up;
  138. }
  139. else
  140. {
  141. *flag=false;
  142.                 if(ViewPos->cb>90)
  143.            ViewPos->cb=90;
  144.               if(ViewPos->cb<-90)
  145.             ViewPos->cb=-90;
  146.              ViewPos->cb+=jiaoduY;
  147.           m_Up=0;
  148.                   *up=m_Up;
  149. }
  150. }
  151. }
  152. if(0x80&m_cKeyboardState[DIK_W])
  153. {
  154. ViewPos->z+=5*sin((D3DX_PI/180)*ViewPos->a);
  155. ViewPos->x+=5*cos((D3DX_PI/180)*ViewPos->a);
  156. }
  157. if(0x80&m_cKeyboardState[DIK_S])
  158. {   
  159.     ViewPos->z+=5*sin((D3DX_PI/180)*(ViewPos->a+180));
  160. ViewPos->x+=5*cos((D3DX_PI/180)*(ViewPos->a+180));
  161. }
  162. if(0x80&m_cKeyboardState[DIK_A])
  163. {   
  164. if(0x80&m_MouseState.rgbButtons[1])
  165. {
  166. ViewPos->z+=5*sin((D3DX_PI/180)*(ViewPos->a+90));
  167. ViewPos->x+=5*cos((D3DX_PI/180)*(ViewPos->a+90));
  168. }
  169. else
  170. {
  171. ViewPos->a+=1.5;
  172. }
  173. }
  174. if(0x80&m_cKeyboardState[DIK_D])
  175. {     if(0x80&m_MouseState.rgbButtons[1])
  176. {
  177. ViewPos->z+=5*sin((D3DX_PI/180)*(ViewPos->a-90));
  178. ViewPos->x+=5*cos((D3DX_PI/180)*(ViewPos->a-90));
  179. }
  180.     else
  181.        {
  182. ViewPos->a-=1.5;
  183.         }
  184. }
  185. if(CrossBridge==false)
  186. {
  187.      ViewPos->y=m_pTerrain->GetHeight(ViewPos->x,ViewPos->z);
  188. }
  189. /*if(0x80&m_MouseState.rgbButtons[1])
  190. {
  191. ViewPos->a+=jiaoduX;
  192.         ViewPos->ca+=jiaoduX;
  193.          if(ViewPos->cb>90)
  194.    ViewPos->cb=90;
  195.     if(ViewPos->cb<-90)
  196.    ViewPos->cb=-90;
  197.     ViewPos->cb+=jiaoduY;
  198. }*/
  199.       float x,y;
  200.  
  201.    
  202.  POINT m_Point;
  203.  GetCursorPos(&m_Point);
  204.      //x=(float)m_Point.x-5;
  205.      //y=(600)-(float)m_Point.y;
  206.  ScreenToClient(m_hWnd,&m_Point);
  207.  x=(float)m_Point.x+3;
  208.  y=(600-17)-(float)m_Point.y;
  209.  if(0x80&m_MouseState.rgbButtons[0]|(0x80&m_MouseState.rgbButtons[1]))
  210.  {
  211.   if(((y>=0.0f&y<=100.0f)&(x>0.0f&x<320.0f))|((y>=0.0f&y<=100.0f)&(x>500.0f&x<800.0f)))
  212.  
  213.  {
  214.           *nomoney=true;
  215.  }
  216.  else
  217.  {
  218.  *nomoney=false;
  219.  
  220.  }
  221.  }
  222.       else
  223.  {
  224.  *nomoney=false;
  225.  }
  226. if(0x80&m_MouseState.rgbButtons[0])
  227. {
  228.  for(int i=0;i<NIcon;i++)
  229.      { 
  230. if(CallPress[i]==true)
  231. {
  232.                  if(((x>=NumOfPos[i].ltx)&(x<=NumOfPos[i].rbx))&((y>=NumOfPos[i].rby)&(y<=NumOfPos[i].lty)))
  233.    {
  234.                         
  235.         
  236.              
  237.               ButtonPress[i]=true;mygod[i]=true;
  238.                                 
  239.    }
  240.                           else
  241.       {
  242.                               ButtonPress[i]=false;mygod[i]=false;motherfuck[i]=false;Result[i]=false;
  243.                              
  244.       }
  245.     
  246.   }
  247.  
  248. }
  249. }
  250. else
  251. {    
  252.             for(int i=0;i<NIcon;i++)
  253. {
  254.                if(CallPress[i]==true)
  255.    {
  256.                  if(((x>=NumOfPos[i].ltx)&(x<=NumOfPos[i].rbx))&((y>=NumOfPos[i].rby)&(y<=NumOfPos[i].lty)))
  257.        {
  258.                         
  259.         
  260.              
  261.              ButtonPress[i]=false;
  262.               if(motherfuck[i]==true)
  263.               {
  264.   Result[i]=true;
  265.   mygod[i]=false;
  266.   motherfuck[i]=false;
  267.   }
  268.                    if(mygod[i]==true)
  269.    {
  270.                  motherfuck[i]=true;
  271.    }
  272.                                 
  273.    }
  274.                           else
  275.       {
  276.                               ButtonPress[i]=false;mygod[i]=false;motherfuck[i]=false;Result[i]=false;
  277.       }
  278.     
  279.     }
  280. }
  281. }
  282. if(run==false)
  283. { CallPress[0]=!CallPress[0];ButtonPress[0]=false;mygod[0]=false;motherfuck[0]=false;Result[0]=false;
  284.    CallPress[1]=!CallPress[1];ButtonPress[1]=false;mygod[1]=false;motherfuck[1]=false;Result[1]=false;
  285.    CallPress[2]=!CallPress[2];ButtonPress[2]=false;mygod[2]=false;motherfuck[2]=false;Result[2]=false;
  286. run=true;
  287. }
  288.      if(Result[2]==true)
  289. {
  290.        CallPress[0]=false;ButtonPress[0]=false;mygod[0]=false;motherfuck[0]=false;
  291.    CallPress[1]=false;ButtonPress[1]=false;mygod[1]=false;motherfuck[1]=false;
  292.    CallPress[2]=false;ButtonPress[2]=false;mygod[2]=false;motherfuck[2]=false;
  293.        
  294.       
  295. }
  296.     
  297. static bool ohgod=false;
  298.     if((0x80&m_MouseState.rgbButtons[1])==false)
  299. {
  300. if(ohgod==true)
  301. {
  302. if(timeNow-time<=0.5)
  303. {
  304.                *shoot=true;
  305. }
  306. else
  307. {
  308.               *shoot=false;
  309. }
  310.          ohgod=false;
  311. }
  312. else
  313. {
  314.          *shoot=false;
  315. }
  316.         time=timeNow;
  317. }
  318. else
  319. {
  320.         ohgod=true;
  321. }
  322.  static POSITION PPreViewPos=*ViewPos;
  323.       for(int i=0;i<44;i++)
  324. D3DXVECTOR3 RolePoint=D3DXVECTOR3(ViewPos->x,0.0f,ViewPos->z);
  325.     D3DXVECTOR3 dest=D3DXVECTOR3(col[i].x,0.0f,col[i].z);
  326.         float distance=D3DXVec3Length(&(dest-RolePoint));
  327.     if(distance<=col[i].dis)
  328.     {
  329.   *ViewPos=PPreViewPos;
  330.           break;
  331.     }
  332. }
  333. D3DXVECTOR3 Role=D3DXVECTOR3(ViewPos->x,0.0f,ViewPos->z);
  334. D3DXVECTOR3 ddd=D3DXVECTOR3(BossPos->x,0.0f,BossPos->z);
  335. float juli=D3DXVec3Length(&(Role-ddd));
  336. if(juli<=30)
  337. {
  338.          ViewPos->x=PPreViewPos.x;
  339.  ViewPos->z=PPreViewPos.z;
  340. }
  341. if((ViewPos->x<180)|(ViewPos->z<180)|(ViewPos->x>(128*28))|(ViewPos->z>(128*28)))
  342. {
  343. *ViewPos=PPreViewPos;
  344. }
  345. D3DXVECTOR3 RolePos=D3DXVECTOR3(ViewPos->x,0.0f,ViewPos->z);
  346. D3DXVECTOR3 des=D3DXVECTOR3(780.0f,0.0f,3000.0f);
  347.     float dis=D3DXVec3Length(&(RolePos-des));
  348.      D3DXVECTOR3 Dir2;
  349.  D3DXVECTOR3 Dir1;
  350.  D3DXVECTOR3 Dir3;
  351.  D3DXVec3Normalize(&Dir1,&(RolePos-des));
  352.  D3DXVec3Normalize(&Dir2,&D3DXVECTOR3(0.0f,0.0f,-1.0f));
  353.  D3DXVec3Normalize(&Dir3,&D3DXVECTOR3(0.0f,0.0f,1.0f));
  354. float angle=acosf(D3DXVec3Dot(&Dir1,&Dir2));
  355.    angle=abs(angle-(D3DX_PI/180*100));
  356.        if(angle>=D3DX_PI/2)
  357.   {
  358.             angle=abs(angle-D3DX_PI);
  359.    
  360.    }
  361.    float ass=100/sin(angle);
  362.     float angle_2=acosf(D3DXVec3Dot(&Dir1,&Dir3));
  363.         angle_2=abs(angle_2-(D3DX_PI/180*100));
  364.        if(angle_2>=D3DX_PI/2)
  365.   {
  366.             angle_2=abs(angle_2-D3DX_PI);
  367.    
  368.    }
  369.   float ass_2=100/sin(angle_2);
  370.   float ass_3;  
  371.   if(ViewPos->x>780)
  372.   {
  373.   ass_3=ass_2;
  374.   }
  375.   else
  376.   {
  377.   ass_3=ass;
  378.   }
  379. if(dis<420)
  380. {
  381. if(ViewPos->y>=215)
  382. {
  383.     
  384. ViewPos->y=220;
  385. CrossBridge=true;
  386. if(dis>ass_3)
  387. {
  388. *ViewPos=PPreViewPos;
  389. }
  390. }
  391. else
  392. {
  393.         if((ViewPos->y<76))
  394.      {   
  395.          CrossBridge=false;
  396.      }                   
  397.       else
  398.      {
  399.  if(dis<ass_3)
  400.  {
  401.      *ViewPos=PPreViewPos;
  402.  }
  403.  CrossBridge=false;
  404.      }
  405. }
  406. }
  407. else
  408. {
  409.           CrossBridge=false;
  410.  
  411. }
  412.      PPreViewPos=*ViewPos;
  413. }
  414.    
  415. void CInput::GetOut(bool * hihi)
  416. {
  417.               if(0x80&m_cKeyboardState[DIK_SPACE])
  418.            {
  419.                  *hihi=true;
  420.            }
  421. }