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

游戏

开发平台:

Visual C++

  1. // BilliardsDoc.cpp : implementation of the CBilliardsDoc class
  2. //
  3. #include "stdafx.h"
  4. #include "Billiards.h"
  5. #include "BilliardsDoc.h"
  6. #include "BilliardsFormView.h"
  7. #include "BilliardsPlayView.h"
  8. #include "ClientSocket.h"
  9. #include "MainFrm.h"
  10. #include <Mmsystem.h>
  11. #include "PropertyList.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CBilliardsDoc
  19. IMPLEMENT_DYNCREATE(CBilliardsDoc, CDocument)
  20. BEGIN_MESSAGE_MAP(CBilliardsDoc, CDocument)
  21. //{{AFX_MSG_MAP(CBilliardsDoc)
  22. // NOTE - the ClassWizard will add and remove mapping macros here.
  23. //    DO NOT EDIT what you see in these blocks of generated code!
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CBilliardsDoc construction/destruction
  28. CBilliardsDoc::CBilliardsDoc()
  29. {
  30.     m_pListenSocket=new CListenSocket(this);
  31. m_pClientSocket=new CClientSocket(this);
  32. m_bPlayer=IDLE;
  33. }
  34. CBilliardsDoc::~CBilliardsDoc()
  35. {
  36. }
  37. BOOL CBilliardsDoc::OnNewDocument()
  38. {
  39. if (!CDocument::OnNewDocument())
  40. return FALSE;
  41. return TRUE;
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CBilliardsDoc serialization
  45. void CBilliardsDoc::Serialize(CArchive& ar)
  46. {
  47. if (ar.IsStoring())
  48. {
  49. // TODO: add storing code here
  50. }
  51. else
  52. {
  53. // TODO: add loading code here
  54. }
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CBilliardsDoc diagnostics
  58. #ifdef _DEBUG
  59. void CBilliardsDoc::AssertValid() const
  60. {
  61. CDocument::AssertValid();
  62. }
  63. void CBilliardsDoc::Dump(CDumpContext& dc) const
  64. {
  65. CDocument::Dump(dc);
  66. }
  67. #endif //_DEBUG
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CBilliardsDoc commands
  70. BOOL CBilliardsDoc::AcceptConnection()
  71. {
  72.     if(!m_pListenSocket->Accept(*m_pClientSocket)){
  73.         AfxMessageBox("接受连接失败!");
  74. return 0;
  75. }
  76. CString strTemp;
  77. strTemp="0"+m_strServer;
  78. m_pClientSocket->Send(strTemp,strTemp.GetLength());
  79.     return 1;
  80. }
  81. BOOL CBilliardsDoc::ReceiveMSG(CString strTemp)
  82. {
  83.     
  84.     CBilliardsFormView* pFormView;
  85.     CMainFrame *pView=(CMainFrame *)(AfxGetMainWnd());
  86.     CPropertyItem *pItem=NULL;
  87.     
  88.     POSITION pos;
  89.     
  90.     for(pos=GetFirstViewPosition();pos!=NULL;)
  91. {    
  92.         CView* pView = GetNextView(pos);
  93.         pFormView= DYNAMIC_DOWNCAST(CBilliardsFormView, pView);
  94.     if (pFormView != NULL)
  95. break;
  96.     }
  97.     switch(strTemp.GetAt(0)){
  98.         case '0':
  99.             strTemp.Delete(0);
  100.             if(m_bPlayer==SERVER){
  101.                 
  102.                 pFormView->GetDlgItem(IDC_STATIC_CLIENT)->SetWindowText(strTemp);
  103. pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
  104.                 //pFormView->m_ctlListMSG.AddItem("新用户加入游戏");
  105.                 m_strClient=strTemp;
  106.                 sndPlaySound("res\notify.wav",SND_ASYNC);
  107.                 
  108.             }
  109. else{
  110. pFormView->GetDlgItem(IDC_STATIC_SERVER)->SetWindowText(strTemp);
  111. pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
  112.                 m_strServer=strTemp;
  113.     }
  114.             
  115. break;
  116. case '1':
  117. strTemp.Delete(0);
  118. if(m_bPlayer==SERVER){
  119.                 pItem=new CPropertyItem(m_strClient,strTemp,PIT_EDIT,"");
  120.                 pFormView->m_ctlListMSG.AddPropItem(pItem);
  121. if(pView->m_bSoundMsg)
  122. sndPlaySound("res\ringin.wav",SND_ASYNC);
  123.             }
  124.             else{
  125.                 pItem=new CPropertyItem(m_strServer,strTemp,PIT_EDIT,"");
  126.                 pFormView->m_ctlListMSG.AddPropItem(pItem);
  127. if(pView->m_bSoundMsg)
  128. sndPlaySound("res\ringin.wav",SND_ASYNC);
  129.             }
  130.             pItem=NULL;
  131. break;
  132. case '3':
  133. if(m_bPlayer==SERVER){
  134. AfxMessageBox("客户端已离开游戏",MB_OK);
  135. this->m_pClientSocket->ShutDown();
  136. this->m_pClientSocket->Close();
  137.                 pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
  138.                 
  139. }
  140. else{
  141. AfxMessageBox("服务器端已离开游戏",MB_OK);
  142. this->m_pClientSocket->ShutDown();
  143. this->m_pClientSocket->Close();
  144.                 ((CMainFrame *)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_NEWGAME,FALSE);
  145.                 ((CMainFrame *)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_JOINGAME,FALSE);
  146.                 ((CMainFrame *)AfxGetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_STOPGAME);
  147.                 
  148.                 ((CMainFrame *)AfxGetMainWnd())->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_NEWGAME,MF_ENABLED);
  149.                 ((CMainFrame *)AfxGetMainWnd())->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_JOINGAME,MF_ENABLED);
  150.                 ((CMainFrame *)AfxGetMainWnd())->GetMenu()->GetSubMenu(0)->EnableMenuItem(ID_STOPGAME,MF_GRAYED);    
  151.     pFormView->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
  152.             }
  153. break;
  154. default:
  155. break;
  156.     }
  157. return 1;
  158. }
  159. void CBilliardsDoc::ReceiveVelocity(char *pBuf)
  160. {
  161.     Velocity *v=new Velocity;
  162.     v=(Velocity *)pBuf;
  163.     
  164.     CBilliardsPlayView* pPlayView;
  165.     pPlayView=(CBilliardsPlayView *)((CMainFrame *)AfxGetMainWnd())->m_wndSplitter.GetPane(0,0);
  166.     pPlayView->m_fVelocity[0][0]=v->fvalue0;
  167.     pPlayView->m_fVelocity[0][1]=v->fvalue1;
  168.     pPlayView->m_fVelocity[0][2]=v->fvalue2;
  169.     while (pPlayView->m_bAnimate);
  170.     pPlayView->HitBall();
  171. }
  172. void CBilliardsDoc::SendVelocity(float m_fVelocity[3])
  173. {
  174.     Velocity v;
  175.     v.type='2';
  176.     v.fvalue0=m_fVelocity[0];
  177.     v.fvalue1=m_fVelocity[1];
  178.     v.fvalue2=m_fVelocity[2];
  179.     m_pClientSocket->Send(&v,sizeof(v));
  180. }