KeysTable.cpp
上传用户:royluo
上传日期:2007-01-05
资源大小:1584k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. /*****************************************************************************
  2. *                                                                             
  3. *   KeysTable.cpp                                                            
  4. *                                                                             
  5. *   Electrical Engineering Faculty - Software Lab                             
  6. *   Spring semester 1998                                                      
  7. *                                                                             
  8. *   Tanks game                                                                
  9. *                                                                             
  10. *   Module description: Each player defines it's own set of keys to control the
  11. *                       tank. The key table object loads the last mapping to
  12. *                       a table, either from the registry file or from a default
  13. *                       hard coded table (when playing for the 1st time).
  14. *                       During the game, each key pressed is looked in the table,
  15. *                       and if it's one of the control keys, it alters the 
  16. *                       maneuver set object assigned to our local tank.
  17. *                                                                             
  18. *   Authors: Eran Yariv - 28484475                                           
  19. *            Moshe Zur  - 24070856                                           
  20. *                                                                            
  21. *                                                                            
  22. *   Date: 23/09/98                                                           
  23. *                                                                            
  24. ******************************************************************************/
  25. #include <stdafx.h>
  26. #include "Tanks.h"
  27. #include "KeysTable.h"
  28. #include "GameConsts.h"
  29. CKeysTable::~CKeysTable()
  30. {
  31.     TANKS_APP -> SetStoredRightKey (m_uKeysArr[0]);
  32.     TANKS_APP -> SetStoredLeftKey (m_uKeysArr[1]);
  33.     TANKS_APP -> SetStoredForwardKey (m_uKeysArr[2]);
  34.     TANKS_APP -> SetStoredBackwardKey (m_uKeysArr[3]);
  35.     TANKS_APP -> SetStoredShellKey (m_uKeysArr[4]);
  36.     TANKS_APP -> SetStoredBulletKey (m_uKeysArr[5]);
  37.     TANKS_APP -> SetStoredMineKey (m_uKeysArr[6]);
  38.     TANKS_APP -> SetStoredAerialKey (m_uKeysArr[7]);
  39. }
  40. CKeysTable::CKeysTable (const CKeysTable& rhs)
  41. {
  42.     for (int i = 0; i < CManouverSet::MAX_MANOUVER_BIT; i++)
  43.         m_uKeysArr[i] = rhs.m_uKeysArr[i];
  44. }
  45. /*------------------------------------------------------------------------------
  46.   Function: SetKey
  47.   Purpose:  Assigns a new control key to the indicated action.
  48.   Input:    ind: Index of the action in the table.
  49.             key: Virtual key code of the requested key.
  50.   Output:   Return TRUE if key could be assigned.
  51.   Remarks:  Method return FALSE in case the key is allready in use for a
  52.             different action.
  53. ------------------------------------------------------------------------------*/
  54. BOOL
  55. CKeysTable::SetKey (int ind, UINT key)
  56. {
  57.     BOOL bRes = FALSE;
  58.         // Checks index is in bounds:
  59.     if (0 <= ind && CManouverSet::MAX_MANOUVER_BIT > ind)
  60.     {
  61.         BOOL Duplicated = FALSE;    // Indicates key is already in use
  62.             // Check that key is unique:
  63.         for (int i = 0; i < CManouverSet::MAX_MANOUVER_BIT && !Duplicated; i++)
  64.             Duplicated = (m_uKeysArr[i] == key);
  65.         if (!Duplicated)
  66.         {
  67.             m_uKeysArr[ind] = key;
  68.             bRes = TRUE;
  69.         }
  70.     }
  71.     return bRes;
  72. }
  73. void
  74. CKeysTable::RestoreDefault()
  75. {
  76.     for (int i = 0; i < CManouverSet::MAX_MANOUVER_BIT; i++)
  77.         m_uKeysArr[i] = DEFAULT_KEYS_SETTINGS[i];
  78. }
  79. void
  80. CKeysTable::InitTable()
  81. {
  82.     m_uKeysArr[0] = TANKS_APP -> GetStoredRightKey (DEFAULT_KEYS_SETTINGS[0]);
  83.     m_uKeysArr[1] = TANKS_APP -> GetStoredLeftKey (DEFAULT_KEYS_SETTINGS[1]);
  84.     m_uKeysArr[2] = TANKS_APP -> GetStoredForwardKey (DEFAULT_KEYS_SETTINGS[2]);
  85.     m_uKeysArr[3] = TANKS_APP -> GetStoredBackwardKey (DEFAULT_KEYS_SETTINGS[3]);
  86.     m_uKeysArr[4] = TANKS_APP -> GetStoredShellKey (DEFAULT_KEYS_SETTINGS[4]);
  87.     m_uKeysArr[5] = TANKS_APP -> GetStoredBulletKey (DEFAULT_KEYS_SETTINGS[5]);
  88.     m_uKeysArr[6] = TANKS_APP -> GetStoredMineKey (DEFAULT_KEYS_SETTINGS[6]);
  89.     m_uKeysArr[7] = TANKS_APP -> GetStoredAerialKey (DEFAULT_KEYS_SETTINGS[7]);
  90. }
  91. CKeysTable& 
  92. CKeysTable::operator= (const CKeysTable& rhs)
  93. {
  94.     if (this != &rhs)   // Just copy arrays, if it's not the same object:
  95.     {
  96.         for (int i = 0; i < CManouverSet::MAX_MANOUVER_BIT; i++)
  97.             m_uKeysArr[i] = rhs.m_uKeysArr[i];
  98.     }
  99.     return *this;
  100. }