Clone of PhatAC @ https://github.com/floaterxk/PhatAC

GameMode.cpp 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "StdAfx.h"
  2. #include "World.h"
  3. #include "GameMode.h"
  4. #include "Player.h"
  5. #include "PhysicsObj.h"
  6. #include "ChatMsgs.h"
  7. CGameMode::CGameMode()
  8. {
  9. }
  10. CGameMode::~CGameMode()
  11. {
  12. }
  13. CGameMode_Tag::CGameMode_Tag()
  14. {
  15. m_pSelectedPlayer = NULL;
  16. }
  17. CGameMode_Tag::~CGameMode_Tag()
  18. {
  19. UnselectPlayer();
  20. }
  21. const char *CGameMode_Tag::GetName()
  22. {
  23. return "Tag";
  24. }
  25. void CGameMode_Tag::Think()
  26. {
  27. if (!m_pSelectedPlayer)
  28. {
  29. // Find a player to make "it."
  30. PlayerMap *pPlayers = g_pWorld->GetPlayers();
  31. if (pPlayers->size() < 2)
  32. {
  33. return;
  34. }
  35. int index = RandomLong(0, pPlayers->size() - 1);
  36. CBasePlayer *pSelected = NULL;
  37. int i = 0;
  38. for (auto& player : *pPlayers)
  39. {
  40. if (i == index)
  41. {
  42. pSelected = player.second;
  43. break;
  44. }
  45. i++;
  46. }
  47. SelectPlayer(pSelected);
  48. }
  49. }
  50. void CGameMode_Tag::SelectPlayer(CBasePlayer *pPlayer)
  51. {
  52. if (!pPlayer)
  53. {
  54. UnselectPlayer();
  55. return;
  56. }
  57. m_pSelectedPlayer = pPlayer;
  58. ModelInfo appearance;
  59. appearance.dwBasePalette = 0x7E;
  60. appearance.lPalettes.push_back(PaletteRpl(0x1705, 0, 0));
  61. m_pSelectedPlayer->SetAppearanceOverride(&appearance);
  62. m_pSelectedPlayer->EmitEffect(32, 1.0f);
  63. g_pWorld->BroadcastGlobal(ServerText(csprintf("%s is it!", m_pSelectedPlayer->m_strName.c_str()), 1), PRIVATE_MSG);
  64. }
  65. void CGameMode_Tag::UnselectPlayer()
  66. {
  67. if (!m_pSelectedPlayer)
  68. {
  69. return;
  70. }
  71. m_pSelectedPlayer->SetAppearanceOverride(NULL);
  72. }
  73. void CGameMode_Tag::OnTargetAttacked(CPhysicsObj *pTarget, CPhysicsObj *pSource)
  74. {
  75. if (pSource == m_pSelectedPlayer)
  76. {
  77. if (pTarget->IsPlayer())
  78. {
  79. CBasePlayer *pTargetPlayer = (CBasePlayer *)pTarget;
  80. UnselectPlayer();
  81. SelectPlayer(pTargetPlayer);
  82. }
  83. }
  84. }
  85. void CGameMode_Tag::OnRemoveEntity(CPhysicsObj *pEntity)
  86. {
  87. if (pEntity)
  88. {
  89. if (pEntity == m_pSelectedPlayer)
  90. {
  91. UnselectPlayer();
  92. m_pSelectedPlayer = NULL;
  93. }
  94. }
  95. }