Input.cpp
资源名称:3DRPG.rar [点击查看]
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:10k
源码类别:
游戏
开发平台:
Visual C++
- #include "stdafx.h"
- #include "Input.h"
- CInput::CInput(HINSTANCE hInstance,HWND hwnd,int nIcon)
- {
- NIcon=nIcon;
- mygod=new bool [NIcon];
- motherfuck=new bool[NIcon];
- for(int i=0;i<3;i++)
- {
- mygod[i]=false;
- motherfuck[i]=false;
- }
- control=true;
- m_hInst=hInstance;
- m_hWnd=hwnd;
- if(DI_OK!=DirectInput8Create(m_hInst,DIRECTINPUT_VERSION,IID_IDirectInput8,(LPVOID*)&m_pInput,NULL))
- {
- return ;
- }
- if(DI_OK!=m_pInput->CreateDevice(GUID_SysKeyboard,&m_pInputDevice,NULL))
- {
- return ;
- }
- if(DI_OK!=m_pInputDevice->SetDataFormat(&c_dfDIKeyboard))
- {
- return ;
- }
- m_pInputDevice->SetCooperativeLevel(m_hWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
- m_pInputDevice->Acquire();
- memset(&m_cKeyboardState,0,sizeof(m_cKeyboardState));
- memset(&m_cKeyboardStateOld,0,sizeof(m_cKeyboardStateOld));
- if(DI_OK!=m_pInput->CreateDevice(GUID_SysMouse,&m_pMouseInput,NULL))
- {
- return ;
- }
- if(DI_OK!=m_pMouseInput->SetDataFormat(&c_dfDIMouse))
- {
- return ;
- }
- m_pMouseInput->SetCooperativeLevel(m_hWnd,DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
- m_pMouseInput->Acquire();
- memset(&m_MouseState,0,sizeof(m_MouseState));
- memset(&m_MouseStateOld,0,sizeof(m_MouseStateOld));
- m_Up=0;
- m_PreUp=0;
- run=true;
- }
- CInput::~CInput()
- { m_pMouseInput->Unacquire();
- m_pMouseInput->Release();
- m_pInputDevice->Unacquire();
- m_pInputDevice->Release();
- m_pInput->Release();
- }
- LRESULT CInput::GetKeyState(DWORD Keycode)
- {
- if(Keycode<MK_MIN)
- {
- return (0x80 & m_cKeyboardState[Keycode])|m_cKeyboardStateOld[Keycode]>>4;
- }
- }
- LRESULT CInput::Update()
- {
- memcpy(m_cKeyboardStateOld,m_cKeyboardState,sizeof(m_cKeyboardState));
- if(DI_OK!=m_pInputDevice->GetDeviceState(sizeof(m_cKeyboardState),m_cKeyboardState))
- {
- memset(&m_cKeyboardState,0,sizeof(m_cKeyboardState));
- m_pInputDevice->Acquire();
- }
- memcpy(&m_MouseStateOld,&m_MouseState,sizeof(m_MouseState));
- if(DI_OK!=m_pMouseInput->GetDeviceState(sizeof(m_MouseState),&m_MouseState))
- {
- memset(&m_MouseState,0,sizeof(m_MouseState));
- m_pMouseInput->Acquire();
- }
- return DI_OK;
- }
- void CInput::Control(bool * flag,D3DXVECTOR3 * p1,float * up,bool *shoot,
- POSS * NumOfPos,bool * CallPress,bool * ButtonPress,
- bool * Result,POSITION * ViewPos,COLLIPE * col,
- CTerrain * m_pTerrain,NPOSITION * BossPos,bool * nomoney)
- {
- static bool CrossBridge=false;
- static DWORD time=timeGetTime()/1000;
- DWORD timeNow=timeGetTime()/1000;
- //bool LPress;
- float jiaoduX=-m_MouseState.lX/5;
- float jiaoduY=m_MouseState.lY/5;
- float suofang=-m_MouseState.lZ/5;
- ViewPos->cc+=suofang;
- if(0x80&m_cKeyboardState[DIK_Q])
- {
- ViewPos->cc+=5;
- }
- if(0x80&m_cKeyboardState[DIK_E])
- {
- ViewPos->cc-=5;
- }
- if(ViewPos->cc>=300)
- ViewPos->cc=300;
- if(ViewPos->cc<=50)
- ViewPos->cc=50;
- if(0x80&m_cKeyboardState[DIK_UP])
- {
- ViewPos->cc+=5;
- }
- if(0x80&m_cKeyboardState[DIK_DOWN])
- {
- ViewPos->cc-=5;
- }
- if((0x80&m_MouseState.rgbButtons[0])|(0x80&m_MouseState.rgbButtons[1]))
- {
- if((0x80&m_MouseState.rgbButtons[1]))
- {
- ViewPos->a+=jiaoduX;
- }
- ViewPos->ca+=jiaoduX;
- if(*flag==false)
- {
- if(ViewPos->cb>90)
- ViewPos->cb=90;
- if(ViewPos->cb<-90)
- ViewPos->cb=-90;
- ViewPos->cb+=jiaoduY;
- m_Up=0;
- m_PreUp=0;
- }
- else
- {
- m_PreUp=m_Up;
- m_Up+=jiaoduY;
- if(m_Up<=0)
- {*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));
- *up=m_Up;
- }
- else
- {
- *flag=false;
- if(ViewPos->cb>90)
- ViewPos->cb=90;
- if(ViewPos->cb<-90)
- ViewPos->cb=-90;
- ViewPos->cb+=jiaoduY;
- m_Up=0;
- *up=m_Up;
- }
- }
- }
- if(0x80&m_cKeyboardState[DIK_W])
- {
- ViewPos->z+=5*sin((D3DX_PI/180)*ViewPos->a);
- ViewPos->x+=5*cos((D3DX_PI/180)*ViewPos->a);
- }
- if(0x80&m_cKeyboardState[DIK_S])
- {
- ViewPos->z+=5*sin((D3DX_PI/180)*(ViewPos->a+180));
- ViewPos->x+=5*cos((D3DX_PI/180)*(ViewPos->a+180));
- }
- if(0x80&m_cKeyboardState[DIK_A])
- {
- if(0x80&m_MouseState.rgbButtons[1])
- {
- ViewPos->z+=5*sin((D3DX_PI/180)*(ViewPos->a+90));
- ViewPos->x+=5*cos((D3DX_PI/180)*(ViewPos->a+90));
- }
- else
- {
- ViewPos->a+=1.5;
- }
- }
- if(0x80&m_cKeyboardState[DIK_D])
- { if(0x80&m_MouseState.rgbButtons[1])
- {
- ViewPos->z+=5*sin((D3DX_PI/180)*(ViewPos->a-90));
- ViewPos->x+=5*cos((D3DX_PI/180)*(ViewPos->a-90));
- }
- else
- {
- ViewPos->a-=1.5;
- }
- }
- if(CrossBridge==false)
- {
- ViewPos->y=m_pTerrain->GetHeight(ViewPos->x,ViewPos->z);
- }
- /*if(0x80&m_MouseState.rgbButtons[1])
- {
- ViewPos->a+=jiaoduX;
- ViewPos->ca+=jiaoduX;
- if(ViewPos->cb>90)
- ViewPos->cb=90;
- if(ViewPos->cb<-90)
- ViewPos->cb=-90;
- ViewPos->cb+=jiaoduY;
- }*/
- float x,y;
- POINT m_Point;
- GetCursorPos(&m_Point);
- //x=(float)m_Point.x-5;
- //y=(600)-(float)m_Point.y;
- ScreenToClient(m_hWnd,&m_Point);
- x=(float)m_Point.x+3;
- y=(600-17)-(float)m_Point.y;
- if(0x80&m_MouseState.rgbButtons[0]|(0x80&m_MouseState.rgbButtons[1]))
- {
- 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)))
- {
- *nomoney=true;
- }
- else
- {
- *nomoney=false;
- }
- }
- else
- {
- *nomoney=false;
- }
- if(0x80&m_MouseState.rgbButtons[0])
- {
- for(int i=0;i<NIcon;i++)
- {
- if(CallPress[i]==true)
- {
- if(((x>=NumOfPos[i].ltx)&(x<=NumOfPos[i].rbx))&((y>=NumOfPos[i].rby)&(y<=NumOfPos[i].lty)))
- {
- ButtonPress[i]=true;mygod[i]=true;
- }
- else
- {
- ButtonPress[i]=false;mygod[i]=false;motherfuck[i]=false;Result[i]=false;
- }
- }
- }
- }
- else
- {
- for(int i=0;i<NIcon;i++)
- {
- if(CallPress[i]==true)
- {
- if(((x>=NumOfPos[i].ltx)&(x<=NumOfPos[i].rbx))&((y>=NumOfPos[i].rby)&(y<=NumOfPos[i].lty)))
- {
- ButtonPress[i]=false;
- if(motherfuck[i]==true)
- {
- Result[i]=true;
- mygod[i]=false;
- motherfuck[i]=false;
- }
- if(mygod[i]==true)
- {
- motherfuck[i]=true;
- }
- }
- else
- {
- ButtonPress[i]=false;mygod[i]=false;motherfuck[i]=false;Result[i]=false;
- }
- }
- }
- }
- if(run==false)
- { CallPress[0]=!CallPress[0];ButtonPress[0]=false;mygod[0]=false;motherfuck[0]=false;Result[0]=false;
- CallPress[1]=!CallPress[1];ButtonPress[1]=false;mygod[1]=false;motherfuck[1]=false;Result[1]=false;
- CallPress[2]=!CallPress[2];ButtonPress[2]=false;mygod[2]=false;motherfuck[2]=false;Result[2]=false;
- run=true;
- }
- if(Result[2]==true)
- {
- CallPress[0]=false;ButtonPress[0]=false;mygod[0]=false;motherfuck[0]=false;
- CallPress[1]=false;ButtonPress[1]=false;mygod[1]=false;motherfuck[1]=false;
- CallPress[2]=false;ButtonPress[2]=false;mygod[2]=false;motherfuck[2]=false;
- }
- static bool ohgod=false;
- if((0x80&m_MouseState.rgbButtons[1])==false)
- {
- if(ohgod==true)
- {
- if(timeNow-time<=0.5)
- {
- *shoot=true;
- }
- else
- {
- *shoot=false;
- }
- ohgod=false;
- }
- else
- {
- *shoot=false;
- }
- time=timeNow;
- }
- else
- {
- ohgod=true;
- }
- static POSITION PPreViewPos=*ViewPos;
- for(int i=0;i<44;i++)
- {
- D3DXVECTOR3 RolePoint=D3DXVECTOR3(ViewPos->x,0.0f,ViewPos->z);
- D3DXVECTOR3 dest=D3DXVECTOR3(col[i].x,0.0f,col[i].z);
- float distance=D3DXVec3Length(&(dest-RolePoint));
- if(distance<=col[i].dis)
- {
- *ViewPos=PPreViewPos;
- break;
- }
- }
- D3DXVECTOR3 Role=D3DXVECTOR3(ViewPos->x,0.0f,ViewPos->z);
- D3DXVECTOR3 ddd=D3DXVECTOR3(BossPos->x,0.0f,BossPos->z);
- float juli=D3DXVec3Length(&(Role-ddd));
- if(juli<=30)
- {
- ViewPos->x=PPreViewPos.x;
- ViewPos->z=PPreViewPos.z;
- }
- if((ViewPos->x<180)|(ViewPos->z<180)|(ViewPos->x>(128*28))|(ViewPos->z>(128*28)))
- {
- *ViewPos=PPreViewPos;
- }
- D3DXVECTOR3 RolePos=D3DXVECTOR3(ViewPos->x,0.0f,ViewPos->z);
- D3DXVECTOR3 des=D3DXVECTOR3(780.0f,0.0f,3000.0f);
- float dis=D3DXVec3Length(&(RolePos-des));
- D3DXVECTOR3 Dir2;
- D3DXVECTOR3 Dir1;
- D3DXVECTOR3 Dir3;
- D3DXVec3Normalize(&Dir1,&(RolePos-des));
- D3DXVec3Normalize(&Dir2,&D3DXVECTOR3(0.0f,0.0f,-1.0f));
- D3DXVec3Normalize(&Dir3,&D3DXVECTOR3(0.0f,0.0f,1.0f));
- float angle=acosf(D3DXVec3Dot(&Dir1,&Dir2));
- angle=abs(angle-(D3DX_PI/180*100));
- if(angle>=D3DX_PI/2)
- {
- angle=abs(angle-D3DX_PI);
- }
- float ass=100/sin(angle);
- float angle_2=acosf(D3DXVec3Dot(&Dir1,&Dir3));
- angle_2=abs(angle_2-(D3DX_PI/180*100));
- if(angle_2>=D3DX_PI/2)
- {
- angle_2=abs(angle_2-D3DX_PI);
- }
- float ass_2=100/sin(angle_2);
- float ass_3;
- if(ViewPos->x>780)
- {
- ass_3=ass_2;
- }
- else
- {
- ass_3=ass;
- }
- if(dis<420)
- {
- if(ViewPos->y>=215)
- {
- ViewPos->y=220;
- CrossBridge=true;
- if(dis>ass_3)
- {
- *ViewPos=PPreViewPos;
- }
- }
- else
- {
- if((ViewPos->y<76))
- {
- CrossBridge=false;
- }
- else
- {
- if(dis<ass_3)
- {
- *ViewPos=PPreViewPos;
- }
- CrossBridge=false;
- }
- }
- }
- else
- {
- CrossBridge=false;
- }
- PPreViewPos=*ViewPos;
- }
- void CInput::GetOut(bool * hihi)
- {
- if(0x80&m_cKeyboardState[DIK_SPACE])
- {
- *hihi=true;
- }
- }